[gs-commits] rev 11000 - branches/jpx_openjpeg/gs/base

masaki at ghostscript.com masaki at ghostscript.com
Wed Mar 31 13:48:15 UTC 2010


Author: masaki
Date: 2010-03-31 13:48:15 +0000 (Wed, 31 Mar 2010)
New Revision: 11000

Modified:
   branches/jpx_openjpeg/gs/base/sjpx_opj.c
   branches/jpx_openjpeg/gs/base/sjpx_opj.h
Log:
Now displays RGB image

Modified: branches/jpx_openjpeg/gs/base/sjpx_opj.c
===================================================================
--- branches/jpx_openjpeg/gs/base/sjpx_opj.c	2010-03-31 13:22:01 UTC (rev 10999)
+++ branches/jpx_openjpeg/gs/base/sjpx_opj.c	2010-03-31 13:48:15 UTC (rev 11000)
@@ -31,16 +31,15 @@
 */
 
 private_st_jpxd_state(); /* creates a gc object for our state,
-			    defined in sjpx.h */
+                            defined in sjpx.h */
 
-#if 0
 /* error reporting callback for the jpx library */
 static void
-s_jpx_jas_error_cb(jas_error_t err, char *msg)
+s_jpx_opj_error_cb(const char *msg, void *refcon)
 {
-  dprintf2("jasper (code %d) %s", (int)err, msg);
+    (void)refcon;
+    dprintf1("libopenjpeg: %s", msg);
 }
-#endif
 
 
 /* initialize the steam.
@@ -52,27 +51,20 @@
 {
     stream_jpxd_state *const state = (stream_jpxd_state *) ss;
     int status = 0;
-#if 0
+
     if (state->jpx_memory == NULL) {
-      state->jpx_memory = ss->memory ?
-		ss->memory->non_gc_memory :
-		gs_lib_ctx_get_non_gc_memory_t();
+        state->jpx_memory = ss->memory ?
+                ss->memory->non_gc_memory :
+                gs_lib_ctx_get_non_gc_memory_t();
     }
 
-    status = jas_init();
-    jas_set_error_cb(s_jpx_jas_error_cb);
-#ifdef JPX_DEBUG
-    /* raise the error reporting threshold from the default (0) */
-    jas_setdbglevel(1);
-#endif
-
     if (!status) {
-	state->buffer = gs_malloc(state->jpx_memory, 4096, 1, "JPXDecode temp buffer");
+        state->buffer = gs_malloc(state->jpx_memory, 4096, 1, "JPXDecode temp buffer");
         status = (state->buffer == NULL);
     }
     if (!status)
-    	state->bufsize = 4096;
-#endif
+        state->bufsize = 4096;
+
     return status;
 }
 
