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 /** @FIXME This is just a copy of image_load_jpeg.c, with an adjusted
22 * start address for a .cr3 file
26 #include "image-load.h"
27 #include "image_load_cr3.h"
28 #include "jpeg_parser.h"
36 typedef struct _ImageLoaderJpeg ImageLoaderJpeg;
37 struct _ImageLoaderJpeg {
38 ImageLoaderBackendCbAreaUpdated area_updated_cb;
39 ImageLoaderBackendCbSize size_cb;
40 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
45 guint requested_width;
46 guint requested_height;
53 /* error handler data */
54 struct error_handler_data {
55 struct jpeg_error_mgr pub;
56 sigjmp_buf setjmp_buffer;
60 /* explode gray image data from jpeg library into rgb components in pixbuf */
62 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
68 g_return_if_fail (cinfo != NULL);
69 g_return_if_fail (cinfo->output_components == 1);
70 g_return_if_fail (cinfo->out_color_space == JCS_GRAYSCALE);
72 /* Expand grey->colour. Expand from the end of the
73 * memory down, so we can use the same buffer.
75 w = cinfo->output_width;
76 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
79 from = lines[i] + w - 1;
80 to = lines[i] + (w - 1) * 3;
81 for (j = w - 1; j >= 0; j--) {
93 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
98 g_return_if_fail (cinfo != NULL);
99 g_return_if_fail (cinfo->output_components == 4);
100 g_return_if_fail (cinfo->out_color_space == JCS_CMYK);
102 for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
106 for (j = 0; j < cinfo->output_width; j++) {
112 if (cinfo->saw_Adobe_marker) {
118 p[0] = (255 - k)*(255 - c) / 255;
119 p[1] = (255 - k)*(255 - m) / 255;
120 p[2] = (255 - k)*(255 - y) / 255;
129 static gpointer image_loader_cr3_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
131 ImageLoaderJpeg *loader = g_new0(ImageLoaderJpeg, 1);
133 loader->area_updated_cb = area_updated_cb;
134 loader->size_cb = size_cb;
135 loader->area_prepared_cb = area_prepared_cb;
137 return (gpointer) loader;
141 fatal_error_handler (j_common_ptr cinfo)
143 struct error_handler_data *errmgr;
144 char buffer[JMSG_LENGTH_MAX];
146 errmgr = (struct error_handler_data *) cinfo->err;
148 /* Create the message */
149 (* cinfo->err->format_message) (cinfo, buffer);
151 /* broken check for *error == NULL for robustness against
152 * crappy JPEG library
154 if (errmgr->error && *errmgr->error == NULL) {
155 g_set_error (errmgr->error,
157 cinfo->err->msg_code == JERR_OUT_OF_MEMORY
158 ? GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY
159 : GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
160 _("Error interpreting JPEG image file (%s)"),
164 siglongjmp (errmgr->setjmp_buffer, 1);
166 g_assert_not_reached ();
170 output_message_handler (j_common_ptr cinfo)
172 /* This method keeps libjpeg from dumping crap to stderr */
178 static void image_loader_cr3_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
185 for (i = 0; i < cinfo->rec_outbuf_height; i++)
191 jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
193 switch (cinfo->out_color_space)
196 explode_gray_into_buf (cinfo, lines);
202 convert_cmyk_to_rgb (cinfo, lines);
210 static void init_source (j_decompress_ptr cinfo) {}
211 static boolean fill_input_buffer (j_decompress_ptr cinfo)
213 ERREXIT(cinfo, JERR_INPUT_EMPTY);
216 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
218 struct jpeg_source_mgr* src = (struct jpeg_source_mgr*) cinfo->src;
220 if (num_bytes > src->bytes_in_buffer)
222 ERREXIT(cinfo, JERR_INPUT_EOF);
224 else if (num_bytes > 0)
226 src->next_input_byte += (size_t) num_bytes;
227 src->bytes_in_buffer -= (size_t) num_bytes;
230 static void term_source (j_decompress_ptr cinfo) {}
231 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
233 struct jpeg_source_mgr* src;
235 if (cinfo->src == NULL)
236 { /* first time for this JPEG object? */
237 cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small) (
238 (j_common_ptr) cinfo, JPOOL_PERMANENT,
239 sizeof(struct jpeg_source_mgr));
242 src = (struct jpeg_source_mgr*) cinfo->src;
243 src->init_source = init_source;
244 src->fill_input_buffer = fill_input_buffer;
245 src->skip_input_data = skip_input_data;
246 src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
247 src->term_source = term_source;
248 src->bytes_in_buffer = nbytes;
249 src->next_input_byte = (JOCTET*)buffer;
253 static gboolean image_loader_cr3_load (gpointer loader, const guchar *buf, gsize count, GError **error)
255 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
256 struct jpeg_decompress_struct cinfo;
257 struct jpeg_decompress_struct cinfo2;
258 guchar *dptr, *dptr2;
260 guchar *stereo_buf2 = NULL;
261 guint stereo_length = 0;
263 struct error_handler_data jerr;
265 /** @FIXME Just start search at where full size jpeg should be,
266 * / then search through the file looking for a jpeg end-marker
268 gboolean found = FALSE;
273 while (n < count - 4 && !found)
275 if (memcmp(&buf[n], "mdat", 4) == 0)
277 if (memcmp(&buf[n + 12], "\xFF\xD8", 2) == 0)
282 if (memcmp(&buf[n + 12 + i], "\xFF\xD9", 2) == 0)
306 buf = (unsigned char *)buf + n + 12;
310 MPOData *mpo = jpeg_get_mpo_data(buf, count);
311 if (mpo && mpo->num_images > 1)
314 gint idx1 = -1, idx2 = -1;
317 for (i = 0; i < mpo->num_images; i++)
319 if (mpo->images[i].type_code == 0x20002)
321 if (mpo->images[i].MPIndividualNum == 1)
325 else if (mpo->images[i].MPIndividualNum > num2)
328 num2 = mpo->images[i].MPIndividualNum;
333 if (idx1 >= 0 && idx2 >= 0)
336 stereo_buf2 = (unsigned char *)buf + mpo->images[idx2].offset;
337 stereo_length = mpo->images[idx2].length;
338 buf = (unsigned char *)buf + mpo->images[idx1].offset;
339 count = mpo->images[idx1].length;
342 jpeg_mpo_data_free(mpo);
344 /* setup error handler */
345 cinfo.err = jpeg_std_error (&jerr.pub);
346 if (lj->stereo) cinfo2.err = jpeg_std_error (&jerr.pub);
347 jerr.pub.error_exit = fatal_error_handler;
348 jerr.pub.output_message = output_message_handler;
353 if (setjmp(jerr.setjmp_buffer))
355 /* If we get here, the JPEG code has signaled an error.
356 * We need to clean up the JPEG object, close the input file, and return.
358 jpeg_destroy_decompress(&cinfo);
359 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
363 jpeg_create_decompress(&cinfo);
365 set_mem_src(&cinfo, (unsigned char *)buf, count);
368 jpeg_read_header(&cinfo, TRUE);
372 jpeg_create_decompress(&cinfo2);
373 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
374 jpeg_read_header(&cinfo2, TRUE);
376 if (cinfo.image_width != cinfo2.image_width ||
377 cinfo.image_height != cinfo2.image_height)
379 DEBUG_1("stereo data with different size");
380 jpeg_destroy_decompress(&cinfo2);
387 lj->requested_width = lj->stereo ? cinfo.image_width * 2: cinfo.image_width;
388 lj->requested_height = cinfo.image_height;
389 lj->size_cb(loader, lj->requested_width, lj->requested_height, lj->data);
392 for (cinfo.scale_denom = 2; cinfo.scale_denom <= 8; cinfo.scale_denom *= 2) {
393 jpeg_calc_output_dimensions(&cinfo);
394 if (cinfo.output_width < (lj->stereo ? lj->requested_width / 2 : lj->requested_width) || cinfo.output_height < lj->requested_height) {
395 cinfo.scale_denom /= 2;
399 jpeg_calc_output_dimensions(&cinfo);
402 cinfo2.scale_num = cinfo.scale_num;
403 cinfo2.scale_denom = cinfo.scale_denom;
404 jpeg_calc_output_dimensions(&cinfo2);
405 jpeg_start_decompress(&cinfo2);
409 jpeg_start_decompress(&cinfo);
414 if (cinfo.output_width != cinfo2.output_width ||
415 cinfo.output_height != cinfo2.output_height ||
416 cinfo.out_color_components != cinfo2.out_color_components)
418 DEBUG_1("stereo data with different output size");
419 jpeg_destroy_decompress(&cinfo2);
425 lj->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
426 cinfo.out_color_components == 4 ? TRUE : FALSE,
427 8, lj->stereo ? cinfo.output_width * 2: cinfo.output_width, cinfo.output_height);
431 jpeg_destroy_decompress (&cinfo);
432 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
435 if (lj->stereo) g_object_set_data(G_OBJECT(lj->pixbuf), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
436 lj->area_prepared_cb(loader, lj->data);
438 rowstride = gdk_pixbuf_get_rowstride(lj->pixbuf);
439 dptr = gdk_pixbuf_get_pixels(lj->pixbuf);
440 dptr2 = gdk_pixbuf_get_pixels(lj->pixbuf) + ((cinfo.out_color_components == 4) ? 4 * cinfo.output_width : 3 * cinfo.output_width);
443 while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
445 guint scanline = cinfo.output_scanline;
446 image_loader_cr3_read_scanline(&cinfo, &dptr, rowstride);
447 lj->area_updated_cb(loader, 0, scanline, cinfo.output_width, cinfo.rec_outbuf_height, lj->data);
450 guint scanline = cinfo2.output_scanline;
451 image_loader_cr3_read_scanline(&cinfo2, &dptr2, rowstride);
452 lj->area_updated_cb(loader, cinfo.output_width, scanline, cinfo2.output_width, cinfo2.rec_outbuf_height, lj->data);
456 jpeg_finish_decompress(&cinfo);
457 jpeg_destroy_decompress(&cinfo);
460 jpeg_finish_decompress(&cinfo);
461 jpeg_destroy_decompress(&cinfo);
467 static void image_loader_cr3_set_size(gpointer loader, int width, int height)
469 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
470 lj->requested_width = width;
471 lj->requested_height = height;
474 static GdkPixbuf* image_loader_cr3_get_pixbuf(gpointer loader)
476 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
480 static gchar* image_loader_cr3_get_format_name(gpointer loader)
482 return g_strdup("cr3");
484 static gchar** image_loader_cr3_get_format_mime_types(gpointer loader)
486 static gchar *mime[] = {"image/x-canon-cr3", NULL};
487 return g_strdupv(mime);
490 static gboolean image_loader_cr3_close(gpointer loader, GError **error)
495 static void image_loader_cr3_abort(gpointer loader)
497 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
501 static void image_loader_cr3_free(gpointer loader)
503 ImageLoaderJpeg *lj = (ImageLoaderJpeg *) loader;
504 if (lj->pixbuf) g_object_unref(lj->pixbuf);
509 void image_loader_backend_set_cr3(ImageLoaderBackend *funcs)
511 funcs->loader_new = image_loader_cr3_new;
512 funcs->set_size = image_loader_cr3_set_size;
513 funcs->load = image_loader_cr3_load;
515 funcs->get_pixbuf = image_loader_cr3_get_pixbuf;
516 funcs->close = image_loader_cr3_close;
517 funcs->abort = image_loader_cr3_abort;
518 funcs->free = image_loader_cr3_free;
520 funcs->get_format_name = image_loader_cr3_get_format_name;
521 funcs->get_format_mime_types = image_loader_cr3_get_format_mime_types;
529 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */