2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
28 # include <sys/types.h>
31 #include "BLI_fileops.h"
33 #include "BLI_path_util.h"
34 #include "BLI_string.h"
35 #include "BLI_utildefines.h"
38 #include "IMB_allocimbuf.h"
39 #include "IMB_filetype.h"
40 #include "IMB_imbuf.h"
41 #include "IMB_imbuf_types.h"
44 #include "IMB_colormanagement.h"
45 #include "IMB_colormanagement_intern.h"
47 static void imb_handle_alpha(ImBuf *ibuf,
49 char colorspace[IM_MAX_SPACE],
50 char effective_colorspace[IM_MAX_SPACE])
53 if (ibuf->rect != NULL && ibuf->rect_float == NULL) {
54 /* byte buffer is never internally converted to some standard space,
55 * store pointer to its color space descriptor instead
57 ibuf->rect_colorspace = colormanage_colorspace_get_named(effective_colorspace);
60 BLI_strncpy(colorspace, effective_colorspace, IM_MAX_SPACE);
63 bool is_data = (colorspace && IMB_colormanagement_space_name_is_data(colorspace));
64 int alpha_flags = (flags & IB_alphamode_detect) ? ibuf->flags : flags;
66 if (is_data || (flags & IB_alphamode_channel_packed)) {
67 /* Don't touch alpha. */
68 ibuf->flags |= IB_alphamode_channel_packed;
70 else if (flags & IB_alphamode_ignore) {
72 IMB_rectfill_alpha(ibuf, 1.0f);
73 ibuf->flags |= IB_alphamode_ignore;
76 if (alpha_flags & IB_alphamode_premul) {
78 IMB_unpremultiply_alpha(ibuf);
81 /* pass, floats are expected to be premul */
85 if (ibuf->rect_float) {
86 IMB_premultiply_alpha(ibuf);
89 /* pass, bytes are expected to be straight */
94 /* OCIO_TODO: in some cases it's faster to do threaded conversion,
95 * but how to distinguish such cases */
96 colormanage_imbuf_make_linear(ibuf, effective_colorspace);
99 ImBuf *IMB_ibImageFromMemory(const unsigned char *mem,
102 char colorspace[IM_MAX_SPACE],
106 const ImFileType *type;
107 char effective_colorspace[IM_MAX_SPACE] = "";
110 fprintf(stderr, "%s: NULL pointer\n", __func__);
115 BLI_strncpy(effective_colorspace, colorspace, sizeof(effective_colorspace));
118 for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
120 ibuf = type->load(mem, size, flags, effective_colorspace);
122 imb_handle_alpha(ibuf, flags, colorspace, effective_colorspace);
128 if ((flags & IB_test) == 0) {
129 fprintf(stderr, "%s: unknown fileformat (%s)\n", __func__, descr);
135 static ImBuf *IMB_ibImageFromFile(const char *filepath,
137 char colorspace[IM_MAX_SPACE],
141 const ImFileType *type;
142 char effective_colorspace[IM_MAX_SPACE] = "";
145 BLI_strncpy(effective_colorspace, colorspace, sizeof(effective_colorspace));
148 for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
149 if (type->load_filepath) {
150 ibuf = type->load_filepath(filepath, flags, effective_colorspace);
152 imb_handle_alpha(ibuf, flags, colorspace, effective_colorspace);
158 if ((flags & IB_test) == 0) {
159 fprintf(stderr, "%s: unknown fileformat (%s)\n", __func__, descr);
165 static bool imb_is_filepath_format(const char *filepath)
167 /* return true if this is one of the formats that can't be loaded from memory */
168 return BLI_path_extension_check_array(filepath, imb_ext_image_filepath_only);
171 ImBuf *IMB_loadifffile(
172 int file, const char *filepath, int flags, char colorspace[IM_MAX_SPACE], const char *descr)
182 if (imb_is_filepath_format(filepath)) {
183 return IMB_ibImageFromFile(filepath, flags, colorspace, descr);
186 size = BLI_file_descriptor_size(file);
189 BLI_mmap_file *mmap_file = BLI_mmap_open(file);
191 if (mmap_file == NULL) {
192 fprintf(stderr, "%s: couldn't get mapping %s\n", __func__, descr);
196 mem = BLI_mmap_get_pointer(mmap_file);
198 ibuf = IMB_ibImageFromMemory(mem, size, flags, colorspace, descr);
201 BLI_mmap_free(mmap_file);
207 static void imb_cache_filename(char *filename, const char *name, int flags)
209 /* read .tx instead if it exists and is not older */
210 if (flags & IB_tilecache) {
211 BLI_strncpy(filename, name, IMB_FILENAME_SIZE);
212 if (!BLI_path_extension_replace(filename, IMB_FILENAME_SIZE, ".tx")) {
216 if (BLI_file_older(name, filename)) {
221 BLI_strncpy(filename, name, IMB_FILENAME_SIZE);
224 ImBuf *IMB_loadiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
228 char filepath_tx[IMB_FILENAME_SIZE];
230 BLI_assert(!BLI_path_is_rel(filepath));
232 imb_cache_filename(filepath_tx, filepath, flags);
234 file = BLI_open(filepath_tx, O_BINARY | O_RDONLY, 0);
239 ibuf = IMB_loadifffile(file, filepath, flags, colorspace, filepath_tx);
242 BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
243 BLI_strncpy(ibuf->cachename, filepath_tx, sizeof(ibuf->cachename));
244 for (a = 1; a < ibuf->miptot; a++) {
245 BLI_strncpy(ibuf->mipmap[a - 1]->cachename, filepath_tx, sizeof(ibuf->cachename));
254 ImBuf *IMB_testiffname(const char *filepath, int flags)
258 char filepath_tx[IMB_FILENAME_SIZE];
259 char colorspace[IM_MAX_SPACE] = "\0";
261 BLI_assert(!BLI_path_is_rel(filepath));
263 imb_cache_filename(filepath_tx, filepath, flags);
265 file = BLI_open(filepath_tx, O_BINARY | O_RDONLY, 0);
270 ibuf = IMB_loadifffile(file, filepath, flags | IB_test | IB_multilayer, colorspace, filepath_tx);
273 BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
274 BLI_strncpy(ibuf->cachename, filepath_tx, sizeof(ibuf->cachename));
282 static void imb_loadtilefile(ImBuf *ibuf, int file, int tx, int ty, unsigned int *rect)
291 size = BLI_file_descriptor_size(file);
294 BLI_mmap_file *mmap_file = BLI_mmap_open(file);
296 if (mmap_file == NULL) {
297 fprintf(stderr, "Couldn't get memory mapping for %s\n", ibuf->cachename);
301 mem = BLI_mmap_get_pointer(mmap_file);
303 const ImFileType *type = IMB_file_type_from_ibuf(ibuf);
305 if (type->load_tile != NULL) {
306 type->load_tile(ibuf, mem, size, tx, ty, rect);
311 BLI_mmap_free(mmap_file);
315 void imb_loadtile(ImBuf *ibuf, int tx, int ty, unsigned int *rect)
319 file = BLI_open(ibuf->cachename, O_BINARY | O_RDONLY, 0);
324 imb_loadtilefile(ibuf, file, tx, ty, rect);