[gs-commits] rev 11867 - in branches/jpx_openjpeg/gs: base openjpeg/libopenjpeg

larsu at ghostscript.com larsu at ghostscript.com
Thu Oct 28 16:11:35 UTC 2010


Author: larsu
Date: 2010-10-28 16:11:35 +0000 (Thu, 28 Oct 2010)
New Revision: 11867

Modified:
   branches/jpx_openjpeg/gs/base/sjpx_opj.c
   branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.c
   branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.h
   branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/openjpeg.h
Log:
Patch openjpeg to report component types correctly.



Modified: branches/jpx_openjpeg/gs/base/sjpx_opj.c
===================================================================
--- branches/jpx_openjpeg/gs/base/sjpx_opj.c	2010-10-28 15:00:37 UTC (rev 11866)
+++ branches/jpx_openjpeg/gs/base/sjpx_opj.c	2010-10-28 16:11:35 UTC (rev 11867)
@@ -20,7 +20,6 @@
 #include "strimpl.h"
 #include "gsmalloc.h"
 #include "sjpx_opj.h"
-#include <stdarg.h>
 
 static void s_jpxd_set_defaults(stream_state *ss);
 
@@ -68,32 +67,44 @@
 }
 
 
-/* Maps JPX components to the predefined order given in the varargs.
+static int
+nr_color_components(opj_image_t *image)
+{
+    int i, cnt = 0;
+
+    for (i = 0; i < image->numcomps; i++) {
+	OPJ_COMPONENT_TYPE type = image->comps[i].comp_type;
+	if (type != COMPTYPE_UNKNOWN && type != COMPTYPE_OPACITY)
+	    cnt++;
+    }
+    return cnt;
+}
+
+
+/* Maps JPX components to their "natural" order, i.e. RGB, CMYK, etc.
  * 'comps' receives pointers to components in that order.
  */
 static int
