15 #include "pnmFileTypeJPG.h"
19 #include "config_pnmimagetypes.h"
54 struct jpeg_source_mgr pub;
58 boolean start_of_file;
61 typedef my_source_mgr * my_src_ptr;
63 #define INPUT_BUF_SIZE 4096
72 init_source (j_decompress_ptr cinfo)
74 my_src_ptr src = (my_src_ptr) cinfo->src;
80 src->start_of_file = TRUE;
118 fill_input_buffer (j_decompress_ptr cinfo)
120 my_src_ptr src = (my_src_ptr) cinfo->src;
123 src->infile->read((
char *)src->buffer, INPUT_BUF_SIZE);
124 nbytes = src->infile->gcount();
128 if (src->start_of_file)
129 ERREXIT(cinfo, JERR_INPUT_EMPTY);
130 WARNMS(cinfo, JWRN_JPEG_EOF);
132 src->buffer[0] = (JOCTET) 0xFF;
133 src->buffer[1] = (JOCTET) JPEG_EOI;
137 src->pub.next_input_byte = src->buffer;
138 src->pub.bytes_in_buffer = nbytes;
139 src->start_of_file = FALSE;
158 skip_input_data (j_decompress_ptr cinfo,
long num_bytes)
160 my_src_ptr src = (my_src_ptr) cinfo->src;
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);
174 src->pub.next_input_byte += (size_t) num_bytes;
175 src->pub.bytes_in_buffer -= (size_t) num_bytes;
199 term_source (j_decompress_ptr cinfo)
212 jpeg_istream_src (j_decompress_ptr cinfo, istream * infile)
223 if (cinfo->src == NULL) {
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));
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;
238 src->pub.term_source = term_source;
239 src->infile = infile;
240 src->pub.bytes_in_buffer = 0;
241 src->pub.next_input_byte = NULL;
255 PNMFileTypeJPG::Reader::
256 Reader(
PNMFileType *type, istream *file,
bool owns_file,
string magic_number) :
260 for (string::reverse_iterator mi = magic_number.rbegin();
261 mi != magic_number.rend();
266 pnmimage_jpg_cat.error()
267 <<
"Unable to put back magic number.\n";
276 _cinfo.err = jpeg_std_error(&_jerr.pub);
279 jpeg_create_decompress(&_cinfo);
282 jpeg_istream_src(&_cinfo, file);
285 jpeg_save_markers(&_cinfo, JPEG_COM, 0xffff);
288 jpeg_read_header(&_cinfo, TRUE);
295 _num_channels = _cinfo.num_components;
296 _x_size = (int)_cinfo.image_width;
297 _y_size = (
int)_cinfo.image_height;
298 _maxval = MAXJSAMPLE;
301 _cinfo.scale_num = 1;
302 _cinfo.scale_denom = 1;
317 void PNMFileTypeJPG::Reader::
319 if (_has_read_size && _read_x_size != 0 && _read_y_size != 0) {
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);
328 jpeg_start_decompress(&_cinfo);
333 _num_channels = _cinfo.output_components;
334 _x_size = (int)_cinfo.output_width;
335 _y_size = (
int)_cinfo.output_height;
343 PNMFileTypeJPG::Reader::
346 jpeg_destroy_decompress(&_cinfo);
364 int PNMFileTypeJPG::Reader::
365 read_data(
xel *array, xelval *) {
372 nassertr(_cinfo.output_components == 1 || _cinfo.output_components == 3, 0);
381 row_stride = _cinfo.output_width * _cinfo.output_components;
384 buffer = (*_cinfo.mem->alloc_sarray)
385 ((j_common_ptr) &_cinfo, JPOOL_IMAGE, row_stride, 1);
394 while (_cinfo.output_scanline < _cinfo.output_height) {
399 jpeg_read_scanlines(&_cinfo, buffer, 1);
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);
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);
423 jpeg_finish_decompress(&_cinfo);
432 if (_jerr.pub.num_warnings) {
433 pnmimage_jpg_cat.warning()
434 <<
"Jpeg data may be corrupt" << endl;
This is the base class of a family of classes that represent particular image file types that PNMImag...
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
This is an abstract base class that defines the interface for reading image files of various types...