1 /* GdkPixbuf library - JPEG image loader
3 * Copyright (C) 1999 Michael Zucchi
4 * Copyright (C) 1999 The Free Software Foundation
6 * Progressive loading code Copyright (C) 1999 Red Hat, Inc.
8 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
9 * Federico Mena-Quintero <federico@gimp.org>
10 * Michael Fulbright <drmike@redhat.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
30 Progressive file loading notes (11/03/1999) <drmike@redhat.com>...
32 These are issues I know of and will be dealing with shortly:
34 - Currently does not handle progressive jpegs - this
35 requires a change in the way image_load_increment () calls
36 libjpeg. Progressive jpegs are rarer but I will add this
39 - error handling is not as good as it should be
50 #include "gdk-pixbuf.h"
51 #include "gdk-pixbuf-io.h"
55 /* we are a "source manager" as far as libjpeg is concerned */
56 #define JPEG_PROG_BUF_SIZE 4096
59 struct jpeg_source_mgr pub; /* public fields */
61 JOCTET buffer[JPEG_PROG_BUF_SIZE]; /* start of buffer */
62 long skip_next; /* number of bytes to skip next read */
66 typedef my_source_mgr * my_src_ptr;
68 /* error handler data */
69 struct error_handler_data {
70 struct jpeg_error_mgr pub;
71 sigjmp_buf setjmp_buffer;
74 /* progressive loader context */
76 ModuleUpdatedNotifyFunc updated_func;
77 ModulePreparedNotifyFunc prepared_func;
81 guchar *dptr; /* current position in pixbuf */
83 gboolean did_prescan; /* are we in image data yet? */
84 gboolean got_header; /* have we loaded jpeg header? */
85 gboolean src_initialized;/* TRUE when jpeg lib initialized */
86 struct jpeg_decompress_struct cinfo;
87 struct error_handler_data jerr;
90 GdkPixbuf *image_load (FILE *f);
91 gpointer image_begin_load (ModulePreparedNotifyFunc func,
92 ModuleUpdatedNotifyFunc func2,
93 ModuleFrameDoneNotifyFunc func3,
94 ModuleAnimationDoneNotifyFunc func4,
96 void image_stop_load (gpointer context);
97 gboolean image_load_increment(gpointer context, guchar *buf, guint size);
101 fatal_error_handler (j_common_ptr cinfo)
104 * We should somehow signal what error occurred to the caller so the
105 * caller can handle the error message */
106 struct error_handler_data *errmgr;
108 errmgr = (struct error_handler_data *) cinfo->err;
109 cinfo->err->output_message (cinfo);
110 siglongjmp (errmgr->setjmp_buffer, 1);
114 /* Destroy notification function for the libart pixbuf */
116 free_buffer (gpointer user_data, gpointer data)
122 /* explode gray image data from jpeg library into rgb components in pixbuf */
124 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
130 g_return_if_fail (cinfo != NULL);
131 g_return_if_fail (cinfo->output_components == 1);
133 /* Expand grey->colour. Expand from the end of the
134 * memory down, so we can use the same buffer.
136 w = cinfo->image_width;
137 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
140 from = lines[i] + w - 1;
141 to = lines[i] + (w - 1) * 3;
142 for (j = w - 1; j >= 0; j--) {
152 /* Shared library entry point */
157 guchar *pixels = NULL;
159 guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height,
160 * from the header file:
161 * " Usually rec_outbuf_height will be 1 or 2,
165 struct jpeg_decompress_struct cinfo;
166 struct error_handler_data jerr;
168 /* setup error handler */
169 cinfo.err = jpeg_std_error (&jerr.pub);
170 jerr.pub.error_exit = fatal_error_handler;
172 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
173 /* Whoops there was a jpeg error */
177 jpeg_destroy_decompress (&cinfo);
181 /* load header, setup */
182 jpeg_create_decompress (&cinfo);
183 jpeg_stdio_src (&cinfo, f);
184 jpeg_read_header (&cinfo, TRUE);
185 jpeg_start_decompress (&cinfo);
186 cinfo.do_fancy_upsampling = FALSE;
187 cinfo.do_block_smoothing = FALSE;
189 w = cinfo.output_width;
190 h = cinfo.output_height;
192 pixels = g_malloc (h * w * 3);
194 jpeg_destroy_decompress (&cinfo);
200 /* decompress all the lines, a few at a time */
202 while (cinfo.output_scanline < cinfo.output_height) {
204 for (i = 0; i < cinfo.rec_outbuf_height; i++) {
209 jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height);
211 if (cinfo.output_components == 1)
212 explode_gray_into_buf (&cinfo, lines);
215 jpeg_finish_decompress (&cinfo);
216 jpeg_destroy_decompress (&cinfo);
218 return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, FALSE,
224 /**** Progressive image loading handling *****/
226 /* these routines required because we are acting as a source manager for */
229 init_source (j_decompress_ptr cinfo)
231 my_src_ptr src = (my_src_ptr) cinfo->src;
238 term_source (j_decompress_ptr cinfo)
240 /* XXXX - probably should scream something has happened */
244 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
245 /* we do nothing except return "FALSE" */
247 fill_input_buffer (j_decompress_ptr cinfo)
254 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
256 my_src_ptr src = (my_src_ptr) cinfo->src;
259 /* move as far as we can into current buffer */
260 /* then set skip_next to catch the rest */
262 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
263 src->pub.next_input_byte += (size_t) num_can_do;
264 src->pub.bytes_in_buffer -= (size_t) num_can_do;
266 src->skip_next = num_bytes - num_can_do;
272 * func - called when we have pixmap created (but no image data)
273 * user_data - passed as arg 1 to func
274 * return context (opaque to user)
278 image_begin_load (ModulePreparedNotifyFunc prepared_func,
279 ModuleUpdatedNotifyFunc updated_func,
280 ModuleFrameDoneNotifyFunc frame_func,
281 ModuleAnimationDoneNotifyFunc anim_done_func,
284 JpegProgContext *context;
287 context = g_new0 (JpegProgContext, 1);
288 context->prepared_func = prepared_func;
289 context->updated_func = updated_func;
290 context->user_data = user_data;
291 context->pixbuf = NULL;
292 context->got_header = FALSE;
293 context->did_prescan = FALSE;
294 context->src_initialized = FALSE;
296 /* create libjpeg structures */
297 jpeg_create_decompress (&context->cinfo);
299 context->cinfo.src = (struct jpeg_source_mgr *) g_new0 (my_source_mgr, 1);
300 src = (my_src_ptr) context->cinfo.src;
302 context->cinfo.err = jpeg_std_error (&context->jerr.pub);
304 src = (my_src_ptr) context->cinfo.src;
305 src->pub.init_source = init_source;
306 src->pub.fill_input_buffer = fill_input_buffer;
307 src->pub.skip_input_data = skip_input_data;
308 src->pub.resync_to_restart = jpeg_resync_to_restart;
309 src->pub.term_source = term_source;
310 src->pub.bytes_in_buffer = 0;
311 src->pub.next_input_byte = NULL;
313 return (gpointer) context;
317 * context - returned from image_begin_load
319 * free context, unref gdk_pixbuf
322 image_stop_load (gpointer data)
324 JpegProgContext *context = (JpegProgContext *) data;
326 g_return_if_fail (context != NULL);
329 gdk_pixbuf_unref (context->pixbuf);
331 jpeg_finish_decompress(&context->cinfo);
332 jpeg_destroy_decompress(&context->cinfo);
334 if (context->cinfo.src) {
335 my_src_ptr src = (my_src_ptr) context->cinfo.src;
347 * context - from image_begin_load
348 * buf - new image data
349 * size - length of new image data
351 * append image data onto inrecrementally built output image
354 image_load_increment (gpointer data, guchar *buf, guint size)
356 JpegProgContext *context = (JpegProgContext *)data;
357 struct jpeg_decompress_struct *cinfo;
359 guint num_left, num_copy;
360 guint last_bytes_left;
365 g_return_val_if_fail (context != NULL, FALSE);
366 g_return_val_if_fail (buf != NULL, FALSE);
368 src = (my_src_ptr) context->cinfo.src;
370 cinfo = &context->cinfo;
372 /* XXXXXXX (drmike) - loop(s) below need to be recoded now I
373 * have a grasp of what the flow needs to be!
377 /* skip over data if requested, handle unsigned int sizes cleanly */
378 /* only can happen if we've already called jpeg_get_header once */
379 if (context->src_initialized && src->skip_next) {
380 if (src->skip_next > size) {
381 src->skip_next -= size;
384 num_left = size - src->skip_next;
385 bufhd = buf + src->skip_next;
401 /* handle any data from caller we haven't processed yet */
403 if(src->pub.bytes_in_buffer &&
404 src->pub.next_input_byte != src->buffer)
405 memmove(src->buffer, src->pub.next_input_byte,
406 src->pub.bytes_in_buffer);
409 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
412 /* if (num_copy == 0)
413 g_error ("Buffer overflow!");
415 memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy);
416 src->pub.next_input_byte = src->buffer;
417 src->pub.bytes_in_buffer += num_copy;
419 num_left -= num_copy;
421 /* did anything change from last pass, if not return */
423 last_bytes_left = src->pub.bytes_in_buffer;
425 } else if (src->pub.bytes_in_buffer == last_bytes_left)
428 last_bytes_left = src->pub.bytes_in_buffer;
431 /* should not go through twice and not pull bytes out of buf */
435 /* try to load jpeg header */
436 if (!context->got_header) {
439 rc = jpeg_read_header (cinfo, TRUE);
440 context->src_initialized = TRUE;
442 if (rc == JPEG_SUSPENDED)
445 context->got_header = TRUE;
448 if (jpeg_has_multiple_scans (cinfo)) {
449 g_print ("io-jpeg.c: Does not currently "
450 "support progressive jpeg files.\n");
454 context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB,
455 /*have_alpha*/ FALSE,
458 cinfo->image_height);
460 if (context->pixbuf == NULL) {
461 /* Failed to allocate memory */
462 g_error ("Couldn't allocate gdkpixbuf");
465 /* Use pixbuf buffer to store decompressed data */
466 context->dptr = context->pixbuf->art_pixbuf->pixels;
468 /* Notify the client that we are ready to go */
469 (* context->prepared_func) (context->pixbuf,
472 } else if (!context->did_prescan) {
475 /* start decompression */
476 rc = jpeg_start_decompress (cinfo);
477 cinfo->do_fancy_upsampling = FALSE;
478 cinfo->do_block_smoothing = FALSE;
480 if (rc == JPEG_SUSPENDED)
483 context->did_prescan = TRUE;
486 /* we're decompressing so feed jpeg lib scanlines */
493 /* keep going until we've done all scanlines */
494 while (cinfo->output_scanline < cinfo->output_height) {
495 start_scanline = cinfo->output_scanline;
497 rowptr = context->dptr;
498 for (i=0; i < cinfo->rec_outbuf_height; i++) {
500 rowptr += context->pixbuf->art_pixbuf->rowstride;
503 nlines = jpeg_read_scanlines (cinfo, lines,
504 cinfo->rec_outbuf_height);
509 if (cinfo->output_components == 1)
510 explode_gray_into_buf (cinfo, lines);
512 context->dptr += nlines * context->pixbuf->art_pixbuf->rowstride;
514 /* send updated signal */
515 (* context->updated_func) (context->pixbuf,
517 cinfo->output_scanline-1,
522 #undef DEBUG_JPEG_PROGRESSIVE
523 #ifdef DEBUG_JPEG_PROGRESSIVE
525 if (start_scanline != cinfo->output_scanline)
526 g_print("jpeg: Input pass=%2d, next input scanline=%3d,"
527 " emitted %3d - %3d\n",
528 cinfo->input_scan_number, cinfo->input_iMCU_row * 16,
529 start_scanline, cinfo->output_scanline - 1);
533 g_print ("Scanline %d of %d - ",
534 cinfo->output_scanline,
535 cinfo->output_height);
536 /* g_print ("rec_height %d -", cinfo->rec_outbuf_height); */
537 g_print ("Processed %d lines - bytes left = %d\n",
538 nlines, cinfo->src->bytes_in_buffer);
541 /* did entire image */
542 if (cinfo->output_scanline >= cinfo->output_height)