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

Last change on this file since 257 was 257, checked in by Eugene Romanenko, 13 years ago

PDF plugin: Poppler library updated to version 0.10.0

File size: 4.1 KB
Line 
1//========================================================================
2//
3// JPEG2000Stream.cc
4//
5// A JPX stream decoder using OpenJPEG
6//
7// Copyright 2008 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  int bufSize = BUFFER_INCREASE;
57  unsigned char *buf = (unsigned char*)gmallocn(bufSize, sizeof(unsigned char));
58  int index = 0;
59
60  str->reset();
61  int c = str->getChar();
62  while(c != EOF)
63  {
64    buf[index] = c;
65    ++index;
66    if (index >= bufSize)
67    {
68      bufSize += BUFFER_INCREASE;
69      buf = (unsigned char*)greallocn(buf, bufSize, sizeof(unsigned char));
70    }
71    c = str->getChar();
72  }
73
74  init2(buf, index, CODEC_JP2);
75
76  free(buf);
77
78  counter = 0;
79  inited = gTrue;
80}
81
82static void libopenjpeg_error_callback(const char *msg, void * /*client_data*/) {
83  error(-1, (char*)msg);
84}
85
86static void libopenjpeg_warning_callback(const char *msg, void * /*client_data*/) {
87  error(-1, (char*)msg);
88}
89
90void JPXStream::init2(unsigned char *buf, int bufLen, OPJ_CODEC_FORMAT format)
91{
92  opj_cio_t *cio = NULL;
93
94  /* Use default decompression parameters */
95  opj_dparameters_t parameters;
96  opj_set_default_decoder_parameters(&parameters);
97
98  /* Configure the event manager to receive errors and warnings */
99  opj_event_mgr_t event_mgr;
100  memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
101  event_mgr.error_handler = libopenjpeg_error_callback;
102  event_mgr.warning_handler = libopenjpeg_warning_callback;
103
104  /* Get the decoder handle of the format */
105  dinfo = opj_create_decompress(format);
106  if (dinfo == NULL) goto error;
107
108  /* Catch events using our callbacks */
109  opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, NULL);
110
111  /* Setup the decoder decoding parameters */
112  opj_setup_decoder(dinfo, &parameters);
113
114  /* Open a byte stream */
115  cio = opj_cio_open((opj_common_ptr)dinfo, buf, bufLen);
116  if (cio == NULL) goto error;
117
118  /* Decode the stream and fill the image structure */
119  image = opj_decode(dinfo, cio);
120
121  /* Close the byte stream */
122  opj_cio_close(cio);
123
124  if (image == NULL) goto error;
125  else return;
126
127error:
128  if (format == CODEC_JP2) {
129    error(-1, "Did no succeed opening JPX Stream as JP2, trying as J2K.");
130    init2(buf, bufLen, CODEC_J2K);
131  } else if (format == CODEC_J2K) {
132    error(-1, "Did no succeed opening JPX Stream as J2K, trying as JPT.");
133    init2(buf, bufLen, CODEC_JPT);
134  } else {
135    error(-1, "Did no succeed opening JPX Stream.");
136  }
137}
138
139int JPXStream::lookChar() {
140  if (inited == gFalse) init();
141
142  if (!image) return EOF;
143
144  int w = image->comps[0].w;
145  int h = image->comps[0].h;
146
147  int y = (counter / image->numcomps) / w;
148  int x = (counter / image->numcomps) % w;
149  if (y >= h) return EOF;
150
151  int component = counter % image->numcomps;
152
153  int adjust = 0;
154  if (image->comps[component].prec > 8) {
155    adjust = image->comps[component].prec - 8;
156  }
157
158  int r = image->comps[component].data[y * w + x];
159  r += (image->comps[component].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
160
161  unsigned char rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
162
163  return rc;
164}
165
166GooString *JPXStream::getPSFilter(int psLevel, char *indent) {
167  return NULL;
168}
169
170GBool JPXStream::isBinary(GBool last) {
171  return str->isBinary(gTrue);
172}
173
174void JPXStream::getImageParams(int *bitsPerComponent, StreamColorSpaceMode *csMode) {
175  if (inited == gFalse) init();
176
177  *bitsPerComponent = 8;
178  if (image && image->numcomps == 3) *csMode = streamCSDeviceRGB;
179  else *csMode = streamCSDeviceGray;
180}
181
Note: See TracBrowser for help on using the repository browser.