[trunk] Fix overflow in opj_image_comp_header_update (fixes issue 495)
[openjpeg.git] / src / lib / openjp2 / image.c
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_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;
113
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 */
120
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;
133                 ++l_img_comp;
134         }
135 }
136
137
138 /**
139  * Copy only header of image and its component header (no data are copied)
140  * if dest image have data, they will be freed
141  *
142  * @param       p_image_src             the src image
143  * @param       p_image_dest    the dest image
144  *
145  */
146 void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest)
147 {
148         OPJ_UINT32 compno;
149
150         /* preconditions */
151         assert(p_image_src != 00);
152         assert(p_image_dest != 00);
153
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;
158
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);
164                         }
165                 }
166                 opj_free(p_image_dest->comps);
167                 p_image_dest->comps = NULL;
168         }
169
170         p_image_dest->numcomps = p_image_src->numcomps;
171
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;
176                 return;
177         }
178
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;
184         }
185
186         p_image_dest->color_space = p_image_src->color_space;
187         p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
188
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;
194                         return;
195                 }
196                 memcpy( p_image_dest->icc_profile_buf,
197                                 p_image_src->icc_profile_buf,
198                                 p_image_src->icc_profile_len);
199                 }
200                 else
201                         p_image_dest->icc_profile_buf = NULL;
202
203         return;
204 }
205
206 opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
207         OPJ_UINT32 compno;
208         opj_image_t *image = 00;
209
210         image = (opj_image_t*) opj_calloc(1,sizeof(opj_image_t));
211         if (image)
212         {
213                 
214                 image->color_space = clrspc;
215                 image->numcomps = numcmpts;
216                 
217                 /* allocate memory for the per-component information */
218                 image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
219                 if (!image->comps) {
220                         opj_image_destroy(image);
221                         return 00;
222                 }
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 }