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