compilation fixes
[geeqie.git] / src / image_load_jpeg.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2011 The Geeqie Team
5  *
6  * Author: Vladimir Nadvornik
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 /* based on code from GdkPixbuf library - JPEG image loader
14  *
15  * Copyright (C) 1999 Michael Zucchi
16  * Copyright (C) 1999 The Free Software Foundation
17  * 
18  * Progressive loading code Copyright (C) 1999 Red Hat, Inc.
19  *
20  * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
21  *          Federico Mena-Quintero <federico@gimp.org>
22  *          Michael Fulbright <drmike@redhat.com>
23  *
24  * This library is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2 of the License, or (at your option) any later version.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with this library; if not, write to the
36  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
37  * Boston, MA 02111-1307, USA.
38  */
39
40
41 #include "main.h"
42
43 #include "image-load.h"
44 #include "image_load_jpeg.h"
45 #include "jpeg_parser.h"
46
47 #ifdef HAVE_JPEG
48
49 #include <setjmp.h>
50 #include <jpeglib.h>
51 #include <jerror.h>
52
53 typedef struct _ImageLoaderJpeg ImageLoaderJpeg;
54 struct _ImageLoaderJpeg {
55         ImageLoaderBackendCbAreaUpdated area_updated_cb;
56         ImageLoaderBackendCbSize size_cb;
57         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
58         
59         gpointer data;
60         
61         GdkPixbuf *pixbuf;
62         guint requested_width;
63         guint requested_height;
64         
65         gboolean abort;
66         gboolean stereo;
67         
68 };
69
70 /* error handler data */
71 struct error_handler_data {
72         struct jpeg_error_mgr pub;
73         sigjmp_buf setjmp_buffer;
74         GError **error;
75 };
76
77 /* explode gray image data from jpeg library into rgb components in pixbuf */
78 static void
79 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
80                        guchar **lines) 
81 {
82         gint i, j;
83         guint w;
84
85         g_return_if_fail (cinfo != NULL);
86         g_return_if_fail (cinfo->output_components == 1);
87         g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
88
89         /* Expand grey->colour.  Expand from the end of the
90          * memory down, so we can use the same buffer.
91          */
92         w = cinfo->output_width;
93         for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
94                 guchar *from, *to;
95                 
96                 from = lines[i] + w - 1;
97                 to = lines[i] + (w - 1) * 3;
98                 for (j = w - 1; j >= 0; j--) {
99                         to[0] = from[0];
100                         to[1] = from[0];
101                         to[2] = from[0];
102                         to -= 3;
103                         from--;
104                 }
105         }
106 }
107
108
109 static void
110 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
111                      guchar **lines) 
112 {
113         gint i, j;
114
115         g_return_if_fail (cinfo != NULL);
116         g_return_if_fail (cinfo->output_components == 4);
117         g_return_if_fail (cinfo->out_color_space == JCS_CMYK);
118
119         for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
120                 guchar *p;
121                 
122                 p = lines[i];
123                 for (j = 0; j < cinfo->output_width; j++) {
124                         int c, m, y, k;
125                         c = p[0];
126                         m = p[1];
127                         y = p[2];
128                         k = p[3];
129                         if (cinfo->saw_Adobe_marker) {
130                                 p[0] = k*c / 255;
131                                 p[1] = k*m / 255;
132                                 p[2] = k*y / 255;
133                         }
134                         else {
135                                 p[0] = (255 - k)*(255 - c) / 255;
136                                 p[1] = (255 - k)*(255 - m) / 255;
137                                 p[2] = (255 - k)*(255 - y) / 255;
138                         }
139                         p[3] = 255;
140                         p += 4;
141                 }
142         }
143 }
144
145
146 static gpointer image_loader_jpeg_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
147 {
148         ImageLoaderJpeg *loader = g_new0(ImageLoaderJpeg, 1);
149         
150         loader->area_updated_cb = area_updated_cb;
151         loader->size_cb = size_cb;
152         loader->area_prepared_cb = area_prepared_cb;
153         loader->data = data;
154         return (gpointer) loader;
155 }
156
157 static void
158 fatal_error_handler (j_common_ptr cinfo)
159 {
160         struct error_handler_data *errmgr;
161         char buffer[JMSG_LENGTH_MAX];
162         
163         errmgr = (struct error_handler_data *) cinfo->err;
164         
165         /* Create the message */
166         (* cinfo->err->format_message) (cinfo, buffer);
167
168         /* broken check for *error == NULL for robustness against
169          * crappy JPEG library
170          */
171         if (errmgr->error && *errmgr->error == NULL) {
172                 g_set_error (errmgr->error,
173                              GDK_PIXBUF_ERROR,
174                              cinfo->err->msg_code == JERR_OUT_OF_MEMORY 
175                              ? GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY 
176                              : GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
177                              _("Error interpreting JPEG image file (%s)"),
178                              buffer);
179         }
180         
181         siglongjmp (errmgr->setjmp_buffer, 1);
182
183         g_assert_not_reached ();
184 }
185
186 static void
187 output_message_handler (j_common_ptr cinfo)
188 {
189   /* This method keeps libjpeg from dumping crap to stderr */
190
191   /* do nothing */
192 }
193
194
195 void image_loader_jpeg_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
196 {
197         guchar *lines[4];
198         guchar **lptr;
199         gint i;
200
201         lptr = lines;
202         for (i = 0; i < cinfo->rec_outbuf_height; i++) 
203                 {
204                 *lptr++ = *dptr;
205                 *dptr += rowstride;
206                 }
207
208         jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
209
210         switch (cinfo->out_color_space) 
211                 {
212                     case JCS_GRAYSCALE:
213                       explode_gray_into_buf (cinfo, lines);
214                       break;
215                     case JCS_RGB:
216                       /* do nothing */
217                       break;
218                     case JCS_CMYK:
219                       convert_cmyk_to_rgb (cinfo, lines);
220                       break;
221                     default:
222                       break;
223                 }
224 }
225
226
227 static void init_source (j_decompress_ptr cinfo) {}
228 static boolean fill_input_buffer (j_decompress_ptr cinfo)
229 {
230         ERREXIT(cinfo, JERR_INPUT_EMPTY);
231         return TRUE;
232 }
233 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
234 {
235         struct jpeg_source_mgr* src = (struct jpeg_source_mgr*) cinfo->src;
236
237         if (num_bytes > 0) 
238                 {
239                 src->next_input_byte += (size_t) num_bytes;
240                 src->bytes_in_buffer -= (size_t) num_bytes;
241                 }
242 }
243 static void term_source (j_decompress_ptr cinfo) {}
244 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
245 {
246         struct jpeg_source_mgr* src;
247
248         if (cinfo->src == NULL) 
249                 {   /* first time for this JPEG object? */
250                 cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) (
251                                         (j_common_ptr) cinfo, JPOOL_PERMANENT,
252                                         sizeof(struct jpeg_source_mgr));
253                 }
254
255         src = (struct jpeg_source_mgr*) cinfo->src;
256         src->init_source = init_source;
257         src->fill_input_buffer = fill_input_buffer;
258         src->skip_input_data = skip_input_data;
259         src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
260         src->term_source = term_source;
261         src->bytes_in_buffer = nbytes;
262         src->next_input_byte = (JOCTET*)buffer;
263 }
264
265
266 static gboolean image_loader_jpeg_load (gpointer loader, const guchar *buf, gsize count, GError **error)
267 {
268         ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
269         struct jpeg_decompress_struct cinfo;
270         struct jpeg_decompress_struct cinfo2;
271         guchar *dptr, *dptr2;
272         guint rowstride;
273         guchar *stereo_buf2 = NULL;
274         guint stereo_length = 0;
275
276         struct error_handler_data jerr;
277         
278         lj->stereo = FALSE;
279
280         MPOData *mpo = jpeg_get_mpo_data(buf, count);
281         if (mpo && mpo->num_images > 1)
282                 {
283                 guint i;
284                 gint idx1 = -1, idx2 = -1;
285                 guint num2 = 1;
286                 
287                 for (i = 0; i < mpo->num_images; i++)
288                         {
289                         if (mpo->images[i].type_code == 0x20002)
290                                 {
291                                 if (mpo->images[i].MPIndividualNum == 1)
292                                         {
293                                         idx1 = i;
294                                         }
295                                 else if (mpo->images[i].MPIndividualNum > num2)
296                                         {
297                                         idx2 = i;
298                                         num2 = mpo->images[i].MPIndividualNum;
299                                         }
300                                 }
301                         }
302                         
303                 if (idx1 >= 0 && idx2 >= 0)
304                         {
305                         lj->stereo = TRUE;
306                         stereo_buf2 = (unsigned char *)buf + mpo->images[idx2].offset;
307                         stereo_length = mpo->images[idx2].length;
308                         buf = (unsigned char *)buf + mpo->images[idx1].offset;
309                         count = mpo->images[idx1].length;
310                         }
311                 }
312         jpeg_mpo_data_free(mpo);
313
314         /* setup error handler */
315         cinfo.err = jpeg_std_error (&jerr.pub);
316         if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
317         jerr.pub.error_exit = fatal_error_handler;
318         jerr.pub.output_message = output_message_handler;
319
320         jerr.error = error;
321
322
323         if (setjmp(jerr.setjmp_buffer)) 
324                 {
325                 /* If we get here, the JPEG code has signaled an error.
326                  * We need to clean up the JPEG object, close the input file, and return.
327                 */
328                 jpeg_destroy_decompress(&cinfo);
329                 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
330                 return FALSE;
331                 }
332         
333         jpeg_create_decompress(&cinfo);
334
335         set_mem_src(&cinfo, (unsigned char *)buf, count);
336
337
338         jpeg_read_header(&cinfo, TRUE);
339         
340         if (lj->stereo)
341                 {
342                 jpeg_create_decompress(&cinfo2);
343                 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
344                 jpeg_read_header(&cinfo2, TRUE);
345                 
346                 if (cinfo.image_width != cinfo2.image_width ||
347                     cinfo.image_height != cinfo2.image_height) 
348                         {
349                         DEBUG_1("stereo data with different size");
350                         jpeg_destroy_decompress(&cinfo2);
351                         lj->stereo = FALSE;
352                         }
353                 }
354
355                     
356
357         lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
358         lj->requested_height = cinfo.image_height;
359         lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
360                         
361         cinfo.scale_num = 1;
362         for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
363                 jpeg_calc_output_dimensions(&cinfo);
364                 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
365                         cinfo.scale_denom /= 2;
366                         break;
367                 }
368         }
369         jpeg_calc_output_dimensions(&cinfo);
370         if (lj->stereo)
371                 {
372                 cinfo2.scale_num = cinfo.scale_num;
373                 cinfo2.scale_denom = cinfo.scale_denom;
374                 jpeg_calc_output_dimensions(&cinfo2);
375                 jpeg_start_decompress(&cinfo2);
376                 }
377                 
378
379         jpeg_start_decompress(&cinfo);
380         
381
382         if (lj->stereo)
383                 {
384                 if (cinfo.output_width != cinfo2.output_width ||
385                     cinfo.output_height != cinfo2.output_height ||
386                     cinfo.out_color_components != cinfo2.out_color_components) 
387                         {
388                         DEBUG_1("stereo data with different output size");
389                         jpeg_destroy_decompress(&cinfo2);
390                         lj->stereo = FALSE;
391                         }
392                 }
393         
394         
395         lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 
396                                      cinfo.out_color_components == 4 ? TRUE : FALSE, 
397                                      8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
398
399         if (!lj->pixbuf) 
400                 {
401                 jpeg_destroy_decompress (&cinfo);
402                 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
403                 return 0;
404                 }
405         if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
406         lj->area_prepared_cb(loader, lj->data);
407
408         rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
409         dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
410         dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
411         
412
413         while (cinfo.output_scanline < cinfo.output_height && !lj->abort) 
414                 {
415                 guint scanline = cinfo.output_scanline;
416                 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
417                 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
418                 if (lj->stereo)
419                         {
420                         guint scanline = cinfo2.output_scanline;
421                         image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
422                         lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
423                         }
424                 }
425
426         jpeg_finish_decompress(&cinfo);
427         jpeg_destroy_decompress(&cinfo);
428         if (lj->stereo)
429                 {
430                 jpeg_finish_decompress(&cinfo);
431                 jpeg_destroy_decompress(&cinfo);
432                 }
433
434         return TRUE;
435 }
436
437 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
438 {
439         ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
440         lj->requested_width = width;
441         lj->requested_height = height;
442 }
443
444 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
445 {
446         ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
447         return lj->pixbuf;
448 }
449
450 static gchar* image_loader_jpeg_get_format_name(gpointer loader)
451 {
452         return g_strdup("jpeg");
453 }
454 static gchar** image_loader_jpeg_get_format_mime_types(gpointer loader)
455 {
456         static gchar *mime[] = {"image/jpeg", NULL};
457         return g_strdupv(mime);
458 }
459
460 static gboolean image_loader_jpeg_close(gpointer loader, GError **error)
461 {
462         return TRUE;
463 }
464
465 static void image_loader_jpeg_abort(gpointer loader)
466 {
467         ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
468         lj->abort = TRUE;
469 }
470
471 static void image_loader_jpeg_free(gpointer loader)
472 {
473         ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
474         if (lj->pixbuf) g_object_unref(lj->pixbuf);
475         g_free(lj);
476 }
477
478
479 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
480 {
481         funcs->loader_new = image_loader_jpeg_new;
482         funcs->set_size = image_loader_jpeg_set_size;
483         funcs->load = image_loader_jpeg_load;
484         funcs->write = NULL;
485         funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
486         funcs->close = image_loader_jpeg_close;
487         funcs->abort = image_loader_jpeg_abort;
488         funcs->free = image_loader_jpeg_free;
489         
490         funcs->get_format_name = image_loader_jpeg_get_format_name;
491         funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;
492 }
493
494
495
496 #endif