2 * Copyright (C) 2021 The Geeqie Team
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.
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.
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.
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.
28 #include "image-load-libraw.h"
32 #include "image-load.h"
40 #include <libraw/libraw.h>
44 #include "filefilter.h"
54 static GList *libraw_unmap_list = nullptr;
56 void libraw_free_preview(const guchar *buf)
58 GList *work = libraw_unmap_list;
62 auto ud = static_cast<UnmapData *>(work->data);
65 munmap(ud->map_data, ud->map_len);
66 libraw_close(ud->lrdt);
67 libraw_unmap_list = g_list_remove_link(libraw_unmap_list, work);
73 g_assert_not_reached();
76 guchar *libraw_get_preview(ImageLoader *il, guint *data_len)
86 if (!filter_file_class(il->fd->path, FORMAT_CLASS_RAWIMAGE)) return nullptr;
88 fd = open(il->fd->path, O_RDONLY);
94 if (fstat(fd, &st) == -1)
100 map_len = st.st_size;
101 map_data = static_cast<guchar *>(mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0));
104 if (map_data == MAP_FAILED)
109 lrdt = libraw_init(0);
112 log_printf("Warning: Cannot create libraw handle\n");
116 ret = libraw_open_buffer(lrdt, map_data, map_len);
117 if (ret == LIBRAW_SUCCESS)
119 ret = libraw_unpack_thumb(lrdt);
120 if (ret == LIBRAW_SUCCESS)
122 il->mapped_file = reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
123 *data_len = lrdt->thumbnail.tlength;
125 ud = g_new(UnmapData, 1);
126 ud->ptr =reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
127 ud->map_data = map_data;
128 ud->map_len = lrdt->thumbnail.tlength;
131 libraw_unmap_list = g_list_prepend(libraw_unmap_list, ud);
133 return reinterpret_cast<guchar *>(lrdt->thumbnail.thumb);
142 #else /* !define HAVE_RAW */
144 void libraw_free_preview(const guchar *)
148 guchar *libraw_get_preview(ImageLoader *, guint *)
155 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */