openjp2/jp2: Fix two format strings
[openjpeg.git] / src / lib / openjp2 / jp2.c
index cf1c390b0bcc9328ac92700aabbe5aeefd2a9a21..f15f45ae7718dea4e20855e60bf86b6406a1b9c2 100644 (file)
@@ -162,7 +162,7 @@ static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
                                   opj_event_mgr_t * p_manager);
 
 static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
-                                  opj_stream_private_t *cio,
+                                  opj_stream_private_t *stream,
                                   opj_event_mgr_t * p_manager);
 
 /**
@@ -619,7 +619,7 @@ static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
     opj_read_bytes(p_image_header_data, &(jp2->IPR), 1);        /* IPR */
     ++ p_image_header_data;
 
-    jp2->j2k->m_cp.bpc_is_255 = (jp2->bpc == 255);
+    jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255);
     jp2->j2k->ihdr_w = jp2->w;
     jp2->j2k->ihdr_h = jp2->h;
     jp2->has_ihdr = 1;
@@ -958,26 +958,35 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
         }
         /* verify that no component is targeted more than once */
         for (i = 0; i < nr_channels; i++) {
-            OPJ_UINT16 pcol = cmap[i].pcol;
+            OPJ_BYTE mtyp = cmap[i].mtyp;
+            OPJ_BYTE pcol = cmap[i].pcol;
             /* See ISO 15444-1 Table I.14 – MTYPi field values */
-            if (cmap[i].mtyp != 0 && cmap[i].mtyp != 1) {
+            if (mtyp != 0 && mtyp != 1) {
                 opj_event_msg(p_manager, EVT_ERROR,
                               "Invalid value for cmap[%d].mtyp = %d.\n", i,
-                              cmap[i].mtyp);
+                              mtyp);
                 is_sane = OPJ_FALSE;
             } else if (pcol >= nr_channels) {
                 opj_event_msg(p_manager, EVT_ERROR,
                               "Invalid component/palette index for direct mapping %d.\n", pcol);
                 is_sane = OPJ_FALSE;
-            } else if (pcol_usage[pcol] && cmap[i].mtyp == 1) {
+            } else if (pcol_usage[pcol] && mtyp == 1) {
                 opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
                 is_sane = OPJ_FALSE;
-            } else if (cmap[i].mtyp == 0 && cmap[i].pcol != 0) {
+            } else if (mtyp == 0 && pcol != 0) {
                 /* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
                  * the value of this field shall be 0. */
                 opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
                               pcol);
                 is_sane = OPJ_FALSE;
+            } else if (mtyp == 1 && pcol != i) {
+                /* OpenJPEG implementation limitation. See assert(i == pcol); */
+                /* in opj_jp2_apply_pclr() */
+                opj_event_msg(p_manager, EVT_ERROR,
+                              "Implementation limitation: for palette mapping, "
+                              "pcol[%d] should be equal to %d, but is equal "
+                              "to %d.\n", i, i, pcol);
+                is_sane = OPJ_FALSE;
             } else {
                 pcol_usage[pcol] = OPJ_TRUE;
             }
@@ -996,7 +1005,7 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
                 if (!pcol_usage[i]) {
                     is_sane = 0U;
                     opj_event_msg(p_manager, EVT_WARNING,
-                                  "Component mapping seems wrong. Trying to correct.\n", i);
+                                  "Component mapping seems wrong. Trying to correct.\n");
                     break;
                 }
             }
@@ -1070,11 +1079,11 @@ static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
 
         /* Palette mapping: */
         new_comps[i].data = (OPJ_INT32*)
-                            opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
+                            opj_image_data_alloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(OPJ_INT32));
         if (!new_comps[i].data) {
             while (i > 0) {
                 -- i;
-                opj_free(new_comps[i].data);
+                opj_image_data_free(new_comps[i].data);
             }
             opj_free(new_comps);
             opj_event_msg(p_manager, EVT_ERROR,
@@ -1124,7 +1133,7 @@ static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
     max = image->numcomps;
     for (i = 0; i < max; ++i) {
         if (old_comps[i].data) {
-            opj_free(old_comps[i].data);
+            opj_image_data_free(old_comps[i].data);
         }
     }
 
@@ -1132,8 +1141,6 @@ static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
     image->comps = new_comps;
     image->numcomps = nr_channels;
 
-    opj_jp2_free_pclr(color);
-
     return OPJ_TRUE;
 }/* apply_pclr() */
 
@@ -1600,6 +1607,11 @@ OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2,
         return OPJ_FALSE;
     }
 
+    if (jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
+        /* Bypass all JP2 component transforms */
+        return OPJ_TRUE;
+    }
+
     if (!jp2->ignore_pclr_cmap_cdef) {
         if (!opj_jp2_check_color(p_image, &(jp2->color), p_manager)) {
             return OPJ_FALSE;
@@ -3062,6 +3074,16 @@ void opj_jp2_destroy(opj_jp2_t *jp2)
     }
 }
 
+OPJ_BOOL opj_jp2_set_decoded_components(opj_jp2_t *p_jp2,
+                                        OPJ_UINT32 numcomps,
+                                        const OPJ_UINT32* comps_indices,
+                                        opj_event_mgr_t * p_manager)
+{
+    return opj_j2k_set_decoded_components(p_jp2->j2k,
+                                          numcomps, comps_indices,
+                                          p_manager);
+}
+
 OPJ_BOOL opj_jp2_set_decode_area(opj_jp2_t *p_jp2,
                                  opj_image_t* p_image,
                                  OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
@@ -3093,6 +3115,11 @@ OPJ_BOOL opj_jp2_get_tile(opj_jp2_t *p_jp2,
         return OPJ_FALSE;
     }
 
+    if (p_jp2->j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
+        /* Bypass all JP2 component transforms */
+        return OPJ_TRUE;
+    }
+
     if (!opj_jp2_check_color(p_image, &(p_jp2->color), p_manager)) {
         return OPJ_FALSE;
     }