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.
27 /** This is a Will Not Fix */
28 #pragma GCC diagnostic ignored "-Wclobbered"
32 #include "image-load.h"
33 #include "image-load-jpeg.h"
34 #include "jpeg-parser.h"
42 struct ImageLoaderJpeg {
43 ImageLoaderBackendCbAreaUpdated area_updated_cb;
44 ImageLoaderBackendCbSize size_cb;
45 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
50 guint requested_width;
51 guint requested_height;
58 /* error handler data */
59 struct error_handler_data {
60 struct jpeg_error_mgr pub;
61 sigjmp_buf setjmp_buffer;
65 /* explode gray image data from jpeg library into rgb components in pixbuf */
67 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
73 g_return_if_fail (cinfo != nullptr);
74 g_return_if_fail (cinfo->output_components == 1);
75 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
77 /* Expand grey->colour. Expand from the end of the
78 * memory down, so we can use the same buffer.
80 w = cinfo->output_width;
81 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
84 from = lines[i] + w - 1;
85 to = lines[i] + (w - 1) * 3;
86 for (j = w - 1; j >= 0; j--) {
98 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
104 g_return_if_fail (cinfo != nullptr);
105 g_return_if_fail (cinfo->output_components == 4);
106 g_return_if_fail (cinfo->out_color_space == JCS_CMYK);
108 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
112 for (j = 0; j < cinfo->output_width; j++) {
118 if (cinfo->saw_Adobe_marker) {
124 p[0] = (255 - k)*(255 - c) / 255;
125 p[1] = (255 - k)*(255 - m) / 255;
126 p[2] = (255 - k)*(255 - y) / 255;
135 static gpointer image_loader_jpeg_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
137 auto loader = g_new0(ImageLoaderJpeg, 1);
139 loader->area_updated_cb = area_updated_cb;
140 loader->size_cb = size_cb;
141 loader->area_prepared_cb = area_prepared_cb;
147 fatal_error_handler (j_common_ptr cinfo)
149 struct error_handler_data *errmgr;
150 char buffer[JMSG_LENGTH_MAX];
152 errmgr = reinterpret_cast<struct error_handler_data *>(cinfo->err);
154 /* Create the message */
155 (* cinfo->err->format_message) (cinfo, buffer);
157 /* broken check for *error == NULL for robustness against
158 * crappy JPEG library
160 if (errmgr->error && *errmgr->error == nullptr) {
161 g_set_error (errmgr->error,
163 cinfo->err->msg_code == JERR_OUT_OF_MEMORY
164 ? GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY
165 : GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
166 _("Error interpreting JPEG image file (%s)"),
170 siglongjmp (errmgr->setjmp_buffer, 1);
172 g_assert_not_reached ();
176 output_message_handler (j_common_ptr)
178 /* This method keeps libjpeg from dumping crap to stderr */
184 void image_loader_jpeg_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
191 for (i = 0; i < cinfo->rec_outbuf_height; i++)
197 jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
199 switch (cinfo->out_color_space)
202 explode_gray_into_buf (cinfo, lines);
208 convert_cmyk_to_rgb (cinfo, lines);
216 static void init_source (j_decompress_ptr) {}
217 static boolean fill_input_buffer (j_decompress_ptr cinfo)
219 ERREXIT(cinfo, JERR_INPUT_EMPTY);
222 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
224 auto src = static_cast<struct jpeg_source_mgr*>(cinfo->src);
226 if (static_cast<gulong>(num_bytes) > src->bytes_in_buffer)
228 ERREXIT(cinfo, JERR_INPUT_EOF);
230 else if (num_bytes > 0)
232 src->next_input_byte += static_cast<size_t>(num_bytes);
233 src->bytes_in_buffer -= static_cast<size_t>(num_bytes);
236 static void term_source (j_decompress_ptr) {}
237 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
239 struct jpeg_source_mgr* src;
241 if (cinfo->src == nullptr)
242 { /* first time for this JPEG object? */
243 cinfo->src = static_cast<struct jpeg_source_mgr *>((*cinfo->mem->alloc_small) (
244 reinterpret_cast<j_common_ptr>(cinfo), JPOOL_PERMANENT,
245 sizeof(struct jpeg_source_mgr)));
248 src = static_cast<struct jpeg_source_mgr*>(cinfo->src);
249 src->init_source = init_source;
250 src->fill_input_buffer = fill_input_buffer;
251 src->skip_input_data = skip_input_data;
252 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
253 src->term_source = term_source;
254 src->bytes_in_buffer = nbytes;
255 src->next_input_byte = static_cast<JOCTET*>(buffer);
259 static gboolean image_loader_jpeg_load (gpointer loader, const guchar *buf, gsize count, GError **error)
261 auto lj = static_cast<ImageLoaderJpeg *>(loader);
262 struct jpeg_decompress_struct cinfo;
263 struct jpeg_decompress_struct cinfo2;
264 guchar *dptr, *dptr2;
266 guchar *stereo_buf2 = nullptr;
267 guint stereo_length = 0;
269 struct error_handler_data jerr;
273 MPOData *mpo = jpeg_get_mpo_data(buf, count);
274 if (mpo && mpo->num_images > 1)
277 gint idx1 = -1, idx2 = -1;
280 for (i = 0; i < mpo->num_images; i++)
282 if (mpo->images[i].type_code == 0x20002)
284 if (mpo->images[i].MPIndividualNum == 1)
288 else if (mpo->images[i].MPIndividualNum > num2)
291 num2 = mpo->images[i].MPIndividualNum;
296 if (idx1 >= 0 && idx2 >= 0)
299 stereo_buf2 = const_cast<unsigned char *>(buf) + mpo->images[idx2].offset;
300 stereo_length = mpo->images[idx2].length;
301 buf = const_cast<unsigned char *>(buf) + mpo->images[idx1].offset;
302 count = mpo->images[idx1].length;
305 jpeg_mpo_data_free(mpo);
307 /* setup error handler */
308 cinfo.err = jpeg_std_error (&jerr.pub);
309 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
310 jerr.pub.error_exit = fatal_error_handler;
311 jerr.pub.output_message = output_message_handler;
316 if (sigsetjmp(jerr.setjmp_buffer, 0))
318 /* If we get here, the JPEG code has signaled an error.
319 * We need to clean up the JPEG object, close the input file, and return.
321 jpeg_destroy_decompress(&cinfo);
322 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
326 jpeg_create_decompress(&cinfo);
328 set_mem_src(&cinfo, const_cast<unsigned char *>(buf), count);
331 jpeg_read_header(&cinfo, TRUE);
335 jpeg_create_decompress(&cinfo2);
336 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
337 jpeg_read_header(&cinfo2, TRUE);
339 if (cinfo.image_width != cinfo2.image_width ||
340 cinfo.image_height != cinfo2.image_height)
342 DEBUG_1("stereo data with different size");
343 jpeg_destroy_decompress(&cinfo2);
350 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
351 lj->requested_height = cinfo.image_height;
352 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
355 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
356 jpeg_calc_output_dimensions(&cinfo);
357 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
358 cinfo.scale_denom /= 2;
362 jpeg_calc_output_dimensions(&cinfo);
365 cinfo2.scale_num = cinfo.scale_num;
366 cinfo2.scale_denom = cinfo.scale_denom;
367 jpeg_calc_output_dimensions(&cinfo2);
368 jpeg_start_decompress(&cinfo2);
372 jpeg_start_decompress(&cinfo);
377 if (cinfo.output_width != cinfo2.output_width ||
378 cinfo.output_height != cinfo2.output_height ||
379 cinfo.out_color_components != cinfo2.out_color_components)
381 DEBUG_1("stereo data with different output size");
382 jpeg_destroy_decompress(&cinfo2);
388 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
389 cinfo.out_color_components == 4 ? TRUE : FALSE,
390 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
394 jpeg_destroy_decompress (&cinfo);
395 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
398 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
399 lj->area_prepared_cb(loader, lj->data);
401 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
402 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
403 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
406 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
408 guint scanline = cinfo.output_scanline;
409 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
410 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
413 guint scanline = cinfo2.output_scanline;
414 image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
415 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
419 jpeg_finish_decompress(&cinfo);
420 jpeg_destroy_decompress(&cinfo);
423 jpeg_finish_decompress(&cinfo);
424 jpeg_destroy_decompress(&cinfo);
430 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
432 auto lj = static_cast<ImageLoaderJpeg *>(loader);
433 lj->requested_width = width;
434 lj->requested_height = height;
437 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
439 auto lj = static_cast<ImageLoaderJpeg *>(loader);
443 static gchar* image_loader_jpeg_get_format_name(gpointer)
445 return g_strdup("jpeg");
447 static gchar** image_loader_jpeg_get_format_mime_types(gpointer)
449 static const gchar *mime[] = {"image/jpeg", nullptr};
450 return g_strdupv(const_cast<gchar **>(mime));
453 static gboolean image_loader_jpeg_close(gpointer, GError **)
458 static void image_loader_jpeg_abort(gpointer loader)
460 auto lj = static_cast<ImageLoaderJpeg *>(loader);
464 static void image_loader_jpeg_free(gpointer loader)
466 auto lj = static_cast<ImageLoaderJpeg *>(loader);
467 if (lj->pixbuf) g_object_unref(lj->pixbuf);
472 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
474 funcs->loader_new = image_loader_jpeg_new;
475 funcs->set_size = image_loader_jpeg_set_size;
476 funcs->load = image_loader_jpeg_load;
477 funcs->write = nullptr;
478 funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
479 funcs->close = image_loader_jpeg_close;
480 funcs->abort = image_loader_jpeg_abort;
481 funcs->free = image_loader_jpeg_free;
483 funcs->get_format_name = image_loader_jpeg_get_format_name;
484 funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;
492 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */