clang-tidy: bugprone-integer-division
[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 "main.h"
29
30 #include "filedata.h"
31 #include "filefilter.h"
32 #include "image-load.h"
33 #include "image-load-libraw.h"
34
35 #ifdef HAVE_RAW
36
37 #include <libraw/libraw.h>
38 #include <sys/mman.h>
39
40 struct UnmapData
41 {
42         guchar *ptr;
43         guchar *map_data;
44         size_t map_len;
45         libraw_data_t *lrdt;
46 };
47
48 static GList *libraw_unmap_list = nullptr;
49
50 void libraw_free_preview(const guchar *buf)
51 {
52         GList *work = libraw_unmap_list;
53
54         while (work)
55                 {
56                 auto ud = static_cast<UnmapData *>(work->data);
57                 if (ud->ptr == buf)
58                         {
59                         munmap(ud->map_data, ud->map_len);
60                         libraw_close(ud->lrdt);
61                         libraw_unmap_list = g_list_remove_link(libraw_unmap_list, work);
62                         g_free(ud);
63                         return;
64                         }
65                 work = work->next;
66                 }
67         g_assert_not_reached();
68 }
69
70 guchar *libraw_get_preview(ImageLoader *il, guint *data_len)
71 {
72         libraw_data_t *lrdt;
73         int ret;
74         UnmapData *ud;
75         struct stat st;
76         guchar *map_data;
77         size_t map_len;
78         int fd;
79
80         if (!filter_file_class(il->fd->path, FORMAT_CLASS_RAWIMAGE)) return nullptr;
81
82         fd = open(il->fd->path, O_RDONLY);
83         if (fd == -1)
84                 {
85                 return nullptr;
86                 }
87
88         if (fstat(fd, &st) == -1)
89                 {
90                 close(fd);
91                 return nullptr;
92                 }
93
94         map_len = st.st_size;
95         map_data = static_cast<guchar *>(mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0));
96         close(fd);
97
98         if (map_data == MAP_FAILED)
99                 {
100                 return nullptr;
101                 }
102
103         lrdt = libraw_init(0);
104         if (!lrdt)
105                 {
106                 log_printf("Warning: Cannot create libraw handle\n");
107                 return nullptr;
108                 }
109
110         ret = libraw_open_buffer(lrdt, map_data, map_len);
111         if (ret == LIBRAW_SUCCESS)
112                 {
113                 ret = libraw_unpack_thumb(lrdt);
114                 if (ret == LIBRAW_SUCCESS)
115                         {
116                         il->mapped_file = reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
117                         *data_len = lrdt->thumbnail.tlength;
118
119                         ud = g_new(UnmapData, 1);
120                         ud->ptr =reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
121                         ud->map_data = map_data;
122                         ud->map_len = lrdt->thumbnail.tlength;
123                         ud->lrdt = lrdt;
124
125                         libraw_unmap_list = g_list_prepend(libraw_unmap_list, ud);
126
127                         return reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
128                         }
129                 }
130
131         libraw_close(lrdt);
132
133         return nullptr;
134 }
135
136 #else /* !define HAVE_RAW */
137
138 void libraw_free_preview(const guchar *)
139 {
140 }
141
142 guchar *libraw_get_preview(ImageLoader *, guint *)
143 {
144         return nullptr;
145 }
146
147 #endif
148
149 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */