[trunk] verify channel definitions is complete when cdef is present (fixes issue...
authorAntonin Descampe <antonin@gmail.com>
Tue, 21 Oct 2014 12:54:08 +0000 (12:54 +0000)
committerAntonin Descampe <antonin@gmail.com>
Tue, 21 Oct 2014 12:54:08 +0000 (12:54 +0000)
src/lib/openjp2/jp2.c

index b6ecba660dbd9728444f5ad198cea022cd21f34d..78a15026a970eae4402d0b540f5c2892ac779641 100644 (file)
@@ -764,6 +764,12 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
        if (color->jp2_cdef) {
                opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
                OPJ_UINT16 n = color->jp2_cdef->n;
+               OPJ_UINT32 nr_channels = image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
+
+               /* cdef applies to cmap channels if any */
+               if (color->jp2_pclr && color->jp2_pclr->cmap) {
+                       nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
+               }
 
                for (i = 0; i < n; i++) {
                        if (info[i].cn >= image->numcomps) {
@@ -775,6 +781,22 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
                                return OPJ_FALSE;
                        }
                }
+
+               /* issue 397 */
+               /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
+               while (nr_channels > 0)
+               {
+                       for(i = 0; i < n; ++i) {
+                               if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
+                                       break;
+                               }
+                       }
+                       if (i == n) {
+                               opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
+                               return OPJ_FALSE;
+                       }
+                       --nr_channels;
+               }
        }
 
        /* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and