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