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