Blender V5.3
source/blender/imbuf/intern/util.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#endif
12
13#include <cstdlib>
14
15#include "BLI_fileops.hh"
16#include "BLI_path_utils.hh"
17#ifdef _WIN32
18# include "BLI_winstuff.hh"
19#endif
20
21#include "IMB_filetype.hh"
22#include "IMB_imbuf.hh"
23#include "IMB_imbuf_types.hh"
24#include "imbuf.hh"
25
26#include "CLG_log.h"
27
28namespace blender {
29
30static CLG_LogRef LOG = {"image.read"};
31
32const char *imb_ext_image[] = {
33 /* #IMB_FTYPE_PNG */
34 ".png",
35 /* #IMB_FTYPE_TGA */
36 ".tga",
37 /* #IMB_FTYPE_BMP */
38 ".bmp",
39 /* #IMB_FTYPE_JPG */
40 ".jpg",
41 ".jpeg",
42 /* #IMB_FTYPE_IRIS */
43 ".sgi",
44 ".rgb",
45 ".rgba",
46 /* #IMB_FTYPE_TIF */
47 ".tif",
48 ".tiff",
49 /* A convention for naming tiled images at different resolutions (MIP-mapped),
50 * supported by various render engines texture caching systems.
51 * These are typically TIFF or EXR images. See the tool `maketx` from OpenImageIO. */
52 ".tx",
53 /* #IMB_FTYPE_AVIF */
54 ".avif",
55#ifdef WITH_IMAGE_OPENJPEG
56 /* #IMB_FTYPE_JP2 */
57 ".jp2",
58 ".j2c",
59#endif
60 /* #IMB_FTYPE_RADHDR */
61 ".hdr",
62 /* #IMB_FTYPE_DDS */
63 ".dds",
64#ifdef WITH_IMAGE_CINEON
65 /* #IMB_FTYPE_DPX */
66 ".dpx",
67 /* #IMB_FTYPE_CINEON */
68 ".cin",
69#endif
70 /* #IMB_FTYPE_EXR */
71 ".exr",
72 /* #IMB_FTYPE_PSD */
73 ".psd",
74 ".pdd",
75 ".psb",
76#ifdef WITH_IMAGE_WEBP
77 /* #IMB_FTYPE_WEBP */
78 ".webp",
79#endif
80 nullptr,
81};
82
83const char *imb_ext_movie[] = {
84 ".avi", ".flc", ".mov", ".movie", ".mp4", ".m4v", ".m2v", ".m2t", ".m2ts", ".mts",
85 ".ts", ".mv", ".avs", ".wmv", ".ogv", ".ogg", ".r3d", ".dv", ".mpeg", ".mpg",
86 ".mpg2", ".vob", ".mkv", ".flv", ".divx", ".xvid", ".mxf", ".webm", ".gif", nullptr,
87};
88
90
91const char *imb_ext_audio[] = {
92 ".wav",
93 ".ogg",
94 ".oga",
95 ".mp3",
96 ".mp2",
97 ".ac3",
98 ".aac",
99 ".flac",
100 ".wma",
101 ".eac3",
102 ".aif",
103 ".aiff",
104 ".m4a",
105 ".mka",
106 ".opus",
107 nullptr,
108};
109
110/* OIIO will validate the entire header of some files and DPX requires 2048 */
111#define HEADER_SIZE 2048
112
114 uchar buf[HEADER_SIZE])
115{
116 BLI_stat_t st;
117 int fp;
118
119 BLI_assert(!BLI_path_is_rel(filepath));
120
121 CLOG_TRACE(&LOG, "%s: loading %s", __func__, filepath);
122
123 if (BLI_stat(filepath, &st) == -1) {
124 return -1;
125 }
126 if (((st.st_mode) & S_IFMT) != S_IFREG) {
127 return -1;
128 }
129
130 if ((fp = BLI_open(filepath, O_BINARY | O_RDONLY, 0)) == -1) {
131 return -1;
132 }
133
134 const int64_t size = BLI_read(fp, buf, HEADER_SIZE);
135
136 close(fp);
137 return size;
138}
139
140eImbFileType IMB_test_image_type_from_memory(const uchar *buf, const size_t buf_size)
141{
142 for (const ImFileType *type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
143 if (type->is_a != nullptr) {
144 if (type->is_a(buf, buf_size)) {
145 return type->filetype;
146 }
147 }
148 }
149
150 return IMB_FTYPE_NONE;
151}
152
154{
155 uchar buf[HEADER_SIZE];
156 const int64_t buf_size = imb_test_image_read_header_from_filepath(filepath, buf);
157 if (buf_size <= 0) {
158 return IMB_FTYPE_NONE;
159 }
160 return IMB_test_image_type_from_memory(buf, size_t(buf_size));
161}
162
163bool IMB_test_image_type_matches(const char *filepath, eImbFileType filetype)
164{
165 uchar buf[HEADER_SIZE];
166 const int64_t buf_size = imb_test_image_read_header_from_filepath(filepath, buf);
167 if (buf_size <= 0) {
168 return false;
169 }
170
171 const ImFileType *type = IMB_file_type_from_ftype(filetype);
172 if (type != nullptr) {
173 /* Requesting to load a type that can't check its own header doesn't make sense.
174 * Keep the check for developers. */
175 BLI_assert(type->is_a != nullptr);
176 if (type->is_a != nullptr) {
177 return type->is_a(buf, size_t(buf_size));
178 }
179 }
180 return false;
181}
182
183#undef HEADER_SIZE
184
185bool IMB_test_image(const char *filepath)
186{
187 return (IMB_test_image_type(filepath) != IMB_FTYPE_NONE);
188}
189
190} // namespace blender
File and directory operations.
#define O_BINARY
Compatibility-like things for windows.
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
BMesh Iterators.
bool IMB_test_image_type_matches(const char *filepath, eImbFileType filetype)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
const ImFileType * IMB_file_type_from_ftype(eImbFileType ftype)
Definition filetype.cc:328
int64_t BLI_read(int fd, void *buf, size_t nbytes)
Definition fileops_c.cc:98
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
unsigned char uchar
Definition BKE_text.h:58
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static int64_t imb_test_image_read_header_from_filepath(const char *filepath, uchar buf[HEADER_SIZE])
const ImFileType IMB_FILE_TYPES[]
Definition filetype.cc:24
eImbFileType IMB_test_image_type(const char *filepath)
eImbFileType IMB_test_image_type_from_memory(const unsigned char *buf, size_t buf_size)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool IMB_test_image(const char *filepath)
static CLG_LogRef LOG
Definition logging.cpp:12
struct stat BLI_stat_t
const ImFileType * IMB_FILE_TYPES_LAST
Definition filetype.cc:326
bool(*) is_a(const unsigned char *buf, size_t size)