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;
270 MPOData *mpo = jpeg_get_mpo_data(buf, count);
271 if (mpo && mpo->num_images > 1)
274 gint idx1 = -1, idx2 = -1;
277 for (i = 0; i < mpo->num_images; i++)
279 if (mpo->images[i].type_code == 0x20002)
281 if (mpo->images[i].MPIndividualNum == 1)
285 else if (mpo->images[i].MPIndividualNum > num2)
288 num2 = mpo->images[i].MPIndividualNum;
293 if (idx1 >= 0 && idx2 >= 0)
296 stereo_buf2 = (unsigned char *)buf + mpo->images[idx2].offset;
297 stereo_length = mpo->images[idx2].length;
298 buf = (unsigned char *)buf + mpo->images[idx1].offset;
299 count = mpo->images[idx1].length;
302 jpeg_mpo_data_free(mpo);
304 /* setup error handler */
305 cinfo.err = jpeg_std_error (&jerr.pub);
306 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
307 jerr.pub.error_exit = fatal_error_handler;
308 jerr.pub.output_message = output_message_handler;
313 if (setjmp(jerr.setjmp_buffer))
315 /* If we get here, the JPEG code has signaled an error.
316 * We need to clean up the JPEG object, close the input file, and return.
318 jpeg_destroy_decompress(&cinfo);
319 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
323 jpeg_create_decompress(&cinfo);
325 set_mem_src(&cinfo, (unsigned char *)buf, count);
328 jpeg_read_header(&cinfo, TRUE);
332 jpeg_create_decompress(&cinfo2);
333 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
334 jpeg_read_header(&cinfo2, TRUE);
336 if (cinfo.image_width != cinfo2.image_width ||
337 cinfo.image_height != cinfo2.image_height)
339 DEBUG_1("stereo data with different size");
340 jpeg_destroy_decompress(&cinfo2);
347 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
348 lj->requested_height = cinfo.image_height;
349 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
352 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
353 jpeg_calc_output_dimensions(&cinfo);
354 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
355 cinfo.scale_denom /= 2;
359 jpeg_calc_output_dimensions(&cinfo);
362 cinfo2.scale_num = cinfo.scale_num;
363 cinfo2.scale_denom = cinfo.scale_denom;
364 jpeg_calc_output_dimensions(&cinfo2);
365 jpeg_start_decompress(&cinfo2);
369 jpeg_start_decompress(&cinfo);
374 if (cinfo.output_width != cinfo2.output_width ||
375 cinfo.output_height != cinfo2.output_height ||
376 cinfo.out_color_components != cinfo2.out_color_components)
378 DEBUG_1("stereo data with different output size");
379 jpeg_destroy_decompress(&cinfo2);
385 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
386 cinfo.out_color_components == 4 ? TRUE : FALSE,
387 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
391 jpeg_destroy_decompress (&cinfo);
392 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
395 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
396 lj->area_prepared_cb(loader, lj->data);
398 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
399 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
400 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
403 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
405 guint scanline = cinfo.output_scanline;
406 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
407 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
410 guint scanline = cinfo2.output_scanline;
411 image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
412 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
416 jpeg_finish_decompress(&cinfo);
417 jpeg_destroy_decompress(&cinfo);
420 jpeg_finish_decompress(&cinfo);
421 jpeg_destroy_decompress(&cinfo);
427 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
429 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
430 lj->requested_width = width;
431 lj->requested_height = height;
434 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
436 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
440 static gchar* image_loader_jpeg_get_format_name(gpointer loader)
442 return g_strdup("jpeg");
444 static gchar** image_loader_jpeg_get_format_mime_types(gpointer loader)
446 static gchar *mime[] = {"image/jpeg", NULL};
447 return g_strdupv(mime);
450 static gboolean image_loader_jpeg_close(gpointer loader, GError **error)
455 static void image_loader_jpeg_abort(gpointer loader)
457 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
461 static void image_loader_jpeg_free(gpointer loader)
463 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
464 if (lj->pixbuf) g_object_unref(lj->pixbuf);
469 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
471 funcs->loader_new = image_loader_jpeg_new;
472 funcs->set_size = image_loader_jpeg_set_size;
473 funcs->load = image_loader_jpeg_load;
475 funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
476 funcs->close = image_loader_jpeg_close;
477 funcs->abort = image_loader_jpeg_abort;
478 funcs->free = image_loader_jpeg_free;
480 funcs->get_format_name = image_loader_jpeg_get_format_name;
481 funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;
489 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */