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.
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include "opj_includes.h"
34 opj_image_t* opj_image_create0(void) {
35 opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t));
39 opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
41 opj_image_t *image = NULL;
43 image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
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));
50 fprintf(stderr,"Unable to allocate memory for image.\n");
51 opj_image_destroy(image);
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));
68 fprintf(stderr,"Unable to allocate memory for image.\n");
69 opj_image_destroy(image);
78 void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) {
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);
90 opj_free(image->comps);
93 if(image->icc_profile_buf) {
94 opj_free(image->icc_profile_buf);
102 * Updates the components characteristics of the image from the coding parameters.
104 * @param p_image_header the image header to update.
105 * @param p_cp the coding parameters from which to update the image.
107 void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp * p_cp)
109 OPJ_UINT32 i, l_width, l_height;
110 OPJ_UINT32 l_x0, l_y0, l_x1, l_y1;
111 OPJ_UINT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
112 opj_image_comp_t* l_img_comp = NULL;
114 l_x0 = opj_uint_max(p_cp->tx0 , p_image_header->x0);
115 l_y0 = opj_uint_max(p_cp->ty0 , p_image_header->y0);
116 l_x1 = p_cp->tx0 + (p_cp->tw - 1U) * p_cp->tdx; /* validity of p_cp members used here checked in opj_j2k_read_siz. Can't overflow. */
117 l_y1 = p_cp->ty0 + (p_cp->th - 1U) * p_cp->tdy; /* can't overflow */
118 l_x1 = opj_uint_min(opj_uint_adds(l_x1, p_cp->tdx), p_image_header->x1); /* use add saturated to prevent overflow */
119 l_y1 = opj_uint_min(opj_uint_adds(l_y1, p_cp->tdy), p_image_header->y1); /* use add saturated to prevent overflow */
121 l_img_comp = p_image_header->comps;
122 for (i = 0; i < p_image_header->numcomps; ++i) {
123 l_comp_x0 = opj_uint_ceildiv(l_x0, l_img_comp->dx);
124 l_comp_y0 = opj_uint_ceildiv(l_y0, l_img_comp->dy);
125 l_comp_x1 = opj_uint_ceildiv(l_x1, l_img_comp->dx);
126 l_comp_y1 = opj_uint_ceildiv(l_y1, l_img_comp->dy);
127 l_width = opj_uint_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
128 l_height = opj_uint_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
129 l_img_comp->w = l_width;
130 l_img_comp->h = l_height;
131 l_img_comp->x0 = l_comp_x0;
132 l_img_comp->y0 = l_comp_y0;
139 * Copy only header of image and its component header (no data are copied)
140 * if dest image have data, they will be freed
142 * @param p_image_src the src image
143 * @param p_image_dest the dest image
146 void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest)
151 assert(p_image_src != 00);
152 assert(p_image_dest != 00);
154 p_image_dest->x0 = p_image_src->x0;
155 p_image_dest->y0 = p_image_src->y0;
156 p_image_dest->x1 = p_image_src->x1;
157 p_image_dest->y1 = p_image_src->y1;
159 if (p_image_dest->comps){
160 for(compno = 0; compno < p_image_dest->numcomps; compno++) {
161 opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
162 if(image_comp->data) {
163 opj_free(image_comp->data);
166 opj_free(p_image_dest->comps);
167 p_image_dest->comps = NULL;
170 p_image_dest->numcomps = p_image_src->numcomps;
172 p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * sizeof(opj_image_comp_t));
173 if (!p_image_dest->comps){
174 p_image_dest->comps = NULL;
175 p_image_dest->numcomps = 0;
179 for (compno=0; compno < p_image_dest->numcomps; compno++){
180 memcpy( &(p_image_dest->comps[compno]),
181 &(p_image_src->comps[compno]),
182 sizeof(opj_image_comp_t));
183 p_image_dest->comps[compno].data = NULL;
186 p_image_dest->color_space = p_image_src->color_space;
187 p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
189 if (p_image_dest->icc_profile_len) {
190 p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(p_image_dest->icc_profile_len);
191 if (!p_image_dest->icc_profile_buf){
192 p_image_dest->icc_profile_buf = NULL;
193 p_image_dest->icc_profile_len = 0;
196 memcpy( p_image_dest->icc_profile_buf,
197 p_image_src->icc_profile_buf,
198 p_image_src->icc_profile_len);
201 p_image_dest->icc_profile_buf = NULL;
206 opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
208 opj_image_t *image = 00;
210 image = (opj_image_t*) opj_calloc(1,sizeof(opj_image_t));
214 image->color_space = clrspc;
215 image->numcomps = numcmpts;
217 /* allocate memory for the per-component information */
218 image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
220 opj_image_destroy(image);
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;