Let image loader backend decide how to process image buffer
[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 #include <config.h>
25
26 #if HAVE_JPEG && !HAVE_RAW
27 #include "image-load-cr3.h"
28
29 #include <cstring>
30
31 #include <glib.h>
32
33 #include "image-load-jpeg.h"
34 #include "image-load.h"
35
36 namespace
37 {
38
39 gboolean image_loader_cr3_write(gpointer loader, const guchar *buf, gsize &chunk_size, gsize count, GError **error)
40 {
41         /** @FIXME Just start search at where full size jpeg should be,
42          * then search through the file looking for a jpeg end-marker
43          */
44         gboolean found = FALSE;
45         gint i;
46         guint n;
47
48         n = 0;
49         while (n < count - 4 && !found)
50                 {
51                 if (memcmp(&buf[n], "mdat", 4) == 0)
52                         {
53                         if (memcmp(&buf[n + 12], "\xFF\xD8", 2) == 0)
54                                 {
55                                 i = 0;
56                                 while (!found )
57                                         {
58                                         if (memcmp(&buf[n + 12 + i], "\xFF\xD9", 2) == 0)
59                                                 {
60                                                 found = TRUE;
61                                                 }
62                                         i++;
63                                         }
64                                 }
65                         else
66                                 {
67                                 break;
68                                 }
69                         }
70                 else
71                         {
72                         n++;
73                         }
74                 }
75
76         if (!found)
77                 {
78                 return FALSE;
79                 }
80
81         gboolean ret = image_loader_jpeg_write(loader, buf + n + 12, chunk_size, i, error);;
82         if (ret)
83                 {
84                 chunk_size = count;
85                 }
86         return ret;
87 }
88
89 gchar* image_loader_cr3_get_format_name(gpointer)
90 {
91         return g_strdup("cr3");
92 }
93
94 gchar** image_loader_cr3_get_format_mime_types(gpointer)
95 {
96         static const gchar *mime[] = {"image/x-canon-cr3", nullptr};
97         return g_strdupv(const_cast<gchar **>(mime));
98 }
99
100 } // namespace
101
102 void image_loader_backend_set_cr3(ImageLoaderBackend *funcs)
103 {
104         image_loader_backend_set_jpeg(funcs);
105
106         funcs->write = image_loader_cr3_write;
107
108         funcs->get_format_name = image_loader_cr3_get_format_name;
109         funcs->get_format_mime_types = image_loader_cr3_get_format_mime_types;
110 }
111
112 #endif // HAVE_JPEG && !HAVE_RAW
113
114 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */