2 Copyright (c) 2001, Loki software, inc.
\r
5 Redistribution and use in source and binary forms, with or without modification,
\r
6 are permitted provided that the following conditions are met:
\r
8 Redistributions of source code must retain the above copyright notice, this list
\r
9 of conditions and the following disclaimer.
\r
11 Redistributions in binary form must reproduce the above copyright notice, this
\r
12 list of conditions and the following disclaimer in the documentation and/or
\r
13 other materials provided with the distribution.
\r
15 Neither the name of Loki software nor the names of its contributors may be used
\r
16 to endorse or promote products derived from this software without specific prior
\r
17 written permission.
\r
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
\r
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
\r
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
\r
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 // Functions to load JPEG files from a buffer, based on jdatasrc.c
\r
34 // Leonardo Zide (leo@lokigames.com)
\r
44 #include "radiant_jpeglib.h"
\r
45 #include "jpeg6/jerror.h"
\r
50 /* Expanded data source object for stdio input */
\r
53 struct jpeg_source_mgr pub; /* public fields */
\r
56 JOCTET * src_buffer;
\r
58 JOCTET * buffer; /* start of buffer */
\r
59 boolean start_of_file; /* have we gotten any data yet? */
\r
62 typedef my_source_mgr * my_src_ptr;
\r
64 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
\r
68 * Initialize source --- called by jpeg_read_header
\r
69 * before any data is actually read.
\r
72 static void my_init_source (j_decompress_ptr cinfo)
\r
74 my_src_ptr src = (my_src_ptr) cinfo->src;
\r
76 /* We reset the empty-input-file flag for each image,
\r
77 * but we don't clear the input buffer.
\r
78 * This is correct behavior for reading a series of images from one source.
\r
80 src->start_of_file = TRUE;
\r
85 * Fill the input buffer --- called whenever buffer is emptied.
\r
87 * In typical applications, this should read fresh data into the buffer
\r
88 * (ignoring the current state of next_input_byte & bytes_in_buffer),
\r
89 * reset the pointer & count to the start of the buffer, and return TRUE
\r
90 * indicating that the buffer has been reloaded. It is not necessary to
\r
91 * fill the buffer entirely, only to obtain at least one more byte.
\r
93 * There is no such thing as an EOF return. If the end of the file has been
\r
94 * reached, the routine has a choice of ERREXIT() or inserting fake data into
\r
95 * the buffer. In most cases, generating a warning message and inserting a
\r
96 * fake EOI marker is the best course of action --- this will allow the
\r
97 * decompressor to output however much of the image is there. However,
\r
98 * the resulting error message is misleading if the real problem is an empty
\r
99 * input file, so we handle that case specially.
\r
101 * In applications that need to be able to suspend compression due to input
\r
102 * not being available yet, a FALSE return indicates that no more data can be
\r
103 * obtained right now, but more may be forthcoming later. In this situation,
\r
104 * the decompressor will return to its caller (with an indication of the
\r
105 * number of scanlines it has read, if any). The application should resume
\r
106 * decompression after it has loaded more data into the input buffer. Note
\r
107 * that there are substantial restrictions on the use of suspension --- see
\r
108 * the documentation.
\r
110 * When suspending, the decompressor will back up to a convenient restart point
\r
111 * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
\r
112 * indicate where the restart point will be if the current call returns FALSE.
\r
113 * Data beyond this point must be rescanned after resumption, so move it to
\r
114 * the front of the buffer rather than discarding it.
\r
117 static boolean my_fill_input_buffer (j_decompress_ptr cinfo)
\r
119 my_src_ptr src = (my_src_ptr) cinfo->src;
\r
122 if (src->src_size > INPUT_BUF_SIZE)
\r
123 nbytes = INPUT_BUF_SIZE;
\r
125 nbytes = src->src_size;
\r
127 memcpy (src->buffer, src->src_buffer, nbytes);
\r
128 src->src_buffer += nbytes;
\r
129 src->src_size -= nbytes;
\r
132 if (src->start_of_file) /* Treat empty input file as fatal error */
\r
133 ERREXIT(cinfo, JERR_INPUT_EMPTY);
\r
134 WARNMS(cinfo, JWRN_JPEG_EOF);
\r
135 /* Insert a fake EOI marker */
\r
136 src->buffer[0] = (JOCTET) 0xFF;
\r
137 src->buffer[1] = (JOCTET) JPEG_EOI;
\r
141 src->pub.next_input_byte = src->buffer;
\r
142 src->pub.bytes_in_buffer = nbytes;
\r
143 src->start_of_file = FALSE;
\r
150 * Skip data --- used to skip over a potentially large amount of
\r
151 * uninteresting data (such as an APPn marker).
\r
153 * Writers of suspendable-input applications must note that skip_input_data
\r
154 * is not granted the right to give a suspension return. If the skip extends
\r
155 * beyond the data currently in the buffer, the buffer can be marked empty so
\r
156 * that the next read will cause a fill_input_buffer call that can suspend.
\r
157 * Arranging for additional bytes to be discarded before reloading the input
\r
158 * buffer is the application writer's problem.
\r
161 static void my_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
\r
163 my_src_ptr src = (my_src_ptr) cinfo->src;
\r
165 /* Just a dumb implementation for now. Could use fseek() except
\r
166 * it doesn't work on pipes. Not clear that being smart is worth
\r
167 * any trouble anyway --- large skips are infrequent.
\r
169 if (num_bytes > 0) {
\r
170 while (num_bytes > (long) src->pub.bytes_in_buffer) {
\r
171 num_bytes -= (long) src->pub.bytes_in_buffer;
\r
172 (void) my_fill_input_buffer(cinfo);
\r
173 /* note we assume that fill_input_buffer will never return FALSE,
\r
174 * so suspension need not be handled.
\r
177 src->pub.next_input_byte += (size_t) num_bytes;
\r
178 src->pub.bytes_in_buffer -= (size_t) num_bytes;
\r
184 * An additional method that can be provided by data source modules is the
\r
185 * resync_to_restart method for error recovery in the presence of RST markers.
\r
186 * For the moment, this source module just uses the default resync method
\r
187 * provided by the JPEG library. That method assumes that no backtracking
\r
193 * Terminate source --- called by jpeg_finish_decompress
\r
194 * after all data has been read. Often a no-op.
\r
196 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
\r
197 * application must deal with any cleanup that should happen even
\r
201 static void my_term_source (j_decompress_ptr cinfo)
\r
203 /* no work necessary here */
\r
208 * Prepare for input from a stdio stream.
\r
209 * The caller must have already opened the stream, and is responsible
\r
210 * for closing it after finishing decompression.
\r
213 static void jpeg_buffer_src (j_decompress_ptr cinfo, void* buffer, int bufsize)
\r
217 /* The source object and input buffer are made permanent so that a series
\r
218 * of JPEG images can be read from the same file by calling jpeg_stdio_src
\r
219 * only before the first one. (If we discarded the buffer at the end of
\r
220 * one image, we'd likely lose the start of the next one.)
\r
221 * This makes it unsafe to use this manager and a different source
\r
222 * manager serially with the same JPEG object. Caveat programmer.
\r
224 if (cinfo->src == NULL) { /* first time for this JPEG object? */
\r
225 cinfo->src = (struct jpeg_source_mgr *)
\r
226 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
\r
227 sizeof (my_source_mgr));
\r
228 src = (my_src_ptr) cinfo->src;
\r
229 src->buffer = (JOCTET *)
\r
230 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
\r
231 INPUT_BUF_SIZE * sizeof (JOCTET));
\r
234 src = (my_src_ptr) cinfo->src;
\r
235 src->pub.init_source = my_init_source;
\r
236 src->pub.fill_input_buffer = my_fill_input_buffer;
\r
237 src->pub.skip_input_data = my_skip_input_data;
\r
238 src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
\r
239 src->pub.term_source = my_term_source;
\r
240 src->src_buffer = (JOCTET *)buffer;
\r
241 src->src_size = bufsize;
\r
242 src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
\r
243 src->pub.next_input_byte = NULL; /* until buffer loaded */
\r
246 // =============================================================================
\r
248 static char errormsg[JMSG_LENGTH_MAX];
\r
250 typedef struct my_jpeg_error_mgr
\r
252 struct jpeg_error_mgr pub; // "public" fields
\r
253 jmp_buf setjmp_buffer; // for return to caller
\r
254 } bt_jpeg_error_mgr;
\r
256 static void my_jpeg_error_exit (j_common_ptr cinfo)
\r
258 my_jpeg_error_mgr* myerr = (bt_jpeg_error_mgr*) cinfo->err;
\r
260 (*cinfo->err->format_message) (cinfo, errormsg);
\r
262 longjmp (myerr->setjmp_buffer, 1);
\r
265 // stash a scanline
\r
266 static void j_putRGBScanline (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
\r
268 int offset = row * widthPix * 4;
\r
271 for (count = 0; count < widthPix; count++)
\r
273 unsigned char iRed, iBlu, iGrn;
\r
274 unsigned char *oRed, *oBlu, *oGrn, *oAlp;
\r
276 iRed = *(jpegline + count * 3 + 0);
\r
277 iGrn = *(jpegline + count * 3 + 1);
\r
278 iBlu = *(jpegline + count * 3 + 2);
\r
280 oRed = outBuf + offset + count * 4 + 0;
\r
281 oGrn = outBuf + offset + count * 4 + 1;
\r
282 oBlu = outBuf + offset + count * 4 + 2;
\r
283 oAlp = outBuf + offset + count * 4 + 3;
\r
292 // stash a scanline
\r
293 static void j_putRGBAScanline (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
\r
295 int offset = row * widthPix * 4;
\r
298 for (count = 0; count < widthPix; count++)
\r
300 unsigned char iRed, iBlu, iGrn, iAlp;
\r
301 unsigned char *oRed, *oBlu, *oGrn, *oAlp;
\r
303 iRed = *(jpegline + count * 4 + 0);
\r
304 iGrn = *(jpegline + count * 4 + 1);
\r
305 iBlu = *(jpegline + count * 4 + 2);
\r
306 iAlp = *(jpegline + count * 4 + 3);
\r
308 oRed = outBuf + offset + count * 4 + 0;
\r
309 oGrn = outBuf + offset + count * 4 + 1;
\r
310 oBlu = outBuf + offset + count * 4 + 2;
\r
311 oAlp = outBuf + offset + count * 4 + 3;
\r
316 // ydnar: see bug 900
\r
317 *oAlp = 255; //% iAlp;
\r
321 // stash a gray scanline
\r
322 static void j_putGrayScanlineToRGB (unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
\r
324 int offset = row * widthPix * 4;
\r
327 for (count = 0; count < widthPix; count++)
\r
329 unsigned char iGray;
\r
330 unsigned char *oRed, *oBlu, *oGrn, *oAlp;
\r
332 // get our grayscale value
\r
333 iGray = *(jpegline + count);
\r
335 oRed = outBuf + offset + count * 4;
\r
336 oGrn = outBuf + offset + count * 4 + 1;
\r
337 oBlu = outBuf + offset + count * 4 + 2;
\r
338 oAlp = outBuf + offset + count * 4 + 3;
\r
347 static int _LoadJPGBuff (void *src_buffer, int src_size, unsigned char **pic, int *width, int *height)
\r
349 struct jpeg_decompress_struct cinfo;
\r
350 struct my_jpeg_error_mgr jerr;
\r
352 int row_stride, size;
\r
354 cinfo.err = jpeg_std_error (&jerr.pub);
\r
355 jerr.pub.error_exit = my_jpeg_error_exit;
\r
357 if (setjmp (jerr.setjmp_buffer))
\r
359 *pic = (unsigned char*)errormsg;
\r
360 jpeg_destroy_decompress (&cinfo);
\r
364 jpeg_create_decompress (&cinfo);
\r
365 jpeg_buffer_src (&cinfo, src_buffer, src_size);
\r
366 jpeg_read_header (&cinfo, TRUE);
\r
367 jpeg_start_decompress (&cinfo);
\r
369 row_stride = cinfo.output_width * cinfo.output_components;
\r
371 size = cinfo.output_width * cinfo.output_height * 4;
\r
372 *width = cinfo.output_width;
\r
373 *height = cinfo.output_height;
\r
374 *pic = (unsigned char*) (g_malloc (size+1));
\r
375 memset (*pic, 0, size+1);
\r
377 buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
\r
379 while (cinfo.output_scanline < cinfo.output_height)
\r
381 jpeg_read_scanlines (&cinfo, buffer, 1);
\r
383 if (cinfo.out_color_components == 4)
\r
384 j_putRGBAScanline (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
\r
385 else if (cinfo.out_color_components == 3)
\r
386 j_putRGBScanline (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
\r
387 else if (cinfo.out_color_components == 1)
\r
388 j_putGrayScanlineToRGB (buffer[0], cinfo.output_width, *pic, cinfo.output_scanline-1);
\r
391 jpeg_finish_decompress (&cinfo);
\r
392 jpeg_destroy_decompress (&cinfo);
\r
397 void LoadJPG (const char *filename, unsigned char **pic, int *width, int *height)
\r
399 unsigned char *fbuffer = NULL;
\r
400 int nLen = vfsLoadFile ((char *)filename, (void **)&fbuffer, 0 );
\r
404 if (_LoadJPGBuff (fbuffer, nLen, pic, width, height) != 0)
\r
406 g_FuncTable.m_pfnSysPrintf( "WARNING: JPEG library failed to load %s because %s\n", filename, *pic );
\r
410 vfsFreeFile (fbuffer);
\r