openjp2/j2k: Report error if all wanted components are not decoded. 1164/head
authorSebastian Rasmussen <sebras@gmail.com>
Wed, 31 Oct 2018 19:22:11 +0000 (20:22 +0100)
committerSebastian Rasmussen <sebras@gmail.com>
Thu, 21 Feb 2019 08:48:02 +0000 (16:48 +0800)
Previously the caller had to check whether each component data had
been decoded. This means duplicating the checking in every user of
openjpeg which is unnecessary. If the caller wantes to decode all
or a set of, or a specific component then openjpeg ought to error
out if it was unable to do so.

Fixes #1158.

src/bin/jp2/opj_decompress.c
src/lib/openjp2/j2k.c

index 4b9583b798e0c245b72cfff1b0c70b50e2ff4525..7eeb0952f672a0b86284ab0e767aa804ef2bcd52 100644 (file)
@@ -1571,17 +1571,6 @@ int main(int argc, char **argv)
             }
         }
 
-        /* FIXME? Shouldn't that situation be considered as an error of */
-        /* opj_decode() / opj_get_decoded_tile() ? */
-        if (image->comps[0].data == NULL) {
-            fprintf(stderr, "ERROR -> opj_decompress: no image data!\n");
-            opj_destroy_codec(l_codec);
-            opj_stream_destroy(l_stream);
-            opj_image_destroy(image);
-            failed = 1;
-            goto fin;
-        }
-
         tCumulative += opj_clock() - t;
         numDecompressedImages++;
 
index 4169cd672b78eae5a7ea9222b0288820ccbd9772..8aaa34e8f222e5eebdb52e0bd82742dd2eadc85a 100644 (file)
@@ -10657,6 +10657,42 @@ static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
     return OPJ_TRUE;
 }
 
+static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k,
+        opj_event_mgr_t * p_manager)
+{
+    OPJ_UINT32 compno;
+    OPJ_BOOL decoded_all_used_components = OPJ_TRUE;
+
+    if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
+        for (compno = 0;
+                compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
+            OPJ_UINT32 dec_compno =
+                p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
+            if (p_j2k->m_output_image->comps[dec_compno].data == NULL) {
+                opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
+                              dec_compno);
+                decoded_all_used_components = OPJ_FALSE;
+            }
+        }
+    } else {
+        for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) {
+            if (p_j2k->m_output_image->comps[compno].data == NULL) {
+                opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
+                              compno);
+                decoded_all_used_components = OPJ_FALSE;
+            }
+        }
+    }
+
+    if (decoded_all_used_components == OPJ_FALSE) {
+        opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n");
+        return OPJ_FALSE;
+    }
+
+    return OPJ_TRUE;
+}
+
+
 static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
                                      opj_stream_private_t *p_stream,
                                      opj_event_mgr_t * p_manager)
@@ -10768,6 +10804,10 @@ static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
         }
     }
 
+    if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
+        return OPJ_FALSE;
+    }
+
     return OPJ_TRUE;
 }
 
@@ -10896,6 +10936,10 @@ static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k,
 
     }
 
+    if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
+        return OPJ_FALSE;
+    }
+
     return OPJ_TRUE;
 }