source: trunk/libdjvu/JPEGDecoder.cpp @ 136

Last change on this file since 136 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

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