]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-jpeg.c
revert
[~andy/gtk] / gdk-pixbuf / io-jpeg.c
1 /* GdkPixbuf library - JPEG image loader
2  *
3  * Copyright (C) 1999 Michael Zucchi
4  * Copyright (C) 1999 The Free Software Foundation
5  * 
6  * Progressive loading code Copyright (C) 1999 Red Hat, Inc.
7  *
8  * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
9  *          Federico Mena-Quintero <federico@gimp.org>
10  *          Michael Fulbright <drmike@redhat.com>
11  *
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.
16  *
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.
21  *
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.
26  */
27
28
29 /*
30   Progressive file loading notes (10/29/199) <drmike@redhat.com>...
31
32   These are issues I know of and will be dealing with shortly:
33
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
37        support asap.
38
39     - gray images are not properly loaded
40
41     - error handling is not as good as it should be
42
43  */
44
45
46 #include <config.h>
47 #include <stdio.h>
48 #include <setjmp.h>
49 #include <jpeglib.h>
50 #include "gdk-pixbuf.h"
51 #include "gdk-pixbuf-io.h"
52
53 \f
54
55 /* we are a "source manager" as far as libjpeg is concerned */
56 typedef struct {
57         struct jpeg_source_mgr pub;   /* public fields */
58
59         JOCTET * buffer;              /* start of buffer */
60         gboolean start_of_file;       /* have we gotten any data yet? */
61         long  skip_next;              /* number of bytes to skip next read */
62
63 } my_source_mgr;
64
65 typedef my_source_mgr * my_src_ptr;
66
67 /* error handler data */
68 struct error_handler_data {
69         struct jpeg_error_mgr pub;
70         sigjmp_buf setjmp_buffer;
71 };
72
73 /* progressive loader context */
74 typedef struct {
75         ModulePreparedNotifyFunc notify_func;
76         gpointer                 notify_user_data;
77
78         GdkPixbuf                *pixbuf;
79         guchar                   *dptr;   /* current position in pixbuf */
80
81         gboolean                 did_prescan;  /* are we in image data yet? */
82         gboolean                 got_header;  /* have we loaded jpeg header? */
83         gboolean                 src_initialized;/* TRUE when jpeg lib initialized */
84         struct jpeg_decompress_struct cinfo;
85         struct error_handler_data     jerr;
86 } JpegProgContext;
87
88 #define JPEG_PROG_BUF_SIZE 4096
89
90 GdkPixbuf *image_load (FILE *f);
91 gpointer image_begin_load (ModulePreparedNotifyFunc func, gpointer user_data);
92 void image_stop_load (gpointer context);
93 gboolean image_load_increment(gpointer context, guchar *buf, guint size);
94
95
96 static void
97 fatal_error_handler (j_common_ptr cinfo)
98 {
99         /* FIXME:
100          * We should somehow signal what error occurred to the caller so the
101          * caller can handle the error message */
102         struct error_handler_data *errmgr;
103
104         errmgr = (struct error_handler_data *) cinfo->err;
105         cinfo->err->output_message (cinfo);
106         siglongjmp (errmgr->setjmp_buffer, 1);
107         return;
108 }
109
110 /* Destroy notification function for the libart pixbuf */
111 static void
112 free_buffer (gpointer user_data, gpointer data)
113 {
114         free (data);
115 }
116
117
118 /* explode gray image data from jpeg library into rgb components in pixbuf */
119 static void
120 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
121                        guchar **lines) 
122 {
123         gint i, j;
124         guint w;
125
126         g_return_if_fail (cinfo != NULL);
127         g_return_if_fail (cinfo->output_components == 1);
128
129         /* Expand grey->colour.  Expand from the end of the
130          * memory down, so we can use the same buffer.
131          */
132         w = cinfo->image_width;
133         for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
134                 guchar *from, *to;
135                 
136                 from = lines[i] + w - 1;
137                 to = lines[i] + (w - 1) * 3;
138                 for (j = w - 1; j >= 0; j--) {
139                         to[0] = from[0];
140                         to[1] = from[0];
141                         to[2] = from[0];
142                         to -= 3;
143                         from--;
144                 }
145         }
146 }
147
148 /* Shared library entry point */
149 GdkPixbuf *
150 image_load (FILE *f)
151 {
152         int w, h, i;
153         guchar *pixels = NULL;
154         guchar *dptr;
155         guchar *lines[4]; /* Used to expand rows, via rec_outbuf_height, from the header file:
156                            * "* Usually rec_outbuf_height will be 1 or 2, at most 4."
157                            */
158         guchar **lptr;
159         struct jpeg_decompress_struct cinfo;
160         struct error_handler_data jerr;
161         GdkPixbuf *pixbuf;
162
163         /* setup error handler */
164         cinfo.err = jpeg_std_error (&jerr.pub);
165         jerr.pub.error_exit = fatal_error_handler;
166
167         if (sigsetjmp (jerr.setjmp_buffer, 1)) {
168                 /* Whoops there was a jpeg error */
169                 if (pixels)
170                         free (pixels);
171
172                 jpeg_destroy_decompress (&cinfo);
173                 return NULL;
174         }
175
176         /* load header, setup */
177         jpeg_create_decompress (&cinfo);
178         jpeg_stdio_src (&cinfo, f);
179         jpeg_read_header (&cinfo, TRUE);
180         jpeg_start_decompress (&cinfo);
181         cinfo.do_fancy_upsampling = FALSE;
182         cinfo.do_block_smoothing = FALSE;
183
184         w = cinfo.output_width;
185         h = cinfo.output_height;
186
187         pixels = malloc (h * w * 3);
188         if (!pixels) {
189                 jpeg_destroy_decompress (&cinfo);
190                 return NULL;
191         }
192
193         dptr = pixels;
194
195         /* decompress all the lines, a few at a time */
196
197         while (cinfo.output_scanline < cinfo.output_height) {
198                 lptr = lines;
199                 for (i = 0; i < cinfo.rec_outbuf_height; i++) {
200                         *lptr++ = dptr;
201                         dptr += w * 3;
202                 }
203
204                 jpeg_read_scanlines (&cinfo, lines, cinfo.rec_outbuf_height);
205
206                 if (cinfo.output_components == 1)
207                         explode_gray_into_buf (&cinfo, lines);
208         }
209
210         jpeg_finish_decompress (&cinfo);
211         jpeg_destroy_decompress (&cinfo);
212
213         return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, FALSE,
214                                          w, h, w * 3,
215                                          free_buffer, NULL);
216
217         return pixbuf;
218 }
219
220
221 /**** Progressive image loading handling *****/
222
223 /* these routines required because we are acting as a source manager for */
224 /* libjpeg. */
225 static void
226 init_source (j_decompress_ptr cinfo)
227 {
228         my_src_ptr src = (my_src_ptr) cinfo->src;
229
230         src->start_of_file = TRUE;
231         src->skip_next = 0;
232 }
233
234
235 static void
236 term_source (j_decompress_ptr cinfo)
237 {
238         /* XXXX - probably should scream something has happened */
239 }
240
241
242 /* for progressive loading (called "I/O Suspension" by libjpeg docs) */
243 /* we do nothing except return "FALSE"                               */
244 static boolean
245 fill_input_buffer (j_decompress_ptr cinfo)
246 {
247         return FALSE;
248 }
249
250
251 static void
252 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
253 {
254         my_src_ptr src = (my_src_ptr) cinfo->src;
255         long   num_can_do;
256
257         /* move as far as we can into current buffer */
258         /* then set skip_next to catch the rest      */
259         if (num_bytes > 0) {
260                 num_can_do = MIN (src->pub.bytes_in_buffer, num_bytes);
261                 src->pub.next_input_byte += (size_t) num_can_do;
262                 src->pub.bytes_in_buffer -= (size_t) num_can_do;
263
264                 src->skip_next = num_bytes - num_can_do;
265         }
266 }
267
268  
269 /* 
270  * func - called when we have pixmap created (but no image data)
271  * user_data - passed as arg 1 to func
272  * return context (opaque to user)
273  */
274
275 gpointer
276 image_begin_load (ModulePreparedNotifyFunc func, gpointer user_data)
277 {
278         JpegProgContext *context;
279         my_source_mgr   *src;
280
281         context = g_new (JpegProgContext, 1);
282         context->notify_func = func;
283         context->notify_user_data = user_data;
284         context->pixbuf = NULL;
285         context->got_header = FALSE;
286         context->did_prescan = FALSE;
287         context->src_initialized = FALSE;
288
289         /* create libjpeg structures */
290         jpeg_create_decompress (&context->cinfo);
291
292         context->cinfo.src = (struct jpeg_source_mgr *) g_new (my_source_mgr, 1);
293         src = (my_src_ptr) context->cinfo.src;
294         src->buffer = g_malloc (JPEG_PROG_BUF_SIZE);
295
296         context->cinfo.err = jpeg_std_error (&context->jerr.pub);
297
298         src = (my_src_ptr) context->cinfo.src;
299         src->pub.init_source = init_source;
300         src->pub.fill_input_buffer = fill_input_buffer;
301         src->pub.skip_input_data = skip_input_data;
302         src->pub.resync_to_restart = jpeg_resync_to_restart;
303         src->pub.term_source = term_source;
304         src->pub.bytes_in_buffer = 0;
305         src->pub.next_input_byte = NULL;
306
307         return (gpointer) context;
308 }
309
310 /*
311  * context - returned from image_begin_load
312  *
313  * free context, unref gdk_pixbuf
314  */
315 void
316 image_stop_load (gpointer data)
317 {
318         JpegProgContext *context = (JpegProgContext *) data;
319         g_return_if_fail (context != NULL);
320
321         if (context->pixbuf)
322                 gdk_pixbuf_unref (context->pixbuf);
323
324         if (context->cinfo.src) {
325                 my_src_ptr src = (my_src_ptr) context->cinfo.src;
326                 
327                 if (src->buffer)
328                         g_free (src->buffer);
329                 g_free (src);
330         }
331
332         jpeg_finish_decompress(&context->cinfo);
333         jpeg_destroy_decompress(&context->cinfo);
334
335         g_free (context);
336 }
337
338
339
340
341 /*
342  * context - from image_begin_load
343  * buf - new image data
344  * size - length of new image data
345  *
346  * append image data onto inrecrementally built output image
347  */
348 gboolean
349 image_load_increment (gpointer data, guchar *buf, guint size)
350 {
351         JpegProgContext *context = (JpegProgContext *)data;
352         struct jpeg_decompress_struct *cinfo;
353         my_src_ptr  src;
354         guint       num_left, num_copy;
355         guchar      *nextptr;
356
357         g_return_val_if_fail (context != NULL, FALSE);
358         g_return_val_if_fail (buf != NULL, FALSE);
359
360         src = (my_src_ptr) context->cinfo.src;
361         cinfo = &context->cinfo;
362
363         /* skip over data if requested, handle unsigned int sizes cleanly */
364         /* only can happen if we've already called jpeg_get_header once   */
365         if (context->src_initialized && src->skip_next) {
366                 if (src->skip_next > size) {
367                         src->skip_next -= size;
368                         return TRUE;
369                 } else {
370                         num_left = size - src->skip_next;
371                         src->skip_next = 0;
372                 }
373         } else {
374                 num_left = size;
375         }
376
377         while (num_left > 0) {
378                 /* copy as much data into buffer as possible */
379                 num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
380                                 size);
381
382                 if (num_copy == 0) 
383                         g_assert ("Buffer overflow!\n");
384
385                 nextptr = src->buffer + src->pub.bytes_in_buffer;
386                 memcpy (nextptr, buf, num_copy);
387                 
388                 if (src->pub.next_input_byte == NULL ||
389                     src->pub.bytes_in_buffer == 0)
390                 src->pub.next_input_byte = src->buffer;
391
392                 src->pub.bytes_in_buffer += num_copy;
393
394                 num_left -= num_copy;
395
396                 /* try to load jpeg header */
397                 if (!context->got_header) {
398                         int rc;
399
400                         rc = jpeg_read_header (cinfo, TRUE);
401                         context->src_initialized = TRUE;
402                         if (rc == JPEG_SUSPENDED)
403                                 continue;
404
405                         context->got_header = TRUE;
406
407                         if (jpeg_has_multiple_scans (cinfo)) {
408                                 g_print ("io-jpeg.c: Does not currently "
409                                          "support progressive jpeg files.\n");
410                                 return FALSE;
411                         }
412
413                         context->pixbuf = gdk_pixbuf_new(ART_PIX_RGB, 
414                                                          /*have_alpha*/ FALSE,
415                                                          8, 
416                                                          cinfo->image_width,
417                                                          cinfo->image_height);
418
419                         if (context->pixbuf == NULL) {
420                                 /* Failed to allocate memory */
421                                 g_assert ("Couldn't allocate gdkpixbuf\n");
422                         }
423
424                         /* Use pixbuf buffer to store decompressed data */
425                         context->dptr = context->pixbuf->art_pixbuf->pixels;
426
427                         /* Notify the client that we are ready to go */
428
429                         if (context->notify_func)
430                                 (* context->notify_func) (context->pixbuf,
431                                                           context->notify_user_data);
432
433                         src->start_of_file = FALSE;
434                 } else if (!context->did_prescan) {
435                         int rc;
436
437                         /* start decompression */
438                         rc = jpeg_start_decompress (cinfo);
439                         cinfo->do_fancy_upsampling = FALSE;
440                         cinfo->do_block_smoothing = FALSE;
441
442                         if (rc == JPEG_SUSPENDED)
443                                 continue;
444
445                         context->did_prescan = TRUE;
446                 } else {
447                         /* we're decompressing so feed jpeg lib scanlines */
448                         guchar *lines[4];
449                         guchar **lptr;
450                         guchar *rowptr, *p;
451                         gint   nlines, i;
452                         gint   start_scanline;
453
454                         /* keep going until we've done all scanlines */
455                         while (cinfo->output_scanline < cinfo->output_height) {
456                                 start_scanline = cinfo->output_scanline;
457                                 lptr = lines;
458                                 rowptr = context->dptr;
459                                 for (i=0; i < cinfo->rec_outbuf_height; i++) {
460                                         *lptr++ = rowptr;
461                                         rowptr += context->pixbuf->art_pixbuf->rowstride;;
462                                 }
463
464                                 for (p=lines[0],i=0; i< context->pixbuf->art_pixbuf->rowstride;i++, p++)
465                                         *p = 0;
466                                 
467                                 nlines = jpeg_read_scanlines (cinfo, lines,
468                                                               cinfo->rec_outbuf_height);
469                                 if (nlines == 0)
470                                         break;
471
472                                 /* handle gray */
473                                 if (cinfo->output_components == 1)
474                                         explode_gray_into_buf (cinfo, lines);
475
476                                 context->dptr += nlines * context->pixbuf->art_pixbuf->rowstride;
477 #ifdef DEBUG_JPEG_PROGRESSIVE
478                                 
479                                 if (start_scanline != cinfo->output_scanline)
480                                         g_print("jpeg: Input pass=%2d, next input scanline=%3d,"
481                                                 " emitted %3d - %3d\n",
482                                                 cinfo->input_scan_number, cinfo->input_iMCU_row * 16,
483                                                 start_scanline, cinfo->output_scanline - 1);
484                                 
485                                 
486                                 
487                                 g_print ("Scanline %d of %d - ", 
488                                          cinfo->output_scanline,
489                                          cinfo->output_height);
490 /*                      g_print ("rec_height %d -", cinfo->rec_outbuf_height); */
491                                 g_print ("Processed %d lines - bytes left = %d\n",
492                                          nlines, cinfo->src->bytes_in_buffer);
493 #endif
494                         }
495                         /* did entire image */
496                         return TRUE;
497                 }
498         }
499
500         return TRUE;
501 }