@@ -89,55 +81,55 @@
     if (image == NULL) return 1;
 
     if_debug2('w', "[w]JPX image is %d x %d\n",
-	(int)jas_image_width(image), (int)jas_image_height(image));
+        (int)jas_image_width(image), (int)jas_image_height(image));
 
     /* sort the colorspace */
     if jas_clrspc_isunknown(clrspc) csname = "unknown";
     else switch (clrspc) {
-	case JAS_CLRSPC_CIEXYZ: csname = "CIE XYZ"; break;
-	case JAS_CLRSPC_CIELAB: csname = "CIE Lab"; break;
-	case JAS_CLRSPC_SGRAY: csname = "calibrated grayscale"; break;
-	case JAS_CLRSPC_SRGB: csname = "sRGB"; break;
-	case JAS_CLRSPC_SYCBCR: csname = "calibrated YCbCr"; break;
-	case JAS_CLRSPC_GENGRAY: csname = "generic gray"; break;
-	case JAS_CLRSPC_GENRGB: csname = "generic RGB"; break;
-	case JAS_CLRSPC_GENYCBCR: csname = "generic YCbCr"; break;
+        case JAS_CLRSPC_CIEXYZ: csname = "CIE XYZ"; break;
+        case JAS_CLRSPC_CIELAB: csname = "CIE Lab"; break;
+        case JAS_CLRSPC_SGRAY: csname = "calibrated grayscale"; break;
+        case JAS_CLRSPC_SRGB: csname = "sRGB"; break;
+        case JAS_CLRSPC_SYCBCR: csname = "calibrated YCbCr"; break;
+        case JAS_CLRSPC_GENGRAY: csname = "generic gray"; break;
+        case JAS_CLRSPC_GENRGB: csname = "generic RGB"; break;
+        case JAS_CLRSPC_GENYCBCR: csname = "generic YCbCr"; break;
     }
     if_debug3('w',"[w]  colorspace is %s (family %d, member %d)\n",
-	csname, jas_clrspc_fam(clrspc), jas_clrspc_mbr(clrspc));
+        csname, jas_clrspc_fam(clrspc), jas_clrspc_mbr(clrspc));
 
     for (i = 0; i < numcmpts; i++) {
-	int type = jas_image_cmpttype(image, i);
-	const char *opacity = (type & JAS_IMAGE_CT_OPACITY) ? " opacity" : "";
-	const char *name = "unrecognized";
-	const char *issigned = "";
-	if (jas_clrspc_fam(clrspc) == JAS_CLRSPC_FAM_GRAY)
-	    name = "gray";
-	else if (jas_clrspc_fam(clrspc) == JAS_CLRSPC_FAM_RGB)
-	    switch (JAS_IMAGE_CT_COLOR(type)) {
-		case JAS_IMAGE_CT_RGB_R: name = "red"; break;
-		case JAS_IMAGE_CT_RGB_G: name = "green"; break;
-		case JAS_IMAGE_CT_RGB_B: name = "blue"; break;
-		case JAS_IMAGE_CT_UNKNOWN:
-		default:
-		    name = "unknown";
-	    }
-	else if (jas_clrspc_fam(clrspc) == JAS_CLRSPC_FAM_YCBCR)
-	    switch (JAS_IMAGE_CT_COLOR(type)) {
-		case JAS_IMAGE_CT_YCBCR_Y: name = "luminance Y"; break;
-		case JAS_IMAGE_CT_YCBCR_CB: name = "chrominance Cb"; break;
-		case JAS_IMAGE_CT_YCBCR_CR: name = "chrominance Cr"; break;
-		case JAS_IMAGE_CT_UNKNOWN:
-		default:
-		    name = "unknown";
-	    }
-	if (jas_image_cmptsgnd(image, i))
-	    issigned = ", signed";
-	if_debug6('w', "[w]  component %d: type %d '%s%s' (%d bits%s)",
-	    i, type, name, opacity, jas_image_cmptprec(image, i), issigned);
-	if_debug4('w', " grid step (%d,%d) offset (%d,%d)\n",
-	    (int)jas_image_cmpthstep(image, i), (int)jas_image_cmptvstep(image, i),
-	    (int)jas_image_cmpttlx(image, i), (int)jas_image_cmpttly(image, i));
+        int type = jas_image_cmpttype(image, i);
+        const char *opacity = (type & JAS_IMAGE_CT_OPACITY) ? " opacity" : "";
+        const char *name = "unrecognized";
+        const char *issigned = "";
+        if (jas_clrspc_fam(clrspc) == JAS_CLRSPC_FAM_GRAY)
+            name = "gray";
+        else if (jas_clrspc_fam(clrspc) == JAS_CLRSPC_FAM_RGB)
+            switch (JAS_IMAGE_CT_COLOR(type)) {
+                case JAS_IMAGE_CT_RGB_R: name = "red"; break;
+                case JAS_IMAGE_CT_RGB_G: name = "green"; break;
+                case JAS_IMAGE_CT_RGB_B: name = "blue"; break;
+                case JAS_IMAGE_CT_UNKNOWN:
+                default:
+                    name = "unknown";
+            }
+        else if (jas_clrspc_fam(clrspc) == JAS_CLRSPC_FAM_YCBCR)
+            switch (JAS_IMAGE_CT_COLOR(type)) {
+                case JAS_IMAGE_CT_YCBCR_Y: name = "luminance Y"; break;
+                case JAS_IMAGE_CT_YCBCR_CB: name = "chrominance Cb"; break;
+                case JAS_IMAGE_CT_YCBCR_CR: name = "chrominance Cr"; break;
+                case JAS_IMAGE_CT_UNKNOWN:
+                default:
+                    name = "unknown";
+            }
+        if (jas_image_cmptsgnd(image, i))
+            issigned = ", signed";
+        if_debug6('w', "[w]  component %d: type %d '%s%s' (%d bits%s)",
+            i, type, name, opacity, jas_image_cmptprec(image, i), issigned);
+        if_debug4('w', " grid step (%d,%d) offset (%d,%d)\n",
+            (int)jas_image_cmpthstep(image, i), (int)jas_image_cmptvstep(image, i),
+            (int)jas_image_cmpttlx(image, i), (int)jas_image_cmpttly(image, i));
     }
 
     return 0;
