2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // Functions to load JPEG files from a buffer, based on jdatasrc.c
34 // Leonardo Zide (leo@lokigames.com)
45 /* Expanded data source object for stdio input */
48 struct jpeg_source_mgr pub; /* public fields */
53 JOCTET * buffer; /* start of buffer */
54 boolean start_of_file; /* have we gotten any data yet? */
57 typedef my_source_mgr * my_src_ptr;
59 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
63 * Initialize source --- called by jpeg_read_header
64 * before any data is actually read.
67 static void my_init_source( j_decompress_ptr cinfo ){
68 my_src_ptr src = (my_src_ptr) cinfo->src;
70 /* We reset the empty-input-file flag for each image,
71 * but we don't clear the input buffer.
72 * This is correct behavior for reading a series of images from one source.
74 src->start_of_file = TRUE;
79 * Fill the input buffer --- called whenever buffer is emptied.
81 * In typical applications, this should read fresh data into the buffer
82 * (ignoring the current state of next_input_byte & bytes_in_buffer),
83 * reset the pointer & count to the start of the buffer, and return TRUE
84 * indicating that the buffer has been reloaded. It is not necessary to
85 * fill the buffer entirely, only to obtain at least one more byte.
87 * There is no such thing as an EOF return. If the end of the file has been
88 * reached, the routine has a choice of ERREXIT() or inserting fake data into
89 * the buffer. In most cases, generating a warning message and inserting a
90 * fake EOI marker is the best course of action --- this will allow the
91 * decompressor to output however much of the image is there. However,
92 * the resulting error message is misleading if the real problem is an empty
93 * input file, so we handle that case specially.
95 * In applications that need to be able to suspend compression due to input
96 * not being available yet, a FALSE return indicates that no more data can be
97 * obtained right now, but more may be forthcoming later. In this situation,
98 * the decompressor will return to its caller (with an indication of the
99 * number of scanlines it has read, if any). The application should resume
100 * decompression after it has loaded more data into the input buffer. Note
101 * that there are substantial restrictions on the use of suspension --- see
104 * When suspending, the decompressor will back up to a convenient restart point
105 * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
106 * indicate where the restart point will be if the current call returns FALSE.
107 * Data beyond this point must be rescanned after resumption, so move it to
108 * the front of the buffer rather than discarding it.
111 static boolean my_fill_input_buffer( j_decompress_ptr cinfo ){
112 my_src_ptr src = (my_src_ptr) cinfo->src;
115 if ( src->src_size > INPUT_BUF_SIZE ) {
116 nbytes = INPUT_BUF_SIZE;
119 nbytes = src->src_size;
122 memcpy( src->buffer, src->src_buffer, nbytes );
123 src->src_buffer += nbytes;
124 src->src_size -= nbytes;
127 if ( src->start_of_file ) { /* Treat empty input file as fatal error */
128 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;
137 src->pub.next_input_byte = src->buffer;
138 src->pub.bytes_in_buffer = nbytes;
139 src->start_of_file = FALSE;
146 * Skip data --- used to skip over a potentially large amount of
147 * uninteresting data (such as an APPn marker).
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.
157 static void my_skip_input_data( j_decompress_ptr cinfo, long num_bytes ){
158 my_src_ptr src = (my_src_ptr) cinfo->src;
160 /* Just a dumb implementation for now. Could use fseek() except
161 * it doesn't work on pipes. Not clear that being smart is worth
162 * any trouble anyway --- large skips are infrequent.
164 if ( num_bytes > 0 ) {
165 while ( num_bytes > (long) src->pub.bytes_in_buffer ) {
166 num_bytes -= (long) src->pub.bytes_in_buffer;
167 (void) my_fill_input_buffer( cinfo );
168 /* note we assume that fill_input_buffer will never return FALSE,
169 * so suspension need not be handled.
172 src->pub.next_input_byte += (size_t) num_bytes;
173 src->pub.bytes_in_buffer -= (size_t) num_bytes;
179 * An additional method that can be provided by data source modules is the
180 * resync_to_restart method for error recovery in the presence of RST markers.
181 * For the moment, this source module just uses the default resync method
182 * provided by the JPEG library. That method assumes that no backtracking
188 * Terminate source --- called by jpeg_finish_decompress
189 * after all data has been read. Often a no-op.
191 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
192 * application must deal with any cleanup that should happen even
196 static void my_term_source( j_decompress_ptr cinfo ){
197 /* no work necessary here */
202 * Prepare for input from a stdio stream.
203 * The caller must have already opened the stream, and is responsible
204 * for closing it after finishing decompression.
207 static void jpeg_buffer_src( j_decompress_ptr cinfo, void* buffer, int bufsize ){
210 /* The source object and input buffer are made permanent so that a series
211 * of JPEG images can be read from the same file by calling jpeg_stdio_src
212 * only before the first one. (If we discarded the buffer at the end of
213 * one image, we'd likely lose the start of the next one.)
214 * This makes it unsafe to use this manager and a different source
215 * manager serially with the same JPEG object. Caveat programmer.
217 if ( cinfo->src == NULL ) { /* first time for this JPEG object? */
218 cinfo->src = (struct jpeg_source_mgr *)
219 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_PERMANENT,
220 sizeof( my_source_mgr ) );
221 src = (my_src_ptr) cinfo->src;
222 src->buffer = (JOCTET *)
223 ( *cinfo->mem->alloc_small )( (j_common_ptr) cinfo, JPOOL_PERMANENT,
224 INPUT_BUF_SIZE * sizeof( JOCTET ) );
227 src = (my_src_ptr) cinfo->src;
228 src->pub.init_source = my_init_source;
229 src->pub.fill_input_buffer = my_fill_input_buffer;
230 src->pub.skip_input_data = my_skip_input_data;
231 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
232 src->pub.term_source = my_term_source;
233 src->src_buffer = (JOCTET *)buffer;
234 src->src_size = bufsize;
235 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
236 src->pub.next_input_byte = NULL; /* until buffer loaded */
239 // =============================================================================
241 static char errormsg[JMSG_LENGTH_MAX];
243 typedef struct my_jpeg_error_mgr
245 struct jpeg_error_mgr pub; // "public" fields
246 jmp_buf setjmp_buffer; // for return to caller
249 static void my_jpeg_error_exit( j_common_ptr cinfo ){
250 bt_jpeg_error_mgr* myerr = (bt_jpeg_error_mgr*) cinfo->err;
252 ( *cinfo->err->format_message )( cinfo, errormsg );
254 longjmp( myerr->setjmp_buffer, 1 );
258 static void j_putRGBScanline( unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row ){
259 int offset = row * widthPix * 4;
262 for ( count = 0; count < widthPix; count++ )
264 unsigned char iRed, iBlu, iGrn;
265 unsigned char *oRed, *oBlu, *oGrn, *oAlp;
267 iRed = *( jpegline + count * 3 + 0 );
268 iGrn = *( jpegline + count * 3 + 1 );
269 iBlu = *( jpegline + count * 3 + 2 );
271 oRed = outBuf + offset + count * 4 + 0;
272 oGrn = outBuf + offset + count * 4 + 1;
273 oBlu = outBuf + offset + count * 4 + 2;
274 oAlp = outBuf + offset + count * 4 + 3;
284 static void j_putRGBAScanline( unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row ){
285 int offset = row * widthPix * 4;
288 for ( count = 0; count < widthPix; count++ )
290 unsigned char iRed, iBlu, iGrn /* , iAlp */;
291 unsigned char *oRed, *oBlu, *oGrn, *oAlp;
293 iRed = *( jpegline + count * 4 + 0 );
294 iGrn = *( jpegline + count * 4 + 1 );
295 iBlu = *( jpegline + count * 4 + 2 );
296 /* iAlp = *( jpegline + count * 4 + 3 ); */
298 oRed = outBuf + offset + count * 4 + 0;
299 oGrn = outBuf + offset + count * 4 + 1;
300 oBlu = outBuf + offset + count * 4 + 2;
301 oAlp = outBuf + offset + count * 4 + 3;
306 // ydnar: see bug 900
307 *oAlp = 255; //% iAlp;
311 // stash a gray scanline
312 static void j_putGrayScanlineToRGB( unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row ){
313 int offset = row * widthPix * 4;
316 for ( count = 0; count < widthPix; count++ )
319 unsigned char *oRed, *oBlu, *oGrn, *oAlp;
321 // get our grayscale value
322 iGray = *( jpegline + count );
324 oRed = outBuf + offset + count * 4;
325 oGrn = outBuf + offset + count * 4 + 1;
326 oBlu = outBuf + offset + count * 4 + 2;
327 oAlp = outBuf + offset + count * 4 + 3;
336 int LoadJPGBuff( void *src_buffer, int src_size, unsigned char **pic, int *width, int *height ) {
337 struct jpeg_decompress_struct cinfo;
338 struct my_jpeg_error_mgr jerr;
340 int row_stride, size;
342 cinfo.err = jpeg_std_error( &jerr.pub );
343 jerr.pub.error_exit = my_jpeg_error_exit;
345 if ( setjmp( jerr.setjmp_buffer ) ) {
346 *pic = (unsigned char*)errormsg;
347 jpeg_destroy_decompress( &cinfo );
351 jpeg_create_decompress( &cinfo );
352 jpeg_buffer_src( &cinfo, src_buffer, src_size );
353 jpeg_read_header( &cinfo, TRUE );
354 jpeg_start_decompress( &cinfo );
356 row_stride = cinfo.output_width * cinfo.output_components;
358 size = cinfo.output_width * cinfo.output_height * 4;
359 *width = cinfo.output_width;
360 *height = cinfo.output_height;
361 *pic = (unsigned char*)( malloc( size + 1 ) );
362 memset( *pic, 0, size + 1 );
364 buffer = ( *cinfo.mem->alloc_sarray )( ( j_common_ptr ) & cinfo, JPOOL_IMAGE, row_stride, 1 );
366 while ( cinfo.output_scanline < cinfo.output_height )
368 jpeg_read_scanlines( &cinfo, buffer, 1 );
370 if ( cinfo.out_color_components == 4 ) {
371 j_putRGBAScanline( buffer[0], cinfo.output_width, *pic, cinfo.output_scanline - 1 );
373 else if ( cinfo.out_color_components == 3 ) {
374 j_putRGBScanline( buffer[0], cinfo.output_width, *pic, cinfo.output_scanline - 1 );
376 else if ( cinfo.out_color_components == 1 ) {
377 j_putGrayScanlineToRGB( buffer[0], cinfo.output_width, *pic, cinfo.output_scanline - 1 );
381 jpeg_finish_decompress( &cinfo );
382 jpeg_destroy_decompress( &cinfo );