source: trunk/libdjvu/JPEGDecoder.cpp @ 206

Last change on this file since 206 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

File size: 11.3 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: JPEGDecoder.cpp,v 1.9 2007/03/25 20:48:32 leonb Exp $
57// $Name: release_3_5_19 $
58
59#ifdef HAVE_CONFIG_H
60# include "config.h"
61#endif
62#if NEED_GNUG_PRAGMAS
63# pragma implementation
64#endif
65
66#ifdef NEED_JPEG_DECODER
67
68#include "JPEGDecoder.h"
69
70#ifdef __cplusplus
71extern "C" {
72#endif
73#undef HAVE_STDLIB_H
74#undef HAVE_STDDEF_H
75#include <stdio.h>
76#include <jconfig.h>
77#include <jpeglib.h>
78#include <jerror.h>
79#ifdef __cplusplus
80}
81#endif
82
83#include "ByteStream.h"
84#include "GPixmap.h"
85#ifdef LIBJPEGNAME
86#include "DjVuDynamic.h"
87#include "GString.h"
88#endif // LIBJPEGNAME
89
90
91
92#ifdef HAVE_NAMESPACES
93namespace DJVU {
94# ifdef NOT_DEFINED // Just to fool emacs c++ mode
95}
96#endif
97#endif
98
99
100class JPEGDecoder::Impl : public JPEGDecoder
101{
102public:
103  static void jpeg_byte_stream_src(j_decompress_ptr, ByteStream &);
104};
105
106extern "C"
107{
108
109struct djvu_error_mgr
110{
111  struct jpeg_error_mgr pub;  /* "public" fields */
112
113  jmp_buf setjmp_buffer;  /* for return to caller */
114};
115
116typedef struct djvu_error_mgr * djvu_error_ptr;
117
118METHODDEF(void)
119djvu_error_exit (j_common_ptr cinfo)
120{
121  /* cinfo->err really points to a djvu_error_mgr struct, so coerce pointer */
122  djvu_error_ptr djvuerr = (djvu_error_ptr) cinfo->err;
123
124  /* Always display the message. */
125  /* We could postpone this until after returning, if we chose. */
126  (*cinfo->err->output_message) (cinfo);
127
128  /* Return control to the setjmp point */
129  longjmp(djvuerr->setjmp_buffer, 1);
130}
131
132}
133
134GP<GPixmap>
135JPEGDecoder::decode(ByteStream & bs )
136{
137  GP<GPixmap> retval=GPixmap::create();
138  G_TRY
139  {
140    decode(bs,*retval);
141  } G_CATCH_ALL
142  {
143    retval=0;
144  }
145  G_ENDCATCH;
146  return retval;
147}
148
149void
150JPEGDecoder::decode(ByteStream & bs,GPixmap &pix)
151{
152  struct jpeg_decompress_struct cinfo;
153
154  /* We use our private extension JPEG error handler. */
155  struct djvu_error_mgr jerr;
156
157  JSAMPARRAY buffer;    /* Output row buffer */
158  int row_stride;   /* physical row width in output buffer */
159  int full_buf_size;
160  int isGrey,i;
161
162  cinfo.err = jpeg_std_error(&jerr.pub);
163
164  jerr.pub.error_exit = djvu_error_exit;
165
166  if (setjmp(jerr.setjmp_buffer))
167  {
168
169    jpeg_destroy_decompress(&cinfo);
170    G_THROW( ERR_MSG("GPixmap.unk_PPM") );
171  }
172
173  jpeg_create_decompress(&cinfo);
174
175  Impl::jpeg_byte_stream_src(&cinfo, bs);
176
177  (void) jpeg_read_header(&cinfo, TRUE);
178
179  jpeg_start_decompress(&cinfo);
180 
181  /* We may need to do some setup of our own at this point before reading
182   * the data.  After jpeg_start_decompress() we have the correct scaled
183   * output image dimensions available, as well as the output colormap
184   * if we asked for color quantization.
185   * In this example, we need to make an output work buffer of the right size.
186   */
187
188  /* JSAMPLEs per row in output buffer */
189  row_stride = cinfo.output_width * cinfo.output_components;
190  full_buf_size = row_stride * cinfo.output_height;
191
192  /* Make a one-row-high sample array that will go away when done with image */
193  buffer = (*cinfo.mem->alloc_sarray)
194    ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
195
196  GP<ByteStream> goutputBlock=ByteStream::create();
197  ByteStream &outputBlock=*goutputBlock;
198  outputBlock.format("P6\n%d %d\n%d\n",cinfo.output_width, 
199                                 cinfo.output_height,255);
200
201  isGrey = ( cinfo.out_color_space == JCS_GRAYSCALE) ? 1 : 0; 
202
203  while (cinfo.output_scanline < cinfo.output_height)
204  {
205    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
206
207    if ( isGrey == 1 )
208    {
209      for (i=0; i<row_stride; i++)
210      {
211        outputBlock.write8((char)buffer[0][i]); 
212        outputBlock.write8((char)buffer[0][i]); 
213        outputBlock.write8((char)buffer[0][i]); 
214      }
215    }else
216    {
217      for (i=0; i<row_stride; i++) 
218        outputBlock.write8((char)buffer[0][i]); 
219    }
220  }
221
222  (void) jpeg_finish_decompress(&cinfo);   
223
224  jpeg_destroy_decompress(&cinfo);
225 
226  outputBlock.seek(0,SEEK_SET);
227
228  pix.init(outputBlock);
229}         
230
231/*** From here onwards code is to make ByteStream as the data
232     source for the JPEG library */
233
234extern "C"
235{
236
237typedef struct
238{
239  struct jpeg_source_mgr pub; /* public fields */
240
241  ByteStream * byteStream;    /* source stream */
242  JOCTET * buffer;    /* start of buffer */
243  boolean start_of_stream; 
244} byte_stream_src_mgr;
245               
246
247typedef byte_stream_src_mgr * byte_stream_src_ptr; 
248
249#define INPUT_BUF_SIZE   4096
250
251METHODDEF(void)
252init_source (j_decompress_ptr cinfo)
253{
254  byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
255
256  src->start_of_stream = TRUE;
257}
258
259METHODDEF(boolean)
260fill_input_buffer (j_decompress_ptr cinfo)
261{
262  byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
263  size_t nbytes;
264
265  nbytes = src->byteStream->readall(src->buffer, INPUT_BUF_SIZE);
266
267  if (nbytes <= 0)
268  {
269    if (src->start_of_stream) /* Treat empty input as fatal error */
270      ERREXIT(cinfo, JERR_INPUT_EMPTY);
271    WARNMS(cinfo, JWRN_JPEG_EOF);
272    /* Insert a fake EOI marker */
273    src->buffer[0] = (JOCTET) 0xFF;
274    src->buffer[1] = (JOCTET) JPEG_EOI;
275    nbytes = 2;
276  }
277
278  src->pub.next_input_byte = src->buffer;
279  src->pub.bytes_in_buffer = nbytes;
280  src->start_of_stream = FALSE; 
281
282  return TRUE;
283}
284
285
286METHODDEF(void)
287skip_input_data (j_decompress_ptr cinfo, long num_bytes)
288{
289  byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
290
291  if (num_bytes > (long) src->pub.bytes_in_buffer)
292  {
293    src->byteStream->seek((num_bytes - src->pub.bytes_in_buffer), SEEK_CUR);
294    (void) fill_input_buffer(cinfo);
295  }else
296  {
297    src->pub.bytes_in_buffer -= num_bytes;
298    src->pub.next_input_byte += num_bytes;
299  }
300}
301                 
302METHODDEF(void)
303term_source (j_decompress_ptr cinfo)
304{
305  /* no work necessary here */
306}
307
308}
309
310void
311JPEGDecoder::Impl::jpeg_byte_stream_src(j_decompress_ptr cinfo,ByteStream &bs)
312{
313  byte_stream_src_ptr src;
314
315  if (cinfo->src == NULL)
316  { /* first time for this JPEG object? */
317    cinfo->src = (struct jpeg_source_mgr *)     
318      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
319          sizeof(byte_stream_src_mgr));
320    src = (byte_stream_src_ptr) cinfo->src;
321    src->buffer = (JOCTET *)
322      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
323          INPUT_BUF_SIZE * sizeof(JOCTET));
324  }
325
326  src = (byte_stream_src_ptr) cinfo->src;
327  src->pub.init_source = init_source;
328  src->pub.fill_input_buffer = fill_input_buffer;
329  src->pub.skip_input_data = skip_input_data;
330  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
331  src->pub.term_source = term_source;
332  src->byteStream = &bs;
333  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
334  src->pub.next_input_byte = NULL; /* until buffer loaded */
335}
336
337#ifdef LIBJPEGNAME
338void *
339JPEGDecoder::jpeg_lookup(const GUTF8String &name)
340{
341  static DjVuDynamic lib(GUTF8String(LIBJPEGNAME));
342  void *sym=lib.lookup(name);
343  if(!sym)
344    G_THROW(ERR_MSG("DjVuFile.JPEG_bg2"));
345  return sym;
346}
347
348jpeg_error_mgr *
349JPEGDecoder::jpeg_std_error(jpeg_error_mgr *x)
350{
351  static void *sym=jpeg_lookup("jpeg_std_error");
352  return ((jpeg_error_mgr *(*)(jpeg_error_mgr *))sym)(x);
353}
354
355void
356JPEGDecoder::jpeg_CreateDecompress(jpeg_decompress_struct *x,int v, size_t s)
357{
358  static void *sym=jpeg_lookup("jpeg_CreateDecompress");
359  ((void (*)(jpeg_decompress_struct *,int,size_t))sym)(x,v,s);
360}
361
362void
363JPEGDecoder::jpeg_destroy_decompress(j_decompress_ptr x)
364{
365  static void *sym=jpeg_lookup("jpeg_destroy_decompress");
366  ((void (*)(j_decompress_ptr))sym)(x);
367}
368
369int
370JPEGDecoder::jpeg_read_header(j_decompress_ptr x,boolean y)
371{
372  static void *sym=jpeg_lookup("jpeg_read_header");
373  return ((int (*)(j_decompress_ptr,boolean))sym)(x,y);
374}
375
376JDIMENSION
377JPEGDecoder::jpeg_read_scanlines(j_decompress_ptr x,JSAMPARRAY y,JDIMENSION z)
378{
379  static void *sym=jpeg_lookup("jpeg_read_scanlines");
380  return ((JDIMENSION (*)(j_decompress_ptr,JSAMPARRAY,JDIMENSION))sym)(x,y,z);
381}
382
383boolean
384JPEGDecoder::jpeg_finish_decompress(j_decompress_ptr x)
385{
386  static void *sym=jpeg_lookup("jpeg_finish_decompress");
387  return ((boolean (*)(j_decompress_ptr))sym)(x);
388}
389
390boolean
391JPEGDecoder::jpeg_resync_to_restart(jpeg_decompress_struct *x,int d)
392{
393  static void *sym=jpeg_lookup("jpeg_resync_to_restart");
394  return ((boolean (*)(jpeg_decompress_struct *,int))sym)(x,d);
395}
396
397boolean
398JPEGDecoder::jpeg_start_decompress(j_decompress_ptr x)
399{
400  static void *sym=jpeg_lookup("jpeg_start_decompress");
401  return ((boolean (*)(j_decompress_ptr))sym)(x);
402}
403
404#endif // LIBJPEGNAME
405
406
407#ifdef HAVE_NAMESPACES
408}
409# ifndef NOT_USING_DJVU_NAMESPACE
410using namespace DJVU;
411# endif
412#endif
413
414#endif
415
Note: See TracBrowser for help on using the repository browser.