// jpgimage.cpp -- image data from jpeg file // // Written by Frederic Bouvier, started October 2001. // // Copyright (C) 2001 Frederic Bouvier - fredb@users.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: jpgimage.cpp,v 1.4 2005/05/09 07:01:52 fredb Exp $ #include "jpgimage.hpp" #include #include /* Expanded data source object for stdio input */ #pragma pack( 1 ) typedef struct { struct jpeg_source_mgr pub; /* public fields */ const unsigned char *_input; /* source stream */ size_t _read; /* size read so far */ size_t _length; /* source length */ JOCTET *buffer; /* start of buffer */ boolean start_of_file; /* have we gotten any data yet? */ } my_source_mgr; #pragma pack() typedef my_source_mgr * my_src_ptr; #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */ FGSD_JPEGImage::FGSD_JPEGImage( bool __reverted ) : _reverted( __reverted ) { } FGSD_JPEGImage::~FGSD_JPEGImage () { } bool FGSD_JPEGImage::load (const char *__fileName) { bool ret = false; FILE *infile = fopen( __fileName, "rb" ); if ( infile != 0 ) { jpeg_decompress_struct cinfo; memset( &cinfo, 0, sizeof cinfo ); jpeg_error_mgr jerr; memset( &jerr, 0, sizeof jerr ); cinfo.err = jpeg_std_error( &jerr ); jpeg_create_decompress( &cinfo ); jpeg_stdio_src( &cinfo, infile ); jpeg_read_header( &cinfo, TRUE ); jpeg_start_decompress( &cinfo ); if ( cinfo.out_color_space == JCS_RGB ) { allocImage( cinfo.output_width, cinfo.output_height ); while ( cinfo.output_scanline < cinfo.output_height) { unsigned char *buf; if ( _reverted ) { buf = buffer( cinfo.output_height - ( cinfo.output_scanline + 1 ) ); } else { buf = buffer( cinfo.output_scanline ); } jpeg_read_scanlines( &cinfo, (JSAMPARRAY)&buf, 1 ); } } jpeg_finish_decompress( &cinfo ); jpeg_destroy_decompress( &cinfo ); fclose(infile); ret = true; } return ret; } bool FGSD_JPEGImage::loadFromBuffer( const unsigned char *__buffer, size_t __len ) { jpeg_decompress_struct cinfo; memset( &cinfo, 0, sizeof cinfo ); jpeg_error_mgr jerr; memset( &jerr, 0, sizeof jerr ); cinfo.err = jpeg_std_error( &jerr ); jpeg_create_decompress( &cinfo ); jpeg_mem_src( &cinfo, __buffer, __len ); jpeg_read_header( &cinfo, TRUE ); jpeg_start_decompress( &cinfo ); if ( cinfo.out_color_space == JCS_RGB ) { allocImage( cinfo.output_width, cinfo.output_height ); } else if ( cinfo.out_color_space == JCS_GRAYSCALE ) { allocImage( cinfo.output_width, cinfo.output_height, 1 ); } while ( cinfo.output_scanline < cinfo.output_height) { unsigned char *buf; if ( _reverted ) { buf = buffer( cinfo.output_height - ( cinfo.output_scanline + 1 ) ); } else { buf = buffer( cinfo.output_scanline ); } jpeg_read_scanlines( &cinfo, (JSAMPARRAY)&buf, 1 ); } jpeg_finish_decompress( &cinfo ); jpeg_destroy_decompress( &cinfo ); return true; } void FGSD_JPEGImage::jpeg_mem_src( j_decompress_ptr cinfo, const unsigned char *__buffer, size_t __len ) { my_src_ptr src; /* The source object and input buffer are made permanent so that a series * of JPEG images can be read from the same file by calling jpeg_stdio_src * only before the first one. (If we discarded the buffer at the end of * one image, we'd likely lose the start of the next one.) * This makes it unsafe to use this manager and a different source * manager serially with the same JPEG object. Caveat programmer. */ if (cinfo->src == NULL) { /* first time for this JPEG object? */ cinfo->src = (struct jpeg_source_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof( my_source_mgr ) ); src = (my_src_ptr) cinfo->src; src->buffer = (JOCTET *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof( JOCTET ) ); } src = (my_src_ptr) cinfo->src; src->pub.init_source = &FGSD_JPEGImage::init_source; src->pub.fill_input_buffer = &FGSD_JPEGImage::fill_input_buffer; src->pub.skip_input_data = &FGSD_JPEGImage::skip_input_data; src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->pub.term_source = &FGSD_JPEGImage::term_source; src->_input = __buffer; src->_read = 0; src->_length = __len; src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */ src->pub.next_input_byte = NULL; /* until buffer loaded */ } void FGSD_JPEGImage::init_source(j_decompress_ptr cinfo) { my_src_ptr src = (my_src_ptr) cinfo->src; /* We reset the empty-input-file flag for each image, * but we don't clear the input buffer. * This is correct behavior for reading a series of images from one source. */ src->start_of_file = TRUE; } boolean FGSD_JPEGImage::fill_input_buffer(j_decompress_ptr cinfo) { my_src_ptr src = (my_src_ptr) cinfo->src; size_t nbytes = src->_length - src->_read; if ( nbytes > INPUT_BUF_SIZE ) { nbytes = INPUT_BUF_SIZE; } memcpy( src->buffer, src->_input + src->_read, nbytes ); src->_read += nbytes; if (nbytes <= 0) { if (src->start_of_file) /* Treat empty input file as fatal error */ ERREXIT(cinfo, JERR_INPUT_EMPTY); WARNMS(cinfo, JWRN_JPEG_EOF); /* Insert a fake EOI marker */ src->buffer[0] = (JOCTET) 0xFF; src->buffer[1] = (JOCTET) JPEG_EOI; nbytes = 2; } src->pub.next_input_byte = src->buffer; src->pub.bytes_in_buffer = nbytes; src->start_of_file = FALSE; return TRUE; } void FGSD_JPEGImage::skip_input_data(j_decompress_ptr cinfo, long num_bytes) { my_src_ptr src = (my_src_ptr) cinfo->src; /* Just a dumb implementation for now. Could use fseek() except * it doesn't work on pipes. Not clear that being smart is worth * any trouble anyway --- large skips are infrequent. */ if (num_bytes > 0) { while (num_bytes > (long) src->pub.bytes_in_buffer) { num_bytes -= (long) src->pub.bytes_in_buffer; (void) fill_input_buffer(cinfo); /* note we assume that fill_input_buffer will never return FALSE, * so suspension need not be handled. */ } src->pub.next_input_byte += (size_t) num_bytes; src->pub.bytes_in_buffer -= (size_t) num_bytes; } } void FGSD_JPEGImage::term_source(j_decompress_ptr cinfo) { // nothing } bool FGSD_JPEGImage::save (const char *__fileName) { bool ret = false; FILE *outfile = fopen( __fileName, "wb" ); if ( outfile != 0 ) { jpeg_compress_struct cinfo; memset( &cinfo, 0, sizeof cinfo ); jpeg_error_mgr jerr; memset( &jerr, 0, sizeof jerr ); cinfo.err = jpeg_std_error( &jerr ); jpeg_create_compress( &cinfo ); jpeg_stdio_dest( &cinfo, outfile ); cinfo.image_width = _width; cinfo.image_height = _height; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_start_compress( &cinfo, TRUE ); while ( cinfo.next_scanline < cinfo.image_height ) { unsigned char *buf; if ( _reverted ) { buf = buffer( cinfo.image_height - ( cinfo.next_scanline + 1 ) ); } else { buf = buffer( cinfo.next_scanline ); } jpeg_write_scanlines( &cinfo, (JSAMPARRAY)&buf, 1 ); } jpeg_finish_compress( &cinfo ); jpeg_destroy_compress( &cinfo ); fclose(outfile); ret = true; } return ret; }