Blender V4.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
9#ifdef _WIN32
10# include <io.h>
11# include <stddef.h>
12# include <sys/types.h>
13#endif
14
15#include "BLI_fileops.h"
16#include "BLI_mmap.h"
17#include "BLI_path_utils.hh" /* For assertions. */
18#include "BLI_string.h"
19#include "BLI_utildefines.h"
20#include <cstdlib>
21
22#include "IMB_allocimbuf.hh"
23#include "IMB_filetype.hh"
24#include "IMB_imbuf.hh"
25#include "IMB_imbuf_types.hh"
26#include "IMB_metadata.hh"
27#include "IMB_thumbs.hh"
28#include "imbuf.hh"
29
32
33static void imb_handle_alpha(ImBuf *ibuf,
34 int flags,
35 char colorspace[IM_MAX_SPACE],
36 const char effective_colorspace[IM_MAX_SPACE])
37{
38 if (colorspace) {
39 if (ibuf->byte_buffer.data != nullptr && ibuf->float_buffer.data == nullptr) {
40 /* byte buffer is never internally converted to some standard space,
41 * store pointer to its color space descriptor instead
42 */
43 ibuf->byte_buffer.colorspace = colormanage_colorspace_get_named(effective_colorspace);
44 }
45
46 BLI_strncpy(colorspace, effective_colorspace, IM_MAX_SPACE);
47 }
48
49 bool is_data = (colorspace && IMB_colormanagement_space_name_is_data(colorspace));
50 int alpha_flags = (flags & IB_alphamode_detect) ? ibuf->flags : flags;
51
52 if (is_data || (flags & IB_alphamode_channel_packed)) {
53 /* Don't touch alpha. */
55 }
56 else if (flags & IB_alphamode_ignore) {
57 /* Make opaque. */
58 IMB_rectfill_alpha(ibuf, 1.0f);
60 }
61 else {
62 if (alpha_flags & IB_alphamode_premul) {
63 if (ibuf->byte_buffer.data) {
65 }
66 else {
67 /* pass, floats are expected to be premul */
68 }
69 }
70 else {
71 if (ibuf->float_buffer.data) {
73 }
74 else {
75 /* pass, bytes are expected to be straight */
76 }
77 }
78 }
79
80 /* OCIO_TODO: in some cases it's faster to do threaded conversion,
81 * but how to distinguish such cases */
82 colormanage_imbuf_make_linear(ibuf, effective_colorspace);
83}
84
86 const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
87{
88 ImBuf *ibuf;
89 const ImFileType *type;
90 char effective_colorspace[IM_MAX_SPACE] = "";
91
92 if (mem == nullptr) {
93 fprintf(stderr, "%s: nullptr pointer\n", __func__);
94 return nullptr;
95 }
96
97 if (colorspace) {
98 STRNCPY(effective_colorspace, colorspace);
99 }
100
101 for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
102 if (type->load) {
103 ibuf = type->load(mem, size, flags, effective_colorspace);
104 if (ibuf) {
105 imb_handle_alpha(ibuf, flags, colorspace, effective_colorspace);
106 return ibuf;
107 }
108 }
109 }
110
111 if ((flags & IB_test) == 0) {
112 fprintf(stderr, "%s: unknown file-format (%s)\n", __func__, descr);
113 }
114
115 return nullptr;
116}
117
118ImBuf *IMB_loadifffile(int file, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
119{
120 ImBuf *ibuf;
121
122 if (file == -1) {
123 return nullptr;
124 }
125
127 BLI_mmap_file *mmap_file = BLI_mmap_open(file);
129 if (mmap_file == nullptr) {
130 fprintf(stderr, "%s: couldn't get mapping %s\n", __func__, descr);
131 return nullptr;
132 }
133
134 const uchar *mem = static_cast<const uchar *>(BLI_mmap_get_pointer(mmap_file));
135 const size_t size = BLI_mmap_get_length(mmap_file);
136
137 ibuf = IMB_ibImageFromMemory(mem, size, flags, colorspace, descr);
138
140 BLI_mmap_free(mmap_file);
142
143 return ibuf;
144}
145
146ImBuf *IMB_loadiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
147{
148 ImBuf *ibuf;
149 int file;
150
151 BLI_assert(!BLI_path_is_rel(filepath));
152
153 file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
154 if (file == -1) {
155 return nullptr;
156 }
157
158 ibuf = IMB_loadifffile(file, flags, colorspace, filepath);
159
160 if (ibuf) {
161 STRNCPY(ibuf->filepath, filepath);
162 }
163
164 close(file);
165
166 return ibuf;
167}
168
169ImBuf *IMB_thumb_load_image(const char *filepath,
170 size_t max_thumb_size,
171 char colorspace[IM_MAX_SPACE],
172 IMBThumbLoadFlags load_flags)
173{
174 const ImFileType *type = IMB_file_type_from_ftype(IMB_ispic_type(filepath));
175 if (type == nullptr) {
176 return nullptr;
177 }
178
179 ImBuf *ibuf = nullptr;
180 int flags = IB_rect | IB_metadata;
181 /* Size of the original image. */
182 size_t width = 0;
183 size_t height = 0;
184
185 char effective_colorspace[IM_MAX_SPACE] = "";
186 if (colorspace) {
187 STRNCPY(effective_colorspace, colorspace);
188 }
189
190 if (type->load_filepath_thumbnail) {
191 ibuf = type->load_filepath_thumbnail(
192 filepath, flags, max_thumb_size, colorspace, &width, &height);
193 }
194 else {
195 /* Skip images of other types if over 100MB. */
197 const size_t file_size = BLI_file_size(filepath);
198 if (file_size != size_t(-1) && file_size > THUMB_SIZE_MAX) {
199 return nullptr;
200 }
201 }
202 ibuf = IMB_loadiffname(filepath, flags, colorspace);
203 if (ibuf) {
204 width = ibuf->x;
205 height = ibuf->y;
206 }
207 }
208
209 if (ibuf) {
210 imb_handle_alpha(ibuf, flags, colorspace, effective_colorspace);
211
212 if (width > 0 && height > 0) {
213 /* Save dimensions of original image into the thumbnail metadata. */
214 char cwidth[40];
215 char cheight[40];
216 SNPRINTF(cwidth, "%zu", width);
217 SNPRINTF(cheight, "%zu", height);
219 IMB_metadata_set_field(ibuf->metadata, "Thumb::Image::Width", cwidth);
220 IMB_metadata_set_field(ibuf->metadata, "Thumb::Image::Height", cheight);
221 }
222 }
223
224 return ibuf;
225}
226
227ImBuf *IMB_testiffname(const char *filepath, int flags)
228{
229 ImBuf *ibuf;
230 int file;
231 char colorspace[IM_MAX_SPACE] = "\0";
232
233 BLI_assert(!BLI_path_is_rel(filepath));
234
235 file = BLI_open(filepath, O_BINARY | O_RDONLY, 0);
236 if (file == -1) {
237 return nullptr;
238 }
239
240 ibuf = IMB_loadifffile(file, flags | IB_test | IB_multilayer, colorspace, filepath);
241
242 if (ibuf) {
243 STRNCPY(ibuf->filepath, filepath);
244 }
245
246 close(file);
247
248 return ibuf;
249}
#define BLI_assert(a)
Definition BLI_assert.h:50
File and directory operations.
#define O_BINARY
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:215
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void * BLI_mmap_get_pointer(BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.c:211
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
Definition BLI_mmap.c:221
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.c:132
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.c:216
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define STRNCPY(dst, src)
Definition BLI_string.h:593
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:597
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
unsigned char uchar
bool IMB_colormanagement_space_name_is_data(const char *name)
void IMB_premultiply_alpha(ImBuf *ibuf)
Definition filter.cc:579
void IMB_rectfill_alpha(ImBuf *ibuf, float value)
Definition rectop.cc:1259
void IMB_unpremultiply_alpha(ImBuf *ibuf)
Definition filter.cc:641
int IMB_ispic_type(const char *filepath)
#define IM_MAX_SPACE
Definition IMB_imbuf.hh:49
IMBThumbLoadFlags
Definition IMB_imbuf.hh:76
Contains defines and structs used throughout the imbuf module.
@ IB_alphamode_channel_packed
@ IB_alphamode_premul
@ IB_alphamode_ignore
@ IB_metadata
@ IB_multilayer
@ IB_alphamode_detect
@ IB_test
@ IB_rect
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
Definition metadata.cc:69
void IMB_metadata_ensure(IDProperty **metadata)
Definition metadata.cc:24
#define THUMB_SIZE_MAX
Definition IMB_thumbs.hh:36
void imb_mmap_lock()
Definition allocimbuf.cc:56
void imb_mmap_unlock()
Definition allocimbuf.cc:61
void colormanage_imbuf_make_linear(ImBuf *ibuf, const char *from_colorspace)
ColorSpace * colormanage_colorspace_get_named(const char *name)
FILE * file
const ImFileType * IMB_file_type_from_ftype(int ftype)
Definition filetype.cc:222
const ImFileType IMB_FILE_TYPES[]
Definition filetype.cc:24
const ImFileType * IMB_FILE_TYPES_LAST
Definition filetype.cc:220
ImBuf * IMB_loadifffile(int file, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
Definition readimage.cc:118
ImBuf * IMB_testiffname(const char *filepath, int flags)
Definition readimage.cc:227
ImBuf * IMB_loadiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
Definition readimage.cc:146
ImBuf * IMB_ibImageFromMemory(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
Definition readimage.cc:85
ImBuf * IMB_thumb_load_image(const char *filepath, size_t max_thumb_size, char colorspace[IM_MAX_SPACE], IMBThumbLoadFlags load_flags)
Definition readimage.cc:169
static void imb_handle_alpha(ImBuf *ibuf, int flags, char colorspace[IM_MAX_SPACE], const char effective_colorspace[IM_MAX_SPACE])
Definition readimage.cc:33
ColorSpace * colorspace
char filepath[IMB_FILEPATH_SIZE]
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer
IDProperty * metadata