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
opj_image_create0(void)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
opj_image_create(OPJ_UINT32 numcmpts,opj_image_cmptparm_t * cmptparms,OPJ_COLOR_SPACE clrspc)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 /* TODO replace with event manager, breaks API */
51 /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
52 opj_image_destroy(image);
53 return NULL;
54 }
55 /* create the individual image components */
56 for(compno = 0; compno < numcmpts; compno++) {
57 opj_image_comp_t *comp = &image->comps[compno];
58 comp->dx = cmptparms[compno].dx;
59 comp->dy = cmptparms[compno].dy;
60 comp->w = cmptparms[compno].w;
61 comp->h = cmptparms[compno].h;
62 comp->x0 = cmptparms[compno].x0;
63 comp->y0 = cmptparms[compno].y0;
64 comp->prec = cmptparms[compno].prec;
65 comp->bpp = cmptparms[compno].bpp;
66 comp->sgnd = cmptparms[compno].sgnd;
67 comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
68 if(!comp->data) {
69 /* TODO replace with event manager, breaks API */
70 /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
71 opj_image_destroy(image);
72 return NULL;
73 }
74 }
75 }
76
77 return image;
78 }
79
opj_image_destroy(opj_image_t * image)80 void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) {
81 if(image) {
82 if(image->comps) {
83 OPJ_UINT32 compno;
84
85 /* image components */
86 for(compno = 0; compno < image->numcomps; compno++) {
87 opj_image_comp_t *image_comp = &(image->comps[compno]);
88 if(image_comp->data) {
89 opj_free(image_comp->data);
90 }
91 }
92 opj_free(image->comps);
93 }
94
95 if(image->icc_profile_buf) {
96 opj_free(image->icc_profile_buf);
97 }
98
99 opj_free(image);
100 }
101 }
102
103 /**
104 * Updates the components characteristics of the image from the coding parameters.
105 *
106 * @param p_image_header the image header to update.
107 * @param p_cp the coding parameters from which to update the image.
108 */
opj_image_comp_header_update(opj_image_t * p_image_header,const struct opj_cp * p_cp)109 void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp * p_cp)
110 {
111 OPJ_UINT32 i, l_width, l_height;
112 OPJ_UINT32 l_x0, l_y0, l_x1, l_y1;
113 OPJ_UINT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
114 opj_image_comp_t* l_img_comp = NULL;
115
116 l_x0 = opj_uint_max(p_cp->tx0 , p_image_header->x0);
117 l_y0 = opj_uint_max(p_cp->ty0 , p_image_header->y0);
118 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. */
119 l_y1 = p_cp->ty0 + (p_cp->th - 1U) * p_cp->tdy; /* can't overflow */
120 l_x1 = opj_uint_min(opj_uint_adds(l_x1, p_cp->tdx), p_image_header->x1); /* use add saturated to prevent overflow */
121 l_y1 = opj_uint_min(opj_uint_adds(l_y1, p_cp->tdy), p_image_header->y1); /* use add saturated to prevent overflow */
122
123 l_img_comp = p_image_header->comps;
124 for (i = 0; i < p_image_header->numcomps; ++i) {
125 l_comp_x0 = opj_uint_ceildiv(l_x0, l_img_comp->dx);
126 l_comp_y0 = opj_uint_ceildiv(l_y0, l_img_comp->dy);
127 l_comp_x1 = opj_uint_ceildiv(l_x1, l_img_comp->dx);
128 l_comp_y1 = opj_uint_ceildiv(l_y1, l_img_comp->dy);
129 l_width = opj_uint_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
130 l_height = opj_uint_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
131 l_img_comp->w = l_width;
132 l_img_comp->h = l_height;
133 l_img_comp->x0 = l_comp_x0;
134 l_img_comp->y0 = l_comp_y0;
135 ++l_img_comp;
136 }
137 }
138
139
140 /**
141 * Copy only header of image and its component header (no data are copied)
142 * if dest image have data, they will be freed
143 *
144 * @param p_image_src the src image
145 * @param p_image_dest the dest image
146 *
147 */
opj_copy_image_header(const opj_image_t * p_image_src,opj_image_t * p_image_dest)148 void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest)
149 {
150 OPJ_UINT32 compno;
151
152 /* preconditions */
153 assert(p_image_src != 00);
154 assert(p_image_dest != 00);
155
156 p_image_dest->x0 = p_image_src->x0;
157 p_image_dest->y0 = p_image_src->y0;
158 p_image_dest->x1 = p_image_src->x1;
159 p_image_dest->y1 = p_image_src->y1;
160
161 if (p_image_dest->comps){
162 for(compno = 0; compno < p_image_dest->numcomps; compno++) {
163 opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
164 if(image_comp->data) {
165 opj_free(image_comp->data);
166 }
167 }
168 opj_free(p_image_dest->comps);
169 p_image_dest->comps = NULL;
170 }
171
172 p_image_dest->numcomps = p_image_src->numcomps;
173
174 p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * sizeof(opj_image_comp_t));
175 if (!p_image_dest->comps){
176 p_image_dest->comps = NULL;
177 p_image_dest->numcomps = 0;
178 return;
179 }
180
181 for (compno=0; compno < p_image_dest->numcomps; compno++){
182 memcpy( &(p_image_dest->comps[compno]),
183 &(p_image_src->comps[compno]),
184 sizeof(opj_image_comp_t));
185 p_image_dest->comps[compno].data = NULL;
186 }
187
188 p_image_dest->color_space = p_image_src->color_space;
189 p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
190
191 if (p_image_dest->icc_profile_len) {
192 p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(p_image_dest->icc_profile_len);
193 if (!p_image_dest->icc_profile_buf){
194 p_image_dest->icc_profile_buf = NULL;
195 p_image_dest->icc_profile_len = 0;
196 return;
197 }
198 memcpy( p_image_dest->icc_profile_buf,
199 p_image_src->icc_profile_buf,
200 p_image_src->icc_profile_len);
201 }
202 else
203 p_image_dest->icc_profile_buf = NULL;
204
205 return;
206 }
207
opj_image_tile_create(OPJ_UINT32 numcmpts,opj_image_cmptparm_t * cmptparms,OPJ_COLOR_SPACE clrspc)208 opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
209 OPJ_UINT32 compno;
210 opj_image_t *image = 00;
211
212 image = (opj_image_t*) opj_calloc(1,sizeof(opj_image_t));
213 if (image)
214 {
215
216 image->color_space = clrspc;
217 image->numcomps = numcmpts;
218
219 /* allocate memory for the per-component information */
220 image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
221 if (!image->comps) {
222 opj_image_destroy(image);
223 return 00;
224 }
225
226 /* create the individual image components */
227 for(compno = 0; compno < numcmpts; compno++) {
228 opj_image_comp_t *comp = &image->comps[compno];
229 comp->dx = cmptparms[compno].dx;
230 comp->dy = cmptparms[compno].dy;
231 comp->w = cmptparms[compno].w;
232 comp->h = cmptparms[compno].h;
233 comp->x0 = cmptparms[compno].x0;
234 comp->y0 = cmptparms[compno].y0;
235 comp->prec = cmptparms[compno].prec;
236 comp->sgnd = cmptparms[compno].sgnd;
237 comp->data = 0;
238 }
239 }
240
241 return image;
242 }
243