Blender V5.3
image_oiio.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/image_oiio.h"
6
7#include "util/image.h"
8#include "util/image_maketx.h"
10#include "util/path.h"
11#include "util/progress.h"
12#include "util/string.h"
13#include "util/types_base.h"
14#include "util/types_image.h"
15#include "util/unique_ptr.h"
16
18
19/* File handle cache limiter. This is shared across multiple Cycles instances as
20 * the limit we risk running into is per process.
21 *
22 * Increasing this limit to be closer to system_max_open_files() would improve
23 * performance, and on Windows the system limit is adjustable. However this would
24 * need careful testing as other parts of the software uses file handles too. */
26
27OIIOImageLoader::OIIOImageLoader(const string &filepath) : original_filepath_(filepath) {}
28
30
32 const ImageLoaderParams &params,
33 Progress &progress)
34{
35 if (original_filepath_.empty()) {
36 return false;
37 }
38
39 if (params.use_texture_cache) {
40 const std::string &filepath = get_filepath();
41 const bool found = resolve_tx(filepath,
42 params.texture_cache_path,
43 params.colorspace,
44 params.alpha_type,
47 metadata);
48
49 if (found) {
50 return true;
51 }
52
53 if (params.auto_texture_cache) {
54 progress.set_status("Generating tx cache | " + path_filename(filepath), "");
55
56 if (!make_tx(filepath,
58 params.colorspace,
59 params.alpha_type,
61 {
63 params.tx_failure_num++;
64 }
65 }
66 else {
68 }
69 }
70
71 if (!metadata.oiio_load_metadata(get_filepath())) {
72 params.load_failure_num++;
73 return false;
74 }
75
76 return true;
77}
78
79bool OIIOImageLoader::load_pixels(const ImageMetaData &metadata, void *pixels)
80{
81 if (!metadata.oiio_load_pixels(get_filepath(), pixels)) {
82 return false;
83 }
84
85 metadata.conform_pixels(pixels);
86 return true;
87}
88
89template<typename StorageType>
91 const ImageMetaData &metadata,
92 const int miplevel,
93 const int64_t x,
94 const int64_t y,
95 const int64_t w,
96 const int64_t h,
97 const OIIO::TypeDesc typedesc,
98 const int64_t x_stride,
99 const int64_t y_stride,
100 StorageType *pixels)
101
102{
103 const int channels = metadata.channels;
104 int64_t read_x_stride = x_stride;
105 int64_t read_y_stride = y_stride;
106
107 /* Read pixels through OpenImageIO. */
108 StorageType *readpixels = pixels;
109 vector<StorageType> tmppixels;
110 if (channels > 4) {
111 tmppixels.resize(w * h * channels);
112 readpixels = &tmppixels[0];
113 read_x_stride = channels * sizeof(StorageType);
114 read_y_stride = w * channels * sizeof(StorageType);
115 }
116
117 if (!in->read_tiles(0,
118 miplevel,
119 x,
120 x + w,
121 y,
122 y + h,
123 0,
124 1,
125 0,
126 channels,
127 typedesc,
128 readpixels,
129 read_x_stride,
130 read_y_stride))
131 {
132 return false;
133 }
134
135 if (channels > 4) {
136 for (int64_t j = 0; j < h; j++) {
137 const StorageType *in_pixels = tmppixels.data() + j * w * channels;
138 StorageType *out_pixels = pixels + j * (y_stride / sizeof(StorageType));
139 for (int64_t i = 0; i < w; i++) {
140 out_pixels[i * 4 + 3] = in_pixels[i * channels + 3];
141 out_pixels[i * 4 + 2] = in_pixels[i * channels + 2];
142 out_pixels[i * 4 + 1] = in_pixels[i * channels + 1];
143 out_pixels[i * 4 + 0] = in_pixels[i * channels + 0];
144 }
145 }
146 tmppixels.clear();
147 }
148
149 /* Can skip conform for speed if it's a Blender native tx file. */
150 if (metadata.tile_need_conform) {
151 metadata.conform_pixels(pixels,
152 w,
153 h,
154 x_stride / sizeof(StorageType),
155 y_stride / sizeof(StorageType),
156 y_stride / sizeof(StorageType));
157 }
158
159 return true;
160}
161
163 const ImageMetaData &metadata,
164 const int64_t /*height*/,
165 const int miplevel,
166 const int64_t x,
167 const int64_t y,
168 const int64_t w,
169 const int64_t h,
170 const int64_t x_stride,
171 const int64_t y_stride,
172 uint8_t *pixels)
173{
174 switch (metadata.type) {
178 in, metadata, miplevel, x, y, w, h, TypeDesc::UINT8, x_stride, y_stride, pixels);
182 metadata,
183 miplevel,
184 x,
185 y,
186 w,
187 h,
188 TypeDesc::USHORT,
189 x_stride,
190 y_stride,
191 reinterpret_cast<uint16_t *>(pixels));
192 break;
196 metadata,
197 miplevel,
198 x,
199 y,
200 w,
201 h,
202 TypeDesc::HALF,
203 x_stride,
204 y_stride,
205 reinterpret_cast<half *>(pixels));
209 metadata,
210 miplevel,
211 x,
212 y,
213 w,
214 h,
215 TypeDesc::FLOAT,
216 x_stride,
217 y_stride,
218 reinterpret_cast<float *>(pixels));
226 return false;
227 }
228
229 return false;
230}
231
233 const ImageMetaData &metadata,
234 const int64_t width,
235 const int64_t height,
236 const int miplevel,
237 const int64_t x,
238 const int64_t y,
239 const int64_t w,
240 const int64_t h,
241 const int64_t x_stride,
242 const int64_t y_stride,
243 const int x_adjacent,
244 const int y_adjacent,
245 const int64_t padding,
246 const ExtensionType extension,
247 uint8_t *pixels)
248{
249 const int64_t tile_size = metadata.tile_size;
250
251 int64_t x_new = x + x_adjacent * tile_size;
252 int64_t y_new = y + y_adjacent * tile_size;
253
254 const bool in_range = x_new >= 0 && x_new < width && y_new >= 0 && y_new < height;
255
256 const int64_t pad_x = (x_adjacent < 0) ? 0 : (x_adjacent == 0) ? padding : padding + w;
257 const int64_t pad_y = (y_adjacent < 0) ? 0 : (y_adjacent == 0) ? padding : padding + h;
258 const int64_t pad_w = (x_adjacent == 0) ? w : padding;
259 const int64_t pad_h = (y_adjacent == 0) ? h : padding;
260
261 if (!in_range) {
262 /* Adjacent tile does not exist, fill in padding depending on extension mode. */
263 if (extension == EXTENSION_EXTEND) {
264 /* Duplicate pixels from border of tile. */
265 for (int64_t j = 0; j < pad_h; j++) {
266 for (int64_t i = 0; i < pad_w; i++) {
267 const int64_t source_x = (x_adjacent < 0) ? 0 : (x_adjacent == 0) ? i : w - 1;
268 const int64_t source_y = (y_adjacent < 0) ? 0 : (y_adjacent == 0) ? j : h - 1;
269
270 std::copy_n(pixels + (padding + source_x) * x_stride + (padding + source_y) * y_stride,
271 x_stride,
272 pixels + (pad_x + i) * x_stride + (pad_y + j) * y_stride);
273 }
274 }
275 return true;
276 }
277 if (extension == EXTENSION_MIRROR) {
278 /* Mirror pixels from border of tile. */
279 for (int64_t j = 0; j < pad_h; j++) {
280 for (int64_t i = 0; i < pad_w; i++) {
281 const int64_t source_x = (x_adjacent < 0) ? padding - 1 - i :
282 (x_adjacent == 0) ? i :
283 w - 1 - i;
284 const int64_t source_y = (y_adjacent < 0) ? padding - 1 - j :
285 (y_adjacent == 0) ? j :
286 h - 1 - j;
287
288 std::copy_n(pixels + (padding + source_x) * x_stride + (padding + source_y) * y_stride,
289 x_stride,
290 pixels + (pad_x + i) * x_stride + (pad_y + j) * y_stride);
291 }
292 }
293 return true;
294 }
295 if (extension == EXTENSION_CLIP) {
296 /* Fill with zeros. */
297 for (int64_t j = 0; j < pad_h; j++) {
298 std::fill_n(pixels + pad_x * x_stride + (pad_y + j) * y_stride, x_stride * pad_w, 0);
299 }
300 return true;
301 }
302 if (extension == EXTENSION_REPEAT) {
303 /* Wrap around for repeat mode. */
304 if (x_new < 0) {
305 x_new = (divide_up(width, tile_size) - 1) * tile_size;
306 }
307 else if (x_new >= width) {
308 x_new = 0;
309 }
310
311 if (y_new < 0) {
312 y_new = (divide_up(height, tile_size) - 1) * tile_size;
313 }
314 else if (y_new >= height) {
315 y_new = 0;
316 }
317 }
318 }
319
320 /* Load adjacent tile, clamped to the image bounds as the adjacent tile may
321 * be at the edge and smaller than tile_size.
322 *
323 * OpenImageIO accepts either full tile size or clamped, however we support
324 * loading multiple smaller tiles in one go so tile size can not be assumed
325 * to match. */
326 const int64_t adj_tile_w = std::min(width - x_new, tile_size);
327 const int64_t adj_tile_h = std::min(height - y_new, tile_size);
328
329 vector<uint8_t> tile_pixels(tile_size * tile_size * x_stride, 0);
331 metadata,
332 height,
333 miplevel,
334 x_new,
335 y_new,
336 adj_tile_w,
337 adj_tile_h,
338 x_stride,
339 tile_size * x_stride,
340 tile_pixels.data()))
341 {
342 return false;
343 }
344 const int64_t tile_x = (x_adjacent < 0) ? adj_tile_w - padding : 0;
345 const int64_t tile_y = (y_adjacent < 0) ? adj_tile_h - padding : 0;
346
347 for (int64_t j = 0; j < pad_h; j++) {
348 std::copy_n(tile_pixels.data() + tile_x * x_stride + (tile_y + j) * (tile_size * x_stride),
349 x_stride * pad_w,
350 pixels + pad_x * x_stride + (pad_y + j) * y_stride);
351 }
352
353 return true;
354}
355
357 const int miplevel,
358 const int64_t x,
359 const int64_t y,
360 const int64_t w,
361 const int64_t h,
362 const int64_t x_stride,
363 const int64_t y_stride,
364 const int64_t padding,
365 const ExtensionType extension,
366 uint8_t *pixels)
367{
368 assert(metadata.tile_size != 0);
369
370 /* Don't keep retrying files that failed to open once. */
372 return false;
373 }
374
375 /* Create cached file handle if it was not created yet or it was deleted by the cache limiter. */
378 const string &filepath = get_filepath();
379 unique_ptr<ImageInput> in = unique_ptr<ImageInput>(ImageInput::create(filepath));
380 if (!in) {
382 return std::unique_ptr<ImageInput>();
383 }
384
385 ImageSpec spec = ImageSpec();
386 ImageSpec config;
387 config.attribute("oiio:UnassociatedAlpha", 1);
388
389 if (!in->open(filepath, spec, config)) {
391 return std::unique_ptr<ImageInput>();
392 }
393
394 return in;
395 });
396
398 return false;
399 }
400
401 const int64_t width = metadata.width >> miplevel;
402 const int64_t height = metadata.height >> miplevel;
403
404 /* Load center pixels. */
405 bool ok = oiio_load_pixels_tile(file_handle_user.get(),
406 metadata,
407 height,
408 miplevel,
409 x,
410 y,
411 w,
412 h,
413 x_stride,
414 y_stride,
415 pixels + padding * x_stride + padding * y_stride);
416
417 /* Pad tile borders from adjacent tiles. */
418 if (padding > 0) {
419 for (int j = -1; j <= 1; j++) {
420 for (int i = -1; i <= 1; i++) {
421 if (i == 0 && j == 0) {
422 continue;
423 }
424 ok &= oiio_load_pixels_tile_adjacent(file_handle_user.get(),
425 metadata,
426 width,
427 height,
428 miplevel,
429 x,
430 y,
431 w,
432 h,
433 x_stride,
434 y_stride,
435 i,
436 j,
437 padding,
438 extension,
439 pixels);
440 }
441 }
442 }
443
444 return ok;
445}
446
448{
449 return path_filename(get_filepath());
450}
451
456
457bool OIIOImageLoader::equals(const ImageLoader &other) const
458{
459 const OIIOImageLoader &other_loader = (const OIIOImageLoader &)other;
460 return original_filepath_ == other_loader.original_filepath_;
461}
462
long long int int64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
const std::unique_ptr< T > & get() const
CacheHandleGuard< T > acquire(CacheLimiter< T > &limiter, const std::function< std::unique_ptr< T >()> &creator)
ImageDataType type
bool oiio_load_metadata(OIIO::string_view filepath, OIIO::ImageSpec *r_spec=nullptr)
bool oiio_load_pixels(OIIO::string_view filepath, void *pixels, const bool flip_y=true) const
uint32_t tile_size
void conform_pixels(void *pixels) const
bool texture_cache_file_handle_failed
Definition image_oiio.h:49
const string & get_filepath() const
bool equals(const ImageLoader &other) const override
OIIOImageLoader(const string &filepath)
string texture_cache_filepath_
Definition image_oiio.h:47
bool load_metadata(ImageMetaData &metadata, const ImageLoaderParams &params, Progress &progress) override
CacheHandle< ImageInput > texture_cache_file_handle
Definition image_oiio.h:48
bool load_pixels(const ImageMetaData &metadata, void *pixels) override
~OIIOImageLoader() override
bool load_pixels_tile(const ImageMetaData &metadata, int miplevel, int64_t x, int64_t y, int64_t w, int64_t h, int64_t x_stride, int64_t y_stride, int64_t padding, ExtensionType extension, uint8_t *pixels) override
string name() const override
string original_filepath_
Definition image_oiio.h:46
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
Definition half.h:29
#define CCL_NAMESPACE_END
#define assert(assertion)
bool resolve_tx(const string &filepath, const string &texture_cache_path, ustring colorspace, const ImageAlphaType alpha_type, const ImageFormatType format_type, string &out_filepath, ImageMetaData &out_metadata)
bool make_tx(const string &filepath, const string &out_filepath, ustring colorspace, const ImageAlphaType alpha_type, const ImageFormatType format_type)
static bool oiio_load_pixels_tile_adjacent(const unique_ptr< ImageInput > &in, const ImageMetaData &metadata, const int64_t width, const int64_t height, const int miplevel, const int64_t x, const int64_t y, const int64_t w, const int64_t h, const int64_t x_stride, const int64_t y_stride, const int x_adjacent, const int y_adjacent, const int64_t padding, const ExtensionType extension, uint8_t *pixels)
static bool oiio_load_pixels_tile(const unique_ptr< ImageInput > &in, const ImageMetaData &metadata, const int miplevel, const int64_t x, const int64_t y, const int64_t w, const int64_t h, const OIIO::TypeDesc typedesc, const int64_t x_stride, const int64_t y_stride, StorageType *pixels)
static CCL_NAMESPACE_BEGIN CacheLimiter< ImageInput > cache_limiter_image_input(128)
string path_filename(const string &path)
Definition path.cpp:399
string texture_cache_path
std::atomic< int > & tx_failure_num
std::atomic< int > & load_failure_num
ImageAlphaType alpha_type
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:54
@ IMAGE_DATA_NUM_TYPES
Definition types_image.h:51
@ IMAGE_DATA_TYPE_BYTE
Definition types_image.h:40
@ IMAGE_DATA_TYPE_FLOAT
Definition types_image.h:39
@ IMAGE_DATA_TYPE_NANOVDB_FP16
Definition types_image.h:48
@ IMAGE_DATA_TYPE_FLOAT4
Definition types_image.h:36
@ IMAGE_DATA_TYPE_USHORT4
Definition types_image.h:42
@ IMAGE_DATA_TYPE_USHORT
Definition types_image.h:43
@ IMAGE_DATA_TYPE_NANOVDB_EMPTY
Definition types_image.h:49
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
Definition types_image.h:44
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
Definition types_image.h:45
@ IMAGE_DATA_TYPE_HALF
Definition types_image.h:41
@ IMAGE_DATA_TYPE_BYTE4
Definition types_image.h:37
@ IMAGE_DATA_TYPE_HALF4
Definition types_image.h:38
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT4
Definition types_image.h:46
@ IMAGE_DATA_TYPE_NANOVDB_FPN
Definition types_image.h:47
ExtensionType
Definition types_image.h:80
@ EXTENSION_REPEAT
Definition types_image.h:82
@ EXTENSION_CLIP
Definition types_image.h:86
@ EXTENSION_EXTEND
Definition types_image.h:84
@ EXTENSION_MIRROR
Definition types_image.h:88
@ IMAGE_FORMAT_PLAIN
Definition types_image.h:73