@@ -163,16 +155,18 @@
   }
 
   if_debug1('w', "[w]Interpreter has set an external %s color space\n",
-	cspace);
+        cspace);
 
   return 0;
 }
 #endif /* DEBUG */
+#endif
 
 static int
-copy_row_gray(unsigned char *dest, jas_image_t *image,
-	int x, int y, int bytes)
+copy_row_gray(unsigned char *dest, opj_image_t *image,
+        int x, int y, int bytes)
 {
+#if 0
     int i, p;
     int v;
     int shift, bits;
@@ -182,32 +176,52 @@
 
     bits = jas_image_cmptprec(image, v);
     if (bits >= 8) {
-	/* shift down to 8 bpp */
-	shift = max(jas_image_cmptprec(image, v) - 8, 0);
+        /* shift down to 8 bpp */
+        shift = max(jas_image_cmptprec(image, v) - 8, 0);
 
-	for (i = 1; i <= bytes; i++) {
-	    p = jas_image_readcmptsample(image, v, x++, y);
-	    dest[i] = p >> shift;
-	}
+        for (i = 1; i <= bytes; i++) {
+            p = jas_image_readcmptsample(image, v, x++, y);
+            dest[i] = p >> shift;
+        }
    } else if (bits == 4) {
-	/* return two packed pixels per byte */
-	for (i = 1; i <= bytes; i++) {
-	    p = jas_image_readcmptsample(image, v, x++, y) << 4;
-	    p |= jas_image_readcmptsample(image, v, x++, y);
-	    dest[i] = p;
-	}
+        /* return two packed pixels per byte */
+        for (i = 1; i <= bytes; i++) {
+            p = jas_image_readcmptsample(image, v, x++, y) << 4;
+            p |= jas_image_readcmptsample(image, v, x++, y);
+            dest[i] = p;
+        }
     } else {
-	/* todo: handle other bit depths */
-	memset(dest + 1, 0x80, bytes);
+        /* todo: handle other bit depths */
+        memset(dest + 1, 0x80, bytes);
     }
 
     return bytes;
+#endif
+    return 0;
 }
 
 static int
-copy_row_rgb(unsigned char *dest, jas_image_t *image,
-	int x, int y, int bytes)
+copy_row_rgb(unsigned char *dest, opj_image_t *image,
+        int x, int y, int bytes)
 {
+    int i;
+    int j = y * image->comps[0].w + x;
+    int *pr = &image->comps[0].data[j];
+    int *pg = &image->comps[1].data[j];
+    int *pb = &image->comps[2].data[j];
+    int shiftR = image->comps[0].prec > 8 ? image->comps[0].prec - 8 : 0;
+    int shiftG = image->comps[1].prec > 8 ? image->comps[1].prec - 8 : 0;
+    int shiftB = image->comps[2].prec > 8 ? image->comps[2].prec - 8 : 0;
+    int count = (bytes / 3) * 3;
+
+    for ( i = 1; i <= count; ) {
+        dest[i++] = *pr++ >> shiftR;
+        dest[i++] = *pg++ >> shiftG;
+        dest[i++] = *pb++ >> shiftB;
+    }
+    return count;
+
+#if 0
     int i, p;
     int r = jas_image_getcmptbytype(image, JAS_IMAGE_CT_RGB_R);
     int g = jas_image_getcmptbytype(image, JAS_IMAGE_CT_RGB_G);
@@ -219,22 +233,24 @@
     if (r < 0 || g < 0 || b < 0) return 0;
 
     for (i = 1; i <= count; i+=3) {
-	p = jas_image_readcmptsample(image, r, x, y);
-	dest[i] = p >> shift;
-	p = jas_image_readcmptsample(image, g, x, y);
-	dest[i+1] = p >> shift;
-	p = jas_image_readcmptsample(image, b, x, y);
-	dest[i+2] = p >> shift;
-	x++;
+        p = jas_image_readcmptsample(image, r, x, y);
+        dest[i] = p >> shift;
+        p = jas_image_readcmptsample(image, g, x, y);
+        dest[i+1] = p >> shift;
+        p = jas_image_readcmptsample(image, b, x, y);
+        dest[i+2] = p >> shift;
+        x++;
     }
 
     return count;
+#endif
 }
 
 static int
-copy_row_yuv(unsigned char *dest, jas_image_t *image,
-	int x, int y, int bytes)
+copy_row_yuv(unsigned char *dest, opj_image_t *image,
+        int x, int y, int bytes)
 {
+#if 0
     int i,j;
     int count = (bytes/3) * 3;
     int shift[3];
@@ -251,68 +267,73 @@
     if (clut[0] < 0 || clut[1] < 0 || clut[2] < 0) return 0;
 
     for (i = 0; i < 3; i++) {
-	/* shift each component up to 16 bits */
-	shift[i] = 16 - jas_image_cmptprec(image, clut[i]);
-	/* repeat subsampled pixels */
-	hstep[i] = jas_image_cmpthstep(image, clut[i]);
-	vstep[i] = jas_image_cmptvstep(image, clut[i]);
+        /* shift each component up to 16 bits */
+        shift[i] = 16 - jas_image_cmptprec(image, clut[i]);
+        /* repeat subsampled pixels */
+        hstep[i] = jas_image_cmpthstep(image, clut[i]);
+        vstep[i] = jas_image_cmptvstep(image, clut[i]);
     }
     for (i = 1; i <= count; i+=3) {
-	/* read the sample values */
-	for (j = 0; j < 3; j++) {
-	    p[j] = jas_image_readcmptsample(image, clut[j], x/hstep[j], y/vstep[j]);
-	    p[j] <<= shift[j];
-	}
-	/* center chroma channels if necessary */
-	if (!jas_image_cmptsgnd(image, clut[1])) p[1] -= 0x8000;
-	if (!jas_image_cmptsgnd(image, clut[2])) p[2] -= 0x8000;
-	/* rotate to RGB */
+        /* read the sample values */
+        for (j = 0; j < 3; j++) {
+            p[j] = jas_image_readcmptsample(image, clut[j], x/hstep[j], y/vstep[j]);
+            p[j] <<= shift[j];
+        }
+        /* center chroma channels if necessary */
+        if (!jas_image_cmptsgnd(image, clut[1])) p[1] -= 0x8000;
+        if (!jas_image_cmptsgnd(image, clut[2])) p[2] -= 0x8000;
+        /* rotate to RGB */
 #ifdef JPX_USE_IRT
-	q[1] = p[0] - ((p[1] + p[2])>>2);
-	q[0] = p[1] + q[1];
-	q[2] = p[2] + q[1];
+        q[1] = p[0] - ((p[1] + p[2])>>2);
+        q[0] = p[1] + q[1];
+        q[2] = p[2] + q[1];
 #else
-	q[0] = (int)((double)p[0] + 1.402 * p[2]);
-	q[1] = (int)((double)p[0] - 0.34413 * p[1] - 0.71414 * p[2]);
-	q[2] = (int)((double)p[0] + 1.772 * p[1]);
+        q[0] = (int)((double)p[0] + 1.402 * p[2]);
+        q[1] = (int)((double)p[0] - 0.34413 * p[1] - 0.71414 * p[2]);
+        q[2] = (int)((double)p[0] + 1.772 * p[1]);
 #endif
-	/* clamp */
-	for (j = 0; j < 3; j++){
-	  if (q[j] < 0) q[j] = 0;
-	  else if (q[j] > 0xFFFF) q[j] = 0xFFFF;
-   	}
-	/* write out the pixel */
-	dest[i] = q[0] >> 8;
-	dest[i+1] = q[1] >> 8;
-	dest[i+2] = q[2] >> 8;
-	x++;
+        /* clamp */
+        for (j = 0; j < 3; j++){
+          if (q[j] < 0) q[j] = 0;
+          else if (q[j] > 0xFFFF) q[j] = 0xFFFF;
+        }
+        /* write out the pixel */
+        dest[i] = q[0] >> 8;
+        dest[i+1] = q[1] >> 8;
+        dest[i+2] = q[2] >> 8;
+        x++;
     }
 
     return count;
+#endif
+    return 0;
 }
 
 static int
-copy_row_default(unsigned char *dest, jas_image_t *image,
-	int x, int y, int bytes)
+copy_row_default(unsigned char *dest, opj_image_t *image,
+        int x, int y, int bytes)
 {
-    int i, c,n;
+#if 0
+    int i, c, n;
     int count;
 
     n = jas_image_numcmpts(image);
-    count = (bytes/n) * n;
-    for (i = 1; i <= count; i+=n) {
-	for (c = 0; c < n; c++)
-	    dest[i+c] = jas_image_readcmptsample(image, c, x, y);
-	x++;
+    count = (bytes / n) * n;
+    for (i = 1; i <= count; i += n) {
+        for (c = 0; c < n; c++)
+            dest[i + c] = jas_image_readcmptsample(image, c, x, y);
+        x++;
     }
 
     return count;
+#endif
+    return 0;
 }
 
 /* buffer the input stream into our state */
 static int
 s_jpxd_buffer_input(stream_jpxd_state *const state, stream_cursor_read *pr,
-		       long bytes)
+                       long bytes)
 {
     /* grow internal buffer if necessary */
     if (bytes > state->bufsize - state->buffill) {
@@ -321,11 +342,11 @@
         while (newsize - state->buffill < bytes)
             newsize <<= 1;
         newbuf = (unsigned char *)gs_malloc(state->jpx_memory, newsize, 1,
-					    "JPXDecode temp buffer");
+                                            "JPXDecode temp buffer");
         /* TODO: check for allocation failure */
         memcpy(newbuf, state->buffer, state->buffill);
         gs_free(state->jpx_memory, state->buffer, state->bufsize, 1,
-		"JPXDecode temp buffer");
+                "JPXDecode temp buffer");
         state->buffer = newbuf;
         state->bufsize = newsize;
     }
