diff --git a/poppler/JPEG2000Stream.cc b/poppler/JPEG2000Stream.cc index d52f088..08e5055 100644 --- a/poppler/JPEG2000Stream.cc +++ b/poppler/JPEG2000Stream.cc @@ -17,6 +17,7 @@ JPXStream::JPXStream(Stream *strA) : FilterStream(strA) inited = gFalse; image = NULL; dinfo = NULL; + npixels = 0; } JPXStream::~JPXStream() { @@ -32,6 +33,7 @@ void JPXStream::close() { if (image != NULL) { opj_image_destroy(image); image = NULL; + npixels = 0; } if (dinfo != NULL) { opj_destroy_decompress(dinfo); @@ -73,6 +75,38 @@ void JPXStream::init() init2(buf, length, CODEC_JP2); free(buf); + if (image) { + data = (unsigned char *)image->comps[0].data; + npixels = image->comps[0].w * image->comps[0].h; + int ncomps = image->numcomps < 4 ? image->numcomps : 4; + for (int component = 0; component < ncomps; component++) { + if (image->comps[component].data == NULL) { + close(); + break; + } + int adjust = 0; + unsigned char *cdata = data + component; + if (image->comps[component].prec > 8) + adjust = image->comps[component].prec - 8; + int sgndcorr = 0; + if (image->comps[component].sgnd) + sgndcorr = 1 << (image->comps[0].prec - 1); + for (int i = 0; i < npixels; i++) { + int r = image->comps[component].data[i]; + r += sgndcorr; + if (adjust) { + r = (r >> adjust)+((r >> (adjust-1))%2); + if (unlikely(r > 255)) + r = 255; + } + *cdata = r; + cdata += ncomps; + } + } + npixels *= ncomps; + } else + npixels = 0; + counter = 0; inited = gTrue; } diff --git a/poppler/JPEG2000Stream.h b/poppler/JPEG2000Stream.h index bda2721..08449b0 100644 --- a/poppler/JPEG2000Stream.h +++ b/poppler/JPEG2000Stream.h @@ -49,37 +49,18 @@ private: } inline int doLookChar() { - if (inited == gFalse) init(); + if (unlikely(inited == gFalse)) init(); - if (!image) return EOF; + if (unlikely(counter >= npixels)) return EOF; - int w = image->comps[0].w; - int h = image->comps[0].h; - - int y = (counter / image->numcomps) / w; - int x = (counter / image->numcomps) % w; - if (y >= h) return EOF; - - int component = counter % image->numcomps; - - int adjust = 0; - if (image->comps[component].prec > 8) { - adjust = image->comps[component].prec - 8; - } - - if (unlikely(image->comps[component].data == NULL)) return EOF; - - int r = image->comps[component].data[y * w + x]; - r += (image->comps[component].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - - unsigned char rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2)); - - return rc; + return data[counter]; } opj_image_t *image; opj_dinfo_t *dinfo; + unsigned char *data; int counter; + int npixels; GBool inited; };