source: trunk/poppler/mypoppler/poppler/JPEG2000Stream.cc @ 461

Last change on this file since 461 was 277, checked in by rbri, 12 years ago

PDF plugin: Poppler library updated to version 0.12.3

File size: 4.2 KB
Line 
1//========================================================================
2//
3// JPEG2000Stream.cc
4//
5// A JPX stream decoder using OpenJPEG
6//
7// Copyright 2008, 2009 Albert Astals Cid <aacid@kde.org>
8//
9// Licensed under GPLv2 or later
10//
11//========================================================================
12
13#include "JPEG2000Stream.h"
14
15JPXStream::JPXStream(Stream *strA) : FilterStream(strA)
16{
17  inited = gFalse;
18  image = NULL;
19  dinfo = NULL;
20}
21
22JPXStream::~JPXStream() {
23  delete str;
24  close();
25}
26
27void JPXStream::reset() {
28  counter = 0;
29}
30
31void JPXStream::close() {
32  if (image != NULL) {
33    opj_image_destroy(image);
34    image = NULL;
35  }
36  if (dinfo != NULL) {
37    opj_destroy_decompress(dinfo);
38    dinfo = NULL;
39  }
40}
41
42int JPXStream::getPos() {
43  return counter;
44}
45
46int JPXStream::getChar() {
47  int result = lookChar();
48  ++counter;
49  return result;
50}
51
52#define BUFFER_INCREASE 4096
53
54void JPXStream::init()
55{
56  Object oLen;
57  if (getDict()) getDict()->lookup("Length", &oLen);
58
59  int bufSize = BUFFER_INCREASE;
60  if (oLen.isInt()) bufSize = oLen.getInt();
61  oLen.free();
62
63  unsigned char *buf = (unsigned char*)gmallocn(bufSize, sizeof(unsigned char));
64  int index = 0;
65
66  str->reset();
67  int c = str->getChar();
68  while(c != EOF)
69  {
70    if (index >= bufSize)
71    {
72      bufSize += BUFFER_INCREASE;
73      buf = (unsigned char*)greallocn(buf, bufSize, sizeof(unsigned char));
74    }
75    buf[index] = c;
76    ++index;
77    c = str->getChar();
78  }
79
80  init2(buf, index, CODEC_JP2);
81
82  free(buf);
83
84  counter = 0;
85  inited = gTrue;
86}
87
88static void libopenjpeg_error_callback(const char *msg, void * /*client_data*/) {
89  error(-1, "%s", msg);
90}
91
92static void libopenjpeg_warning_callback(const char *msg, void * /*client_data*/) {
93  error(-1, "%s", msg);
94}
95
96void JPXStream::init2(unsigned char *buf, int bufLen, OPJ_CODEC_FORMAT format)
97{
98  opj_cio_t *cio = NULL;
99
100  /* Use default decompression parameters */
101  opj_dparameters_t parameters;
102  opj_set_default_decoder_parameters(&parameters);
103
104  /* Configure the event manager to receive errors and warnings */
105  opj_event_mgr_t event_mgr;
106  memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
107  event_mgr.error_handler = libopenjpeg_error_callback;
108  event_mgr.warning_handler = libopenjpeg_warning_callback;
109
110  /* Get the decoder handle of the format */
111  dinfo = opj_create_decompress(format);
112  if (dinfo == NULL) goto error;
113
114  /* Catch events using our callbacks */
115  opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, NULL);
116
117  /* Setup the decoder decoding parameters */
118  opj_setup_decoder(dinfo, &parameters);
119
120  /* Open a byte stream */
121  cio = opj_cio_open((opj_common_ptr)dinfo, buf, bufLen);
122  if (cio == NULL) goto error;
123
124  /* Decode the stream and fill the image structure */
125  image = opj_decode(dinfo, cio);
126
127  /* Close the byte stream */
128  opj_cio_close(cio);
129
130  if (image == NULL) goto error;
131  else return;
132
133error:
134  if (format == CODEC_JP2) {
135    error(-1, "Did no succeed opening JPX Stream as JP2, trying as J2K.");
136    init2(buf, bufLen, CODEC_J2K);
137  } else if (format == CODEC_J2K) {
138    error(-1, "Did no succeed opening JPX Stream as J2K, trying as JPT.");
139    init2(buf, bufLen, CODEC_JPT);
140  } else {
141    error(-1, "Did no succeed opening JPX Stream.");
142  }
143}
144
145int JPXStream::lookChar() {
146  if (inited == gFalse) init();
147
148  if (!image) return EOF;
149
150  int w = image->comps[0].w;
151  int h = image->comps[0].h;
152
153  int y = (counter / image->numcomps) / w;
154  int x = (counter / image->numcomps) % w;
155  if (y >= h) return EOF;
156
157  int component = counter % image->numcomps;
158
159  int adjust = 0;
160  if (image->comps[component].prec > 8) {
161    adjust = image->comps[component].prec - 8;
162  }
163
164  int r = image->comps[component].data[y * w + x];
165  r += (image->comps[component].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
166
167  unsigned char rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
168
169  return rc;
170}
171
172GooString *JPXStream::getPSFilter(int psLevel, char *indent) {
173  return NULL;
174}
175
176GBool JPXStream::isBinary(GBool last) {
177  return str->isBinary(gTrue);
178}
179
180void JPXStream::getImageParams(int *bitsPerComponent, StreamColorSpaceMode *csMode) {
181  if (inited == gFalse) init();
182
183  *bitsPerComponent = 8;
184  if (image && image->numcomps == 3) *csMode = streamCSDeviceRGB;
185  else *csMode = streamCSDeviceGray;
186}
187
Note: See TracBrowser for help on using the repository browser.