@@ -341,55 +362,49 @@
 static int
 s_jpxd_decode_image(stream_jpxd_state * state)
 {
-    jas_stream_t *stream = NULL;
-    jas_image_t *image = NULL;
-    char *optstr = NULL;
+    static opj_event_mgr_t opj_event_handers = {
+	s_jpx_opj_error_cb, /* error_handler */
+	s_jpx_opj_error_cb, /* warning_handler */
+	s_jpx_opj_error_cb, /* info_handler */
+    };
+    opj_dparameters_t opj_decodeparams;
+    opj_dinfo_t *opj_dinfo = NULL;
+    opj_cio_t *opj_cio = NULL;
+    opj_image_t *opj_image = NULL;
+    int ret = 0;
 
-    /* if the external Colorspace key is indexed, we need to ask
-       for raw index values so the external palette can be applied */
-    if (state->colorspace == gs_jpx_cs_indexed) {
-	if_debug0('w', "[w] got indexed colorspace in s_jpxd_decode_image\n");
-	optstr = (char *)"raw";
+    opj_set_default_decoder_parameters(&opj_decodeparams);
+    opj_decodeparams.decod_format = 1; /* JP2 */
+    opj_dinfo = opj_create_decompress(CODEC_JP2);
+    if ( opj_dinfo == NULL ) {
+        dprintf("unable to create OpenJPEG decompressor object.\n");
+        ret = ERRC;
+    } else {
+        opj_set_event_mgr((opj_common_ptr)opj_dinfo, &opj_event_handers, NULL);
+        opj_setup_decoder(opj_dinfo, &opj_decodeparams);
+        opj_cio = opj_cio_open((opj_common_ptr)opj_dinfo, state->buffer, state->buffill);
+        if ( opj_cio == NULL ) {
+            dprintf("unable to create OpenJPEG I/O object.\n");
+            ret = ERRC;
+        } else {
+            opj_image = opj_decode(opj_dinfo, opj_cio);
+            if ( opj_image == NULL ) {
+                dprintf("unable to decode JPX image data.\n");
+                ret = ERRC;
+            }
+            opj_cio_close(opj_cio);
+        }
     }
-    /* wrap our buffer in a jas_stream */
-    stream = jas_stream_memopen((char*)state->buffer, state->buffill);
-    if (stream == NULL) {
-	dprintf("unable to create stream for JPX image data.\n");
-	return ERRC;
+    if ( ret ) {
+        if ( opj_dinfo != NULL ) {
+            opj_destroy_decompress(opj_dinfo);
+            opj_dinfo = NULL;
+        }
     }
-    /* decode an image */
-    image = jas_image_decode(stream, -1, optstr);
-    if (image == NULL) {
-	dprintf("unable to decode JPX image data.\n");
-	return ERRC;
-    }
-#ifdef JPX_USE_JASPER_CM
-	/* convert non-rgb multicomponent colorspaces to sRGB */
-	if (jas_image_numcmpts(image) > 1 &&
-	    jas_clrspc_fam(jas_image_clrspc(image)) != JAS_CLRSPC_FAM_RGB) {
-	    jas_cmprof_t *outprof;
-	    jas_image_t *rgbimage = NULL;
-	    outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB);
-	    if (outprof != NULL)
-		rgbimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER);
-	    if (rgbimage != NULL) {
-		jas_image_destroy(image);
-		image = rgbimage;
-	    }
-	}
-#endif
-    state->image = image;
-    state->offset = 0;
-    jas_stream_close(stream);
-
-#ifdef DEBUG
-	dump_jas_image(image);
-	dump_jpxd_colorspace(state);
-#endif
-
-    return 0;
+    state->opj_dinfo = opj_dinfo;
+    state->opj_image = opj_image;
+    return ret;
 }
-#endif
 
 
 /* process a secton of the input and return any decoded data.
@@ -399,7 +414,6 @@
 s_jpxd_process(stream_state * ss, stream_cursor_read * pr,
                  stream_cursor_write * pw, bool last)
 {
-#if 0
     stream_jpxd_state *const state = (stream_jpxd_state *) ss;
     long in_size = pr->limit - pr->ptr;
     long out_size = pw->limit - pw->ptr;
@@ -408,91 +422,81 @@
     /* note that the gs stream library expects offset-by-one
        indexing of its buffers while we use zero indexing */
 
-    /* JasPer has its own stream library, but there's no public
-       api for handing it pieces. We need to add some plumbing
-       to convert between gs and jasper streams. In the meantime
-       just buffer the entire stream, since it can handle that
-       as input. */
-
     /* pass all available input to the decoder */
     if (in_size > 0) {
-	s_jpxd_buffer_input(state, pr, in_size);
+        s_jpxd_buffer_input(state, pr, in_size);
     }
     if (last) {
-      if (state->image == NULL) {
-	status = s_jpxd_decode_image(state);
-      }
-      if (state->image != NULL) {
-	jas_image_t *image = state->image;
-	int numcmpts = jas_image_numcmpts(image);
-	int bits = jas_image_cmptprec(image, 0);
-	int stride = numcmpts*jas_image_width(image);
-	long image_size = stride*jas_image_height(image);
-	int clrspc = jas_image_clrspc(image);
-	int x, y;
-	long usable, done;
+        if (state->opj_image == NULL) {
+            status = s_jpxd_decode_image(state);
+        }
+        if (state->opj_image != NULL) {
+            opj_image_t *image = state->opj_image;
+            int numcmpts = image->numcomps;
+            int bits = image->comps[0].prec;
+            int stride = numcmpts * image->comps[0].w;
+            long image_size = stride * image->comps[0].h;
+            int clrspc = image->color_space;
+            int x, y;
+            long usable, done;
 
-	if (bits == 4) stride = (stride + 1)/2;
+            if (bits == 4) stride = (stride + 1) / 2;
 
-	/* copy data out of the decoded image data */
-	/* be lazy and only write the rest of the current row */
-	y = state->offset / stride;
-	x = state->offset - y*stride; /* bytes, not samples */
-	usable = min(out_size, stride - x);
-	x = x/numcmpts;               /* now samples */
+            /* copy data out of the decoded image data */
+            /* be lazy and only write the rest of the current row */
+            y = state->offset / stride;
+            x = state->offset - y*stride; /* bytes, not samples */
+            usable = min(out_size, stride - x);
+            x = x / numcmpts;               /* now samples */
 
-	/* Make sure we can return a full pixel.
-	   This can fail if we get the colorspace wrong. */
-	if (usable < numcmpts) return ERRC;
+            /* Make sure we can return a full pixel.
+               This can fail if we get the colorspace wrong. */
+            if (usable < numcmpts) return ERRC;
 
-	if (state->colorspace != gs_jpx_cs_unset)
-	  /* An external colorspace from the interpreter overrides */
-	  switch (state->colorspace) {
-	    case gs_jpx_cs_gray:
-	    case gs_jpx_cs_indexed:
-	    /* we've passed 'raw' but the palette is the same pixel
-	       format as a grayscale image. The PDF interpreter will
-	       know to handle it differently. */
-	      done = copy_row_gray(pw->ptr, image, x, y, usable);
-	      break;
-	    case gs_jpx_cs_rgb:
-	      done = copy_row_rgb(pw->ptr, image, x, y, usable);
-	      break;
-	    case gs_jpx_cs_cmyk:
-	    default:
-	      done = copy_row_default(pw->ptr, image, x, y, usable);
-	      break;
-	  }
-	else /* use the stream's colorspace */
-	  switch (jas_clrspc_fam(clrspc)) {
-		case JAS_CLRSPC_FAM_GRAY:
-		    done = copy_row_gray(pw->ptr, image, x, y, usable);
-		    break;
-		case JAS_CLRSPC_FAM_RGB:
-		    done = copy_row_rgb(pw->ptr, image, x, y, usable);
-		    break;
-		case JAS_CLRSPC_FAM_YCBCR:
-		    done = copy_row_yuv(pw->ptr, image, x, y, usable);
-		    break;
-		case JAS_CLRSPC_FAM_XYZ:
-		case JAS_CLRSPC_FAM_LAB:
-		case JAS_CLRSPC_FAM_UNKNOWN:
-		default:
-		    done = copy_row_default(pw->ptr, image, x, y, usable);
-		    break;
-	 }
-	/* advance pointers for returned data */
-	pw->ptr += done;
-        state->offset += done;
-        status = (state->offset < image_size) ? 1 : EOFC;
-        /* return an error if we failed to advance decoding */
-        if (done <= 0) status = ERRC;
-      } /* image != NULL */
+            if (state->colorspace != gs_jpx_cs_unset) {
+                /* An external colorspace from the interpreter overrides */
+                switch (state->colorspace) {
+                case gs_jpx_cs_gray:
+                case gs_jpx_cs_indexed:
+                /* we've passed 'raw' but the palette is the same pixel
+                   format as a grayscale image. The PDF interpreter will
+                   know to handle it differently. */
+                    done = copy_row_gray(pw->ptr, image, x, y, usable);
+                    break;
+                case gs_jpx_cs_rgb:
+                    done = copy_row_rgb(pw->ptr, image, x, y, usable);
+                    break;
+                case gs_jpx_cs_cmyk:
+                default:
+                    done = copy_row_default(pw->ptr, image, x, y, usable);
+                    break;
+                }
+            } else { /* use the stream's colorspace */
+                switch (clrspc) {
+                case CLRSPC_GRAY:
+                    done = copy_row_gray(pw->ptr, image, x, y, usable);
+                    break;
+                case CLRSPC_SRGB:
+                    done = copy_row_rgb(pw->ptr, image, x, y, usable);
+                    break;
+                case CLRSPC_SYCC:
+                    done = copy_row_yuv(pw->ptr, image, x, y, usable);
+                    break;
+                case CLRSPC_UNKNOWN:
+                default:
+                    done = copy_row_default(pw->ptr, image, x, y, usable);
+                    break;
+                }
+            }
+            /* advance pointers for returned data */
+            pw->ptr += done;
+            state->offset += done;
+            status = (state->offset < image_size) ? 1 : EOFC;
+            /* return an error if we failed to advance decoding */
+            if (done <= 0) status = ERRC;
+        } /* image != NULL */
     } /* last */
-
     return status;
-#endif
-    return 0;
 }
 
 /* stream release.
@@ -501,15 +505,25 @@
 static void
 s_jpxd_release(stream_state *ss)
 {
-#if 0
     stream_jpxd_state *const state = (stream_jpxd_state *) ss;
 
     if (state) {
-        if (state->image) jas_image_destroy(state->image);
-	if (state->buffer) gs_free(state->jpx_memory, state->buffer, state->bufsize, 1,
-				"JPXDecode temp buffer");
+        if (state->opj_dinfo) {
+            opj_destroy_decompress(state->opj_dinfo);
+            state->opj_dinfo = NULL;
+        }
+        if (state->opj_image) {
+            opj_image_destroy(state->opj_image);
+            state->opj_image = NULL;
+        }
+        if (state->buffer) {
+            gs_free(state->jpx_memory, state->buffer, state->bufsize, 1,
+                                "JPXDecode temp buffer");
+            state->buffer = NULL;
+            state->bufsize = 0;
+            state->buffill = 0;
+        }
     }
-#endif
 }
 
 /* set stream defaults.
@@ -521,15 +535,14 @@
 s_jpxd_set_defaults(stream_state *ss)
 {
     stream_jpxd_state *const state = (stream_jpxd_state *) ss;
-/*
-    state->image = NULL;
-*/
     state->offset = 0;
     state->buffer = NULL;
     state->bufsize = 0;
     state->buffill = 0;
     /* the following can be set by the client before calling init() */
     state->colorspace = gs_jpx_cs_unset;
+    state->opj_dinfo = NULL;
+    state->opj_image = NULL;
 }
 
 

Modified: branches/jpx_openjpeg/gs/base/sjpx_opj.h
===================================================================
--- branches/jpx_openjpeg/gs/base/sjpx_opj.h	2010-03-31 13:22:01 UTC (rev 10999)
+++ branches/jpx_openjpeg/gs/base/sjpx_opj.h	2010-03-31 13:48:15 UTC (rev 11000)
@@ -42,10 +42,7 @@
  */
 typedef struct stream_jpxd_state_s
 {
-    stream_state_common;	/* a define from scommon.h */
-/*
-    jas_image_t *image;
-*/
+    stream_state_common;        /* a define from scommon.h */
     long offset; /* offset into the image bitmap of the next
                     byte to be returned */
     const gs_memory_t *jpx_memory;
@@ -53,10 +50,12 @@
     long bufsize; /* total size of the buffer */
     long buffill; /* number of bytes written into the buffer */
     gs_jpx_cs colorspace; /* external colorspace setting */
+    opj_dinfo_t *opj_dinfo; /* OpenJPEG decoder info */
+    opj_image_t *opj_image; /* OpenJPEG image object */
 }
 stream_jpxd_state;
 
-#define private_st_jpxd_state()	\
+#define private_st_jpxd_state() \
   gs_private_st_simple(st_jpxd_state, stream_jpxd_state,\
     "JPXDecode filter state")
 extern const stream_template s_jpxd_template;



More information about the gs-commits mailing list