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.
43 #include "image-load.h"
44 #include "image_load_jpeg.h"
45 #include "jpeg_parser.h"
53 typedef struct _ImageLoaderJpeg ImageLoaderJpeg;
54 struct _ImageLoaderJpeg {
55 ImageLoaderBackendCbAreaUpdated area_updated_cb;
56 ImageLoaderBackendCbSize size_cb;
57 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
62 guint requested_width;
63 guint requested_height;
70 /* error handler data */
71 struct error_handler_data {
72 struct jpeg_error_mgr pub;
73 sigjmp_buf setjmp_buffer;
77 /* explode gray image data from jpeg library into rgb components in pixbuf */
79 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
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);
89 /* Expand grey->colour. Expand from the end of the
90 * memory down, so we can use the same buffer.
92 w = cinfo->output_width;
93 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
96 from = lines[i] + w - 1;
97 to = lines[i] + (w - 1) * 3;
98 for (j = w - 1; j >= 0; j--) {
110 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
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);
119 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
123 for (j = 0; j < cinfo->output_width; j++) {
129 if (cinfo->saw_Adobe_marker) {
135 p[0] = (255 - k)*(255 - c) / 255;
136 p[1] = (255 - k)*(255 - m) / 255;
137 p[2] = (255 - k)*(255 - y) / 255;
146 static gpointer image_loader_jpeg_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
148 ImageLoaderJpeg *loader = g_new0(ImageLoaderJpeg, 1);
150 loader->area_updated_cb = area_updated_cb;
151 loader->size_cb = size_cb;
152 loader->area_prepared_cb = area_prepared_cb;
154 return (gpointer) loader;
158 fatal_error_handler (j_common_ptr cinfo)
160 struct error_handler_data *errmgr;
161 char buffer[JMSG_LENGTH_MAX];
163 errmgr = (struct error_handler_data *) cinfo->err;
165 /* Create the message */
166 (* cinfo->err->format_message) (cinfo, buffer);
168 /* broken check for *error == NULL for robustness against
169 * crappy JPEG library
171 if (errmgr->error && *errmgr->error == NULL) {
172 g_set_error (errmgr->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)"),
181 siglongjmp (errmgr->setjmp_buffer, 1);
183 g_assert_not_reached ();
187 output_message_handler (j_common_ptr cinfo)
189 /* This method keeps libjpeg from dumping crap to stderr */
195 void image_loader_jpeg_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
202 for (i = 0; i < cinfo->rec_outbuf_height; i++)
208 jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
210 switch (cinfo->out_color_space)
213 explode_gray_into_buf (cinfo, lines);
219 convert_cmyk_to_rgb (cinfo, lines);
227 static void init_source (j_decompress_ptr cinfo) {}
228 static boolean fill_input_buffer (j_decompress_ptr cinfo)
230 ERREXIT(cinfo, JERR_INPUT_EMPTY);
232 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
234 struct jpeg_source_mgr* src = (struct jpeg_source_mgr*) cinfo->src;
238 src->next_input_byte += (size_t) num_bytes;
239 src->bytes_in_buffer -= (size_t) num_bytes;
242 static void term_source (j_decompress_ptr cinfo) {}
243 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
245 struct jpeg_source_mgr* src;
247 if (cinfo->src == NULL)
248 { /* first time for this JPEG object? */
249 cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) (
250 (j_common_ptr) cinfo, JPOOL_PERMANENT,
251 sizeof(struct jpeg_source_mgr));
254 src = (struct jpeg_source_mgr*) cinfo->src;
255 src->init_source = init_source;
256 src->fill_input_buffer = fill_input_buffer;
257 src->skip_input_data = skip_input_data;
258 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
259 src->term_source = term_source;
260 src->bytes_in_buffer = nbytes;
261 src->next_input_byte = (JOCTET*)buffer;
265 static gboolean image_loader_jpeg_load (gpointer loader, const guchar *buf, gsize count, GError **error)
267 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
268 struct jpeg_decompress_struct cinfo;
269 struct jpeg_decompress_struct cinfo2;
270 guchar *dptr, *dptr2;
272 guchar *stereo_buf2 = NULL;
273 guint stereo_length = 0;
275 struct error_handler_data jerr;
279 MPOData *mpo = jpeg_get_mpo_data(buf, count);
280 if (mpo && mpo->num_images > 1)
283 gint idx1 = -1, idx2 = -1;
286 for (i = 0; i < mpo->num_images; i++)
288 if (mpo->images[i].type_code == 0x20002)
290 if (mpo->images[i].MPIndividualNum == 1)
294 else if (mpo->images[i].MPIndividualNum > num2)
297 num2 = mpo->images[i].MPIndividualNum;
302 if (idx1 >= 0 && idx2 >= 0)
305 stereo_buf2 = (unsigned char *)buf + mpo->images[idx2].offset;
306 stereo_length = mpo->images[idx2].length;
307 buf = (unsigned char *)buf + mpo->images[idx1].offset;
308 count = mpo->images[idx1].length;
311 jpeg_mpo_data_free(mpo);
313 /* setup error handler */
314 cinfo.err = jpeg_std_error (&jerr.pub);
315 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
316 jerr.pub.error_exit = fatal_error_handler;
317 jerr.pub.output_message = output_message_handler;
322 if (setjmp(jerr.setjmp_buffer))
324 /* If we get here, the JPEG code has signaled an error.
325 * We need to clean up the JPEG object, close the input file, and return.
327 jpeg_destroy_decompress(&cinfo);
328 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
332 jpeg_create_decompress(&cinfo);
334 set_mem_src(&cinfo, (unsigned char *)buf, count);
337 jpeg_read_header(&cinfo, TRUE);
341 jpeg_create_decompress(&cinfo2);
342 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
343 jpeg_read_header(&cinfo2, TRUE);
345 if (cinfo.image_width != cinfo2.image_width ||
346 cinfo.image_height != cinfo2.image_height)
348 DEBUG_1("stereo data with different size");
349 jpeg_destroy_decompress(&cinfo2);
356 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
357 lj->requested_height = cinfo.image_height;
358 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
361 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
362 jpeg_calc_output_dimensions(&cinfo);
363 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
364 cinfo.scale_denom /= 2;
368 jpeg_calc_output_dimensions(&cinfo);
371 cinfo2.scale_num = cinfo.scale_num;
372 cinfo2.scale_denom = cinfo.scale_denom;
373 jpeg_calc_output_dimensions(&cinfo2);
374 jpeg_start_decompress(&cinfo2);
378 jpeg_start_decompress(&cinfo);
383 if (cinfo.output_width != cinfo2.output_width ||
384 cinfo.output_height != cinfo2.output_height ||
385 cinfo.out_color_components != cinfo2.out_color_components)
387 DEBUG_1("stereo data with different output size");
388 jpeg_destroy_decompress(&cinfo2);
394 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
395 cinfo.out_color_components == 4 ? TRUE : FALSE,
396 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
400 jpeg_destroy_decompress (&cinfo);
401 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
404 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
405 lj->area_prepared_cb(loader, lj->data);
407 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
408 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
409 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
412 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
414 guint scanline = cinfo.output_scanline;
415 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
416 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
419 guint scanline = cinfo2.output_scanline;
420 image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
421 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
425 jpeg_finish_decompress(&cinfo);
426 jpeg_destroy_decompress(&cinfo);
429 jpeg_finish_decompress(&cinfo);
430 jpeg_destroy_decompress(&cinfo);
436 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
438 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
439 lj->requested_width = width;
440 lj->requested_height = height;
443 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
445 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
449 static gchar* image_loader_jpeg_get_format_name(gpointer loader)
451 return g_strdup("jpeg");
453 static gchar** image_loader_jpeg_get_format_mime_types(gpointer loader)
455 static gchar *mime[] = {"image/jpeg", NULL};
456 return g_strdupv(mime);
459 static gboolean image_loader_jpeg_close(gpointer loader, GError **error)
464 static void image_loader_jpeg_abort(gpointer loader)
466 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
470 static void image_loader_jpeg_free(gpointer loader)
472 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
473 if (lj->pixbuf) g_object_unref(lj->pixbuf);
478 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
480 funcs->loader_new = image_loader_jpeg_new;
481 funcs->set_size = image_loader_jpeg_set_size;
482 funcs->load = image_loader_jpeg_load;
484 funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
485 funcs->close = image_loader_jpeg_close;
486 funcs->abort = image_loader_jpeg_abort;
487 funcs->free = image_loader_jpeg_free;
489 funcs->get_format_name = image_loader_jpeg_get_format_name;
490 funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;