clang-tidy: readability-non-const-parameter
[geeqie.git] / src / image-load-cr3.cc
1 /*
2  * Copyright (C) 2020 The Geeqie Team
3  *
4  * Authors: Colin Clark
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 /** This is a Will Not Fix */
22 #pragma GCC diagnostic ignored "-Wclobbered"
23
24 /** @FIXME This is just a copy of image-load-jpeg.cc, with an adjusted
25  * start address for a .cr3 file
26  */
27 #include "main.h"
28
29 #include "image-load.h"
30 #include "image-load-cr3.h"
31 #include "jpeg-parser.h"
32
33 #ifdef HAVE_JPEG
34
35 #include <csetjmp>
36 #include <jerror.h>
37 #include <jpeglib.h>
38
39 struct ImageLoaderJpeg {
40         ImageLoaderBackendCbAreaUpdated area_updated_cb;
41         ImageLoaderBackendCbSize size_cb;
42         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
43
44         gpointer data;
45
46         GdkPixbuf *pixbuf;
47         guint requested_width;
48         guint requested_height;
49
50         gboolean abort;
51         gboolean stereo;
52
53 };
54
55 /* error handler data */
56 struct error_handler_data {
57         struct jpeg_error_mgr pub;
58         sigjmp_buf setjmp_buffer;
59         GError **error;
60 };
61
62 /* explode gray image data from jpeg library into rgb components in pixbuf */
63 static void
64 explode_gray_into_buf (struct jpeg_decompress_struct *cinfo,
65                        guchar **lines)
66 {
67         gint i, j;
68         guint w;
69
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);
73
74         /* Expand grey->colour.  Expand from the end of the
75          * memory down, so we can use the same buffer.
76          */
77         w = cinfo->output_width;
78         for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
79                 guchar *from, *to;
80
81                 from = lines[i] + w - 1;
82                 to = lines[i] + (w - 1) * 3;
83                 for (j = w - 1; j >= 0; j--) {
84                         to[0] = from[0];
85                         to[1] = from[0];
86                         to[2] = from[0];
87                         to -= 3;
88                         from--;
89                 }
90         }
91 }
92
93
94 static void
95 convert_cmyk_to_rgb (struct jpeg_decompress_struct *cinfo,
96                      guchar **lines)
97 {
98         gint i;
99         guint j;
100
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);
104
105         for (i = cinfo->rec_outbuf_height - 1; i >= 0; i--) {
106                 guchar *p;
107
108                 p = lines[i];
109                 for (j = 0; j < cinfo->output_width; j++) {
110                         int c, m, y, k;
111                         c = p[0];
112                         m = p[1];
113                         y = p[2];
114                         k = p[3];
115                         if (cinfo->saw_Adobe_marker) {
116                                 p[0] = k*c / 255;
117                                 p[1] = k*m / 255;
118                                 p[2] = k*y / 255;
119                         }
120                         else {
121                                 p[0] = (255 - k)*(255 - c) / 255;
122                                 p[1] = (255 - k)*(255 - m) / 255;
123                                 p[2] = (255 - k)*(255 - y) / 255;
124                         }
125                         p[3] = 255;
126                         p += 4;
127                 }
128         }
129 }
130
131
132 static gpointer image_loader_cr3_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
133 {
134         auto loader = g_new0(ImageLoaderJpeg, 1);
135
136         loader->area_updated_cb = area_updated_cb;
137         loader->size_cb = size_cb;
138         loader->area_prepared_cb = area_prepared_cb;
139         loader->data = data;
140         return loader;
141 }
142
143 static void
144 fatal_error_handler (j_common_ptr cinfo)
145 {
146         struct error_handler_data *errmgr;
147         char buffer[JMSG_LENGTH_MAX];
148
149         errmgr = reinterpret_cast<struct error_handler_data *>(cinfo->err);
150
151         /* Create the message */
152         (* cinfo->err->format_message) (cinfo, buffer);
153
154         /* broken check for *error == NULL for robustness against
155          * crappy JPEG library
156          */
157         if (errmgr->error && *errmgr->error == nullptr) {
158                 g_set_error (errmgr->error,
159                              GDK_PIXBUF_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)"),
164                              buffer);
165         }
166
167         siglongjmp (errmgr->setjmp_buffer, 1);
168
169         g_assert_not_reached ();
170 }
171
172 static void
173 output_message_handler (j_common_ptr)
174 {
175   /* This method keeps libjpeg from dumping crap to stderr */
176
177   /* do nothing */
178 }
179
180
181 static void image_loader_cr3_read_scanline(struct jpeg_decompress_struct *cinfo, guchar **dptr, guint rowstride)
182 {
183         guchar *lines[4];
184         guchar **lptr;
185         gint i;
186
187         lptr = lines;
188         for (i = 0; i < cinfo->rec_outbuf_height; i++)
189                 {
190                 *lptr++ = *dptr;
191                 *dptr += rowstride;
192                 }
193
194         jpeg_read_scanlines (cinfo, lines, cinfo->rec_outbuf_height);
195
196         switch (cinfo->out_color_space)
197                 {
198                     case JCS_GRAYSCALE:
199                       explode_gray_into_buf (cinfo, lines);
200                       break;
201                     case JCS_RGB:
202                       /* do nothing */
203                       break;
204                     case JCS_CMYK:
205                       convert_cmyk_to_rgb (cinfo, lines);
206                       break;
207                     default:
208                       break;
209                 }
210 }
211
212
213 static void init_source (j_decompress_ptr) {}
214 static boolean fill_input_buffer (j_decompress_ptr cinfo)
215 {
216         ERREXIT(cinfo, JERR_INPUT_EMPTY);
217         return TRUE;
218 }
219 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
220 {
221         auto src = static_cast<struct jpeg_source_mgr*>(cinfo->src);
222
223         if (static_cast<gulong>(num_bytes) > src->bytes_in_buffer)
224                 {
225                 ERREXIT(cinfo, JERR_INPUT_EOF);
226                 }
227         else if (num_bytes > 0)
228                 {
229                 src->next_input_byte += static_cast<size_t>(num_bytes);
230                 src->bytes_in_buffer -= static_cast<size_t>(num_bytes);
231                 }
232 }
233 static void term_source (j_decompress_ptr) {}
234 static void set_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
235 {
236         struct jpeg_source_mgr* src;
237
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)));
243                 }
244
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);
253 }
254
255
256 static gboolean image_loader_cr3_load (gpointer loader, const guchar *buf, gsize count, GError **error)
257 {
258         auto lj = static_cast<ImageLoaderJpeg *>(loader);
259         struct jpeg_decompress_struct cinfo;
260         struct jpeg_decompress_struct cinfo2;
261         guchar *dptr, *dptr2;
262         guint rowstride;
263         guchar *stereo_buf2 = nullptr;
264         guint stereo_length = 0;
265
266         struct error_handler_data jerr;
267
268 /** @FIXME Just start search at where full size jpeg should be,
269  * / then search through the file looking for a jpeg end-marker
270  */
271         gboolean found = FALSE;
272         gint i;
273         guint n;
274
275         n = 0;
276         while (n < count - 4 && !found)
277                 {
278                 if (memcmp(&buf[n], "mdat", 4) == 0)
279                         {
280                         if (memcmp(&buf[n + 12], "\xFF\xD8", 2) == 0)
281                                 {
282                                 i = 0;
283                                 while (!found )
284                                         {
285                                         if (memcmp(&buf[n + 12 + i], "\xFF\xD9", 2) == 0)
286                                                 {
287                                                 found = TRUE;
288                                                 }
289                                         i++;
290                                         }
291                                 }
292                         else
293                                 {
294                                 break;
295                                 }
296                         }
297                 else
298                         {
299                         n++;
300                         }
301                 }
302
303         if (!found)
304                 {
305                 return FALSE;
306                 }
307
308         count = i;
309         buf = const_cast<unsigned char *>(buf) + n + 12;
310
311         lj->stereo = FALSE;
312
313         MPOData *mpo = jpeg_get_mpo_data(buf, count);
314         if (mpo && mpo->num_images > 1)
315                 {
316                 guint i;
317                 gint idx1 = -1, idx2 = -1;
318                 guint num2 = 1;
319
320                 for (i = 0; i < mpo->num_images; i++)
321                         {
322                         if (mpo->images[i].type_code == 0x20002)
323                                 {
324                                 if (mpo->images[i].MPIndividualNum == 1)
325                                         {
326                                         idx1 = i;
327                                         }
328                                 else if (mpo->images[i].MPIndividualNum > num2)
329                                         {
330                                         idx2 = i;
331                                         num2 = mpo->images[i].MPIndividualNum;
332                                         }
333                                 }
334                         }
335
336                 if (idx1 >= 0 && idx2 >= 0)
337                         {
338                         lj->stereo = TRUE;
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;
343                         }
344                 }
345         jpeg_mpo_data_free(mpo);
346
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;
352
353         jerr.error = error;
354
355
356         if (sigsetjmp(jerr.setjmp_buffer, 0))
357                 {
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.
360                 */
361                 jpeg_destroy_decompress(&cinfo);
362                 if (lj->stereo) jpeg_destroy_decompress(&cinfo2);
363                 return FALSE;
364                 }
365
366         jpeg_create_decompress(&cinfo);
367
368         set_mem_src(&cinfo, const_cast<unsigned char *>(buf), count);
369
370
371         jpeg_read_header(&cinfo, TRUE);
372
373         if (lj->stereo)
374                 {
375                 jpeg_create_decompress(&cinfo2);
376                 set_mem_src(&cinfo2, stereo_buf2, stereo_length);
377                 jpeg_read_header(&cinfo2, TRUE);
378
379                 if (cinfo.image_width != cinfo2.image_width ||
380                     cinfo.image_height != cinfo2.image_height)
381                         {
382                         DEBUG_1("stereo data with different size");
383                         jpeg_destroy_decompress(&cinfo2);
384                         lj->stereo = FALSE;
385                         }
386                 }
387
388
389
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);
393
394         cinfo.scale_num = 1;
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;
399                         break;
400                 }
401         }
402         jpeg_calc_output_dimensions(&cinfo);
403         if (lj->stereo)
404                 {
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);
409                 }
410
411
412         jpeg_start_decompress(&cinfo);
413
414
415         if (lj->stereo)
416                 {
417                 if (cinfo.output_width != cinfo2.output_width ||
418                     cinfo.output_height != cinfo2.output_height ||
419                     cinfo.out_color_components != cinfo2.out_color_components)
420                         {
421                         DEBUG_1("stereo data with different output size");
422                         jpeg_destroy_decompress(&cinfo2);
423                         lj->stereo = FALSE;
424                         }
425                 }
426
427
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);
431
432         if (!lj->pixbuf)
433                 {
434                 jpeg_destroy_decompress (&cinfo);
435                 if (lj->stereo) jpeg_destroy_decompress (&cinfo2);
436                 return 0;
437                 }
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);
440
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);
444
445
446         while (cinfo.output_scanline < cinfo.output_height && !lj->abort)
447                 {
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);
451                 if (lj->stereo)
452                         {
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);
456                         }
457                 }
458
459         jpeg_finish_decompress(&cinfo);
460         jpeg_destroy_decompress(&cinfo);
461         if (lj->stereo)
462                 {
463                 jpeg_finish_decompress(&cinfo);
464                 jpeg_destroy_decompress(&cinfo);
465                 }
466
467         return TRUE;
468 }
469
470 static void image_loader_cr3_set_size(gpointer loader, int width, int height)
471 {
472         auto lj = static_cast<ImageLoaderJpeg *>(loader);
473         lj->requested_width = width;
474         lj->requested_height = height;
475 }
476
477 static GdkPixbuf* image_loader_cr3_get_pixbuf(gpointer loader)
478 {
479         auto lj = static_cast<ImageLoaderJpeg *>(loader);
480         return lj->pixbuf;
481 }
482
483 static gchar* image_loader_cr3_get_format_name(gpointer)
484 {
485         return g_strdup("cr3");
486 }
487 static gchar** image_loader_cr3_get_format_mime_types(gpointer)
488 {
489         static const gchar *mime[] = {"image/x-canon-cr3", nullptr};
490         return g_strdupv(const_cast<gchar **>(mime));
491 }
492
493 static gboolean image_loader_cr3_close(gpointer, GError **)
494 {
495         return TRUE;
496 }
497
498 static void image_loader_cr3_abort(gpointer loader)
499 {
500         auto lj = static_cast<ImageLoaderJpeg *>(loader);
501         lj->abort = TRUE;
502 }
503
504 static void image_loader_cr3_free(gpointer loader)
505 {
506         auto lj = static_cast<ImageLoaderJpeg *>(loader);
507         if (lj->pixbuf) g_object_unref(lj->pixbuf);
508         g_free(lj);
509 }
510
511
512 void image_loader_backend_set_cr3(ImageLoaderBackend *funcs)
513 {
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;
522
523         funcs->get_format_name = image_loader_cr3_get_format_name;
524         funcs->get_format_mime_types = image_loader_cr3_get_format_mime_types;
525 }
526
527
528
529 #endif
530
531
532 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */