fz_error fz_processjpxd(fz_filter *filter, fz_buffer *in, fz_buffer *out) { fz_jpxd *d = (fz_jpxd*)filter; int n, bpc, w, h; int i, x, y; switch (d->stage) { case 0: goto input; case 1: goto decode; case 2: goto output; } input: while (in->rp < in->wp) { n = jas_stream_write(d->stream, in->rp, in->wp - in->rp); in->rp += n; } if (!in->eof) return fz_ioneedin; d->stage = 1; decode: jas_stream_seek(d->stream, 0, SEEK_SET); d->image = jas_image_decode(d->stream, -1, nil); if (!d->image) return fz_throw("jasper error: jas_image_decode()"); d->stage = 2; output: w = jas_image_width(d->image); h = jas_image_height(d->image); n = jas_image_numcmpts(d->image); bpc = jas_image_cmptprec(d->image, 0); /* use precision of first component for all... */ while (d->offset < w * h) { y = d->offset / w; x = d->offset - y * w; /* FIXME bpc != 8 */ if (out->wp + n >= out->ep) return fz_ioneedout; for (i = 0; i < n; i++) *out->wp++ = jas_image_readcmptsample(d->image, i, x, y); d->offset ++; } return fz_iodone; }
DEFINE_LOADER_PLUGIN_LOAD(p, st, vw #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , c #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif , priv #if !defined(IDENTIFY_BEFORE_LOAD) __attribute__((unused)) #endif ) { jas_image_t *ji; jas_stream_t *js; unsigned char *d; char *buf = NULL; int k, cmp[3]; unsigned int i, j; int tlx, tly; int vs, hs; //debug_message("JasPer: load() called\n"); #ifdef IDENTIFY_BEFORE_LOAD { LoaderStatus status; if ((status = identify(p, st, vw, c, priv)) != LOAD_OK) return status; stream_rewind(st); } #endif /* Read whole stream into buffer... */ { char *tmp; int size = 0, len; int bufsize = 65536; for (;;) { if ((tmp = realloc(buf, bufsize)) == NULL) { free(buf); return LOAD_ERROR; } buf = tmp; len = stream_read(st, (unsigned char *)(buf + size), bufsize - size); size += len; if (len < bufsize - size) break; bufsize += 65536; } if ((js = jas_stream_memopen(buf, size)) == NULL) { free(buf); return LOAD_ERROR; } } /* loading... */ if ((ji = jas_image_decode(js, -1, 0)) == NULL) { err_message_fnc("jas_image_decode() failed.\n"); goto error_clear; } /* colorspace conversion */ { jas_cmprof_t *jc; jas_image_t *new_ji; if ((jc = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB)) == NULL) goto error_destroy_free; if ((new_ji = jas_image_chclrspc(ji, jc, JAS_CMXFORM_INTENT_PER)) == NULL) goto error_destroy_free; jas_image_destroy(ji); ji = new_ji; } jas_stream_close(js); free(buf); debug_message("JasPer: jas_image_decode() OK: (%ld,%ld)\n", jas_image_cmptwidth(ji, 0), jas_image_cmptheight(ji, 0)); /* convert to enfle format */ p->bits_per_pixel = 24; p->type = _RGB24; p->depth = 24; cmp[0] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); cmp[1] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); cmp[2] = jas_image_getcmptbytype(ji, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); /* dimension */ image_width(p) = jas_image_cmptwidth(ji, cmp[0]); image_height(p) = jas_image_cmptheight(ji, cmp[0]); image_left(p) = 0; image_top(p) = 0; image_bpl(p) = image_width(p) * 3; tlx = jas_image_cmpttlx(ji, cmp[0]); tly = jas_image_cmpttly(ji, cmp[0]); vs = jas_image_cmptvstep(ji, cmp[0]); hs = jas_image_cmpthstep(ji, cmp[0]); debug_message("JasPer: tlx %d tly %d vs %d hs %d ncomponents %d\n", tlx, tly, vs, hs, jas_image_numcmpts(ji)); /* memory allocation */ if ((d = memory_alloc(image_image(p), image_bpl(p) * image_height(p))) == NULL) { err_message("No enough memory (%d bytes)\n", image_bpl(p) * image_height(p)); goto error_destroy_free; } for (i = 0; i < image_height(p); i++) { for (j = 0; j < image_width(p); j++) { for (k = 0; k < 3; k++) *d++ = jas_image_readcmptsample(ji, cmp[k], j, i); } } jas_image_destroy(ji); return LOAD_OK; error_destroy_free: jas_image_destroy(ji); error_clear: return LOAD_ERROR; }
int jas_image_render2(jas_image_t *image, int cmptno, float vtlx, float vtly, float vsx, float vsy, int vw, int vh, GLshort *vdata) { int i; int j; int x; int y; int v; GLshort *vdatap; if (cmptno < 0 || cmptno >= image->numcmpts_) { jas_eprintf("bad parameter\n"); goto error; } for (i = 0; i < vh; ++i) { vdatap = &vdata[(vh - 1 - i) * (4 * vw)]; for (j = 0; j < vw; ++j) { x = vctocc(j, jas_image_cmpttlx(image, cmptno), jas_image_cmpthstep(image, cmptno), vtlx, vsx); y = vctocc(i, jas_image_cmpttly(image, cmptno), jas_image_cmptvstep(image, cmptno), vtly, vsy); v = (x >= 0 && x < jas_image_cmptwidth(image, cmptno) && y >=0 && y < jas_image_cmptheight(image, cmptno)) ? jas_image_readcmptsample(image, cmptno, x, y) : 0; v <<= 16 - jas_image_cmptprec(image, cmptno); if (v < 0) { v = 0; } else if (v > 65535) { v = 65535; } *vdatap++ = v; *vdatap++ = v; *vdatap++ = v; *vdatap++ = 0; } } return 0; error: return -1; }
static int jas_image_render(jas_image_t *image, float vtlx, float vtly, float vsx, float vsy, int vw, int vh, GLshort *vdata) { int i; int j; int k; int x; int y; int v[3]; GLshort *vdatap; int cmptlut[3]; int width; int height; int hs; int vs; int tlx; int tly; if ((cmptlut[0] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 || (cmptlut[1] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0 || (cmptlut[2] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0) goto error; width = jas_image_cmptwidth(image, cmptlut[0]); height = jas_image_cmptheight(image, cmptlut[0]); tlx = jas_image_cmpttlx(image, cmptlut[0]); tly = jas_image_cmpttly(image, cmptlut[0]); vs = jas_image_cmptvstep(image, cmptlut[0]); hs = jas_image_cmpthstep(image, cmptlut[0]); for (i = 1; i < 3; ++i) { if (jas_image_cmptwidth(image, cmptlut[i]) != width || jas_image_cmptheight(image, cmptlut[i]) != height) goto error; } for (i = 0; i < vh; ++i) { vdatap = &vdata[(vh - 1 - i) * (4 * vw)]; for (j = 0; j < vw; ++j) { x = vctocc(j, tlx, hs, vtlx, vsx); y = vctocc(i, tly, vs, vtly, vsy); if (x >= 0 && x < width && y >= 0 && y < height) { for (k = 0; k < 3; ++k) { v[k] = jas_image_readcmptsample(image, cmptlut[k], x, y); v[k] <<= 16 - jas_image_cmptprec(image, cmptlut[k]); if (v[k] < 0) { v[k] = 0; } else if (v[k] > 65535) { v[k] = 65535; } } } else { v[0] = 0; v[1] = 0; v[2] = 0; } *vdatap++ = v[0]; *vdatap++ = v[1]; *vdatap++ = v[2]; *vdatap++ = 0; } } return 0; error: return -1; }