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 #ifdef HAVE_CONFIG_H
57 # include "config.h"
58 #endif
59 #if NEED_GNUG_PRAGMAS
60 # pragma implementation
61 #endif
62
63 #ifdef NEED_JPEG_DECODER
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 #undef HAVE_STDLIB_H
69 #undef HAVE_STDDEF_H
70 #define INT32 jpeg_INT32
71 #define INT16 jpeg_INT16
72 #include <stdio.h>
73 #include <jconfig.h>
74 #include <jpeglib.h>
75 #include <jerror.h>
76 #undef FAR
77 #undef INT32
78 #undef INT16
79 #ifdef __cplusplus
80 }
81 #endif
82
83 #include "JPEGDecoder.h"
84 #include "ByteStream.h"
85 #include "GPixmap.h"
86 #ifdef LIBJPEGNAME
87 #include "DjVuDynamic.h"
88 #include "GString.h"
89 #endif // LIBJPEGNAME
90
91
92
93 #ifdef HAVE_NAMESPACES
94 namespace DJVU {
95 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
96 }
97 #endif
98 #endif
99
100
101 class JPEGDecoder::Impl : public JPEGDecoder
102 {
103 public:
104 static void jpeg_byte_stream_src(j_decompress_ptr, ByteStream &);
105 };
106
107 extern "C"
108 {
109
110 struct djvu_error_mgr
111 {
112 struct jpeg_error_mgr pub; /* "public" fields */
113
114 jmp_buf setjmp_buffer; /* for return to caller */
115 };
116
117 typedef struct djvu_error_mgr * djvu_error_ptr;
118
119 METHODDEF(void)
djvu_error_exit(j_common_ptr cinfo)120 djvu_error_exit (j_common_ptr cinfo)
121 {
122 /* cinfo->err really points to a djvu_error_mgr struct, so coerce pointer */
123 djvu_error_ptr djvuerr = (djvu_error_ptr) cinfo->err;
124 /* Return control to the setjmp point */
125 longjmp(djvuerr->setjmp_buffer, 1);
126 }
127
128 }
129
130 GP<GPixmap>
decode(ByteStream & bs)131 JPEGDecoder::decode(ByteStream & bs )
132 {
133 GP<GPixmap> retval=GPixmap::create();
134 decode(bs,*retval);
135 return retval;
136 }
137
138 void
decode(ByteStream & bs,GPixmap & pix)139 JPEGDecoder::decode(ByteStream & bs,GPixmap &pix)
140 {
141 struct jpeg_decompress_struct cinfo;
142
143 /* We use our private extension JPEG error handler. */
144 struct djvu_error_mgr jerr;
145
146 JSAMPARRAY buffer; /* Output row buffer */
147 int row_stride; /* physical row width in output buffer */
148 int isGrey,i;
149
150 cinfo.err = jpeg_std_error(&jerr.pub);
151
152 jerr.pub.error_exit = djvu_error_exit;
153
154 if (setjmp(jerr.setjmp_buffer))
155 {
156 /* Prepare error message - untranslated */
157 char msg[JMSG_LENGTH_MAX + 100];
158 strcpy(msg, "LibJpeg error: ");
159 char *emsg = msg + strlen(msg);
160 (*cinfo.err->format_message) ((j_common_ptr)&cinfo, emsg);
161 /* clean and throw */
162 jpeg_destroy_decompress(&cinfo);
163 G_THROW( msg );
164 }
165
166 jpeg_create_decompress(&cinfo);
167
168 Impl::jpeg_byte_stream_src(&cinfo, bs);
169
170 (void) jpeg_read_header(&cinfo, TRUE);
171
172 jpeg_start_decompress(&cinfo);
173
174 /* We may need to do some setup of our own at this point before reading
175 * the data. After jpeg_start_decompress() we have the correct scaled
176 * output image dimensions available, as well as the output colormap
177 * if we asked for color quantization.
178 * In this example, we need to make an output work buffer of the right size.
179 */
180
181 /* JSAMPLEs per row in output buffer */
182 row_stride = cinfo.output_width * cinfo.output_components;
183
184 /* Make a one-row-high sample array that will go away when done with image */
185 buffer = (*cinfo.mem->alloc_sarray)
186 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
187
188 GP<ByteStream> goutputBlock=ByteStream::create();
189 ByteStream &outputBlock=*goutputBlock;
190 outputBlock.format("P6\n%d %d\n%d\n",cinfo.output_width,
191 cinfo.output_height,255);
192
193 isGrey = ( cinfo.out_color_space == JCS_GRAYSCALE) ? 1 : 0;
194
195 while (cinfo.output_scanline < cinfo.output_height)
196 {
197 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
198
199 if ( isGrey == 1 )
200 {
201 for (i=0; i<row_stride; i++)
202 {
203 outputBlock.write8((char)buffer[0][i]);
204 outputBlock.write8((char)buffer[0][i]);
205 outputBlock.write8((char)buffer[0][i]);
206 }
207 }else
208 {
209 for (i=0; i<row_stride; i++)
210 outputBlock.write8((char)buffer[0][i]);
211 }
212 }
213
214 (void) jpeg_finish_decompress(&cinfo);
215
216 jpeg_destroy_decompress(&cinfo);
217
218 outputBlock.seek(0,SEEK_SET);
219
220 pix.init(outputBlock);
221 }
222
223 /*** From here onwards code is to make ByteStream as the data
224 source for the JPEG library */
225
226 extern "C"
227 {
228
229 typedef struct
230 {
231 struct jpeg_source_mgr pub; /* public fields */
232
233 ByteStream * byteStream; /* source stream */
234 JOCTET * buffer; /* start of buffer */
235 boolean start_of_stream;
236 } byte_stream_src_mgr;
237
238
239 typedef byte_stream_src_mgr * byte_stream_src_ptr;
240
241 #define INPUT_BUF_SIZE 4096
242
243 METHODDEF(void)
init_source(j_decompress_ptr cinfo)244 init_source (j_decompress_ptr cinfo)
245 {
246 byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
247
248 src->start_of_stream = TRUE;
249 }
250
251 METHODDEF(boolean)
fill_input_buffer(j_decompress_ptr cinfo)252 fill_input_buffer (j_decompress_ptr cinfo)
253 {
254 byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
255 size_t nbytes;
256
257 nbytes = src->byteStream->readall(src->buffer, INPUT_BUF_SIZE);
258
259 if (nbytes <= 0)
260 {
261 if (src->start_of_stream) /* Treat empty input as fatal error */
262 ERREXIT(cinfo, JERR_INPUT_EMPTY);
263 WARNMS(cinfo, JWRN_JPEG_EOF);
264 /* Insert a fake EOI marker */
265 src->buffer[0] = (JOCTET) 0xFF;
266 src->buffer[1] = (JOCTET) JPEG_EOI;
267 nbytes = 2;
268 }
269
270 src->pub.next_input_byte = src->buffer;
271 src->pub.bytes_in_buffer = nbytes;
272 src->start_of_stream = FALSE;
273
274 return TRUE;
275 }
276
277
278 METHODDEF(void)
skip_input_data(j_decompress_ptr cinfo,long num_bytes)279 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
280 {
281 byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
282
283 if (num_bytes > (long) src->pub.bytes_in_buffer)
284 {
285 src->byteStream->seek((num_bytes - src->pub.bytes_in_buffer), SEEK_CUR);
286 (void) fill_input_buffer(cinfo);
287 }else
288 {
289 src->pub.bytes_in_buffer -= num_bytes;
290 src->pub.next_input_byte += num_bytes;
291 }
292 }
293
294 METHODDEF(void)
term_source(j_decompress_ptr cinfo)295 term_source (j_decompress_ptr cinfo)
296 {
297 /* no work necessary here */
298 }
299
300 }
301
302 void
jpeg_byte_stream_src(j_decompress_ptr cinfo,ByteStream & bs)303 JPEGDecoder::Impl::jpeg_byte_stream_src(j_decompress_ptr cinfo,ByteStream &bs)
304 {
305 byte_stream_src_ptr src;
306
307 if (cinfo->src == NULL)
308 { /* first time for this JPEG object? */
309 cinfo->src = (struct jpeg_source_mgr *)
310 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
311 sizeof(byte_stream_src_mgr));
312 src = (byte_stream_src_ptr) cinfo->src;
313 src->buffer = (JOCTET *)
314 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
315 INPUT_BUF_SIZE * sizeof(JOCTET));
316 }
317
318 src = (byte_stream_src_ptr) cinfo->src;
319 src->pub.init_source = init_source;
320 src->pub.fill_input_buffer = fill_input_buffer;
321 src->pub.skip_input_data = skip_input_data;
322 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
323 src->pub.term_source = term_source;
324 src->byteStream = &bs;
325 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
326 src->pub.next_input_byte = NULL; /* until buffer loaded */
327 }
328
329 #ifdef LIBJPEGNAME
330 void *
jpeg_lookup(const GUTF8String & name)331 JPEGDecoder::jpeg_lookup(const GUTF8String &name)
332 {
333 static DjVuDynamic lib(GUTF8String(LIBJPEGNAME));
334 void *sym=lib.lookup(name);
335 if(!sym)
336 G_THROW(ERR_MSG("DjVuFile.JPEG_bg2"));
337 return sym;
338 }
339
340 jpeg_error_mgr *
jpeg_std_error(jpeg_error_mgr * x)341 JPEGDecoder::jpeg_std_error(jpeg_error_mgr *x)
342 {
343 static void *sym=jpeg_lookup("jpeg_std_error");
344 return ((jpeg_error_mgr *(*)(jpeg_error_mgr *))sym)(x);
345 }
346
347 void
jpeg_CreateDecompress(jpeg_decompress_struct * x,int v,size_t s)348 JPEGDecoder::jpeg_CreateDecompress(jpeg_decompress_struct *x,int v, size_t s)
349 {
350 static void *sym=jpeg_lookup("jpeg_CreateDecompress");
351 ((void (*)(jpeg_decompress_struct *,int,size_t))sym)(x,v,s);
352 }
353
354 void
jpeg_destroy_decompress(j_decompress_ptr x)355 JPEGDecoder::jpeg_destroy_decompress(j_decompress_ptr x)
356 {
357 static void *sym=jpeg_lookup("jpeg_destroy_decompress");
358 ((void (*)(j_decompress_ptr))sym)(x);
359 }
360
361 int
jpeg_read_header(j_decompress_ptr x,boolean y)362 JPEGDecoder::jpeg_read_header(j_decompress_ptr x,boolean y)
363 {
364 static void *sym=jpeg_lookup("jpeg_read_header");
365 return ((int (*)(j_decompress_ptr,boolean))sym)(x,y);
366 }
367
368 JDIMENSION
jpeg_read_scanlines(j_decompress_ptr x,JSAMPARRAY y,JDIMENSION z)369 JPEGDecoder::jpeg_read_scanlines(j_decompress_ptr x,JSAMPARRAY y,JDIMENSION z)
370 {
371 static void *sym=jpeg_lookup("jpeg_read_scanlines");
372 return ((JDIMENSION (*)(j_decompress_ptr,JSAMPARRAY,JDIMENSION))sym)(x,y,z);
373 }
374
375 boolean
jpeg_finish_decompress(j_decompress_ptr x)376 JPEGDecoder::jpeg_finish_decompress(j_decompress_ptr x)
377 {
378 static void *sym=jpeg_lookup("jpeg_finish_decompress");
379 return ((boolean (*)(j_decompress_ptr))sym)(x);
380 }
381
382 boolean
jpeg_resync_to_restart(jpeg_decompress_struct * x,int d)383 JPEGDecoder::jpeg_resync_to_restart(jpeg_decompress_struct *x,int d)
384 {
385 static void *sym=jpeg_lookup("jpeg_resync_to_restart");
386 return ((boolean (*)(jpeg_decompress_struct *,int))sym)(x,d);
387 }
388
389 boolean
jpeg_start_decompress(j_decompress_ptr x)390 JPEGDecoder::jpeg_start_decompress(j_decompress_ptr x)
391 {
392 static void *sym=jpeg_lookup("jpeg_start_decompress");
393 return ((boolean (*)(j_decompress_ptr))sym)(x);
394 }
395
396 #endif // LIBJPEGNAME
397
398
399 #ifdef HAVE_NAMESPACES
400 }
401 # ifndef NOT_USING_DJVU_NAMESPACE
402 using namespace DJVU;
403 # endif
404 #endif
405
406 #endif
407
408