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 Lesser 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 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser 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-private.h"
51 #include "gdk-pixbuf-io.h"
53 #ifndef HAVE_SIGSETJMP
54 #define sigjmp_buf jmp_buf
55 #define sigsetjmp(jb, x) setjmp(jb)
56 #define siglongjmp longjmp
60 /* we are a "source manager" as far as libjpeg is concerned */
61 #define JPEG_PROG_BUF_SIZE 4096
64 struct jpeg_source_mgr pub; /* public fields */
66 JOCTET buffer[JPEG_PROG_BUF_SIZE]; /* start of buffer */
67 long skip_next; /* number of bytes to skip next read */
71 typedef my_source_mgr * my_src_ptr;
73 /* error handler data */
74 struct error_handler_data {
75 struct jpeg_error_mgr pub;
76 sigjmp_buf setjmp_buffer;
80 /* progressive loader context */
82 ModuleUpdatedNotifyFunc updated_func;
83 ModulePreparedNotifyFunc prepared_func;
87 guchar *dptr; /* current position in pixbuf */
89 gboolean did_prescan; /* are we in image data yet? */
90 gboolean got_header; /* have we loaded jpeg header? */
91 gboolean src_initialized;/* TRUE when jpeg lib initialized */
92 struct jpeg_decompress_struct cinfo;
93 struct error_handler_data jerr;
96 static GdkPixbuf *gdk_pixbuf__jpeg_image_load (FILE *f, GError **error);
97 static gpointer gdk_pixbuf__jpeg_image_begin_load (ModulePreparedNotifyFunc func,
98 ModuleUpdatedNotifyFunc func2,
101 static gboolean gdk_pixbuf__jpeg_image_stop_load (gpointer context, GError **error);
102 static gboolean gdk_pixbuf__jpeg_image_load_increment(gpointer context,
103 const guchar *buf, guint size,
108 fatal_error_handler (j_common_ptr cinfo)
110 struct error_handler_data *errmgr;
111 char buffer[JMSG_LENGTH_MAX];
113 errmgr = (struct error_handler_data *) cinfo->err;
115 /* Create the message */
116 (* cinfo->err->format_message) (cinfo, buffer);
118 /* broken check for *error == NULL for robustness against
119 * crappy JPEG library
121 if (errmgr->error && *errmgr->error == NULL) {
122 g_set_error (errmgr->error,
124 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
125 _("Error interpreting JPEG image file (%s)"),
129 siglongjmp (errmgr->setjmp_buffer, 1);
131 g_assert_not_reached ();
135 output_message_handler (j_common_ptr cinfo)
137 /* This method keeps libjpeg from dumping crap to stderr */
142 /* Destroy notification function for the pixbuf */
144 free_buffer (guchar *pixels, gpointer data)
150 /* explode gray image data from jpeg library into rgb components in pixbuf */
152 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
158 g_return_if_fail (cinfo != NULL);
159 g_return_if_fail (cinfo->output_components == 1);
161 /* Expand grey->colour. Expand from the end of the
162 * memory down, so we can use the same buffer.
164 w = cinfo->image_width;
165 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
168 from = lines[i] + w - 1;
169 to = lines[i] + (w - 1) * 3;
170 for (j = w - 1; j >= 0; j--) {
180 /* Shared library entry point */
182 gdk_pixbuf__jpeg_image_load (FILE *f, GError **error)
185 guchar *pixels = NULL;
187 guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height,
188 * from the header file:
189 * " Usually rec_outbuf_height will be 1 or 2,
193 struct jpeg_decompress_struct cinfo;
194 struct error_handler_data jerr;
196 /* setup error handler */
197 cinfo.err = jpeg_std_error (&jerr.pub);
198 jerr.pub.error_exit = fatal_error_handler;
199 jerr.pub.output_message = output_message_handler;
203 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
204 /* Whoops there was a jpeg error */
208 jpeg_destroy_decompress (&cinfo);
212 /* load header, setup */
213 jpeg_create_decompress (&cinfo);
214 jpeg_stdio_src (&cinfo, f);
215 jpeg_read_header (&cinfo, TRUE);
216 jpeg_start_decompress (&cinfo);
217 cinfo.do_fancy_upsampling = FALSE;
218 cinfo.do_block_smoothing = FALSE;
220 w = cinfo.output_width;
221 h = cinfo.output_height;
223 pixels = g_try_malloc (h * w * 3);
225 jpeg_destroy_decompress (&cinfo);
227 /* broken check for *error == NULL for robustness against
228 * crappy JPEG library
230 if (error && *error == NULL) {
233 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
234 _("Insufficient memory to load image, try exiting some applications to free memory"));
242 /* decompress all the lines, a few at a time */
244 while (cinfo.output_scanline < cinfo.output_height) {
246 for (i = 0; i < cinfo.rec_outbuf_height; i++) {
251 jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height);
253 if (cinfo.output_components == 1)
254 explode_gray_into_buf (&cinfo, lines);
257 jpeg_finish_decompress (&cinfo);
258 jpeg_destroy_decompress (&cinfo);
260 return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE, 8,
266 /**** Progressive image loading handling *****/
268 /* these routines required because we are acting as a source manager for */
271 init_source (j_decompress_ptr cinfo)
273 my_src_ptr src = (my_src_ptr) cinfo->src;
280 term_source (j_decompress_ptr cinfo)
282 /* XXXX - probably should scream something has happened */
286 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
287 /* we do nothing except return "FALSE" */
289 fill_input_buffer (j_decompress_ptr cinfo)
296 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
298 my_src_ptr src = (my_src_ptr) cinfo->src;
301 /* move as far as we can into current buffer */
302 /* then set skip_next to catch the rest */
304 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
305 src->pub.next_input_byte += (size_t) num_can_do;
306 src->pub.bytes_in_buffer -= (size_t) num_can_do;
308 src->skip_next = num_bytes - num_can_do;
314 * func - called when we have pixmap created (but no image data)
315 * user_data - passed as arg 1 to func
316 * return context (opaque to user)
320 gdk_pixbuf__jpeg_image_begin_load (ModulePreparedNotifyFunc prepared_func,
321 ModuleUpdatedNotifyFunc updated_func,
325 JpegProgContext *context;
328 context = g_new0 (JpegProgContext, 1);
329 context->prepared_func = prepared_func;
330 context->updated_func = updated_func;
331 context->user_data = user_data;
332 context->pixbuf = NULL;
333 context->got_header = FALSE;
334 context->did_prescan = FALSE;
335 context->src_initialized = FALSE;
337 /* create libjpeg structures */
338 jpeg_create_decompress (&context->cinfo);
340 context->cinfo.src = (struct jpeg_source_mgr *) g_new0 (my_source_mgr, 1);
341 src = (my_src_ptr) context->cinfo.src;
343 context->cinfo.err = jpeg_std_error (&context->jerr.pub);
344 context->jerr.pub.error_exit = fatal_error_handler;
345 context->jerr.pub.output_message = output_message_handler;
346 context->jerr.error = error;
348 src = (my_src_ptr) context->cinfo.src;
349 src->pub.init_source = init_source;
350 src->pub.fill_input_buffer = fill_input_buffer;
351 src->pub.skip_input_data = skip_input_data;
352 src->pub.resync_to_restart = jpeg_resync_to_restart;
353 src->pub.term_source = term_source;
354 src->pub.bytes_in_buffer = 0;
355 src->pub.next_input_byte = NULL;
357 context->jerr.error = NULL;
359 return (gpointer) context;
363 * context - returned from image_begin_load
365 * free context, unref gdk_pixbuf
368 gdk_pixbuf__jpeg_image_stop_load (gpointer data, GError **error)
370 JpegProgContext *context = (JpegProgContext *) data;
372 g_return_val_if_fail (context != NULL, TRUE);
374 /* FIXME this thing needs to report errors if
375 * we have unused image data
379 gdk_pixbuf_unref (context->pixbuf);
381 /* if we have an error? */
382 if (sigsetjmp (context->jerr.setjmp_buffer, 1)) {
383 jpeg_destroy_decompress (&context->cinfo);
385 jpeg_finish_decompress(&context->cinfo);
386 jpeg_destroy_decompress(&context->cinfo);
389 if (context->cinfo.src) {
390 my_src_ptr src = (my_src_ptr) context->cinfo.src;
404 * context - from image_begin_load
405 * buf - new image data
406 * size - length of new image data
408 * append image data onto inrecrementally built output image
411 gdk_pixbuf__jpeg_image_load_increment (gpointer data,
412 const guchar *buf, guint size,
415 JpegProgContext *context = (JpegProgContext *)data;
416 struct jpeg_decompress_struct *cinfo;
418 guint num_left, num_copy;
419 guint last_bytes_left;
424 g_return_val_if_fail (context != NULL, FALSE);
425 g_return_val_if_fail (buf != NULL, FALSE);
427 src = (my_src_ptr) context->cinfo.src;
429 cinfo = &context->cinfo;
431 context->jerr.error = error;
433 /* XXXXXXX (drmike) - loop(s) below need to be recoded now I
434 * have a grasp of what the flow needs to be!
437 /* check for fatal error */
438 if (sigsetjmp (context->jerr.setjmp_buffer, 1)) {
442 /* skip over data if requested, handle unsigned int sizes cleanly */
443 /* only can happen if we've already called jpeg_get_header once */
444 if (context->src_initialized && src->skip_next) {
445 if (src->skip_next > size) {
446 src->skip_next -= size;
449 num_left = size - src->skip_next;
450 bufhd = buf + src->skip_next;
466 /* handle any data from caller we haven't processed yet */
468 if(src->pub.bytes_in_buffer &&
469 src->pub.next_input_byte != src->buffer)
470 memmove(src->buffer, src->pub.next_input_byte,
471 src->pub.bytes_in_buffer);
474 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
477 /* if (num_copy == 0)
478 g_error ("Buffer overflow!");
480 memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy);
481 src->pub.next_input_byte = src->buffer;
482 src->pub.bytes_in_buffer += num_copy;
484 num_left -= num_copy;
486 /* did anything change from last pass, if not return */
488 last_bytes_left = src->pub.bytes_in_buffer;
490 } else if (src->pub.bytes_in_buffer == last_bytes_left)
493 last_bytes_left = src->pub.bytes_in_buffer;
496 /* should not go through twice and not pull bytes out of buf */
500 /* try to load jpeg header */
501 if (!context->got_header) {
504 rc = jpeg_read_header (cinfo, TRUE);
505 context->src_initialized = TRUE;
507 if (rc == JPEG_SUSPENDED)
510 context->got_header = TRUE;
513 if (jpeg_has_multiple_scans (cinfo)) {
514 g_print ("io-jpeg.c: Does not currently "
515 "support progressive jpeg files.\n");
519 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
523 cinfo->image_height);
525 if (context->pixbuf == NULL) {
528 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
529 _("Couldn't allocate memory for loading JPEG file"));
533 /* Use pixbuf buffer to store decompressed data */
534 context->dptr = context->pixbuf->pixels;
536 /* Notify the client that we are ready to go */
537 (* context->prepared_func) (context->pixbuf,
541 } else if (!context->did_prescan) {
544 /* start decompression */
545 rc = jpeg_start_decompress (cinfo);
546 cinfo->do_fancy_upsampling = FALSE;
547 cinfo->do_block_smoothing = FALSE;
549 if (rc == JPEG_SUSPENDED)
552 context->did_prescan = TRUE;
555 /* we're decompressing so feed jpeg lib scanlines */
562 /* keep going until we've done all scanlines */
563 while (cinfo->output_scanline < cinfo->output_height) {
564 start_scanline = cinfo->output_scanline;
566 rowptr = context->dptr;
567 for (i=0; i < cinfo->rec_outbuf_height; i++) {
569 rowptr += context->pixbuf->rowstride;
572 nlines = jpeg_read_scanlines (cinfo, lines,
573 cinfo->rec_outbuf_height);
578 if (cinfo->output_components == 1)
579 explode_gray_into_buf (cinfo, lines);
581 context->dptr += nlines * context->pixbuf->rowstride;
583 /* send updated signal */
584 (* context->updated_func) (context->pixbuf,
586 cinfo->output_scanline-1,
591 #undef DEBUG_JPEG_PROGRESSIVE
592 #ifdef DEBUG_JPEG_PROGRESSIVE
594 if (start_scanline != cinfo->output_scanline)
595 g_print("jpeg: Input pass=%2d, next input scanline=%3d,"
596 " emitted %3d - %3d\n",
597 cinfo->input_scan_number, cinfo->input_iMCU_row * 16,
598 start_scanline, cinfo->output_scanline - 1);
602 g_print ("Scanline %d of %d - ",
603 cinfo->output_scanline,
604 cinfo->output_height);
605 /* g_print ("rec_height %d -", cinfo->rec_outbuf_height); */
606 g_print ("Processed %d lines - bytes left = %d\n",
607 nlines, cinfo->src->bytes_in_buffer);
610 /* did entire image */
611 if (cinfo->output_scanline >= cinfo->output_height)
622 gdk_pixbuf__jpeg_image_save (FILE *f,
628 /* FIXME error handling is broken */
630 struct jpeg_compress_struct cinfo;
633 guchar *pixels = NULL;
636 int quality = 75; /* default; must be between 0 and 100 */
640 struct error_handler_data jerr;
643 gchar **kiter = keys;
644 gchar **viter = values;
647 if (strcmp (*kiter, "quality") == 0) {
649 quality = strtol (*viter, &endptr, 10);
651 if (endptr == *viter) {
654 GDK_PIXBUF_ERROR_BAD_OPTION_VALUE,
655 _("JPEG quality must be a value between 0 and 100; value '%s' could not be parsed."),
663 /* This is a user-visible error;
664 * lets people skip the range-checking
669 GDK_PIXBUF_ERROR_BAD_OPTION_VALUE,
670 _("JPEG quality must be a value between 0 and 100; value '%d' is not allowed."),
676 g_warning ("Bad option name '%s' passed to JPEG saver",
686 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
688 w = gdk_pixbuf_get_width (pixbuf);
689 h = gdk_pixbuf_get_height (pixbuf);
691 /* no image data? abort */
692 pixels = gdk_pixbuf_get_pixels (pixbuf);
693 g_return_val_if_fail (pixels != NULL, FALSE);
695 /* allocate a small buffer to convert image data */
696 buf = g_try_malloc (w * 3 * sizeof (guchar));
700 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
701 _("Couldn't allocate memory for loading JPEG file"));
705 /* set up error handling */
706 jerr.pub.error_exit = fatal_error_handler;
707 jerr.pub.output_message = output_message_handler;
710 cinfo.err = jpeg_std_error (&(jerr.pub));
711 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
712 jpeg_destroy_compress (&cinfo);
717 /* setup compress params */
718 jpeg_create_compress (&cinfo);
719 jpeg_stdio_dest (&cinfo, f);
720 cinfo.image_width = w;
721 cinfo.image_height = h;
722 cinfo.input_components = 3;
723 cinfo.in_color_space = JCS_RGB;
725 /* set up jepg compression parameters */
726 jpeg_set_defaults (&cinfo);
727 jpeg_set_quality (&cinfo, quality, TRUE);
728 jpeg_start_compress (&cinfo, TRUE);
729 /* get the start pointer */
731 /* go one scanline at a time... and save */
733 while (cinfo.next_scanline < cinfo.image_height) {
734 /* convert scanline from ARGB to RGB packed */
735 for (j = 0; j < w; j++)
736 memcpy (&(buf[j*3]), &(ptr[i*rowstride + j*3]), 3);
739 jbuf = (JSAMPROW *)(&buf);
740 jpeg_write_scanlines (&cinfo, jbuf, 1);
747 jpeg_finish_compress (&cinfo);
753 gdk_pixbuf__jpeg_fill_vtable (GdkPixbufModule *module)
755 module->load = gdk_pixbuf__jpeg_image_load;
756 module->begin_load = gdk_pixbuf__jpeg_image_begin_load;
757 module->stop_load = gdk_pixbuf__jpeg_image_stop_load;
758 module->load_increment = gdk_pixbuf__jpeg_image_load_increment;
759 module->save = gdk_pixbuf__jpeg_image_save;