4 * Copyright (C) 2008 - 2011 The Geeqie Team
6 * Author: Vladimir Nadvornik
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!
13 /* based on code from GdkPixbuf library - JPEG image loader
15 * Copyright (C) 1999 Michael Zucchi
16 * Copyright (C) 1999 The Free Software Foundation
18 * Progressive loading code Copyright (C) 1999 Red Hat, Inc.
20 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
21 * Federico Mena-Quintero <federico@gimp.org>
22 * Michael Fulbright <drmike@redhat.com>
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.
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.
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.
42 #include "image-load.h"
43 #include "image_load_jpeg.h"
44 #include "jpeg_parser.h"
50 typedef struct _ImageLoaderJpeg ImageLoaderJpeg;
51 struct _ImageLoaderJpeg {
52 ImageLoaderBackendCbAreaUpdated area_updated_cb;
53 ImageLoaderBackendCbSize size_cb;
54 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
59 guint requested_width;
60 guint requested_height;
67 /* error handler data */
68 struct error_handler_data {
69 struct jpeg_error_mgr pub;
70 sigjmp_buf setjmp_buffer;
74 /* explode gray image data from jpeg library into rgb components in pixbuf */
76 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
82 g_return_if_fail (cinfo != NULL);
83 g_return_if_fail (cinfo->output_components == 1);
84 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
86 /* Expand grey->colour. Expand from the end of the
87 * memory down, so we can use the same buffer.
89 w = cinfo->output_width;
90 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
93 from = lines[i] + w - 1;
94 to = lines[i] + (w - 1) * 3;
95 for (j = w - 1; j >= 0; j--) {
107 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
112 g_return_if_fail (cinfo != NULL);
113 g_return_if_fail (cinfo->output_components == 4);
114 g_return_if_fail (cinfo->out_color_space == JCS_CMYK);
116 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
120 for (j = 0; j < cinfo->output_width; j++) {
126 if (cinfo->saw_Adobe_marker) {
132 p[0] = (255 - k)*(255 - c) / 255;
133 p[1] = (255 - k)*(255 - m) / 255;
134 p[2] = (255 - k)*(255 - y) / 255;
143 static gpointer image_loader_jpeg_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
145 ImageLoaderJpeg *loader = g_new0(ImageLoaderJpeg, 1);
147 loader->area_updated_cb = area_updated_cb;
148 loader->size_cb = size_cb;
149 loader->area_prepared_cb = area_prepared_cb;
151 return (gpointer) loader;
155 fatal_error_handler (j_common_ptr cinfo)
157 struct error_handler_data *errmgr;
158 char buffer[JMSG_LENGTH_MAX];
160 errmgr = (struct error_handler_data *) cinfo->err;
162 /* Create the message */
163 (* cinfo->err->format_message) (cinfo, buffer);
165 /* broken check for *error == NULL for robustness against
166 * crappy JPEG library
168 if (errmgr->error && *errmgr->error == NULL) {
169 g_set_error (errmgr->error,
171 cinfo->err->msg_code == JERR_OUT_OF_MEMORY
172 ? GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY
173 : GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
174 _("Error interpreting JPEG image file (%s)"),
178 siglongjmp (errmgr->setjmp_buffer, 1);
180 g_assert_not_reached ();
184 output_message_handler (j_common_ptr cinfo)
186 /* This method keeps libjpeg from dumping crap to stderr */
192 void image_loader_jpeg_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
199 for (i = 0; i < cinfo->rec_outbuf_height; i++)
205 jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
207 switch (cinfo->out_color_space)
210 explode_gray_into_buf (cinfo, lines);
216 convert_cmyk_to_rgb (cinfo, lines);
223 static gboolean image_loader_jpeg_load (gpointer loader, const guchar *buf, gsize count, GError **error)
225 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
226 struct jpeg_decompress_struct cinfo;
227 struct jpeg_decompress_struct cinfo2;
228 guchar *dptr, *dptr2;
230 guchar *stereo_buf2 = NULL;
231 guint stereo_length = 0;
233 struct error_handler_data jerr;
237 MPOData *mpo = jpeg_get_mpo_data(buf, count);
238 if (mpo && mpo->num_images > 1)
241 stereo_buf2 = (unsigned char *)buf + mpo->images[1].offset;
242 stereo_length = mpo->images[1].length;
244 jpeg_mpo_data_free(mpo);
246 /* setup error handler */
247 cinfo.err = jpeg_std_error (&jerr.pub);
248 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
249 jerr.pub.error_exit = fatal_error_handler;
250 jerr.pub.output_message = output_message_handler;
255 if (setjmp(jerr.setjmp_buffer))
257 /* If we get here, the JPEG code has signaled an error.
258 * We need to clean up the JPEG object, close the input file, and return.
260 jpeg_destroy_decompress(&cinfo);
261 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
265 jpeg_create_decompress(&cinfo);
267 jpeg_mem_src(&cinfo, (unsigned char *)buf, count);
270 jpeg_read_header(&cinfo, TRUE);
274 jpeg_create_decompress(&cinfo2);
275 jpeg_mem_src(&cinfo2, stereo_buf2, stereo_length);
276 jpeg_read_header(&cinfo2, TRUE);
278 if (cinfo.image_width != cinfo2.image_width ||
279 cinfo.image_height != cinfo2.image_height)
281 DEBUG_1("stereo data with different size");
282 jpeg_destroy_decompress(&cinfo2);
289 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
290 lj->requested_height = cinfo.image_height;
291 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
294 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
295 jpeg_calc_output_dimensions(&cinfo);
296 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
297 cinfo.scale_denom /= 2;
301 jpeg_calc_output_dimensions(&cinfo);
304 cinfo2.scale_num = cinfo.scale_num;
305 cinfo2.scale_denom = cinfo.scale_denom;
306 jpeg_calc_output_dimensions(&cinfo2);
307 jpeg_start_decompress(&cinfo2);
311 jpeg_start_decompress(&cinfo);
316 if (cinfo.output_width != cinfo2.output_width ||
317 cinfo.output_height != cinfo2.output_height ||
318 cinfo.out_color_components != cinfo2.out_color_components)
320 DEBUG_1("stereo data with different output size");
321 jpeg_destroy_decompress(&cinfo2);
327 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
328 cinfo.out_color_components == 4 ? TRUE : FALSE,
329 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
333 jpeg_destroy_decompress (&cinfo);
334 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
337 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
338 lj->area_prepared_cb(loader, lj->data);
340 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
341 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
342 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
345 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
347 guint scanline = cinfo.output_scanline;
348 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
349 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
352 guint scanline = cinfo2.output_scanline;
353 image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
354 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
358 jpeg_finish_decompress(&cinfo);
359 jpeg_destroy_decompress(&cinfo);
362 jpeg_finish_decompress(&cinfo);
363 jpeg_destroy_decompress(&cinfo);
369 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
371 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
372 lj->requested_width = width;
373 lj->requested_height = height;
376 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
378 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
382 static gchar* image_loader_jpeg_get_format_name(gpointer loader)
384 return g_strdup("jpeg");
386 static gchar** image_loader_jpeg_get_format_mime_types(gpointer loader)
388 static gchar *mime[] = {"image/jpeg", NULL};
389 return g_strdupv(mime);
392 static gboolean image_loader_jpeg_close(gpointer loader, GError **error)
397 static void image_loader_jpeg_abort(gpointer loader)
399 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
403 static void image_loader_jpeg_free(gpointer loader)
405 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
406 if (lj->pixbuf) g_object_unref(lj->pixbuf);
411 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
413 funcs->loader_new = image_loader_jpeg_new;
414 funcs->set_size = image_loader_jpeg_set_size;
415 funcs->load = image_loader_jpeg_load;
417 funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
418 funcs->close = image_loader_jpeg_close;
419 funcs->abort = image_loader_jpeg_abort;
420 funcs->free = image_loader_jpeg_free;
422 funcs->get_format_name = image_loader_jpeg_get_format_name;
423 funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;