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