Blender V4.5
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#define UTIL_DEBUG 0
27
28const char *imb_ext_image[] = {
29 /* #IMB_FTYPE_PNG */
30 ".png",
31 /* #IMB_FTYPE_TGA */
32 ".tga",
33 /* #IMB_FTYPE_BMP */
34 ".bmp",
35 /* #IMB_FTYPE_JPG */
36 ".jpg",
37 ".jpeg",
38 /* #IMB_FTYPE_IRIS */
39 ".sgi",
40 ".rgb",
41 ".rgba",
42 /* #IMB_FTYPE_TIF */
43 ".tif",
44 ".tiff",
45 /* A convention for naming tiled images at different resolutions (MIP-mapped),
46 * supported by various render engines texture caching systems.
47 * These are typically TIFF or EXR images. See the tool `maketx` from OpenImageIO. */
48 ".tx",
49#ifdef WITH_IMAGE_OPENJPEG
50 /* #IMB_FTYPE_JP2 */
51 ".jp2",
52 ".j2c",
53#endif
54 /* #IMB_FTYPE_RADHDR */
55 ".hdr",
56 /* #IMB_FTYPE_DDS */
57 ".dds",
58#ifdef WITH_IMAGE_CINEON
59 /* #IMB_FTYPE_DPX */
60 ".dpx",
61 /* #IMB_FTYPE_CINEON */
62 ".cin",
63#endif
64#ifdef WITH_IMAGE_OPENEXR
65 /* #IMB_FTYPE_EXR */
66 ".exr",
67#endif
68 /* #IMB_FTYPE_PSD */
69 ".psd",
70 ".pdd",
71 ".psb",
72#ifdef WITH_IMAGE_WEBP
73 /* #IMB_FTYPE_WEBP */
74 ".webp",
75#endif
76 nullptr,
77};
78
79const char *imb_ext_movie[] = {
80 ".avi", ".flc", ".mov", ".movie", ".mp4", ".m4v", ".m2v", ".m2t", ".m2ts", ".mts",
81 ".ts", ".mv", ".avs", ".wmv", ".ogv", ".ogg", ".r3d", ".dv", ".mpeg", ".mpg",
82 ".mpg2", ".vob", ".mkv", ".flv", ".divx", ".xvid", ".mxf", ".webm", ".gif", nullptr,
83};
84
86
87const char *imb_ext_audio[] = {
88 ".wav",
89 ".ogg",
90 ".oga",
91 ".mp3",
92 ".mp2",
93 ".ac3",
94 ".aac",
95 ".flac",
96 ".wma",
97 ".eac3",
98 ".aif",
99 ".aiff",
100 ".m4a",
101 ".mka",
102 ".opus",
103 nullptr,
104};
105
106/* OIIO will validate the entire header of some files and DPX requires 2048 */
107#define HEADER_SIZE 2048
108
110 uchar buf[HEADER_SIZE])
111{
112 BLI_stat_t st;
113 int fp;
114
115 BLI_assert(!BLI_path_is_rel(filepath));
116
117 if (UTIL_DEBUG) {
118 printf("%s: loading %s\n", __func__, filepath);
119 }
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.
@ 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 printf(...)
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)