Panda3D
pnmFileTypeJPGReader.cxx
1 // Filename: pnmFileTypeJPGReader.cxx
2 // Created by: mike (19Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "pnmFileTypeJPG.h"
16 
17 #ifdef HAVE_JPEG
18 
19 #include "config_pnmimagetypes.h"
20 #include "thread.h"
21 
22 //
23 // The following bit of code, for setting up jpeg_istream_src(), was
24 // lifted from jpeglib, and modified to work with istream instead of
25 // stdio.
26 //
27 
28 /*
29  * jdatasrc.c
30  *
31  * Copyright (C) 1994-1996, Thomas G. Lane.
32  * This file is part of the Independent JPEG Group's software.
33  * For conditions of distribution and use, see the accompanying README file.
34  *
35  * This file contains decompression data source routines for the case of
36  * reading JPEG data from a file (or any stdio stream). While these routines
37  * are sufficient for most applications, some will want to use a different
38  * source manager.
39  * IMPORTANT: we assume that fread() will correctly transcribe an array of
40  * JOCTETs from 8-bit-wide elements on external storage. If char is wider
41  * than 8 bits on your machine, you may need to do some tweaking.
42  */
43 
44 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
45 extern "C" {
46 #include <jpeglib.h>
47 #include <jerror.h>
48 }
49 
50 
51 /* Expanded data source object for stdio input */
52 
53 typedef struct {
54  struct jpeg_source_mgr pub; /* public fields */
55 
56  istream * infile; /* source stream */
57  JOCTET * buffer; /* start of buffer */
58  boolean start_of_file; /* have we gotten any data yet? */
59 } my_source_mgr;
60 
61 typedef my_source_mgr * my_src_ptr;
62 
63 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
64 
65 
66 /*
67  * Initialize source --- called by jpeg_read_header
68  * before any data is actually read.
69  */
70 
71 METHODDEF(void)
72 init_source (j_decompress_ptr cinfo)
73 {
74  my_src_ptr src = (my_src_ptr) cinfo->src;
75 
76  /* We reset the empty-input-file flag for each image,
77  * but we don't clear the input buffer.
78  * This is correct behavior for reading a series of images from one source.
79  */
80  src->start_of_file = TRUE;
81 }
82 
83 
84 /*
85  * Fill the input buffer --- called whenever buffer is emptied.
86  *
87  * In typical applications, this should read fresh data into the buffer
88  * (ignoring the current state of next_input_byte & bytes_in_buffer),
89  * reset the pointer & count to the start of the buffer, and return TRUE
90  * indicating that the buffer has been reloaded. It is not necessary to
91  * fill the buffer entirely, only to obtain at least one more byte.
92  *
93  * There is no such thing as an EOF return. If the end of the file has been
94  * reached, the routine has a choice of ERREXIT() or inserting fake data into
95  * the buffer. In most cases, generating a warning message and inserting a
96  * fake EOI marker is the best course of action --- this will allow the
97  * decompressor to output however much of the image is there. However,
98  * the resulting error message is misleading if the real problem is an empty
99  * input file, so we handle that case specially.
100  *
101  * In applications that need to be able to suspend compression due to input
102  * not being available yet, a FALSE return indicates that no more data can be
103  * obtained right now, but more may be forthcoming later. In this situation,
104  * the decompressor will return to its caller (with an indication of the
105  * number of scanlines it has read, if any). The application should resume
106  * decompression after it has loaded more data into the input buffer. Note
107  * that there are substantial restrictions on the use of suspension --- see
108  * the documentation.
109  *
110  * When suspending, the decompressor will back up to a convenient restart point
111  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
112  * indicate where the restart point will be if the current call returns FALSE.
113  * Data beyond this point must be rescanned after resumption, so move it to
114  * the front of the buffer rather than discarding it.
115  */
116 
117 METHODDEF(boolean)
118 fill_input_buffer (j_decompress_ptr cinfo)
119 {
120  my_src_ptr src = (my_src_ptr) cinfo->src;
121  size_t nbytes;
122 
123  src->infile->read((char *)src->buffer, INPUT_BUF_SIZE);
124  nbytes = src->infile->gcount();
126 
127  if (nbytes <= 0) {
128  if (src->start_of_file) /* Treat empty input file as fatal error */
129  ERREXIT(cinfo, JERR_INPUT_EMPTY);
130  WARNMS(cinfo, JWRN_JPEG_EOF);
131  /* Insert a fake EOI marker */
132  src->buffer[0] = (JOCTET) 0xFF;
133  src->buffer[1] = (JOCTET) JPEG_EOI;
134  nbytes = 2;
135  }
136 
137  src->pub.next_input_byte = src->buffer;
138  src->pub.bytes_in_buffer = nbytes;
139  src->start_of_file = FALSE;
140 
141  return TRUE;
142 }
143 
144 
145 /*
146  * Skip data --- used to skip over a potentially large amount of
147  * uninteresting data (such as an APPn marker).
148  *
149  * Writers of suspendable-input applications must note that skip_input_data
150  * is not granted the right to give a suspension return. If the skip extends
151  * beyond the data currently in the buffer, the buffer can be marked empty so
152  * that the next read will cause a fill_input_buffer call that can suspend.
153  * Arranging for additional bytes to be discarded before reloading the input
154  * buffer is the application writer's problem.
155  */
156 
157 METHODDEF(void)
158 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
159 {
160  my_src_ptr src = (my_src_ptr) cinfo->src;
161 
162  /* Just a dumb implementation for now. Could use fseek() except
163  * it doesn't work on pipes. Not clear that being smart is worth
164  * any trouble anyway --- large skips are infrequent.
165  */
166  if (num_bytes > 0) {
167  while (num_bytes > (long) src->pub.bytes_in_buffer) {
168  num_bytes -= (long) src->pub.bytes_in_buffer;
169  (void) fill_input_buffer(cinfo);
170  /* note we assume that fill_input_buffer will never return FALSE,
171  * so suspension need not be handled.
172  */
173  }
174  src->pub.next_input_byte += (size_t) num_bytes;
175  src->pub.bytes_in_buffer -= (size_t) num_bytes;
176  }
177 }
178 
179 
180 /*
181  * An additional method that can be provided by data source modules is the
182  * resync_to_restart method for error recovery in the presence of RST markers.
183  * For the moment, this source module just uses the default resync method
184  * provided by the JPEG library. That method assumes that no backtracking
185  * is possible.
186  */
187 
188 
189 /*
190  * Terminate source --- called by jpeg_finish_decompress
191  * after all data has been read. Often a no-op.
192  *
193  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
194  * application must deal with any cleanup that should happen even
195  * for error exit.
196  */
197 
198 METHODDEF(void)
199 term_source (j_decompress_ptr cinfo)
200 {
201  /* no work necessary here */
202 }
203 
204 
205 /*
206  * Prepare for input from a stdio stream.
207  * The caller must have already opened the stream, and is responsible
208  * for closing it after finishing decompression.
209  */
210 
211 GLOBAL(void)
212 jpeg_istream_src (j_decompress_ptr cinfo, istream * infile)
213 {
214  my_src_ptr src;
215 
216  /* The source object and input buffer are made permanent so that a series
217  * of JPEG images can be read from the same file by calling jpeg_stdio_src
218  * only before the first one. (If we discarded the buffer at the end of
219  * one image, we'd likely lose the start of the next one.)
220  * This makes it unsafe to use this manager and a different source
221  * manager serially with the same JPEG object. Caveat programmer.
222  */
223  if (cinfo->src == NULL) { /* first time for this JPEG object? */
224  cinfo->src = (struct jpeg_source_mgr *)
225  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
226  sizeof(my_source_mgr));
227  src = (my_src_ptr) cinfo->src;
228  src->buffer = (JOCTET *)
229  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
230  INPUT_BUF_SIZE * sizeof(JOCTET));
231  }
232 
233  src = (my_src_ptr) cinfo->src;
234  src->pub.init_source = init_source;
235  src->pub.fill_input_buffer = fill_input_buffer;
236  src->pub.skip_input_data = skip_input_data;
237  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
238  src->pub.term_source = term_source;
239  src->infile = infile;
240  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
241  src->pub.next_input_byte = NULL; /* until buffer loaded */
242 }
243 
244 
245 
246 //
247 // The rest of the code in this file is new to Panda.
248 //
249 
250 ////////////////////////////////////////////////////////////////////
251 // Function: PNMFileTypeJPG::Reader::Constructor
252 // Access: Public
253 // Description:
254 ////////////////////////////////////////////////////////////////////
255 PNMFileTypeJPG::Reader::
256 Reader(PNMFileType *type, istream *file, bool owns_file, string magic_number) :
257  PNMReader(type, file, owns_file)
258 {
259  // Hope we can putback() more than one character.
260  for (string::reverse_iterator mi = magic_number.rbegin();
261  mi != magic_number.rend();
262  ++mi) {
263  _file->putback(*mi);
264  }
265  if (_file->fail()) {
266  pnmimage_jpg_cat.error()
267  << "Unable to put back magic number.\n";
268  _is_valid = false;
269  return;
270  }
271  _is_valid = true;
272 
273  /* Step 1: allocate and initialize JPEG decompression object */
274 
275  /* We set up the normal JPEG error routines, then override error_exit. */
276  _cinfo.err = jpeg_std_error(&_jerr.pub);
277 
278  /* Now we can initialize the JPEG decompression object. */
279  jpeg_create_decompress(&_cinfo);
280 
281  /* Step 2: specify data source (eg, a file) */
282  jpeg_istream_src(&_cinfo, file);
283 
284  /* Step 3: let lib jpeg know that we want to read the comment field */
285  jpeg_save_markers(&_cinfo, JPEG_COM, 0xffff);
286 
287  /* Step 4: read file parameters with jpeg_read_header() */
288  jpeg_read_header(&_cinfo, TRUE);
289  /* We can ignore the return value from jpeg_read_header since
290  * (a) suspension is not possible with the stdio data source, and
291  * (b) we passed TRUE to reject a tables-only JPEG file as an error.
292  * See libjpeg.doc for more info.
293  */
294 
295  _num_channels = _cinfo.num_components;
296  _x_size = (int)_cinfo.image_width;
297  _y_size = (int)_cinfo.image_height;
298  _maxval = MAXJSAMPLE;
299 
300  /* Step 6: set parameters for decompression */
301  _cinfo.scale_num = 1;
302  _cinfo.scale_denom = 1;
303 }
304 
305 ////////////////////////////////////////////////////////////////////
306 // Function: PNMFileTypeJPG::Reader::prepare_read
307 // Access: Public, Virtual
308 // Description: This method will be called before read_data() or
309 // read_row() is called. It instructs the reader to
310 // initialize its data structures as necessary to
311 // actually perform the read operation.
312 //
313 // After this call, _x_size and _y_size should reflect
314 // the actual size that will be filled by read_data()
315 // (as possibly modified by set_read_size()).
316 ////////////////////////////////////////////////////////////////////
317 void PNMFileTypeJPG::Reader::
318 prepare_read() {
319  if (_has_read_size && _read_x_size != 0 && _read_y_size != 0) {
320  // Attempt to get the scale close to our target scale.
321  int x_reduction = _cinfo.image_width / _read_x_size;
322  int y_reduction = _cinfo.image_height / _read_y_size;
323  _cinfo.scale_denom = max(min(x_reduction, y_reduction), 1);
324  }
325 
326  /* Step 7: Start decompressor */
327 
328  jpeg_start_decompress(&_cinfo);
329  /* We can ignore the return value since suspension is not possible
330  * with the stdio data source.
331  */
332 
333  _num_channels = _cinfo.output_components;
334  _x_size = (int)_cinfo.output_width;
335  _y_size = (int)_cinfo.output_height;
336 }
337 
338 ////////////////////////////////////////////////////////////////////
339 // Function: PNMFileTypeJPG::Reader::Destructor
340 // Access: Public
341 // Description:
342 ////////////////////////////////////////////////////////////////////
343 PNMFileTypeJPG::Reader::
344 ~Reader() {
345  if (_is_valid) {
346  jpeg_destroy_decompress(&_cinfo);
347  _is_valid = false;
348  }
349 }
350 
351 ////////////////////////////////////////////////////////////////////
352 // Function: PNMFileTypeJPG::Reader::read_data
353 // Access: Public, Virtual
354 // Description: Reads in an entire image all at once, storing it in
355 // the pre-allocated _x_size * _y_size array and alpha
356 // pointers. (If the image type has no alpha channel,
357 // alpha is ignored.) Returns the number of rows
358 // correctly read.
359 //
360 // Derived classes need not override this if they
361 // instead provide supports_read_row() and read_row(),
362 // below.
363 ////////////////////////////////////////////////////////////////////
364 int PNMFileTypeJPG::Reader::
365 read_data(xel *array, xelval *) {
366  if (!_is_valid) {
367  return 0;
368  }
369  JSAMPARRAY buffer; /* Output row buffer */
370  int row_stride; /* physical row width in output buffer */
371 
372  nassertr(_cinfo.output_components == 1 || _cinfo.output_components == 3, 0);
373 
374  /* We may need to do some setup of our own at this point before reading
375  * the data. After jpeg_start_decompress() we have the correct scaled
376  * output image dimensions available, as well as the output colormap
377  * if we asked for color quantization.
378  * In this example, we need to make an output work buffer of the right size.
379  */
380  /* JSAMPLEs per row in output buffer */
381  row_stride = _cinfo.output_width * _cinfo.output_components;
382  /* Make a one-row-high sample array that will go away when done with image */
383 
384  buffer = (*_cinfo.mem->alloc_sarray)
385  ((j_common_ptr) &_cinfo, JPOOL_IMAGE, row_stride, 1);
386 
387  /* Step 6: while (scan lines remain to be read) */
388  /* jpeg_read_scanlines(...); */
389 
390  /* Here we use the library's state variable cinfo.output_scanline as the
391  * loop counter, so that we don't have to keep track ourselves.
392  */
393  int x = 0;
394  while (_cinfo.output_scanline < _cinfo.output_height) {
395  /* jpeg_read_scanlines expects an array of pointers to scanlines.
396  * Here the array is only one element long, but you could ask for
397  * more than one scanline at a time if that's more convenient.
398  */
399  jpeg_read_scanlines(&_cinfo, buffer, 1);
400  /* Assume put_scanline_someplace wants a pointer and sample count. */
401  //put_scanline_someplace(buffer[0], row_stride);
402  JSAMPROW bufptr = buffer[0];
403  for (int i = 0; i < row_stride; i += _cinfo.output_components) {
404  if (_cinfo.output_components == 1) {
405  xelval val = (xelval)bufptr[i];
406  nassertr(x < _x_size * _y_size, 0);
407  PNM_ASSIGN1(array[x], val);
408  } else {
409  xelval red, grn, blu;
410  red = (xelval)bufptr[i];
411  grn = (xelval)bufptr[i+1];
412  blu = (xelval)bufptr[i+2];
413  nassertr(x < _x_size * _y_size, 0);
414  PPM_ASSIGN(array[x], red, grn, blu);
415  }
416  x++;
417  }
419  }
420 
421  /* Step 7: Finish decompression */
422 
423  jpeg_finish_decompress(&_cinfo);
424 
425  /* We can ignore the return value since suspension is not possible
426  * with the stdio data source.
427  */
428 
429  /* At this point you may want to check to see whether any corrupt-data
430  * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
431  */
432  if (_jerr.pub.num_warnings) {
433  pnmimage_jpg_cat.warning()
434  << "Jpeg data may be corrupt" << endl;
435  }
436 
437  return _y_size;
438 }
439 
440 #endif // HAVE_JPEG
This is the base class of a family of classes that represent particular image file types that PNMImag...
Definition: pnmFileType.h:35
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
Definition: thread.I:263
This is an abstract base class that defines the interface for reading image files of various types...
Definition: pnmReader.h:31