Home | History | Annotate | Download | only in libopenjpeg20
      1 /*
      2  * The copyright in this software is being made available under the 2-clauses
      3  * BSD License, included below. This software may be subject to other third
      4  * party and contributor rights, including patent rights, and no such rights
      5  * are granted under this license.
      6  *
      7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
     20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "opj_includes.h"
     33 
     34 opj_image_t* opj_image_create0(void) {
     35 	opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t));
     36 	return image;
     37 }
     38 
     39 opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
     40 	OPJ_UINT32 compno;
     41 	opj_image_t *image = NULL;
     42 
     43 	image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
     44 	if(image) {
     45 		image->color_space = clrspc;
     46 		image->numcomps = numcmpts;
     47 		/* allocate memory for the per-component information */
     48 		image->comps = (opj_image_comp_t*)opj_calloc(1,image->numcomps * sizeof(opj_image_comp_t));
     49 		if(!image->comps) {
     50 			fprintf(stderr,"Unable to allocate memory for image.\n");
     51 			opj_image_destroy(image);
     52 			return NULL;
     53 		}
     54 		/* create the individual image components */
     55 		for(compno = 0; compno < numcmpts; compno++) {
     56 			opj_image_comp_t *comp = &image->comps[compno];
     57 			comp->dx = cmptparms[compno].dx;
     58 			comp->dy = cmptparms[compno].dy;
     59 			comp->w = cmptparms[compno].w;
     60 			comp->h = cmptparms[compno].h;
     61 			comp->x0 = cmptparms[compno].x0;
     62 			comp->y0 = cmptparms[compno].y0;
     63 			comp->prec = cmptparms[compno].prec;
     64 			comp->bpp = cmptparms[compno].bpp;
     65 			comp->sgnd = cmptparms[compno].sgnd;
     66 			comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
     67 			if(!comp->data) {
     68 				fprintf(stderr,"Unable to allocate memory for image.\n");
     69 				opj_image_destroy(image);
     70 				return NULL;
     71 			}
     72 		}
     73 	}
     74 
     75 	return image;
     76 }
     77 
     78 void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) {
     79 	if(image) {
     80 		if(image->comps) {
     81 			OPJ_UINT32 compno;
     82 
     83 			/* image components */
     84 			for(compno = 0; compno < image->numcomps; compno++) {
     85 				opj_image_comp_t *image_comp = &(image->comps[compno]);
     86 				if(image_comp->data) {
     87 					opj_free(image_comp->data);
     88 				}
     89 			}
     90 			opj_free(image->comps);
     91 		}
     92 
     93 		if(image->icc_profile_buf) {
     94 			opj_free(image->icc_profile_buf);
     95 		}
     96 
     97 		opj_free(image);
     98 	}
     99 }
    100 
    101 /**
    102  * Updates the components characteristics of the image from the coding parameters.
    103  *
    104  * @param p_image_header	the image header to update.
    105  * @param p_cp				the coding parameters from which to update the image.
    106  */
    107 void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp * p_cp)
    108 {
    109 	OPJ_UINT32 i, l_width, l_height;
    110 	OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
    111 	OPJ_INT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
    112 	opj_image_comp_t* l_img_comp = NULL;
    113 
    114 	l_x0 = opj_int_max((OPJ_INT32)p_cp->tx0 , (OPJ_INT32)p_image_header->x0);
    115 	l_y0 = opj_int_max((OPJ_INT32)p_cp->ty0 , (OPJ_INT32)p_image_header->y0);
    116 	l_x1 = opj_int_min((OPJ_INT32)(p_cp->tx0 + p_cp->tw * p_cp->tdx), (OPJ_INT32)p_image_header->x1);
    117 	l_y1 = opj_int_min((OPJ_INT32)(p_cp->ty0 + p_cp->th * p_cp->tdy), (OPJ_INT32)p_image_header->y1);
    118 
    119 	l_img_comp = p_image_header->comps;
    120 	for	(i = 0; i < p_image_header->numcomps; ++i) {
    121 		l_comp_x0 = opj_int_ceildiv(l_x0, (OPJ_INT32)l_img_comp->dx);
    122 		l_comp_y0 = opj_int_ceildiv(l_y0, (OPJ_INT32)l_img_comp->dy);
    123 		l_comp_x1 = opj_int_ceildiv(l_x1, (OPJ_INT32)l_img_comp->dx);
    124 		l_comp_y1 = opj_int_ceildiv(l_y1, (OPJ_INT32)l_img_comp->dy);
    125 		l_width = (OPJ_UINT32)opj_int_ceildivpow2(l_comp_x1 - l_comp_x0, (OPJ_INT32)l_img_comp->factor);
    126 		l_height = (OPJ_UINT32)opj_int_ceildivpow2(l_comp_y1 - l_comp_y0, (OPJ_INT32)l_img_comp->factor);
    127 		l_img_comp->w = l_width;
    128 		l_img_comp->h = l_height;
    129 		l_img_comp->x0 = (OPJ_UINT32)l_comp_x0/*l_x0*/;
    130 		l_img_comp->y0 = (OPJ_UINT32)l_comp_y0/*l_y0*/;
    131 		++l_img_comp;
    132 	}
    133 }
    134 
    135 
    136 /**
    137  * Copy only header of image and its component header (no data are copied)
    138  * if dest image have data, they will be freed
    139  *
    140  * @param	p_image_src		the src image
    141  * @param	p_image_dest	the dest image
    142  *
    143  */
    144 void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest)
    145 {
    146 	OPJ_UINT32 compno;
    147 
    148 	/* preconditions */
    149 	assert(p_image_src != 00);
    150 	assert(p_image_dest != 00);
    151 
    152 	p_image_dest->x0 = p_image_src->x0;
    153 	p_image_dest->y0 = p_image_src->y0;
    154 	p_image_dest->x1 = p_image_src->x1;
    155 	p_image_dest->y1 = p_image_src->y1;
    156 
    157 	if (p_image_dest->comps){
    158 		for(compno = 0; compno < p_image_dest->numcomps; compno++) {
    159 			opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
    160 			if(image_comp->data) {
    161 				opj_free(image_comp->data);
    162 			}
    163 		}
    164 		opj_free(p_image_dest->comps);
    165 		p_image_dest->comps = NULL;
    166 	}
    167 
    168 	p_image_dest->numcomps = p_image_src->numcomps;
    169 
    170 	p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * sizeof(opj_image_comp_t));
    171 	if (!p_image_dest->comps){
    172 		p_image_dest->comps = NULL;
    173 		p_image_dest->numcomps = 0;
    174 		return;
    175 	}
    176 
    177 	for (compno=0; compno < p_image_dest->numcomps; compno++){
    178 		memcpy( &(p_image_dest->comps[compno]),
    179 				&(p_image_src->comps[compno]),
    180 				sizeof(opj_image_comp_t));
    181 		p_image_dest->comps[compno].data = NULL;
    182 	}
    183 
    184 	p_image_dest->color_space = p_image_src->color_space;
    185 	p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
    186 
    187 	if (p_image_dest->icc_profile_len) {
    188 		p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(p_image_dest->icc_profile_len);
    189 		if (!p_image_dest->icc_profile_buf){
    190 			p_image_dest->icc_profile_buf = NULL;
    191 			p_image_dest->icc_profile_len = 0;
    192 			return;
    193 		}
    194 		memcpy( p_image_dest->icc_profile_buf,
    195 				p_image_src->icc_profile_buf,
    196 				p_image_src->icc_profile_len);
    197 		}
    198 		else
    199 			p_image_dest->icc_profile_buf = NULL;
    200 
    201 	return;
    202 }
    203 
    204 opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
    205 	OPJ_UINT32 compno;
    206 	opj_image_t *image = 00;
    207 
    208 	image = (opj_image_t*) opj_malloc(sizeof(opj_image_t));
    209 	if (image)
    210 	{
    211 		memset(image,0,sizeof(opj_image_t));
    212 
    213 		image->color_space = clrspc;
    214 		image->numcomps = numcmpts;
    215 
    216 		/* allocate memory for the per-component information */
    217 		image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
    218 		if (!image->comps) {
    219 			opj_image_destroy(image);
    220 			return 00;
    221 		}
    222 		memset(image->comps,0,image->numcomps * sizeof(opj_image_comp_t));
    223 
    224 		/* create the individual image components */
    225 		for(compno = 0; compno < numcmpts; compno++) {
    226 			opj_image_comp_t *comp = &image->comps[compno];
    227 			comp->dx = cmptparms[compno].dx;
    228 			comp->dy = cmptparms[compno].dy;
    229 			comp->w = cmptparms[compno].w;
    230 			comp->h = cmptparms[compno].h;
    231 			comp->x0 = cmptparms[compno].x0;
    232 			comp->y0 = cmptparms[compno].y0;
    233 			comp->prec = cmptparms[compno].prec;
    234 			comp->sgnd = cmptparms[compno].sgnd;
    235 			comp->data = 0;
    236 		}
    237 	}
    238 
    239 	return image;
    240 }
    241