-map_components(opj_image_t *image, opj_image_comp_t *comps[], int ncomps, ...)
+map_color_components(opj_image_t *image, opj_image_comp_t *comps[], int ncomps)
 {
-    va_list ap;
-    int n;
+    int n, i;
 
-    va_start(ap, ncomps);
     for (n = 0; n < ncomps; n++) {
-	int i;
-	int type = va_arg(ap, int);
+	comps[n] = NULL;
 
-	for (i = 0; i < ncomps; i++) {
-	    if (image->comps[i].comp_type == type) {
-		comps[n] = &image->comps[i];
+	for (i = 0; i < image->numcomps; i++) {
+	    opj_image_comp_t *comp = &image->comps[i];
+	    if (comp->comp_type == COMPTYPE_COLOR && comp->assoc == n +1) {
+		comps[n] = comp;
 		break;
 	    }
 	}
-	if (i == ncomps) {
+
+	if (!comps[n]) {
 	    dprintf("**Error: corrupt JPX stream.\n");
 	    return -1;
 	}
     }
-    va_end(ap);
     return 0;
 }
 
@@ -105,7 +116,7 @@
     int i, j, bits, *p;
     opj_image_comp_t *comp;
 
-    if (map_components(image, &comp, 1, COMPTYPE_Y) < 0)
+    if (map_color_components(image, &comp, 1) < 0)
 	return 0;
 
     j = y * comp->w + x;
@@ -144,7 +155,7 @@
     const int count = (bytes / 3) * 3;
     opj_image_comp_t *comps[3];
 
-    if (map_components(image, comps, 3, COMPTYPE_R, COMPTYPE_G, COMPTYPE_B) < 0)
+    if (map_color_components(image, comps, 3) < 0)
 	return 0;
 
     j = y * comps[0]->w + x;
@@ -174,7 +185,7 @@
     int p[3], q[3];
     opj_image_comp_t *comps[3];
 
-    if (map_components(image, comps, 3, COMPTYPE_Y, COMPTYPE_CB, COMPTYPE_CR) < 0)
+    if (map_color_components(image, comps, 3) < 0)
 	return 0;
 
     for (i = 0; i < 3; i++) {
@@ -230,8 +241,7 @@
     int count = (bytes / 4) * 4;
     unsigned char *pdest = dest + 1;   /* stream offset */
 
-    if (map_components(image, comps, 4,
-		       COMPTYPE_C, COMPTYPE_M, COMPTYPE_Y, COMPTYPE_K) < 0)
+    if (map_color_components(image, comps, 4) < 0)
 	return 0;
 
     for (i = 0; i < 4; i++) {
@@ -254,7 +264,7 @@
 {
     int i, c;
     const int b = y * image->comps[0].w;
-    const int n = image->numcomps;
+    const int n = nr_color_components(image);
     const int count = (bytes / n) * n;
     unsigned char *p = dest +1;   /* stream offset */
 
@@ -381,7 +391,7 @@
         }
         if (state->opj_image != NULL) {
             opj_image_t *image = state->opj_image;
-            int numcmpts = image->numcomps;
+            int numcmpts = nr_color_components(image);
             int bits = image->comps[0].prec;
 	    int dst_bits = bits <= 8 ? bits : 16;
             int stride = (numcmpts * image->comps[0].w * dst_bits + 7) / 8;
@@ -443,6 +453,7 @@
 		    case CLRSPC_CMYK:
 			if (numcmpts < 4) return ERRC;
 			done = copy_row_cmyk(pw->ptr, image, x, y, usable);
+			break;
 		    case CLRSPC_UNKNOWN:
 		    default:
 			done = copy_row_default(pw->ptr, image, x, y, usable);

Modified: branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.c
===================================================================
--- branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.c	2010-10-28 15:00:37 UTC (rev 11866)
+++ branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.c	2010-10-28 16:11:35 UTC (rev 11867)
@@ -60,9 +60,6 @@
 
 #define JP2_CDEF_GRAY_Y 1
 
-#define JP2_CDEF_TYPE_COLOR 0
-#define JP2_CDEF_TYPE_OPACITY 1
-
 /** @name Local static functions */
 /*@{*/
 
@@ -323,20 +320,30 @@
 	}
 
 	if (cio_tell(cio) - box.init_pos != box.length) {
-		opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
+		opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
 		return false;
 	}
+
+	/* skip further colr boxes */
+	while (1) {
+	    int oldpos = cio_tell(cio);
+	    jp2_read_boxhdr(cinfo, cio, &box);
+	    if (box.type != JP2_COLR) {
+		cio_seek(cio, oldpos);
+		break;
+	    }
+	}
+
 	return true;
 }
 
 static void jp2_write_cdef(opj_jp2_t *jp2, opj_cio_t *cio) {
 	opj_jp2_box_t box;
-	int i, type, assoc;
+	int i;
 
 	for (i = 0; i < jp2->numcomps; i++) {
 		/* don't write cdef if any comp type is not defined */
-		if (jp2->comps[i].comp_type <= COMPTYPE_UNKNOWN ||
-			jp2->comps[i].comp_type > COMPTYPE_OPACITY) {
+		if (jp2->comps[i].comp_type == COMPTYPE_UNKNOWN) {
 			return;
 		}
 	}
@@ -348,60 +355,10 @@
 	cio_write(cio, jp2->numcomps, 2); /* number of comps */
 	for (i = 0; i < jp2->numcomps; i++) {
 		cio_write(cio, i, 2);  /* channel number */
-
-		type = JP2_CDEF_TYPE_COLOR;
-		assoc = 0;
-
-		switch (jp2->comps[i].comp_type) {
-			case COMPTYPE_UNKNOWN:
-				break;
-			case COMPTYPE_R:
-				assoc = JP2_CDEF_RGB_R;
-				break;
-			case COMPTYPE_G:
-				assoc = JP2_CDEF_RGB_G;
-				break;
-			case COMPTYPE_B:
-				assoc = JP2_CDEF_RGB_B;
-				break;
-			case COMPTYPE_Y:
-				switch (jp2->enumcs) {
-				    case JP2_COLR_SYCC:
-					assoc = JP2_CDEF_YCC_Y;
-					break;
-				    case JP2_COLR_CMYK:
-					assoc = JP2_CDEF_CMYK_Y;
-					break;
-				    case JP2_CDEF_GRAY_Y:
-				    default:
-					assoc = JP2_CDEF_GRAY_Y;
-					break;
+		cio_write(cio, jp2->comps[i].comp_type, 2);  /* channel type */
+		cio_write(cio, jp2->comps[i].assoc, 2);  /* color associated with channel */
 				}
-				break;
-			case COMPTYPE_CB:
-				assoc = JP2_CDEF_YCC_CB;
-				break;
-			case COMPTYPE_CR:
-				assoc = JP2_CDEF_YCC_CR;
-				break;
-			case COMPTYPE_OPACITY:
-				type = JP2_CDEF_TYPE_OPACITY;
-				break;
-			case COMPTYPE_C:
-				type = JP2_CDEF_CMYK_C;
-				break;
-			case COMPTYPE_M:
-				type = JP2_CDEF_CMYK_M;
-				break;
-			case COMPTYPE_K:
-				type = JP2_CDEF_CMYK_K;
-				break;
-		}
 
-		cio_write(cio, type, 2);  /* channel type */
-		cio_write(cio, assoc, 2);  /* color associated with channel */
-	}
-
 	box.length = cio_tell(cio) - box.init_pos;
 	cio_seek(cio, box.init_pos);
 	cio_write(cio, box.length, 4);
@@ -410,55 +367,26 @@
 
 static bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio) {
 	opj_jp2_box_t box;
-	int i, oldpos, numchannels, no, type, assoc;
+	int i, oldpos, numchannels, no;
 	opj_jp2_comps_t *comp;
 
 	opj_common_ptr cinfo = jp2->cinfo;
 
-	/* set default component type */
-	switch (jp2->numcomps) {
-	    case 1:
-		jp2->comps[0].comp_type = COMPTYPE_Y;
-		break;
-
-	    case 3:
-		switch (jp2->enumcs) {
-			case JP2_COLR_SRGB:
-				jp2->comps[0].comp_type = COMPTYPE_R;
-				jp2->comps[1].comp_type = COMPTYPE_G;
-				jp2->comps[2].comp_type = COMPTYPE_B;
-				break;
-
-			case JP2_COLR_SYCC:
-				jp2->comps[0].comp_type = COMPTYPE_Y;
-				jp2->comps[1].comp_type = COMPTYPE_CB;
-				jp2->comps[2].comp_type = COMPTYPE_CR;
-				break;
-		}
-		break;
-
-	    case 4:
-		if (jp2->enumcs == JP2_COLR_CMYK) {
-		    jp2->comps[0].comp_type = COMPTYPE_C;
-		    jp2->comps[1].comp_type = COMPTYPE_M;
-		    jp2->comps[2].comp_type = COMPTYPE_Y;
-		    jp2->comps[3].comp_type = COMPTYPE_K;
-		    break;
-		}
-		/* else fall through */
-
-	    default:
-		for (i = 0; i < jp2->numcomps; i++)
-			jp2->comps[i].comp_type = COMPTYPE_UNKNOWN;
-		break;
-	}
-
 	/* try to read CDEF box */
 	oldpos = cio_tell(cio);
 	jp2_read_boxhdr(cinfo, cio, &box);
 
 	if (JP2_CDEF != box.type) {
 		cio_seek(cio, oldpos);
+
+		/* if no cdef box exists, all components are color components
+		 * and, ordered in ther "natural" order */
+
+		for (i = 0; i < jp2->numcomps; i++) {
+		    jp2->comps[i].comp_type = COMPTYPE_COLOR;
+		    jp2->comps[i].assoc = i +1;
+		}
+
 		return false;
 	}
 
@@ -466,64 +394,11 @@
 
 	for (i = 0; i < numchannels; i++) {
 		no = cio_read(cio, 2);
-		type = cio_read(cio, 2);
-		assoc = cio_read(cio, 2);
 		comp = &jp2->comps[no];
 
-		/* set comp type according to channel type and color association */
-		if (type == JP2_CDEF_TYPE_OPACITY && assoc == 0) {
-			comp->comp_type = COMPTYPE_OPACITY;
-		} else if (type == JP2_CDEF_TYPE_COLOR && assoc >= 1 && assoc <= 65534) {
-			switch (jp2->enumcs) {
-				case JP2_COLR_SRGB:
-					switch (assoc) {
-						case JP2_CDEF_RGB_R:
-							comp->comp_type = COMPTYPE_R;
-							break;
-						case JP2_CDEF_RGB_G:
-							comp->comp_type = COMPTYPE_G;
-							break;
-						case JP2_CDEF_RGB_B:
-							comp->comp_type = COMPTYPE_B;
-							break;
+		comp->comp_type = cio_read(cio, 2);
+		comp->assoc = cio_read(cio, 2);
 					}
-					break;
-				case JP2_COLR_SYCC:
-					switch (assoc) {
-						case JP2_CDEF_YCC_Y:
-							comp->comp_type = COMPTYPE_Y;
-							break;
-						case JP2_CDEF_YCC_CB:
-							comp->comp_type = COMPTYPE_CB;
-							break;
-						case JP2_CDEF_YCC_CR:
-							comp->comp_type = COMPTYPE_CR;
-							break;
-					}
-					break;
-				case JP2_COLR_CMYK:
-					switch (assoc) {
-					    case JP2_CDEF_CMYK_C:
-						comp->comp_type = COMPTYPE_C;
-						break;
-					    case JP2_CDEF_CMYK_M:
-						comp->comp_type = COMPTYPE_M;
-						break;
-					    case JP2_CDEF_CMYK_Y:
-						comp->comp_type = COMPTYPE_Y;
-						break;
-					    case JP2_CDEF_CMYK_K:
-						comp->comp_type = COMPTYPE_K;
-						break;
-					}
-					break;
-				case JP2_COLR_GRAY:
-					if (assoc == JP2_CDEF_GRAY_Y)
-						comp->comp_type = COMPTYPE_Y;
-					break;
-			}
-		}
-	}
 
 	if (cio_tell(cio) - box.init_pos != box.length) {
 		opj_event_msg(cinfo, EVT_ERROR, "Error with CDEF Box\n");
@@ -582,7 +457,7 @@
 	}
 
 	/* extract the resolution for each components */
-	jp2->pclr->Bj = (int *)opj_malloc(jp2->pclr->nbrcomps*sizeof(int));
+	jp2->pclr->Bj = (long *)opj_malloc(jp2->pclr->nbrcomps*sizeof(int));
 	if(jp2->pclr->Bj == NULL) {
 		return false;
 	}
@@ -986,6 +861,7 @@
 	/* Set image component types */
 	for (i = 0; i < image->numcomps; i++) {
 		image->comps[i].comp_type = jp2->comps[i].comp_type;
+		image->comps[i].assoc = jp2->comps[i].assoc;
 	}
 
 	/* Copy the ICC profile */

Modified: branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.h
===================================================================
--- branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.h	2010-10-28 15:00:37 UTC (rev 11866)
+++ branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/jp2.h	2010-10-28 16:11:35 UTC (rev 11867)
@@ -64,6 +64,7 @@
   int sgnd;		   
   int bpcc;
   OPJ_COMPONENT_TYPE comp_type;
+  int assoc;
 } opj_jp2_comps_t;
 
 /**

Modified: branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/openjpeg.h
===================================================================
--- branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/openjpeg.h	2010-10-28 15:00:37 UTC (rev 11866)
+++ branches/jpx_openjpeg/gs/openjpeg/libopenjpeg/openjpeg.h	2010-10-28 16:11:35 UTC (rev 11867)
@@ -161,15 +161,10 @@
 Supported image component types
 */
 typedef enum COMPONENT_TYPE {
-	COMPTYPE_UNKNOWN = 0,	/** unknown component type, cdef box not present */
-	COMPTYPE_R = 1,			/** red component of sRGB image */
-	COMPTYPE_G = 2,			/** green component of sRGB image */
-	COMPTYPE_B = 3,			/** blue component of sRGB image */
-	COMPTYPE_Y = 4,			/** luminance component of YUV and grayscale images */
-	COMPTYPE_CB = 5,		/** Cb component of YUV image */
-	COMPTYPE_CR = 6,		/** Cr component of YUV image */
-	COMPTYPE_OPACITY = 7,	/** opacity/alpha channel */
-	COMPTYPE_C, COMPTYPE_M, COMPTYPE_K
+	COMPTYPE_COLOR = 0,	    /** color component */
+	COMPTYPE_OPACITY = 1,	    /** opacity/alpha channel */
+	COMPTYPE_PREMUL_OPACITY = 2,/* premultiplied opacity channel */
+	COMPTYPE_UNKNOWN = 0xffff   /** component type not specified */
 } OPJ_COMPONENT_TYPE;
 
 /**
@@ -553,6 +548,8 @@
 	int factor;
 	/** type of this component: color channel, opacity, ... */
 	OPJ_COMPONENT_TYPE comp_type;
+	/** associated color  */
+	int assoc;
 	/** image component data */
 	int *data;
 } opj_image_comp_t;



More information about the gs-commits mailing list