Tier 1 decoding: add a colflags array
[openjpeg.git] / src / lib / openjp2 / image.c
index abf1df4be2d7a7346da2830189a2645f8e5b255c..23462f05ff4d1638f88569942a3968ec667bf57f 100644 (file)
@@ -1,4 +1,9 @@
 /*
+ * The copyright in this software is being made available under the 2-clauses 
+ * BSD License, included below. This software may be subject to other third 
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * All rights reserved.
  *
@@ -40,9 +45,10 @@ opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptpa
                image->color_space = clrspc;
                image->numcomps = numcmpts;
                /* allocate memory for the per-component information */
-               image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
+               image->comps = (opj_image_comp_t*)opj_calloc(1,image->numcomps * sizeof(opj_image_comp_t));
                if(!image->comps) {
-                       fprintf(stderr,"Unable to allocate memory for image.\n");
+                       /* TODO replace with event manager, breaks API */
+                       /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
                        opj_image_destroy(image);
                        return NULL;
                }
@@ -58,9 +64,10 @@ opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptpa
                        comp->prec = cmptparms[compno].prec;
                        comp->bpp = cmptparms[compno].bpp;
                        comp->sgnd = cmptparms[compno].sgnd;
-                       comp->data = (int*) opj_calloc(comp->w * comp->h, sizeof(int));
+                       comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
                        if(!comp->data) {
-                               fprintf(stderr,"Unable to allocate memory for image.\n");
+                               /* TODO replace with event manager, breaks API */
+                               /* fprintf(stderr,"Unable to allocate memory for image.\n"); */
                                opj_image_destroy(image);
                                return NULL;
                        }
@@ -99,30 +106,32 @@ void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) {
  * @param p_image_header       the image header to update.
  * @param p_cp                         the coding parameters from which to update the image.
  */
-void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp_v2 * p_cp)
+void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp * p_cp)
 {
        OPJ_UINT32 i, l_width, l_height;
-       OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
-       OPJ_INT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
+       OPJ_UINT32 l_x0, l_y0, l_x1, l_y1;
+       OPJ_UINT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
        opj_image_comp_t* l_img_comp = NULL;
 
-       l_x0 = opj_int_max(p_cp->tx0 , p_image_header->x0);
-       l_y0 = opj_int_max(p_cp->ty0 , p_image_header->y0);
-       l_x1 = opj_int_min(p_cp->tx0 + p_cp->tw * p_cp->tdx, p_image_header->x1);
-       l_y1 = opj_int_min(p_cp->ty0 + p_cp->th * p_cp->tdy, p_image_header->y1);
+       l_x0 = opj_uint_max(p_cp->tx0 , p_image_header->x0);
+       l_y0 = opj_uint_max(p_cp->ty0 , p_image_header->y0);
+       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. */
+       l_y1 = p_cp->ty0 + (p_cp->th - 1U) * p_cp->tdy; /* can't overflow */
+       l_x1 = opj_uint_min(opj_uint_adds(l_x1, p_cp->tdx), p_image_header->x1); /* use add saturated to prevent overflow */
+       l_y1 = opj_uint_min(opj_uint_adds(l_y1, p_cp->tdy), p_image_header->y1); /* use add saturated to prevent overflow */
 
        l_img_comp = p_image_header->comps;
        for     (i = 0; i < p_image_header->numcomps; ++i) {
-               l_comp_x0 = opj_int_ceildiv(l_x0, l_img_comp->dx);
-               l_comp_y0 = opj_int_ceildiv(l_y0, l_img_comp->dy);
-               l_comp_x1 = opj_int_ceildiv(l_x1, l_img_comp->dx);
-               l_comp_y1 = opj_int_ceildiv(l_y1, l_img_comp->dy);
-               l_width = opj_int_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
-               l_height = opj_int_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
+               l_comp_x0 = opj_uint_ceildiv(l_x0, l_img_comp->dx);
+               l_comp_y0 = opj_uint_ceildiv(l_y0, l_img_comp->dy);
+               l_comp_x1 = opj_uint_ceildiv(l_x1, l_img_comp->dx);
+               l_comp_y1 = opj_uint_ceildiv(l_y1, l_img_comp->dy);
+               l_width   = opj_uint_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
+               l_height  = opj_uint_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
                l_img_comp->w = l_width;
                l_img_comp->h = l_height;
-               l_img_comp->x0 = l_comp_x0/*l_x0*/;
-               l_img_comp->y0 = l_comp_y0/*l_y0*/;
+               l_img_comp->x0 = l_comp_x0;
+               l_img_comp->y0 = l_comp_y0;
                ++l_img_comp;
        }
 }
@@ -200,21 +209,19 @@ opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_c
        OPJ_UINT32 compno;
        opj_image_t *image = 00;
 
-       image = (opj_image_t*) opj_malloc(sizeof(opj_image_t));
+       image = (opj_image_t*) opj_calloc(1,sizeof(opj_image_t));
        if (image)
        {
-               memset(image,0,sizeof(opj_image_t));
                
                image->color_space = clrspc;
                image->numcomps = numcmpts;
                
                /* allocate memory for the per-component information */
-               image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
+               image->comps = (opj_image_comp_t*)opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
                if (!image->comps) {
                        opj_image_destroy(image);
                        return 00;
                }
-               memset(image->comps,0,image->numcomps * sizeof(opj_image_comp_t));
                
                /* create the individual image components */
                for(compno = 0; compno < numcmpts; compno++) {