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)
36 opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t));
40 opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts,
41 opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc)
44 opj_image_t *image = NULL;
46 image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
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(1,
52 image->numcomps * sizeof(opj_image_comp_t));
54 /* TODO replace with event manager, breaks API */
55 /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
56 opj_image_destroy(image);
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->bpp = cmptparms[compno].bpp;
70 comp->sgnd = cmptparms[compno].sgnd;
71 comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
73 /* TODO replace with event manager, breaks API */
74 /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
75 opj_image_destroy(image);
84 void OPJ_CALLCONV opj_image_destroy(opj_image_t *image)
90 /* image components */
91 for (compno = 0; compno < image->numcomps; compno++) {
92 opj_image_comp_t *image_comp = &(image->comps[compno]);
93 if (image_comp->data) {
94 opj_free(image_comp->data);
97 opj_free(image->comps);
100 if (image->icc_profile_buf) {
101 opj_free(image->icc_profile_buf);
109 * Updates the components characteristics of the image from the coding parameters.
111 * @param p_image_header the image header to update.
112 * @param p_cp the coding parameters from which to update the image.
114 void opj_image_comp_header_update(opj_image_t * p_image_header,
115 const struct opj_cp * p_cp)
117 OPJ_UINT32 i, l_width, l_height;
118 OPJ_UINT32 l_x0, l_y0, l_x1, l_y1;
119 OPJ_UINT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
120 opj_image_comp_t* l_img_comp = NULL;
122 l_x0 = opj_uint_max(p_cp->tx0, p_image_header->x0);
123 l_y0 = opj_uint_max(p_cp->ty0, p_image_header->y0);
124 l_x1 = p_cp->tx0 + (p_cp->tw - 1U) *
125 p_cp->tdx; /* validity of p_cp members used here checked in opj_j2k_read_siz. Can't overflow. */
126 l_y1 = p_cp->ty0 + (p_cp->th - 1U) * p_cp->tdy; /* can't overflow */
127 l_x1 = opj_uint_min(opj_uint_adds(l_x1, p_cp->tdx),
128 p_image_header->x1); /* use add saturated to prevent overflow */
129 l_y1 = opj_uint_min(opj_uint_adds(l_y1, p_cp->tdy),
130 p_image_header->y1); /* use add saturated to prevent overflow */
132 l_img_comp = p_image_header->comps;
133 for (i = 0; i < p_image_header->numcomps; ++i) {
134 l_comp_x0 = opj_uint_ceildiv(l_x0, l_img_comp->dx);
135 l_comp_y0 = opj_uint_ceildiv(l_y0, l_img_comp->dy);
136 l_comp_x1 = opj_uint_ceildiv(l_x1, l_img_comp->dx);
137 l_comp_y1 = opj_uint_ceildiv(l_y1, l_img_comp->dy);
138 l_width = opj_uint_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
139 l_height = opj_uint_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
140 l_img_comp->w = l_width;
141 l_img_comp->h = l_height;
142 l_img_comp->x0 = l_comp_x0;
143 l_img_comp->y0 = l_comp_y0;
150 * Copy only header of image and its component header (no data are copied)
151 * if dest image have data, they will be freed
153 * @param p_image_src the src image
154 * @param p_image_dest the dest image
157 void opj_copy_image_header(const opj_image_t* p_image_src,
158 opj_image_t* p_image_dest)
163 assert(p_image_src != 00);
164 assert(p_image_dest != 00);
166 p_image_dest->x0 = p_image_src->x0;
167 p_image_dest->y0 = p_image_src->y0;
168 p_image_dest->x1 = p_image_src->x1;
169 p_image_dest->y1 = p_image_src->y1;
171 if (p_image_dest->comps) {
172 for (compno = 0; compno < p_image_dest->numcomps; compno++) {
173 opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
174 if (image_comp->data) {
175 opj_free(image_comp->data);
178 opj_free(p_image_dest->comps);
179 p_image_dest->comps = NULL;
182 p_image_dest->numcomps = p_image_src->numcomps;
184 p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps *
185 sizeof(opj_image_comp_t));
186 if (!p_image_dest->comps) {
187 p_image_dest->comps = NULL;
188 p_image_dest->numcomps = 0;
192 for (compno = 0; compno < p_image_dest->numcomps; compno++) {
193 memcpy(&(p_image_dest->comps[compno]),
194 &(p_image_src->comps[compno]),
195 sizeof(opj_image_comp_t));
196 p_image_dest->comps[compno].data = NULL;
199 p_image_dest->color_space = p_image_src->color_space;
200 p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
202 if (p_image_dest->icc_profile_len) {
203 p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(
204 p_image_dest->icc_profile_len);
205 if (!p_image_dest->icc_profile_buf) {
206 p_image_dest->icc_profile_buf = NULL;
207 p_image_dest->icc_profile_len = 0;
210 memcpy(p_image_dest->icc_profile_buf,
211 p_image_src->icc_profile_buf,
212 p_image_src->icc_profile_len);
214 p_image_dest->icc_profile_buf = NULL;
220 opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts,
221 opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc)
224 opj_image_t *image = 00;
226 image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
229 image->color_space = clrspc;
230 image->numcomps = numcmpts;
232 /* allocate memory for the per-component information */
233 image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps,
234 sizeof(opj_image_comp_t));
236 opj_image_destroy(image);
240 /* create the individual image components */
241 for (compno = 0; compno < numcmpts; compno++) {
242 opj_image_comp_t *comp = &image->comps[compno];
243 comp->dx = cmptparms[compno].dx;
244 comp->dy = cmptparms[compno].dy;
245 comp->w = cmptparms[compno].w;
246 comp->h = cmptparms[compno].h;
247 comp->x0 = cmptparms[compno].x0;
248 comp->y0 = cmptparms[compno].y0;
249 comp->prec = cmptparms[compno].prec;
250 comp->sgnd = cmptparms[compno].sgnd;