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