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;
79 /* progressive loader context */
81 ModuleUpdatedNotifyFunc updated_func;
82 ModulePreparedNotifyFunc prepared_func;
86 guchar *dptr; /* current position in pixbuf */
88 gboolean did_prescan; /* are we in image data yet? */
89 gboolean got_header; /* have we loaded jpeg header? */
90 gboolean src_initialized;/* TRUE when jpeg lib initialized */
91 struct jpeg_decompress_struct cinfo;
92 struct error_handler_data jerr;
95 GdkPixbuf *gdk_pixbuf__jpeg_image_load (FILE *f);
96 gpointer gdk_pixbuf__jpeg_image_begin_load (ModulePreparedNotifyFunc func,
97 ModuleUpdatedNotifyFunc func2,
98 ModuleFrameDoneNotifyFunc func3,
99 ModuleAnimationDoneNotifyFunc func4,
101 void gdk_pixbuf__jpeg_image_stop_load (gpointer context);
102 gboolean gdk_pixbuf__jpeg_image_load_increment(gpointer context, guchar *buf, guint size);
106 fatal_error_handler (j_common_ptr cinfo)
109 * We should somehow signal what error occurred to the caller so the
110 * caller can handle the error message */
111 struct error_handler_data *errmgr;
113 errmgr = (struct error_handler_data *) cinfo->err;
114 cinfo->err->output_message (cinfo);
115 siglongjmp (errmgr->setjmp_buffer, 1);
117 /* incase the jmp buf isn't initted? */
121 /* Destroy notification function for the pixbuf */
123 free_buffer (guchar *pixels, gpointer data)
129 /* explode gray image data from jpeg library into rgb components in pixbuf */
131 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
137 g_return_if_fail (cinfo != NULL);
138 g_return_if_fail (cinfo->output_components == 1);
140 /* Expand grey->colour. Expand from the end of the
141 * memory down, so we can use the same buffer.
143 w = cinfo->image_width;
144 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
147 from = lines[i] + w - 1;
148 to = lines[i] + (w - 1) * 3;
149 for (j = w - 1; j >= 0; j--) {
159 /* Shared library entry point */
161 gdk_pixbuf__jpeg_image_load (FILE *f)
164 guchar *pixels = NULL;
166 guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height,
167 * from the header file:
168 * " Usually rec_outbuf_height will be 1 or 2,
172 struct jpeg_decompress_struct cinfo;
173 struct error_handler_data jerr;
175 /* setup error handler */
176 cinfo.err = jpeg_std_error (&jerr.pub);
177 jerr.pub.error_exit = fatal_error_handler;
179 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
180 /* Whoops there was a jpeg error */
184 jpeg_destroy_decompress (&cinfo);
188 /* load header, setup */
189 jpeg_create_decompress (&cinfo);
190 jpeg_stdio_src (&cinfo, f);
191 jpeg_read_header (&cinfo, TRUE);
192 jpeg_start_decompress (&cinfo);
193 cinfo.do_fancy_upsampling = FALSE;
194 cinfo.do_block_smoothing = FALSE;
196 w = cinfo.output_width;
197 h = cinfo.output_height;
199 pixels = g_malloc (h * w * 3);
201 jpeg_destroy_decompress (&cinfo);
207 /* decompress all the lines, a few at a time */
209 while (cinfo.output_scanline < cinfo.output_height) {
211 for (i = 0; i < cinfo.rec_outbuf_height; i++) {
216 jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height);
218 if (cinfo.output_components == 1)
219 explode_gray_into_buf (&cinfo, lines);
222 jpeg_finish_decompress (&cinfo);
223 jpeg_destroy_decompress (&cinfo);
225 return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE, 8,
231 /**** Progressive image loading handling *****/
233 /* these routines required because we are acting as a source manager for */
236 init_source (j_decompress_ptr cinfo)
238 my_src_ptr src = (my_src_ptr) cinfo->src;
245 term_source (j_decompress_ptr cinfo)
247 /* XXXX - probably should scream something has happened */
251 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
252 /* we do nothing except return "FALSE" */
254 fill_input_buffer (j_decompress_ptr cinfo)
261 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
263 my_src_ptr src = (my_src_ptr) cinfo->src;
266 /* move as far as we can into current buffer */
267 /* then set skip_next to catch the rest */
269 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
270 src->pub.next_input_byte += (size_t) num_can_do;
271 src->pub.bytes_in_buffer -= (size_t) num_can_do;
273 src->skip_next = num_bytes - num_can_do;
279 * func - called when we have pixmap created (but no image data)
280 * user_data - passed as arg 1 to func
281 * return context (opaque to user)
285 gdk_pixbuf__jpeg_image_begin_load (ModulePreparedNotifyFunc prepared_func,
286 ModuleUpdatedNotifyFunc updated_func,
287 ModuleFrameDoneNotifyFunc frame_func,
288 ModuleAnimationDoneNotifyFunc anim_done_func,
291 JpegProgContext *context;
294 context = g_new0 (JpegProgContext, 1);
295 context->prepared_func = prepared_func;
296 context->updated_func = updated_func;
297 context->user_data = user_data;
298 context->pixbuf = NULL;
299 context->got_header = FALSE;
300 context->did_prescan = FALSE;
301 context->src_initialized = FALSE;
303 /* create libjpeg structures */
304 jpeg_create_decompress (&context->cinfo);
306 context->cinfo.src = (struct jpeg_source_mgr *) g_new0 (my_source_mgr, 1);
307 src = (my_src_ptr) context->cinfo.src;
309 context->cinfo.err = jpeg_std_error (&context->jerr.pub);
310 context->jerr.pub.error_exit = fatal_error_handler;
312 src = (my_src_ptr) context->cinfo.src;
313 src->pub.init_source = init_source;
314 src->pub.fill_input_buffer = fill_input_buffer;
315 src->pub.skip_input_data = skip_input_data;
316 src->pub.resync_to_restart = jpeg_resync_to_restart;
317 src->pub.term_source = term_source;
318 src->pub.bytes_in_buffer = 0;
319 src->pub.next_input_byte = NULL;
321 return (gpointer) context;
325 * context - returned from image_begin_load
327 * free context, unref gdk_pixbuf
330 gdk_pixbuf__jpeg_image_stop_load (gpointer data)
332 JpegProgContext *context = (JpegProgContext *) data;
334 g_return_if_fail (context != NULL);
337 gdk_pixbuf_unref (context->pixbuf);
339 /* if we have an error? */
340 if (sigsetjmp (context->jerr.setjmp_buffer, 1)) {
341 jpeg_destroy_decompress (&context->cinfo);
343 jpeg_finish_decompress(&context->cinfo);
344 jpeg_destroy_decompress(&context->cinfo);
347 if (context->cinfo.src) {
348 my_src_ptr src = (my_src_ptr) context->cinfo.src;
360 * context - from image_begin_load
361 * buf - new image data
362 * size - length of new image data
364 * append image data onto inrecrementally built output image
367 gdk_pixbuf__jpeg_image_load_increment (gpointer data, guchar *buf, guint size)
369 JpegProgContext *context = (JpegProgContext *)data;
370 struct jpeg_decompress_struct *cinfo;
372 guint num_left, num_copy;
373 guint last_bytes_left;
378 g_return_val_if_fail (context != NULL, FALSE);
379 g_return_val_if_fail (buf != NULL, FALSE);
381 src = (my_src_ptr) context->cinfo.src;
383 cinfo = &context->cinfo;
385 /* XXXXXXX (drmike) - loop(s) below need to be recoded now I
386 * have a grasp of what the flow needs to be!
389 /* check for fatal error */
390 if (sigsetjmp (context->jerr.setjmp_buffer, 1)) {
394 /* skip over data if requested, handle unsigned int sizes cleanly */
395 /* only can happen if we've already called jpeg_get_header once */
396 if (context->src_initialized && src->skip_next) {
397 if (src->skip_next > size) {
398 src->skip_next -= size;
401 num_left = size - src->skip_next;
402 bufhd = buf + src->skip_next;
418 /* handle any data from caller we haven't processed yet */
420 if(src->pub.bytes_in_buffer &&
421 src->pub.next_input_byte != src->buffer)
422 memmove(src->buffer, src->pub.next_input_byte,
423 src->pub.bytes_in_buffer);
426 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
429 /* if (num_copy == 0)
430 g_error ("Buffer overflow!");
432 memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy);
433 src->pub.next_input_byte = src->buffer;
434 src->pub.bytes_in_buffer += num_copy;
436 num_left -= num_copy;
438 /* did anything change from last pass, if not return */
440 last_bytes_left = src->pub.bytes_in_buffer;
442 } else if (src->pub.bytes_in_buffer == last_bytes_left)
445 last_bytes_left = src->pub.bytes_in_buffer;
448 /* should not go through twice and not pull bytes out of buf */
452 /* try to load jpeg header */
453 if (!context->got_header) {
456 rc = jpeg_read_header (cinfo, TRUE);
457 context->src_initialized = TRUE;
459 if (rc == JPEG_SUSPENDED)
462 context->got_header = TRUE;
465 if (jpeg_has_multiple_scans (cinfo)) {
466 g_print ("io-jpeg.c: Does not currently "
467 "support progressive jpeg files.\n");
471 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
475 cinfo->image_height);
477 if (context->pixbuf == NULL) {
478 /* Failed to allocate memory */
479 g_error ("Couldn't allocate gdkpixbuf");
482 /* Use pixbuf buffer to store decompressed data */
483 context->dptr = context->pixbuf->pixels;
485 /* Notify the client that we are ready to go */
486 (* context->prepared_func) (context->pixbuf,
489 } else if (!context->did_prescan) {
492 /* start decompression */
493 rc = jpeg_start_decompress (cinfo);
494 cinfo->do_fancy_upsampling = FALSE;
495 cinfo->do_block_smoothing = FALSE;
497 if (rc == JPEG_SUSPENDED)
500 context->did_prescan = TRUE;
503 /* we're decompressing so feed jpeg lib scanlines */
510 /* keep going until we've done all scanlines */
511 while (cinfo->output_scanline < cinfo->output_height) {
512 start_scanline = cinfo->output_scanline;
514 rowptr = context->dptr;
515 for (i=0; i < cinfo->rec_outbuf_height; i++) {
517 rowptr += context->pixbuf->rowstride;
520 nlines = jpeg_read_scanlines (cinfo, lines,
521 cinfo->rec_outbuf_height);
526 if (cinfo->output_components == 1)
527 explode_gray_into_buf (cinfo, lines);
529 context->dptr += nlines * context->pixbuf->rowstride;
531 /* send updated signal */
532 (* context->updated_func) (context->pixbuf,
534 cinfo->output_scanline-1,
539 #undef DEBUG_JPEG_PROGRESSIVE
540 #ifdef DEBUG_JPEG_PROGRESSIVE
542 if (start_scanline != cinfo->output_scanline)
543 g_print("jpeg: Input pass=%2d, next input scanline=%3d,"
544 " emitted %3d - %3d\n",
545 cinfo->input_scan_number, cinfo->input_iMCU_row * 16,
546 start_scanline, cinfo->output_scanline - 1);
550 g_print ("Scanline %d of %d - ",
551 cinfo->output_scanline,
552 cinfo->output_height);
553 /* g_print ("rec_height %d -", cinfo->rec_outbuf_height); */
554 g_print ("Processed %d lines - bytes left = %d\n",
555 nlines, cinfo->src->bytes_in_buffer);
558 /* did entire image */
559 if (cinfo->output_scanline >= cinfo->output_height)
570 gdk_pixbuf__jpeg_image_save (FILE *f,
576 /* FIXME error handling is broken */
578 struct jpeg_compress_struct cinfo;
581 guchar *pixels = NULL;
584 int quality = 75; /* default; must be between 0 and 100 */
588 struct error_handler_data jerr;
591 gchar **kiter = keys;
592 gchar **viter = values;
595 if (strcmp (*kiter, "quality") == 0) {
597 quality = strtol (*viter, &endptr, 10);
599 if (endptr == *viter) {
602 GDK_PIXBUF_ERROR_BAD_OPTION_VALUE,
603 _("JPEG quality must be a value between 0 and 100; value '%s' could not be parsed."),
611 /* This is a user-visible error;
612 * lets people skip the range-checking
617 GDK_PIXBUF_ERROR_BAD_OPTION_VALUE,
618 _("JPEG quality must be a value between 0 and 100; value '%d' is not allowed."),
624 g_warning ("Bad option name '%s' passed to JPEG saver",
634 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
636 w = gdk_pixbuf_get_width (pixbuf);
637 h = gdk_pixbuf_get_height (pixbuf);
639 /* no image data? abort */
640 pixels = gdk_pixbuf_get_pixels (pixbuf);
641 g_return_val_if_fail (pixels != NULL, FALSE);
643 /* allocate a small buffer to convert image data */
644 buf = malloc (w * 3 * sizeof (guchar));
645 g_return_val_if_fail (buf != NULL, FALSE);
647 /* set up error handling */
648 jerr.pub.error_exit = fatal_error_handler;
650 cinfo.err = jpeg_std_error (&(jerr.pub));
651 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
652 jpeg_destroy_compress (&cinfo);
657 /* setup compress params */
658 jpeg_create_compress (&cinfo);
659 jpeg_stdio_dest (&cinfo, f);
660 cinfo.image_width = w;
661 cinfo.image_height = h;
662 cinfo.input_components = 3;
663 cinfo.in_color_space = JCS_RGB;
665 /* set up jepg compression parameters */
666 jpeg_set_defaults (&cinfo);
667 jpeg_set_quality (&cinfo, quality, TRUE);
668 jpeg_start_compress (&cinfo, TRUE);
669 /* get the start pointer */
671 /* go one scanline at a time... and save */
673 while (cinfo.next_scanline < cinfo.image_height) {
674 /* convert scanline from ARGB to RGB packed */
675 for (j = 0; j < w; j++)
676 memcpy (&(buf[j*3]), &(ptr[i*rowstride + j*3]), 3);
679 jbuf = (JSAMPROW *)(&buf);
680 jpeg_write_scanlines (&cinfo, jbuf, 1);
687 jpeg_finish_compress (&cinfo);