Deduplicate cr3 image loader
[geeqie.git] / src / image-load-libraw.cc
1 /*
2  * Copyright (C) 2021 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 /**
22  * @file
23  * This uses libraw to extract a thumbnail from a raw image. The exiv2 library
24  * does not (yet) extract thumbnails from .cr3 images.
25  * LibRaw seems to be slower than exiv2, so let exiv2 have priority.
26  */
27
28 #include "image-load-libraw.h"
29
30 #include <config.h>
31
32 #include "image-load.h"
33
34 #if HAVE_RAW
35
36 #include <fcntl.h>
37 #include <sys/mman.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40
41 #include <libraw/libraw.h>
42
43 #include "debug.h"
44 #include "filedata.h"
45 #include "filefilter.h"
46 #include "typedefs.h"
47
48 struct UnmapData
49 {
50         guchar *ptr;
51         guchar *map_data;
52         size_t map_len;
53         libraw_data_t *lrdt;
54 };
55
56 static GList *libraw_unmap_list = nullptr;
57
58 void libraw_free_preview(const guchar *buf)
59 {
60         GList *work = libraw_unmap_list;
61
62         while (work)
63                 {
64                 auto ud = static_cast<UnmapData *>(work->data);
65                 if (ud->ptr == buf)
66                         {
67                         munmap(ud->map_data, ud->map_len);
68                         libraw_close(ud->lrdt);
69                         libraw_unmap_list = g_list_remove_link(libraw_unmap_list, work);
70                         g_free(ud);
71                         return;
72                         }
73                 work = work->next;
74                 }
75         g_assert_not_reached();
76 }
77
78 guchar *libraw_get_preview(ImageLoader *il, guint *data_len)
79 {
80         libraw_data_t *lrdt;
81         int ret;
82         UnmapData *ud;
83         struct stat st;
84         guchar *map_data;
85         size_t map_len;
86         int fd;
87
88         if (!filter_file_class(il->fd->path, FORMAT_CLASS_RAWIMAGE)) return nullptr;
89
90         fd = open(il->fd->path, O_RDONLY);
91         if (fd == -1)
92                 {
93                 return nullptr;
94                 }
95
96         if (fstat(fd, &st) == -1)
97                 {
98                 close(fd);
99                 return nullptr;
100                 }
101
102         map_len = st.st_size;
103         map_data = static_cast<guchar *>(mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0));
104         close(fd);
105
106         if (map_data == MAP_FAILED)
107                 {
108                 return nullptr;
109                 }
110
111         lrdt = libraw_init(0);
112         if (!lrdt)
113                 {
114                 log_printf("Warning: Cannot create libraw handle\n");
115                 return nullptr;
116                 }
117
118         ret = libraw_open_buffer(lrdt, map_data, map_len);
119         if (ret == LIBRAW_SUCCESS)
120                 {
121                 ret = libraw_unpack_thumb(lrdt);
122                 if (ret == LIBRAW_SUCCESS)
123                         {
124                         il->mapped_file = reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
125                         *data_len = lrdt->thumbnail.tlength;
126
127                         ud = g_new(UnmapData, 1);
128                         ud->ptr =reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
129                         ud->map_data = map_data;
130                         ud->map_len = lrdt->thumbnail.tlength;
131                         ud->lrdt = lrdt;
132
133                         libraw_unmap_list = g_list_prepend(libraw_unmap_list, ud);
134
135                         return reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
136                         }
137                 }
138
139         libraw_close(lrdt);
140
141         return nullptr;
142 }
143
144 #else /* !define HAVE_RAW */
145
146 void libraw_free_preview(const guchar *)
147 {
148 }
149
150 guchar *libraw_get_preview(ImageLoader *, guint *)
151 {
152         return nullptr;
153 }
154
155 #endif
156
157 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */