Blender V5.3
readimage.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef _WIN32
10# include <io.h>
11# include <stddef.h>
12# include <sys/types.h>
13#endif
14
15#include <cstdlib>
16
17#include "DNA_color_types.h"
18
19#include "BLI_fileops.hh"
20#include "BLI_mmap.hh"
21#include "BLI_path_utils.hh" /* For assertions. */
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24
25#include "CLG_log.h"
26
27#include "IMB_filetype.hh"
28#include "IMB_imbuf.hh"
29#include "IMB_imbuf_types.hh"
30#include "IMB_metadata.hh"
31#include "IMB_thumbs.hh"
32#include "imbuf.hh"
33
36
37namespace blender {
38
39static CLG_LogRef LOG = {"image.read"};
40
42 const ImBufFlags flags,
43 const char *filepath,
44 const ImFileColorSpace &file_colorspace,
45 ColorManagedColorspaceSettings *colorspace_settings)
46{
47 /* Determine file colorspace. */
48 char new_colorspace[IM_MAX_SPACE];
49
50 if (colorspace_settings && colorspace_settings->name[0]) {
51 /* Existing configured colorspace has priority. */
52 STRNCPY_UTF8(new_colorspace, colorspace_settings->name);
53 }
54 else if (file_colorspace.metadata_colorspace[0] &&
56 {
57 /* Use colorspace from file metadata if provided. */
58 STRNCPY_UTF8(new_colorspace, file_colorspace.metadata_colorspace);
59 }
60 else {
61 /* The color-space from the file-path (not a file-path). */
62 const char *filepath_colorspace = (filepath) ?
64 nullptr;
65 if (filepath_colorspace) {
66 /* Use colorspace from OpenColorIO file rules. */
67 STRNCPY_UTF8(new_colorspace, filepath_colorspace);
68 }
69 else {
70 /* Use float colorspace if the image may contain HDR colors, byte otherwise. */
71 const char *role_colorspace = IMB_colormanagement_role_colorspace_name_get(
73 STRNCPY_UTF8(new_colorspace, role_colorspace);
74 }
75 }
76
77 if (colorspace_settings) {
78 /* Only set when empty, to keep the interop ID of an existing color space. */
79 if (colorspace_settings->name[0] == '\0') {
80 IMB_colormanagement_colorspace_settings_set(colorspace_settings, new_colorspace);
81 }
82
83 if (ibuf->byte_data() != nullptr && ibuf->float_data() == nullptr) {
84 /* byte buffer is never internally converted to some standard space,
85 * store pointer to its color space descriptor instead
86 */
88 }
89 }
90
91 bool is_data = (colorspace_settings && IMB_colormanagement_space_name_is_data(new_colorspace));
92 ImBufFlags alpha_flags = flag_is_set(flags, ImBufFlags::AlphaDetect) ? ibuf->flags : flags;
93
94 if (is_data || flag_is_set(flags, ImBufFlags::AlphaChannelPacked)) {
95 /* Don't touch alpha. */
97 }
98 else if (flag_is_set(flags, ImBufFlags::AlphaIgnore)) {
99 /* Make opaque. */
100 IMB_rectfill_alpha(ibuf, 1.0f);
102 }
103 else {
104 if (flag_is_set(alpha_flags, ImBufFlags::AlphaPremul)) {
105 if (ibuf->byte_data()) {
107 }
108 else {
109 /* pass, floats are expected to be premul */
110 }
111 }
112 else {
113 if (ibuf->float_data()) {
115 }
116 else {
117 /* pass, bytes are expected to be straight */
118 }
119 }
120 }
121
122 if (flag_is_set(flags, ImBufFlags::NoColorspaceConvert)) {
123 if (ibuf->float_data() != nullptr) {
125 }
126 }
127 else {
129 }
130}
131
133 const size_t size,
134 const ImBufFlags flags,
135 const char *descr,
136 const char *filepath,
137 ColorManagedColorspaceSettings *colorspace_settings)
138{
139 ImBuf *ibuf;
140 const ImFileType *type;
141
142 if (mem == nullptr) {
143 CLOG_ERROR(&LOG, "%s: nullptr pointer", __func__);
144 return nullptr;
145 }
146
147 ImFileColorSpace file_colorspace;
148
149 for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
150 if (type->load) {
151 ibuf = type->load(mem, size, flags, file_colorspace);
152 if (ibuf) {
154 ibuf, flags, filepath, file_colorspace, colorspace_settings);
155 return ibuf;
156 }
157 }
158 }
159
160 if (!flag_is_set(flags, ImBufFlags::Test)) {
161 CLOG_ERROR(&LOG, "%s: unknown file-format (%s)", __func__, descr);
162 }
163
164 return nullptr;
165}
166
168 const ImBufFlags flags,
169 const char *filepath,
170 ColorManagedColorspaceSettings *colorspace_settings)
171{
172 ImBuf *ibuf = nullptr;
173
174 if (file == -1) {
175 return nullptr;
176 }
177
178 BLI_mmap_file *mmap_file = BLI_mmap_open(file);
179 if (mmap_file == nullptr) {
180 CLOG_ERROR(&LOG, "%s: couldn't get mapping for \"%s\"", __func__, filepath);
181 return nullptr;
182 }
183
184 const uchar *mem = static_cast<const uchar *>(BLI_mmap_get_pointer(mmap_file));
185 const size_t size = BLI_mmap_get_length(mmap_file);
186
187 ibuf = IMB_load_image_from_memory(mem, size, flags, filepath, filepath, colorspace_settings);
188
189 /* If we got an image but mmap encountered an error,
190 * free the image and return nullptr as it could be corrupted. */
191 if (ibuf != nullptr && BLI_mmap_any_io_error(mmap_file)) {
192 IMB_freeImBuf(ibuf);
193 ibuf = nullptr;
194 }
195
196 BLI_mmap_free(mmap_file);
197
198 return ibuf;
199}
200
202 const ImBufFlags flags,
203 ColorManagedColorspaceSettings *colorspace_settings)
204{
205 ImBuf *ibuf;
206 int file;
207
208 BLI_assert(!BLI_path_is_rel(filepath));
209
210 file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
211 if (file == -1) {
212 return nullptr;
213 }
214
215 ibuf = IMB_load_image_from_file_descriptor(file, flags, filepath, colorspace_settings);
216
217 if (ibuf) {
218 ibuf->filepath = filepath;
219 }
220
221 close(file);
222
223 return ibuf;
224}
225
226ImBuf *IMB_thumb_load_image(const char *filepath,
227 const size_t max_thumb_size,
228 ColorManagedColorspaceSettings *colorspace_settings,
229 const IMBThumbLoadFlags load_flags)
230{
232 if (type == nullptr) {
233 return nullptr;
234 }
235
236 ImBuf *ibuf = nullptr;
238 /* Size of the original image. */
239 size_t width = 0;
240 size_t height = 0;
241
242 if (type->load_filepath_thumbnail) {
243 ImFileColorSpace file_colorspace;
244 ibuf = type->load_filepath_thumbnail(
245 filepath, flags, max_thumb_size, file_colorspace, &width, &height);
246 if (ibuf) {
247 imb_handle_colorspace_and_alpha(ibuf, flags, filepath, file_colorspace, colorspace_settings);
248 }
249 }
250 else {
251 /* Skip images of other types if over 100MB. */
252 if (!flag_is_set(load_flags, IMBThumbLoadFlags::LoadLargeFiles)) {
253 const size_t file_size = BLI_file_size(filepath);
254 if (file_size != size_t(-1) && file_size > THUMB_SIZE_MAX) {
255 return nullptr;
256 }
257 }
258 ibuf = IMB_load_image_from_filepath(filepath, flags, colorspace_settings);
259 if (ibuf) {
260 width = ibuf->x;
261 height = ibuf->y;
262 }
263 }
264
265 if (ibuf) {
266 if (width > 0 && height > 0) {
267 /* Save dimensions of original image into the thumbnail metadata. */
268 char cwidth[40];
269 char cheight[40];
270 SNPRINTF_UTF8(cwidth, "%zu", width);
271 SNPRINTF_UTF8(cheight, "%zu", height);
273 IMB_metadata_set_field(metadata, "Thumb::Image::Width", cwidth);
274 IMB_metadata_set_field(metadata, "Thumb::Image::Height", cheight);
275 }
276 }
277
278 return ibuf;
279}
280
281} // namespace blender
File and directory operations.
#define O_BINARY
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define IM_MAX_SPACE
#define THUMB_SIZE_MAX
Definition IMB_thumbs.hh:74
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void IMB_rectfill_alpha(ImBuf *ibuf, float value)
Definition rectop.cc:1309
void IMB_unpremultiply_alpha(ImBuf *ibuf)
Definition filter.cc:444
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:484
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:369
const ImFileType * IMB_file_type_from_ftype(eImbFileType ftype)
Definition filetype.cc:328
void colormanage_imbuf_make_linear(ImBuf *ibuf, const char *from_colorspace, const ColorManagedFileOutput output)
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
Definition BLI_mmap.cc:494
ImBuf * IMB_load_image_from_filepath(const char *filepath, ImBufFlags flags, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:201
IMBThumbLoadFlags
Definition IMB_imbuf.hh:116
void IMB_freeImBuf(ImBuf *ibuf)
unsigned char uchar
Definition BKE_text.h:58
const char * IMB_colormanagement_role_colorspace_name_get(int role)
const ImFileType IMB_FILE_TYPES[]
Definition filetype.cc:24
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
const char * IMB_colormanagement_space_from_filepath_rules(const char *filepath)
bool BLI_mmap_any_io_error(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:489
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:228
eImbFileType IMB_test_image_type(const char *filepath)
ImBuf * IMB_load_image_from_memory(const unsigned char *mem, size_t size, ImBufFlags flags, const char *descr, const char *filepath=nullptr, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:132
const ColorSpace * colormanage_colorspace_get_named(const char *name)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static void imb_handle_colorspace_and_alpha(ImBuf *ibuf, const ImBufFlags flags, const char *filepath, const ImFileColorSpace &file_colorspace, ColorManagedColorspaceSettings *colorspace_settings)
Definition readimage.cc:41
void * BLI_mmap_get_pointer(BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:479
static CLG_LogRef LOG
Definition logging.cpp:12
ImBuf * IMB_thumb_load_image(const char *filepath, size_t max_thumb_size, ColorManagedColorspaceSettings *colorspace_settings, IMBThumbLoadFlags load_flags=IMBThumbLoadFlags::Zero)
Definition readimage.cc:226
bool IMB_colormanagement_space_name_is_data(const char *name)
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
void IMB_premultiply_alpha(ImBuf *ibuf)
Definition filter.cc:382
ImBuf * IMB_load_image_from_file_descriptor(int file, ImBufFlags flags, const char *filepath=nullptr, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:167
const ImFileType * IMB_FILE_TYPES_LAST
Definition filetype.cc:326
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
const uint8_t * byte_data() const
IDProperty * metadata_for_write()
const float * float_data() const
std::string filepath
char metadata_colorspace[IM_MAX_SPACE]
ImBuf *(*) load(const unsigned char *mem, size_t size, ImBufFlags flags, ImFileColorSpace &r_colorspace)
ImBuf *(*) load_filepath_thumbnail(const char *filepath, ImBufFlags flags, size_t max_thumb_size, ImFileColorSpace &r_colorspace, size_t *r_width, size_t *r_height)