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, gpointer user_data);
93 void image_stop_load (gpointer context);
94 gboolean image_load_increment(gpointer context, guchar *buf, guint size);
98 fatal_error_handler (j_common_ptr cinfo)
101 * We should somehow signal what error occurred to the caller so the
102 * caller can handle the error message */
103 struct error_handler_data *errmgr;
105 errmgr = (struct error_handler_data *) cinfo->err;
106 cinfo->err->output_message (cinfo);
107 siglongjmp (errmgr->setjmp_buffer, 1);
111 /* Destroy notification function for the libart pixbuf */
113 free_buffer (gpointer user_data, gpointer data)
119 /* explode gray image data from jpeg library into rgb components in pixbuf */
121 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
127 g_return_if_fail (cinfo != NULL);
128 g_return_if_fail (cinfo->output_components == 1);
130 /* Expand grey->colour. Expand from the end of the
131 * memory down, so we can use the same buffer.
133 w = cinfo->image_width;
134 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
137 from = lines[i] + w - 1;
138 to = lines[i] + (w - 1) * 3;
139 for (j = w - 1; j >= 0; j--) {
149 /* Shared library entry point */
154 guchar *pixels = NULL;
156 guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height,
157 * from the header file:
158 * " Usually rec_outbuf_height will be 1 or 2,
162 struct jpeg_decompress_struct cinfo;
163 struct error_handler_data jerr;
165 /* setup error handler */
166 cinfo.err = jpeg_std_error (&jerr.pub);
167 jerr.pub.error_exit = fatal_error_handler;
169 if (sigsetjmp (jerr.setjmp_buffer, 1)) {
170 /* Whoops there was a jpeg error */
174 jpeg_destroy_decompress (&cinfo);
178 /* load header, setup */
179 jpeg_create_decompress (&cinfo);
180 jpeg_stdio_src (&cinfo, f);
181 jpeg_read_header (&cinfo, TRUE);
182 jpeg_start_decompress (&cinfo);
183 cinfo.do_fancy_upsampling = FALSE;
184 cinfo.do_block_smoothing = FALSE;
186 w = cinfo.output_width;
187 h = cinfo.output_height;
189 pixels = g_malloc (h * w * 3);
191 jpeg_destroy_decompress (&cinfo);
197 /* decompress all the lines, a few at a time */
199 while (cinfo.output_scanline < cinfo.output_height) {
201 for (i = 0; i < cinfo.rec_outbuf_height; i++) {
206 jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height);
208 if (cinfo.output_components == 1)
209 explode_gray_into_buf (&cinfo, lines);
212 jpeg_finish_decompress (&cinfo);
213 jpeg_destroy_decompress (&cinfo);
215 return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, FALSE,
221 /**** Progressive image loading handling *****/
223 /* these routines required because we are acting as a source manager for */
226 init_source (j_decompress_ptr cinfo)
228 my_src_ptr src = (my_src_ptr) cinfo->src;
235 term_source (j_decompress_ptr cinfo)
237 /* XXXX - probably should scream something has happened */
241 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
242 /* we do nothing except return "FALSE" */
244 fill_input_buffer (j_decompress_ptr cinfo)
251 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
253 my_src_ptr src = (my_src_ptr) cinfo->src;
256 /* move as far as we can into current buffer */
257 /* then set skip_next to catch the rest */
259 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
260 src->pub.next_input_byte += (size_t) num_can_do;
261 src->pub.bytes_in_buffer -= (size_t) num_can_do;
263 src->skip_next = num_bytes - num_can_do;
269 * func - called when we have pixmap created (but no image data)
270 * user_data - passed as arg 1 to func
271 * return context (opaque to user)
275 image_begin_load (ModulePreparedNotifyFunc prepared_func,
276 ModuleUpdatedNotifyFunc updated_func,
279 JpegProgContext *context;
282 context = g_new0 (JpegProgContext, 1);
283 context->prepared_func = prepared_func;
284 context->updated_func = updated_func;
285 context->user_data = user_data;
286 context->pixbuf = NULL;
287 context->got_header = FALSE;
288 context->did_prescan = FALSE;
289 context->src_initialized = FALSE;
291 /* create libjpeg structures */
292 jpeg_create_decompress (&context->cinfo);
294 context->cinfo.src = (struct jpeg_source_mgr *) g_new0 (my_source_mgr, 1);
295 src = (my_src_ptr) context->cinfo.src;
297 context->cinfo.err = jpeg_std_error (&context->jerr.pub);
299 src = (my_src_ptr) context->cinfo.src;
300 src->pub.init_source = init_source;
301 src->pub.fill_input_buffer = fill_input_buffer;
302 src->pub.skip_input_data = skip_input_data;
303 src->pub.resync_to_restart = jpeg_resync_to_restart;
304 src->pub.term_source = term_source;
305 src->pub.bytes_in_buffer = 0;
306 src->pub.next_input_byte = NULL;
308 return (gpointer) context;
312 * context - returned from image_begin_load
314 * free context, unref gdk_pixbuf
317 image_stop_load (gpointer data)
319 JpegProgContext *context = (JpegProgContext *) data;
321 g_return_if_fail (context != NULL);
324 gdk_pixbuf_unref (context->pixbuf);
326 jpeg_finish_decompress(&context->cinfo);
327 jpeg_destroy_decompress(&context->cinfo);
329 if (context->cinfo.src) {
330 my_src_ptr src = (my_src_ptr) context->cinfo.src;
342 * context - from image_begin_load
343 * buf - new image data
344 * size - length of new image data
346 * append image data onto inrecrementally built output image
349 image_load_increment (gpointer data, guchar *buf, guint size)
351 JpegProgContext *context = (JpegProgContext *)data;
352 struct jpeg_decompress_struct *cinfo;
354 guint num_left, num_copy;
355 guint last_bytes_left;
360 g_return_val_if_fail (context != NULL, FALSE);
361 g_return_val_if_fail (buf != NULL, FALSE);
363 src = (my_src_ptr) context->cinfo.src;
365 cinfo = &context->cinfo;
367 /* XXXXXXX (drmike) - loop(s) below need to be recoded now I
368 * have a grasp of what the flow needs to be!
372 /* skip over data if requested, handle unsigned int sizes cleanly */
373 /* only can happen if we've already called jpeg_get_header once */
374 if (context->src_initialized && src->skip_next) {
375 if (src->skip_next > size) {
376 src->skip_next -= size;
379 num_left = size - src->skip_next;
380 bufhd = buf + src->skip_next;
396 /* handle any data from caller we haven't processed yet */
398 if(src->pub.bytes_in_buffer &&
399 src->pub.next_input_byte != src->buffer)
400 memmove(src->buffer, src->pub.next_input_byte,
401 src->pub.bytes_in_buffer);
404 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
407 /* if (num_copy == 0)
408 g_error ("Buffer overflow!");
410 memcpy(src->buffer + src->pub.bytes_in_buffer, bufhd,num_copy);
411 src->pub.next_input_byte = src->buffer;
412 src->pub.bytes_in_buffer += num_copy;
414 num_left -= num_copy;
416 /* did anything change from last pass, if not return */
418 last_bytes_left = src->pub.bytes_in_buffer;
420 } else if (src->pub.bytes_in_buffer == last_bytes_left)
423 last_bytes_left = src->pub.bytes_in_buffer;
426 /* should not go through twice and not pull bytes out of buf */
430 /* try to load jpeg header */
431 if (!context->got_header) {
434 rc = jpeg_read_header (cinfo, TRUE);
435 context->src_initialized = TRUE;
437 if (rc == JPEG_SUSPENDED)
440 context->got_header = TRUE;
443 if (jpeg_has_multiple_scans (cinfo)) {
444 g_print ("io-jpeg.c: Does not currently "
445 "support progressive jpeg files.\n");
449 context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB,
450 /*have_alpha*/ FALSE,
453 cinfo->image_height);
455 if (context->pixbuf == NULL) {
456 /* Failed to allocate memory */
457 g_error ("Couldn't allocate gdkpixbuf");
460 /* Use pixbuf buffer to store decompressed data */
461 context->dptr = context->pixbuf->art_pixbuf->pixels;
463 /* Notify the client that we are ready to go */
464 (* context->prepared_func) (context->pixbuf,
467 } else if (!context->did_prescan) {
470 /* start decompression */
471 rc = jpeg_start_decompress (cinfo);
472 cinfo->do_fancy_upsampling = FALSE;
473 cinfo->do_block_smoothing = FALSE;
475 if (rc == JPEG_SUSPENDED)
478 context->did_prescan = TRUE;
481 /* we're decompressing so feed jpeg lib scanlines */
488 /* keep going until we've done all scanlines */
489 while (cinfo->output_scanline < cinfo->output_height) {
490 start_scanline = cinfo->output_scanline;
492 rowptr = context->dptr;
493 for (i=0; i < cinfo->rec_outbuf_height; i++) {
495 rowptr += context->pixbuf->art_pixbuf->rowstride;
498 nlines = jpeg_read_scanlines (cinfo, lines,
499 cinfo->rec_outbuf_height);
504 if (cinfo->output_components == 1)
505 explode_gray_into_buf (cinfo, lines);
507 context->dptr += nlines * context->pixbuf->art_pixbuf->rowstride;
509 /* send updated signal */
510 (* context->updated_func) (context->pixbuf,
513 cinfo->output_scanline-1,
517 #undef DEBUG_JPEG_PROGRESSIVE
518 #ifdef DEBUG_JPEG_PROGRESSIVE
520 if (start_scanline != cinfo->output_scanline)
521 g_print("jpeg: Input pass=%2d, next input scanline=%3d,"
522 " emitted %3d - %3d\n",
523 cinfo->input_scan_number, cinfo->input_iMCU_row * 16,
524 start_scanline, cinfo->output_scanline - 1);
528 g_print ("Scanline %d of %d - ",
529 cinfo->output_scanline,
530 cinfo->output_height);
531 /* g_print ("rec_height %d -", cinfo->rec_outbuf_height); */
532 g_print ("Processed %d lines - bytes left = %d\n",
533 nlines, cinfo->src->bytes_in_buffer);
536 /* did entire image */
537 if (cinfo->output_scanline >= cinfo->output_height)