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"
34 #include "image-load-jpeg.h"
37 #include <cstdio> // for FILE and size_t in jpeglib.h
43 #include "image-load.h"
45 #include "jpeg-parser.h"
48 struct ImageLoaderJpeg {
49 ImageLoaderBackendCbAreaUpdated area_updated_cb;
50 ImageLoaderBackendCbSize size_cb;
51 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
56 guint requested_width;
57 guint requested_height;
64 /* error handler data */
65 struct error_handler_data {
66 struct jpeg_error_mgr pub;
67 sigjmp_buf setjmp_buffer;
71 /* explode gray image data from jpeg library into rgb components in pixbuf */
73 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
80 g_return_if_fail (cinfo != nullptr);
81 g_return_if_fail (cinfo->output_components == 1);
82 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
84 /* Expand grey->colour. Expand from the end of the
85 * memory down, so we can use the same buffer.
87 w = cinfo->output_width;
88 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
92 from = lines[i] + w - 1;
93 to = lines[i] + (w - 1) * 3;
94 for (j = w - 1; j >= 0; j--) {
106 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
112 g_return_if_fail (cinfo != nullptr);
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++) {
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 auto 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;
158 fatal_error_handler (j_common_ptr cinfo)
160 struct error_handler_data *errmgr;
161 char buffer[JMSG_LENGTH_MAX];
163 errmgr = reinterpret_cast<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 == nullptr) {
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)
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) {}
228 static boolean fill_input_buffer (j_decompress_ptr cinfo)
230 ERREXIT(cinfo, JERR_INPUT_EMPTY);
233 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
235 auto src = static_cast<struct jpeg_source_mgr*>(cinfo->src);
237 if (static_cast<gulong>(num_bytes) > src->bytes_in_buffer)
239 ERREXIT(cinfo, JERR_INPUT_EOF);
241 else if (num_bytes > 0)
243 src->next_input_byte += static_cast<size_t>(num_bytes);
244 src->bytes_in_buffer -= static_cast<size_t>(num_bytes);
247 static void term_source (j_decompress_ptr) {}
248 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
250 struct jpeg_source_mgr* src;
252 if (cinfo->src == nullptr)
253 { /* first time for this JPEG object? */
254 cinfo->src = static_cast<struct jpeg_source_mgr *>((*cinfo->mem->alloc_small) (
255 reinterpret_cast<j_common_ptr>(cinfo), JPOOL_PERMANENT,
256 sizeof(struct jpeg_source_mgr)));
259 src = static_cast<struct jpeg_source_mgr*>(cinfo->src);
260 src->init_source = init_source;
261 src->fill_input_buffer = fill_input_buffer;
262 src->skip_input_data = skip_input_data;
263 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
264 src->term_source = term_source;
265 src->bytes_in_buffer = nbytes;
266 src->next_input_byte = static_cast<JOCTET*>(buffer);
270 static gboolean image_loader_jpeg_load (gpointer loader, const guchar *buf, gsize count, GError **error)
272 auto lj = static_cast<ImageLoaderJpeg *>(loader);
273 struct jpeg_decompress_struct cinfo;
274 struct jpeg_decompress_struct cinfo2;
278 guchar *stereo_buf2 = nullptr;
279 guint stereo_length = 0;
281 struct error_handler_data jerr;
285 MPOData *mpo = jpeg_get_mpo_data(buf, count);
286 if (mpo && mpo->num_images > 1)
293 for (i = 0; i < mpo->num_images; i++)
295 if (mpo->images[i].type_code == 0x20002)
297 if (mpo->images[i].MPIndividualNum == 1)
301 else if (mpo->images[i].MPIndividualNum > num2)
304 num2 = mpo->images[i].MPIndividualNum;
309 if (idx1 >= 0 && idx2 >= 0)
312 stereo_buf2 = const_cast<unsigned char *>(buf) + mpo->images[idx2].offset;
313 stereo_length = mpo->images[idx2].length;
314 buf = const_cast<unsigned char *>(buf) + mpo->images[idx1].offset;
315 count = mpo->images[idx1].length;
318 jpeg_mpo_data_free(mpo);
320 /* setup error handler */
321 cinfo.err = jpeg_std_error (&jerr.pub);
322 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
323 jerr.pub.error_exit = fatal_error_handler;
324 jerr.pub.output_message = output_message_handler;
329 if (sigsetjmp(jerr.setjmp_buffer, 0))
331 /* If we get here, the JPEG code has signaled an error.
332 * We need to clean up the JPEG object, close the input file, and return.
334 jpeg_destroy_decompress(&cinfo);
335 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
339 jpeg_create_decompress(&cinfo);
341 set_mem_src(&cinfo, const_cast<unsigned char *>(buf), count);
344 jpeg_read_header(&cinfo, TRUE);
348 jpeg_create_decompress(&cinfo2);
349 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
350 jpeg_read_header(&cinfo2, TRUE);
352 if (cinfo.image_width != cinfo2.image_width ||
353 cinfo.image_height != cinfo2.image_height)
355 DEBUG_1("stereo data with different size");
356 jpeg_destroy_decompress(&cinfo2);
363 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
364 lj->requested_height = cinfo.image_height;
365 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
368 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
369 jpeg_calc_output_dimensions(&cinfo);
370 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
371 cinfo.scale_denom /= 2;
375 jpeg_calc_output_dimensions(&cinfo);
378 cinfo2.scale_num = cinfo.scale_num;
379 cinfo2.scale_denom = cinfo.scale_denom;
380 jpeg_calc_output_dimensions(&cinfo2);
381 jpeg_start_decompress(&cinfo2);
385 jpeg_start_decompress(&cinfo);
390 if (cinfo.output_width != cinfo2.output_width ||
391 cinfo.output_height != cinfo2.output_height ||
392 cinfo.out_color_components != cinfo2.out_color_components)
394 DEBUG_1("stereo data with different output size");
395 jpeg_destroy_decompress(&cinfo2);
401 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
402 cinfo.out_color_components == 4 ? TRUE : FALSE,
403 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
407 jpeg_destroy_decompress (&cinfo);
408 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
411 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
412 lj->area_prepared_cb(loader, lj->data);
414 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
415 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
416 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
419 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
421 guint scanline = cinfo.output_scanline;
422 image_loader_jpeg_read_scanline(&cinfo, &dptr, rowstride);
423 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
426 guint scanline = cinfo2.output_scanline;
427 image_loader_jpeg_read_scanline(&cinfo2, &dptr2, rowstride);
428 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
432 jpeg_finish_decompress(&cinfo);
433 jpeg_destroy_decompress(&cinfo);
436 jpeg_finish_decompress(&cinfo);
437 jpeg_destroy_decompress(&cinfo);
443 static void image_loader_jpeg_set_size(gpointer loader, int width, int height)
445 auto lj = static_cast<ImageLoaderJpeg *>(loader);
446 lj->requested_width = width;
447 lj->requested_height = height;
450 static GdkPixbuf* image_loader_jpeg_get_pixbuf(gpointer loader)
452 auto lj = static_cast<ImageLoaderJpeg *>(loader);
456 static gchar* image_loader_jpeg_get_format_name(gpointer)
458 return g_strdup("jpeg");
460 static gchar** image_loader_jpeg_get_format_mime_types(gpointer)
462 static const gchar *mime[] = {"image/jpeg", nullptr};
463 return g_strdupv(const_cast<gchar **>(mime));
466 static gboolean image_loader_jpeg_close(gpointer, GError **)
471 static void image_loader_jpeg_abort(gpointer loader)
473 auto lj = static_cast<ImageLoaderJpeg *>(loader);
477 static void image_loader_jpeg_free(gpointer loader)
479 auto lj = static_cast<ImageLoaderJpeg *>(loader);
480 if (lj->pixbuf) g_object_unref(lj->pixbuf);
485 void image_loader_backend_set_jpeg(ImageLoaderBackend *funcs)
487 funcs->loader_new = image_loader_jpeg_new;
488 funcs->set_size = image_loader_jpeg_set_size;
489 funcs->load = image_loader_jpeg_load;
490 funcs->write = nullptr;
491 funcs->get_pixbuf = image_loader_jpeg_get_pixbuf;
492 funcs->close = image_loader_jpeg_close;
493 funcs->abort = image_loader_jpeg_abort;
494 funcs->free = image_loader_jpeg_free;
496 funcs->get_format_name = image_loader_jpeg_get_format_name;
497 funcs->get_format_mime_types = image_loader_jpeg_get_format_mime_types;
503 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */