2 * Copyright (C) 1999 Michael Zucchi
3 * Copyright (C) 1999 The Free Software Foundation
4 * Copyright (C) 2004 John Ellis
5 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
8 * Federico Mena-Quintero <federico@gimp.org>
9 * Michael Fulbright <drmike@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program 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
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "image-load.h"
30 #include "image_load_jpeg.h"
31 #include "jpeg_parser.h"
39 typedef struct _ImageLoaderJpeg ImageLoaderJpeg;
40 struct _ImageLoaderJpeg {
41 ImageLoaderBackendCbAreaUpdated area_updated_cb;
42 ImageLoaderBackendCbSize size_cb;
43 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
48 guint requested_width;
49 guint requested_height;
56 /* error handler data */
57 struct error_handler_data {
58 struct jpeg_error_mgr pub;
59 sigjmp_buf setjmp_buffer;
63 /* explode gray image data from jpeg library into rgb components in pixbuf */
65 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
71 g_return_if_fail (cinfo != NULL);
72 g_return_if_fail (cinfo->output_components == 1);
73 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
75 /* Expand grey->colour. Expand from the end of the
76 * memory down, so we can use the same buffer.
78 w = cinfo->output_width;
79 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
82 from = lines[i] + w - 1;
83 to = lines[i] + (w - 1) * 3;
84 for (j = w - 1; j >= 0; j--) {
96 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
101 g_return_if_fail (cinfo != NULL);
102 g_return_if_fail (cinfo->output_components == 4);
103 g_return_if_fail (cinfo->out_color_space == JCS_CMYK);
105 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
109 for (j = 0; j < cinfo->output_width; j++) {
115 if (cinfo->saw_Adobe_marker) {
121 p[0] = (255 - k)*(255 - c) / 255;
122 p[1] = (255 - k)*(255 - m) / 255;
123 p[2] = (255 - k)*(255 - y) / 255;
132 static gpointer image_loader_jpeg_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
134 ImageLoaderJpeg *loader = g_new0(ImageLoaderJpeg, 1);
136 loader->area_updated_cb = area_updated_cb;
137 loader->size_cb = size_cb;
138 loader->area_prepared_cb = area_prepared_cb;
140 return (gpointer) loader;
144 fatal_error_handler (j_common_ptr cinfo)
146 struct error_handler_data *errmgr;
147 char buffer[JMSG_LENGTH_MAX];
149 errmgr = (struct error_handler_data *) cinfo->err;
151 /* Create the message */
152 (* cinfo->err->format_message) (cinfo, buffer);
154 /* broken check for *error == NULL for robustness against
155 * crappy JPEG library
157 if (errmgr->error && *errmgr->error == NULL) {
158 g_set_error (errmgr->error,
160 cinfo->err->msg_code == JERR_OUT_OF_MEMORY
161 ? GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY
162 : GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
163 _("Error interpreting JPEG image file (%s)"),
167 siglongjmp (errmgr->setjmp_buffer, 1);
169 g_assert_not_reached ();
173 output_message_handler (j_common_ptr cinfo)
175 /* This method keeps libjpeg from dumping crap to stderr */
181 void image_loader_jpeg_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
188 for (i = 0; i < cinfo->rec_outbuf_height; i++)
194 jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
196 switch (cinfo->out_color_space)
199 explode_gray_into_buf (cinfo, lines);
205 convert_cmyk_to_rgb (cinfo, lines);
213 static void init_source (j_decompress_ptr cinfo) {}
214 static boolean fill_input_buffer (j_decompress_ptr cinfo)
216 ERREXIT(cinfo, JERR_INPUT_EMPTY);
219 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
221 struct jpeg_source_mgr* src = (struct jpeg_source_mgr*) cinfo->src;
223 if (num_bytes > src->bytes_in_buffer)
225 ERREXIT(cinfo, JERR_INPUT_EOF);
227 else if (num_bytes > 0)
229 src->next_input_byte += (size_t) num_bytes;
230 src->bytes_in_buffer -= (size_t) num_bytes;
233 static void term_source (j_decompress_ptr cinfo) {}
234 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
236 struct jpeg_source_mgr* src;
238 if (cinfo->src == NULL)
239 { /* first time for this JPEG object? */
240 cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) (
241 (j_common_ptr) cinfo, JPOOL_PERMANENT,
242 sizeof(struct jpeg_source_mgr));
245 src = (struct jpeg_source_mgr*) cinfo->src;
246 src->init_source = init_source;
247 src->fill_input_buffer = fill_input_buffer;
248 src->skip_input_data = skip_input_data;
249 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
250 src->term_source = term_source;
251 src->bytes_in_buffer = nbytes;
252 src->next_input_byte = (JOCTET*)buffer;
256 static gboolean image_loader_jpeg_load (gpointer loader, const guchar *buf, gsize count, GError **error)
258 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
259 struct jpeg_decompress_struct cinfo;
260 struct jpeg_decompress_struct cinfo2;
261 guchar *dptr, *dptr2;
263 guchar *stereo_buf2 = NULL;
264 guint stereo_length = 0;
266 struct error_handler_data jerr;
269 // DEBUG_1("TG: JPG requested size w=%d:h=%d", lj->requested_width > 0, lj->requested_height);
271 MPOData *mpo = jpeg_get_mpo_data(buf, count);
272 if (mpo && mpo->num_images > 1)
275 gint idx1 = -1, idx2 = -1;
278 for (i = 0; i < mpo->num_images; i++)
280 if (mpo->images[i].type_code == 0x20002)
282 if (mpo->images[i].MPIndividualNum == 1)
286 else if (mpo->images[i].MPIndividualNum > num2)
289 num2 = mpo->images[i].MPIndividualNum;
294 if (idx1 >= 0 && idx2 >= 0)
297 stereo_buf2 = (unsigned char *)buf + mpo->images[idx2].offset;
298 stereo_length = mpo->images[idx2].length;
299 buf = (unsigned char *)buf + mpo->images[idx1].offset;
300 count = mpo->images[idx1].length;
303 jpeg_mpo_data_free(mpo);
305 /* setup error handler */
306 cinfo.err = jpeg_std_error (&jerr.pub);
307 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
308 jerr.pub.error_exit = fatal_error_handler;
309 jerr.pub.output_message = output_message_handler;
314 if (setjmp(jerr.setjmp_buffer))
316 /* If we get here, the JPEG code has signaled an error.
317 * We need to clean up the JPEG object, close the input file, and return.
319 jpeg_destroy_decompress(&cinfo);
320 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
324 jpeg_create_decompress(&cinfo);
326 set_mem_src(&cinfo, (unsigned char *)buf, count);
329 jpeg_read_header(&cinfo, TRUE);
333 jpeg_create_decompress(&cinfo2);
334 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
335 jpeg_read_header(&cinfo2, TRUE);
337 if (cinfo.image_width != cinfo2.image_width ||
338 cinfo.image_height != cinfo2.image_height)
340 DEBUG_1("stereo data with different size");
341 jpeg_destroy_decompress(&cinfo2);
348 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
349 lj->requested_height = cinfo.image_height;
350 // DEBUG_1("TG: JPG requested size v2 w=%d:h=%d", lj->requested_width > 0, lj->requested_height);
351 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
354 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
355 jpeg_calc_output_dimensions(&cinfo);
356 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
357 cinfo.scale_denom /= 2;
361 jpeg_calc_output_dimensions(&cinfo);
364 cinfo2.scale_num = cinfo.scale_num;
365 cinfo2.scale_denom = cinfo.scale_denom;
366 jpeg_calc_output_dimensions(&cinfo2);
367 jpeg_start_decompress(&cinfo2);
371 jpeg_start_decompress(&cinfo);
376 if (cinfo.output_width != cinfo2.output_width ||
377 cinfo.output_height != cinfo2.output_height ||
378 cinfo.out_color_components != cinfo2.out_color_components)
380 DEBUG_1("stereo data with different output size");
381 jpeg_destroy_decompress(&cinfo2);
387 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
388 cinfo.out_color_components == 4 ? TRUE : FALSE,
389 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
393 jpeg_destroy_decompress (&cinfo);
394 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
397 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
398 lj->area_prepared_cb(loader, lj->data);
400 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
401 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
402 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
405 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
407 guint scanline = cinfo.output_scanline;
408 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
409 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
412 guint scanline = cinfo2.output_scanline;
413 image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
414 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
418 jpeg_finish_decompress(&cinfo);
419 jpeg_destroy_decompress(&cinfo);
422 jpeg_finish_decompress(&cinfo);
423 jpeg_destroy_decompress(&cinfo);
429 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
431 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
432 lj->requested_width = width;
433 lj->requested_height = height;
436 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
438 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
442 static gchar* image_loader_jpeg_get_format_name(gpointer loader)
444 return g_strdup("jpeg");
446 static gchar** image_loader_jpeg_get_format_mime_types(gpointer loader)
448 static gchar *mime[] = {"image/jpeg", NULL};
449 return g_strdupv(mime);
452 static gboolean image_loader_jpeg_close(gpointer loader, GError **error)
457 static void image_loader_jpeg_abort(gpointer loader)
459 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
463 static void image_loader_jpeg_free(gpointer loader)
465 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
466 if (lj->pixbuf) g_object_unref(lj->pixbuf);
471 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
473 funcs->loader_new = image_loader_jpeg_new;
474 funcs->set_size = image_loader_jpeg_set_size;
475 funcs->load = image_loader_jpeg_load;
477 funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
478 funcs->close = image_loader_jpeg_close;
479 funcs->abort = image_loader_jpeg_abort;
480 funcs->free = image_loader_jpeg_free;
482 funcs->get_format_name = image_loader_jpeg_get_format_name;
483 funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;
491 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */