Move third-party sources to separate sub-directory
[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 struct ImageLoaderCr3 : public ImageLoaderJpeg
40 {
41 public:
42         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
43         gchar *get_format_name() override;
44         gchar **get_format_mime_types() override;
45 };
46
47 gboolean ImageLoaderCr3::write(const guchar *buf, gsize &chunk_size, gsize count, GError **error)
48 {
49         /** @FIXME Just start search at where full size jpeg should be,
50          * then search through the file looking for a jpeg end-marker
51          */
52         gboolean found = FALSE;
53         gint i;
54         guint n;
55
56         n = 0;
57         while (n < count - 4 && !found)
58                 {
59                 if (memcmp(&buf[n], "mdat", 4) == 0)
60                         {
61                         if (memcmp(&buf[n + 12], "\xFF\xD8", 2) == 0)
62                                 {
63                                 i = 0;
64                                 while (!found )
65                                         {
66                                         if (memcmp(&buf[n + 12 + i], "\xFF\xD9", 2) == 0)
67                                                 {
68                                                 found = TRUE;
69                                                 }
70                                         i++;
71                                         }
72                                 }
73                         else
74                                 {
75                                 break;
76                                 }
77                         }
78                 else
79                         {
80                         n++;
81                         }
82                 }
83
84         if (!found)
85                 {
86                 return FALSE;
87                 }
88
89         gboolean ret = ImageLoaderJpeg::write(buf + n + 12, chunk_size, i, error);
90         if (ret)
91                 {
92                 chunk_size = count;
93                 }
94         return ret;
95 }
96
97 gchar *ImageLoaderCr3::get_format_name()
98 {
99         return g_strdup("cr3");
100 }
101
102 gchar **ImageLoaderCr3::get_format_mime_types()
103 {
104         static const gchar *mime[] = {"image/x-canon-cr3", nullptr};
105         return g_strdupv(const_cast<gchar **>(mime));
106 }
107
108 } // namespace
109
110 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_cr3()
111 {
112         return std::make_unique<ImageLoaderCr3>();
113 }
114
115 #endif // HAVE_JPEG && !HAVE_RAW
116
117 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */