File: image-load-libraw.cc

package info (click to toggle)
geeqie 1%3A2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,360 kB
  • sloc: cpp: 89,209; xml: 10,932; sh: 3,764; awk: 124; perl: 88; python: 80; makefile: 24
file content (123 lines) | stat: -rw-r--r-- 2,759 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2021 The Geeqie Team
 *
 * Authors: Colin Clark
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * @file
 * This uses libraw to extract a thumbnail from a raw image. The exiv2 library
 * does not (yet) extract thumbnails from .cr3 images.
 * LibRaw seems to be slower than exiv2, so let exiv2 have priority.
 */

#include "image-load-libraw.h"

#include <config.h>

#if HAVE_RAW

#include <sys/mman.h>

#include <cstddef>

#include <libraw/libraw.h>

#include "filefilter.h"
#include "ui-fileops.h"

struct UnmapData
{
	guchar *ptr;
	guchar *map_data;
	size_t map_len;
	LibRaw *lr;
};

static GList *libraw_unmap_list = nullptr;

void libraw_free_preview(const guchar *buf)
{
	GList *work = libraw_unmap_list;

	while (work)
		{
		auto ud = static_cast<UnmapData *>(work->data);
		if (ud->ptr == buf)
			{
			munmap(ud->map_data, ud->map_len);
			delete ud->lr;
			g_free(ud);
			libraw_unmap_list = g_list_delete_link(libraw_unmap_list, work);
			return;
			}
		work = work->next;
		}
	g_assert_not_reached();
}

guchar *libraw_get_preview(const gchar *path, gsize &data_len)
{
	if (!filter_file_class(path, FORMAT_CLASS_RAWIMAGE)) return nullptr;

	size_t map_len;
	guchar *map_data = map_file(path, map_len);
	if (!map_data)
		{
		return nullptr;
		}

	auto lr = std::make_unique<LibRaw>();

	int ret = lr->open_buffer(map_data, map_len);
	if (ret == LIBRAW_SUCCESS)
		{
		ret = lr->unpack_thumb();
		if (ret == LIBRAW_SUCCESS && lr->is_jpeg_thumb())
			{
			auto *ud = g_new(UnmapData, 1);
			ud->ptr = reinterpret_cast<guchar *>(lr->imgdata.thumbnail.thumb);
			ud->map_data = map_data;
			ud->map_len = map_len;
			ud->lr = lr.release();

			libraw_unmap_list = g_list_prepend(libraw_unmap_list, ud);

			data_len = ud->lr->imgdata.thumbnail.tlength;
			return ud->ptr;
			}
		}

	munmap(map_data, map_len);

	return nullptr;
}

#else /* !define HAVE_RAW */

void libraw_free_preview(const guchar *)
{
}

guchar *libraw_get_preview(const gchar *, gsize &)
{
	return nullptr;
}

#endif

/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */