2 * Copyright (C) 2020 The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /** This is a Will Not Fix */
22 #pragma GCC diagnostic ignored "-Wclobbered"
24 /** @FIXME This is just a copy of image-load-jpeg.cc, with an adjusted
25 * start address for a .cr3 file
29 #include "image-load.h"
30 #include "image-load-cr3.h"
31 #include "jpeg-parser.h"
39 struct ImageLoaderJpeg {
40 ImageLoaderBackendCbAreaUpdated area_updated_cb;
41 ImageLoaderBackendCbSize size_cb;
42 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
47 guint requested_width;
48 guint requested_height;
55 /* error handler data */
56 struct error_handler_data {
57 struct jpeg_error_mgr pub;
58 sigjmp_buf setjmp_buffer;
62 /* explode gray image data from jpeg library into rgb components in pixbuf */
64 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
70 g_return_if_fail (cinfo != nullptr);
71 g_return_if_fail (cinfo->output_components == 1);
72 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
74 /* Expand grey->colour. Expand from the end of the
75 * memory down, so we can use the same buffer.
77 w = cinfo->output_width;
78 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
81 from = lines[i] + w - 1;
82 to = lines[i] + (w - 1) * 3;
83 for (j = w - 1; j >= 0; j--) {
95 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
101 g_return_if_fail (cinfo != nullptr);
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_cr3_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
134 auto 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;
144 fatal_error_handler (j_common_ptr cinfo)
146 struct error_handler_data *errmgr;
147 char buffer[JMSG_LENGTH_MAX];
149 errmgr = reinterpret_cast<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 == nullptr) {
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)
175 /* This method keeps libjpeg from dumping crap to stderr */
181 static void image_loader_cr3_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) {}
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 auto src = static_cast<struct jpeg_source_mgr*>(cinfo->src);
223 if (static_cast<gulong>(num_bytes) > src->bytes_in_buffer)
225 ERREXIT(cinfo, JERR_INPUT_EOF);
227 else if (num_bytes > 0)
229 src->next_input_byte += static_cast<size_t>(num_bytes);
230 src->bytes_in_buffer -= static_cast<size_t>(num_bytes);
233 static void term_source (j_decompress_ptr) {}
234 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
236 struct jpeg_source_mgr* src;
238 if (cinfo->src == nullptr)
239 { /* first time for this JPEG object? */
240 cinfo->src = static_cast<struct jpeg_source_mgr *>((*cinfo->mem->alloc_small) (
241 reinterpret_cast<j_common_ptr>(cinfo), JPOOL_PERMANENT,
242 sizeof(struct jpeg_source_mgr)));
245 src = static_cast<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 = static_cast<JOCTET*>(buffer);
256 static gboolean image_loader_cr3_load (gpointer loader, const guchar *buf, gsize count, GError **error)
258 auto lj = static_cast<ImageLoaderJpeg *>(loader);
259 struct jpeg_decompress_struct cinfo;
260 struct jpeg_decompress_struct cinfo2;
261 guchar *dptr, *dptr2;
263 guchar *stereo_buf2 = nullptr;
264 guint stereo_length = 0;
266 struct error_handler_data jerr;
268 /** @FIXME Just start search at where full size jpeg should be,
269 * / then search through the file looking for a jpeg end-marker
271 gboolean found = FALSE;
276 while (n < count - 4 && !found)
278 if (memcmp(&buf[n], "mdat", 4) == 0)
280 if (memcmp(&buf[n + 12], "\xFF\xD8", 2) == 0)
285 if (memcmp(&buf[n + 12 + i], "\xFF\xD9", 2) == 0)
309 buf = const_cast<unsigned char *>(buf) + n + 12;
313 MPOData *mpo = jpeg_get_mpo_data(buf, count);
314 if (mpo && mpo->num_images > 1)
317 gint idx1 = -1, idx2 = -1;
320 for (i = 0; i < mpo->num_images; i++)
322 if (mpo->images[i].type_code == 0x20002)
324 if (mpo->images[i].MPIndividualNum == 1)
328 else if (mpo->images[i].MPIndividualNum > num2)
331 num2 = mpo->images[i].MPIndividualNum;
336 if (idx1 >= 0 && idx2 >= 0)
339 stereo_buf2 = const_cast<unsigned char *>(buf) + mpo->images[idx2].offset;
340 stereo_length = mpo->images[idx2].length;
341 buf = const_cast<unsigned char *>(buf) + mpo->images[idx1].offset;
342 count = mpo->images[idx1].length;
345 jpeg_mpo_data_free(mpo);
347 /* setup error handler */
348 cinfo.err = jpeg_std_error (&jerr.pub);
349 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
350 jerr.pub.error_exit = fatal_error_handler;
351 jerr.pub.output_message = output_message_handler;
356 if (sigsetjmp(jerr.setjmp_buffer, 0))
358 /* If we get here, the JPEG code has signaled an error.
359 * We need to clean up the JPEG object, close the input file, and return.
361 jpeg_destroy_decompress(&cinfo);
362 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
366 jpeg_create_decompress(&cinfo);
368 set_mem_src(&cinfo, const_cast<unsigned char *>(buf), count);
371 jpeg_read_header(&cinfo, TRUE);
375 jpeg_create_decompress(&cinfo2);
376 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
377 jpeg_read_header(&cinfo2, TRUE);
379 if (cinfo.image_width != cinfo2.image_width ||
380 cinfo.image_height != cinfo2.image_height)
382 DEBUG_1("stereo data with different size");
383 jpeg_destroy_decompress(&cinfo2);
390 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
391 lj->requested_height = cinfo.image_height;
392 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
395 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
396 jpeg_calc_output_dimensions(&cinfo);
397 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
398 cinfo.scale_denom /= 2;
402 jpeg_calc_output_dimensions(&cinfo);
405 cinfo2.scale_num = cinfo.scale_num;
406 cinfo2.scale_denom = cinfo.scale_denom;
407 jpeg_calc_output_dimensions(&cinfo2);
408 jpeg_start_decompress(&cinfo2);
412 jpeg_start_decompress(&cinfo);
417 if (cinfo.output_width != cinfo2.output_width ||
418 cinfo.output_height != cinfo2.output_height ||
419 cinfo.out_color_components != cinfo2.out_color_components)
421 DEBUG_1("stereo data with different output size");
422 jpeg_destroy_decompress(&cinfo2);
428 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
429 cinfo.out_color_components == 4 ? TRUE : FALSE,
430 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
434 jpeg_destroy_decompress (&cinfo);
435 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
438 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
439 lj->area_prepared_cb(loader, lj->data);
441 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
442 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
443 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
446 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
448 guint scanline = cinfo.output_scanline;
449 image_loader_cr3_read_scanline(&cinfo, &dptr, rowstride);
450 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
453 guint scanline = cinfo2.output_scanline;
454 image_loader_cr3_read_scanline(&cinfo2, &dptr2, rowstride);
455 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
459 jpeg_finish_decompress(&cinfo);
460 jpeg_destroy_decompress(&cinfo);
463 jpeg_finish_decompress(&cinfo);
464 jpeg_destroy_decompress(&cinfo);
470 static void image_loader_cr3_set_size(gpointer loader, int width, int height)
472 auto lj = static_cast<ImageLoaderJpeg *>(loader);
473 lj->requested_width = width;
474 lj->requested_height = height;
477 static GdkPixbuf* image_loader_cr3_get_pixbuf(gpointer loader)
479 auto lj = static_cast<ImageLoaderJpeg *>(loader);
483 static gchar* image_loader_cr3_get_format_name(gpointer)
485 return g_strdup("cr3");
487 static gchar** image_loader_cr3_get_format_mime_types(gpointer)
489 static const gchar *mime[] = {"image/x-canon-cr3", nullptr};
490 return g_strdupv(const_cast<gchar **>(mime));
493 static gboolean image_loader_cr3_close(gpointer, GError **)
498 static void image_loader_cr3_abort(gpointer loader)
500 auto lj = static_cast<ImageLoaderJpeg *>(loader);
504 static void image_loader_cr3_free(gpointer loader)
506 auto lj = static_cast<ImageLoaderJpeg *>(loader);
507 if (lj->pixbuf) g_object_unref(lj->pixbuf);
512 void image_loader_backend_set_cr3(ImageLoaderBackend *funcs)
514 funcs->loader_new = image_loader_cr3_new;
515 funcs->set_size = image_loader_cr3_set_size;
516 funcs->load = image_loader_cr3_load;
517 funcs->write = nullptr;
518 funcs->get_pixbuf = image_loader_cr3_get_pixbuf;
519 funcs->close = image_loader_cr3_close;
520 funcs->abort = image_loader_cr3_abort;
521 funcs->free = image_loader_cr3_free;
523 funcs->get_format_name = image_loader_cr3_get_format_name;
524 funcs->get_format_mime_types = image_loader_cr3_get_format_mime_types;
532 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */