9#include <OpenImageIO/filesystem.h>
10#include <OpenImageIO/typedesc.h>
13#include "util/colorspace.h"
15#include "util/image.h"
58 return OIIO::TypeUInt8;
61 return OIIO::TypeUInt16;
64 return OIIO::TypeHalf;
67 return OIIO::TypeFloat;
75 assert(!
"Unknown data type");
85 return sizeof(uint8_t);
87 return sizeof(uint8_t) * 4;
89 return sizeof(uint16_t);
91 return sizeof(uint16_t) * 4;
96 return sizeof(
half) * 4;
101 return sizeof(
float) * 3;
104 return sizeof(
float) * 4;
214 string_view metadata_color = spec.get_string_attribute(name);
215 if (metadata_color.size() == 0) {
220 while (metadata_color.size()) {
222 if (!OIIO::Strutil::parse_float(metadata_color, val)) {
225 color.push_back(val);
226 if (!OIIO::Strutil::parse_char(metadata_color,
',')) {
231 if (color.size() !=
size_t(spec.nchannels)) {
235 switch (spec.nchannels) {
237 r_color =
make_float4(color[0], color[0], color[0], 1.0f);
240 r_color =
make_float4(color[0], color[0], color[0], color[1]);
243 r_color =
make_float4(color[0], color[1], color[2], 1.0f);
246 r_color =
make_float4(color[0], color[1], color[2], color[3]);
254 const ImageSpec &spec,
255 OIIO::string_view filepath)
261 if (spec.tile_width == 0) {
265 const std::string software = spec.get_string_attribute(
"Software");
266 int tx_file_format_version =
INT_MAX;
267 sscanf(software.c_str(),
"Blender maketx v%d", &tx_file_format_version);
269 if (tx_file_format_version !=
INT_MAX &&
272 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
273 <<
" has tiles, but file format version " << tx_file_format_version
274 <<
" is not supported by this version of Cycles";
278 if (tx_file_format_version ==
INT_MAX) {
279 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
280 <<
" has tiles, but is missing blender:TxFileFormatVersion";
284 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
285 <<
" has tiles, but expected 1 or 4 channels, found " <<
channels;
292 bool has_tiles =
false;
293 bool is_small_image =
false;
296 uint32_t load_tile_size = spec.tile_width;
298 if (min_tile_size > 0) {
301 load_tile_size = std::max(load_tile_size, min_tile_size);
305 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
306 <<
" has tiles, but tile size not power of two (" << spec.tile_width <<
")";
308 else if (spec.tile_width != spec.tile_height) {
309 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
310 <<
" has tiles, but tile size is not square (" << spec.tile_width <<
"x"
311 << spec.tile_height <<
")";
313 else if (spec.tile_depth != 1) {
314 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
315 <<
" has tiles, but depth is not 1";
318 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
319 <<
" has tiles, but tile size too small (found " << spec.tile_width <<
", minimum "
322 else if (
width < load_tile_size &&
height < load_tile_size) {
327 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
328 <<
" has tiles, but image resolution is smaller than tile size";
330 is_small_image =
true;
341 for (
int miplevel = 0;; miplevel++) {
342 if (!input.seek_subimage(0, miplevel)) {
343 LOG_DEBUG <<
"Image " << OIIO::Filesystem::filename(filepath)
344 <<
" has tiles, but missing mip levels";
349 const int mip_width =
width >> miplevel;
350 const int mip_height =
height >> miplevel;
356 input.seek_subimage(0, 0);
368 if (!OIIO::Filesystem::exists(filepath)) {
369 LOG_WARNING <<
"Image file " << filepath <<
" does not exist.";
372 if (OIIO::Filesystem::is_directory(filepath)) {
373 LOG_WARNING <<
"Image file " << filepath <<
" is a directory, cannot use as image.";
377 std::unique_ptr<ImageInput>
in(ImageInput::create(filepath));
379 LOG_WARNING <<
"Image file " << filepath <<
" failed to load.";
387 config.attribute(
"oiio:UnassociatedAlpha", 1);
389 if (!
in->open(filepath, spec, config)) {
390 LOG_WARNING <<
"Image file " << filepath <<
" failed to open.";
394 if (spec.depth > 1) {
395 LOG_WARNING <<
"Image file " << filepath <<
" has unsupported depth of " << spec.depth;
404 size_t channel_size = spec.format.basesize();
409 if (spec.format.is_floating_point()) {
413 for (
size_t channel = 0; channel < spec.channelformats.size(); channel++) {
414 channel_size =
max(channel_size, spec.channelformats[channel].basesize());
415 if (spec.channelformats[channel].is_floating_point()) {
421 if (spec.format == TypeDesc::HALF) {
434 else if (spec.format == TypeDesc::USHORT) {
449 if (strcmp(
in->format_name(),
"targa") == 0) {
453 if (strcmp(
in->format_name(),
"dds") == 0) {
458 if (strcmp(
in->format_name(),
"psd") == 0) {
475 <<
", " << (
tile_size ?
"tiled" :
"untiled");
484template<
typename StorageType>
495 const int channels = metadata.
channels;
496 const bool is_rgba = metadata.
is_rgba();
503 for (
int64_t j = height - 1; j >= 0; j--) {
504 StorageType *out_pixels = pixels + j * out_y_stride;
505 StorageType *in_pixels = pixels + j * in_y_stride;
506 for (
int64_t i = width - 1; i >= 0; i--) {
507 out_pixels[i * 4 + 3] = in_pixels[i * x_stride + 1];
508 out_pixels[i * 4 + 2] = in_pixels[i * x_stride + 0];
509 out_pixels[i * 4 + 1] = in_pixels[i * x_stride + 0];
510 out_pixels[i * 4 + 0] = in_pixels[i * x_stride + 0];
514 else if (channels == 3) {
516 for (
int64_t j = height - 1; j >= 0; j--) {
517 StorageType *out_pixels = pixels + j * out_y_stride;
518 StorageType *in_pixels = pixels + j * in_y_stride;
519 for (
int64_t i = width - 1; i >= 0; i--) {
520 out_pixels[i * 4 + 3] = one;
521 out_pixels[i * 4 + 2] = in_pixels[i * x_stride + 2];
522 out_pixels[i * 4 + 1] = in_pixels[i * x_stride + 1];
523 out_pixels[i * 4 + 0] = in_pixels[i * x_stride + 0];
527 else if (channels == 1) {
529 for (
int64_t j = height - 1; j >= 0; j--) {
530 StorageType *out_pixels = pixels + j * out_y_stride;
531 StorageType *in_pixels = pixels + j * in_y_stride;
532 for (
int64_t i = width - 1; i >= 0; i--) {
533 out_pixels[i * 4 + 3] = one;
534 out_pixels[i * 4 + 2] = in_pixels[i * x_stride];
535 out_pixels[i * 4 + 1] = in_pixels[i * x_stride];
536 out_pixels[i * 4 + 0] = in_pixels[i * x_stride];
544 for (
int64_t j = 0; j < height; j++) {
545 StorageType *pixel = pixels + j * out_y_stride;
546 for (
int64_t i = 0; i < width; i++, pixel += 4) {
547 const StorageType alpha = pixel[3];
557 for (
int64_t j = 0; j < height; j++) {
558 StorageType *out_pixels = pixels + j * out_y_stride;
559 for (
int64_t i = 0; i < width; i++) {
560 out_pixels[i * 4 + 3] = one;
582 if constexpr (std::is_same_v<float, StorageType> || std::is_same_v<half, StorageType>) {
587 for (
int64_t j = 0; j < height; j++) {
588 StorageType *pixel = pixels + j * out_y_stride;
589 for (
int64_t i = 0; i < width; i++, pixel += 4) {
602 for (
int64_t j = 0; j < height; j++) {
603 StorageType *pixel = pixels + j * out_y_stride;
604 for (
int64_t i = 0; i < width; i++, pixel++) {
619 const int64_t out_y_stride)
const
625 *
this,
static_cast<uchar *
>(pixels),
width,
height, x_stride, in_y_stride, out_y_stride);
630 static_cast<uint16_t *
>(pixels),
640 *
this,
static_cast<half *
>(pixels),
width,
height, x_stride, in_y_stride, out_y_stride);
645 *
this,
static_cast<float *
>(pixels),
width,
height, x_stride, in_y_stride, out_y_stride);
663template<TypeDesc::BASETYPE FileFormat,
typename StorageType>
665 const std::unique_ptr<ImageInput> &
in,
671 const int channels = metadata.
channels;
672 const int64_t num_pixels = width * height;
673 int64_t read_y_stride = width * channels *
sizeof(StorageType);
676 StorageType *readpixels = pixels;
679 tmppixels.resize(num_pixels * channels);
680 readpixels = &tmppixels[0];
683 if (!
in->read_image(0,
688 (flip_y) ? (
uchar *)readpixels + (height - 1) * read_y_stride :
691 (flip_y) ? -read_y_stride : read_y_stride,
698 for (
int64_t j = 0; j < height; j++) {
699 const StorageType *in_pixels = tmppixels.data() + j * width * channels;
700 StorageType *out_pixels = pixels + j * width * 4;
701 for (
int64_t i = 0; i < width; i++) {
702 out_pixels[i * 4 + 3] = in_pixels[i * channels + 3];
703 out_pixels[i * 4 + 2] = in_pixels[i * channels + 2];
704 out_pixels[i * 4 + 1] = in_pixels[i * channels + 1];
705 out_pixels[i * 4 + 0] = in_pixels[i * channels + 0];
716 const bool flip_y)
const
719 std::unique_ptr<ImageInput>
in = ImageInput::create(filepath);
724 ImageSpec spec = ImageSpec();
725 ImageSpec config = ImageSpec();
730 config.attribute(
"oiio:UnassociatedAlpha", 1);
732 if (!
in->open(filepath, spec, config)) {
736 if (spec.depth > 1) {
737 LOG_WARNING <<
"Image file " << filepath <<
" has unsupported depth " << spec.depth;
static bool colorspace_is_data(ustring colorspace)
static ustring detect_known_colorspace(ustring colorspace, const char *file_colorspace, const char *file_format, bool is_float)
static void to_scene_linear(ustring colorspace, T *pixels, const int64_t width, const int64_t height, const int64_t y_stride, bool is_rgba, bool compress_as_srgb, bool ignore_alpha)
TextureCache texture_cache
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
T util_image_is_finite(T a)
T util_image_cast_from_float(const float value)
T util_image_multiply_native(T a, T b)
#define CCL_NAMESPACE_END
#define assert(assertion)
ustring make_tx_get_file_colorspace(const ImageMetaData &metadata)
#define TX_FILE_FORMAT_VERSION
ccl_device_inline uint next_power_of_two(const uint x)
#define KERNEL_IMAGE_TEX_PADDING
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ccl_device_inline bool is_power_of_two(const size_t x)
@ IMAGE_DATA_TYPE_NANOVDB_FP16
@ IMAGE_DATA_TYPE_USHORT4
@ IMAGE_DATA_TYPE_NANOVDB_EMPTY
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT4
@ IMAGE_DATA_TYPE_NANOVDB_FPN
@ IMAGE_ALPHA_CHANNEL_PACKED
@ IMAGE_ALPHA_UNASSOCIATED
ccl_device_inline bool is_nanovdb_type(int type)
ustring u_colorspace_scene_linear_srgb
ustring u_colorspace_scene_linear
ustring u_colorspace_data