15#include <OpenImageIO/Imath.h>
16#include <OpenImageIO/color.h>
17#include <OpenImageIO/filesystem.h>
18#include <OpenImageIO/filter.h>
19#include <OpenImageIO/fmath.h>
20#include <OpenImageIO/imagebuf.h>
21#include <OpenImageIO/imagebufalgo.h>
22#include <OpenImageIO/imagebufalgo_util.h>
23#include <OpenImageIO/imageio.h>
24#include <OpenImageIO/string_view.h>
25#include <OpenImageIO/strutil.h>
26#include <OpenImageIO/sysutil.h>
27#include <OpenImageIO/thread.h>
30#include "util/colorspace.h"
31#include "util/image.h"
45 OIIO::Sysutil::get_local_time(&t, &mytm);
55template<
class SRCTYPE>
59 OIIO::span<float> pixel,
61 OIIO::ImageBuf::ConstIterator<SRCTYPE> &it,
62 OIIO::ImageBuf::WrapMode wrapmode)
64 const ImageSpec &spec(buf.spec());
69 x =
static_cast<float>(fx) +
x *
static_cast<float>(fw);
70 y =
static_cast<float>(fy) +
y *
static_cast<float>(fh);
72 int n = spec.nchannels;
73 float *p0 = OIIO_ALLOCA(
float, 4 * n), *p1 = p0 + n, *p2 = p1 + n, *p3 = p2 + n;
83 it.rerange(xtexel, xtexel + 2, ytexel, ytexel + 2, 0, 1, wrapmode);
84 for (
int c = 0; c < n; ++c) {
88 for (
int c = 0; c < n; ++c) {
92 for (
int c = 0; c < n; ++c) {
96 for (
int c = 0; c < n; ++c) {
106 int ymax = ymin + spec.height - 1;
107 int ynext = OIIO::clamp(ytexel + 1, ymin, ymax);
108 ytexel = OIIO::clamp(ytexel, ymin, ymax);
109 float w0 = (1.0f - yfrac) *
sinf((
float)
M_PI * (ytexel + 0.5f) / (
float)fh);
110 float w1 = yfrac *
sinf((
float)
M_PI * (ynext + 0.5f) / (
float)fh);
111 yfrac = w1 / (w0 + w1);
115 OIIO::bilerp(p0, p1, p2, p3, xfrac, yfrac, n, pixel.data());
120template<
class SRCTYPE>
122 const OIIO::ImageBuf &src,
126 int x0 = roi.xbegin, x1 = roi.xend, y0 = roi.ybegin, y1 = roi.yend;
128 const ImageSpec &dstspec(dst.spec());
129#if OIIO_VERSION_MAJOR > 3 || (OIIO_VERSION_MAJOR == 3 && OIIO_VERSION_MINOR >= 0)
130 OIIO::span<float> pel = OIIO::OIIO_ALLOCA_SPAN(
float, dstspec.nchannels);
132 std::vector<float> pel_buf(dstspec.nchannels);
133 OIIO::span<float> pel(pel_buf.data(), dstspec.nchannels);
135 float xoffset = (
float)dstspec.x;
136 float yoffset = (
float)dstspec.y;
137 float xscale = 1.0f / (
float)dstspec.width;
138 float yscale = 1.0f / (
float)dstspec.height;
139 int nchannels = dst.nchannels();
140 assert(dst.spec().format == TypeFloat);
141 OIIO::ImageBuf::WrapMode wrapmode = OIIO::ImageBuf::WrapClamp;
142 OIIO::ImageBuf::ConstIterator<SRCTYPE> srcit(src);
143 OIIO::ImageBuf::Iterator<float> d(dst, roi);
144 for (
int y = y0;
y < y1; ++
y) {
145 float t = (
y + 0.5f) * yscale + yoffset;
146 for (
int x = x0;
x < x1; ++
x, ++d) {
147 float s = (
x + 0.5f) * xscale + xoffset;
149 for (
int c = 0; c < nchannels; ++c) {
158template<
class SRCTYPE>
159static void halve_scanline(
const SRCTYPE *s,
const int nchannels,
size_t sw,
float *dst)
161 for (
size_t i = 0; i < sw; i += 2, s += nchannels) {
162 for (
int j = 0; j < nchannels; ++j, ++dst, ++s) {
163 *dst = 0.5f * (
float)(*s + *(s + nchannels));
170template<
class SRCTYPE>
175 if (src.spec().width % 2 || src.spec().height % 2) {
179 assert(roi.ybegin + roi.height() <= dst.spec().height);
182 const int nchannels = dst.nchannels();
183 const size_t row_elem = roi.width() * nchannels;
184 std::unique_ptr<float[]> S0(
new float[row_elem]);
185 std::unique_ptr<float[]> S1(
new float[row_elem]);
190 const SRCTYPE *s = (
const SRCTYPE *)src.localpixels();
191 SRCTYPE *d = (SRCTYPE *)dst.localpixels();
193 d +=
int64_t(roi.ybegin) * dst.spec().width * nchannels;
194 const size_t ystride = src.spec().width * nchannels;
195 s += 2 * roi.ybegin * ystride;
198 const size_t dw = roi.width(), dh = roi.height();
199 const size_t sw = dw * 2;
200 for (
size_t y = 0;
y < dh; ++
y) {
205 const float *s0 = &S0[0], *s1 = &S1[0];
206 for (
size_t x = 0;
x < dw; ++
x) {
207 for (
int i = 0; i < nchannels; ++i, ++s0, ++s1, ++d) {
208 *d = (SRCTYPE)(0.5f * (*s0 + *s1));
216template<
typename SRCTYPE>
218 const OIIO::ImageBuf &src,
222 const ImageSpec &srcspec(src.spec());
223 const ImageSpec &dstspec(dst.spec());
224 assert(dstspec.nchannels == srcspec.nchannels);
225 assert(dst.localpixels());
227 if (src.localpixels() &&
230 dstspec.width == roi.width() &&
231 (2 * dstspec.width) == srcspec.width &&
232 dstspec.format == srcspec.format &&
233 dstspec.x == 0 && dstspec.y == 0 &&
234 srcspec.x == 0 && srcspec.y == 0)
241 assert(dst.spec().format == TypeFloat);
249 int n = buf.nchannels();
251#if OIIO_VERSION_MAJOR > 3 || (OIIO_VERSION_MAJOR == 3 && OIIO_VERSION_MINOR >= 0)
252 OIIO::span<float> left = OIIO::OIIO_ALLOCA_SPAN(
float, n);
253 OIIO::span<float> right = OIIO::OIIO_ALLOCA_SPAN(
float, n);
255 std::vector<float> left_buf(n);
256 std::vector<float> right_buf(n);
258 OIIO::span<float> left(left_buf.data(), n);
259 OIIO::span<float> right(right_buf.data(), n);
263 float wscale = 1.0f / (buf.spec().width);
264 for (
int j = 0; j <= 1; ++j) {
265 int y = (j == 0) ? buf.ybegin() : buf.yend() - 1;
267 for (
int c = 0; c < n; ++c) {
270 for (
int x = buf.xbegin();
x < buf.xend(); ++
x) {
271#if OIIO_VERSION_MAJOR > 3 || (OIIO_VERSION_MAJOR == 3 && OIIO_VERSION_MINOR >= 0)
272 buf.getpixel(
x,
y, right);
274 buf.getpixel(
x,
y, right.data());
276 for (
int c = 0; c < n; ++c) {
280 for (
int c = 0; c < n; ++c) {
283 for (
int x = buf.xbegin();
x < buf.xend(); ++
x) {
284 buf.setpixel(
x,
y, left);
289 for (
int y = buf.ybegin();
y < buf.yend(); ++
y) {
290#if OIIO_VERSION_MAJOR > 3 || (OIIO_VERSION_MAJOR == 3 && OIIO_VERSION_MINOR >= 0)
291 buf.getpixel(buf.xbegin(),
y, left);
292 buf.getpixel(buf.xend() - 1,
y, right);
294 buf.getpixel(buf.xbegin(),
y, left.data());
295 buf.getpixel(buf.xend() - 1,
y, right.data());
297 for (
int c = 0; c < n; ++c) {
298 left[c] = 0.5f * left[c] + 0.5f * right[c];
300 buf.setpixel(buf.xbegin(),
y, left);
301 buf.setpixel(buf.xend() - 1,
y, left);
311 std::time_t in_time = OIIO::Filesystem::last_write_time(filepath);
312 std::time_t out_time = OIIO::Filesystem::last_write_time(tx_filepath);
314 if (in_time == out_time) {
315 LOG_INFO <<
"Using texture cache file: " << tx_filepath;
319 LOG_INFO <<
"Texture cache file is outdated: " << tx_filepath;
325 switch (alpha_type) {
330 return "unassociated";
333 return "channel_packed";
344 assert(!
"Unknown tx alpha type");
351 switch (format_type) {
355 return "equiangular";
358 assert(!
"Unknown tx format type");
364 const string &texture_cache_path,
368 const bool absolute_texture_cache)
392 if (absolute_texture_cache) {
402 const string &texture_cache_path,
406 string &out_filepath,
418 if (!texture_cache_path.empty()) {
421 filepath, texture_cache_path,
colorspace, alpha_type, format_type, absolute_cache_path);
423 absolute_cache_path ?
string(texture_cache_path) :
path_join(filedir, texture_cache_path),
426 out_filepath = tx_filepath;
436 const char *default_texture_cache_dir =
"blender_tx";
437 if (texture_cache_path != default_texture_cache_dir) {
439 filepath, texture_cache_path,
colorspace, alpha_type, format_type,
false);
440 const string tx_default_filepath =
path_join(
path_join(filedir, default_texture_cache_dir),
444 out_filepath = tx_default_filepath;
449 if (texture_cache_path.empty()) {
450 out_filepath = tx_default_filepath;
457 out_filepath = filepath;
466 OIIO::ImageBufAlgo::PixelStats pixel_stats = OIIO::ImageBufAlgo::computePixelStats(buf);
469 std::vector<float> constant_color(buf.nchannels());
470 bool is_constant_color = (pixel_stats.min == pixel_stats.max);
471 if (is_constant_color) {
472 constant_color = pixel_stats.min;
474 ImageSpec spec = buf.spec();
475 spec.width = std::min(spec.tile_width, spec.width);
476 spec.height = std::min(spec.tile_height, spec.height);
477 spec.depth = std::min(spec.tile_depth, spec.depth);
479 OIIO::ImageBufAlgo::fill(buf, constant_color);
480 LOG_INFO <<
" Constant color image detected. Creating " << spec.width <<
"x" << spec.height
481 <<
" texture instead.";
486 const bool is_opaque = buf.spec().alpha_channel == buf.nchannels() - 1 &&
487 pixel_stats.min[buf.spec().alpha_channel] == 1.0f &&
488 pixel_stats.max[buf.spec().alpha_channel] == 1.0f;
491 LOG_INFO <<
" Alpha==1 image detected. Dropping the alpha channel.";
492 OIIO::ImageBuf new_buf(buf.spec());
493 OIIO::ImageBufAlgo::channels(new_buf,
497 OIIO::cspan<float>(),
498 OIIO::cspan<std::string>(),
500 std::swap(buf, new_buf);
505 if (is_opaque && (buf.nchannels() == 3 || buf.nchannels() == 4) &&
506 pixel_stats.avg[0] == pixel_stats.avg[1] && pixel_stats.avg[0] == pixel_stats.avg[2] &&
507 OIIO::ImageBufAlgo::isMonochrome(buf))
509 LOG_INFO <<
" Monochrome image detected. Converting to single channel texture.";
510 OIIO::ImageBuf new_buf(buf.spec());
511 OIIO::ImageBufAlgo::channels(new_buf,
515 OIIO::cspan<float>(),
516 OIIO::cspan<std::string>(),
518 new_buf.specmod().default_channel_names();
519 std::swap(buf, new_buf);
527 const int sha1_blocksize = 256;
528 string extra_hash_data;
529 std::string hash_digest = OIIO::ImageBufAlgo::computePixelHashSHA1(
530 buf, extra_hash_data, OIIO::ROI::All(), sha1_blocksize);
531 if (hash_digest.length()) {
533 buf.specmod().attribute(
"oiio:SHA-1", hash_digest);
539 desc +=
"oiio:SHA-1=";
546 if (is_constant_color) {
547 std::string colstr = OIIO::Strutil::join(constant_color,
",", buf.spec().nchannels);
549 buf.specmod().attribute(
"oiio:ConstantColor", colstr);
552 desc +=
string_printf(
"%soiio:ConstantColor=%s", desc.length() ?
" " :
"", colstr.c_str());
554 LOG_DEBUG <<
" ConstantColor: " << colstr;
558 std::string avgstr = OIIO::Strutil::join(pixel_stats.avg,
",", buf.spec().nchannels);
560 buf.specmod().attribute(
"oiio:AverageColor", avgstr);
563 desc +=
string_printf(
"%soiio:AverageColor=%s", desc.length() ?
" " :
"", avgstr.c_str());
565 LOG_DEBUG <<
" AverageColor: " << avgstr;
568 buf.specmod().attribute(
"ImageDescription", desc);
574 parallel_for(blocked_range<int64_t>(0, num, 65536), [&](
const blocked_range<int64_t> &range) {
575 for (
int64_t i = range.begin(); i < range.end(); i++) {
584 if (out_format != TypeDesc::HALF) {
588 assert(buf.spec().format == TypeFloat);
590 const int64_t num_values =
int64_t(buf.spec().width) * buf.spec().height * buf.spec().nchannels;
591 float *pixels =
static_cast<float *
>(buf.localpixels());
593 [&](
const int64_t i) { pixels[i] =
clamp(pixels[i], -HALF_MAX, HALF_MAX); });
597 const bool from_srgb,
598 const bool unassociate_alpha)
600 assert(buf.spec().format == TypeFloat);
601 assert(buf.spec().nchannels == 1 || buf.spec().nchannels == 4);
603 const int64_t num_pixels =
int64_t(buf.spec().width) * buf.spec().height;
605 if (buf.spec().nchannels == 1) {
606 float *pixels =
static_cast<float *
>(buf.localpixels());
617 float4 *pixels =
static_cast<float4 *
>(buf.localpixels());
620 const bool has_alpha = unassociate_alpha && !(value.
w <= 0.0f || value.
w == 1.0f);
623 const float inv_alpha = 1.0f / value.
w;
624 value.
x *= inv_alpha;
625 value.
y *= inv_alpha;
626 value.
z *= inv_alpha;
648 const string &out_filepath,
649 const TypeDesc out_format,
651 const bool compress_as_srgb,
652 const bool unassociate_alpha,
653 const OIIO::ImageOutput::OpenMode mode,
656 ImageSpec out_spec = buf.spec();
657 out_spec.set_format(out_format);
661 OIIO::ImageBuf srgb_buf;
662 if (compress_as_srgb) {
667 OIIO::ImageBuf &write_buf = (compress_as_srgb) ? srgb_buf : buf;
668 if (!
out->open(out_filepath, out_spec, mode)) {
669 LOG_WARNING <<
"Could not open \"" << out_filepath <<
"\" : " <<
out->geterror();
672 if (!write_buf.write(
out.get())) {
673 LOG_WARNING <<
"Write failed: " << write_buf.geterror();
682 const string &out_filepath,
683 const TypeDesc out_format,
685 const bool compress_as_srgb,
686 const bool unassociate_alpha,
687 OIIO::ImageBuf &large_buf)
690 const ImageOutput::OpenMode append_mode =
out->supports(
"mipmap") ? ImageOutput::AppendMIPLevel :
691 ImageOutput::AppendSubimage;
700 OIIO::ImageOutput::Create,
709 if (compress_as_srgb) {
714 OIIO::ImageBuf small_buf;
715 while (large_buf.spec().width > 1 || large_buf.spec().height > 1) {
716 ImageSpec small_spec = large_buf.spec();
717 small_spec.extra_attribs.free();
720 if (small_spec.width > 1) {
721 small_spec.width /= 2;
723 if (small_spec.height > 1) {
724 small_spec.height /= 2;
726 small_buf.reset(small_spec);
732 const int rows_per_task =
divide_up(16384, small_spec.width);
733 parallel_for(blocked_range<int64_t>(0, small_spec.height, rows_per_task),
734 [&](
const blocked_range<int64_t> &r) {
735 OIIO::ROI roi(0, small_spec.width, r.begin(), r.end());
737 assert(small_buf.spec().format == TypeFloat);
738 resize_block<float>(small_buf, large_buf, roi, envlatlmode);
749 std::swap(large_buf, small_buf);
753 LOG_WARNING <<
"Error writing \"" << out_filepath <<
"\" : " <<
out->geterror();
772 const string &out_filepath,
776 const ImageSpec &in_spec)
778 const bool use_openexr = metadata.
is_float();
779 const TypeDesc out_format = metadata.
typedesc();
784 spec.width = metadata.
width;
785 spec.height = metadata.
height;
787 spec.nchannels = metadata.
is_rgba() ? 4 : 1;
788 spec.set_format(out_format);
789 spec.tile_width = 64;
790 spec.tile_height = 64;
794 spec.extra_attribs = in_spec.extra_attribs;
800 std::string in_compression = in_spec.get_string_attribute(
"compression",
"none");
801 if (in_compression ==
"none") {
802 spec.attribute(
"compression",
"zip");
804 spec.attribute(
"planarconfig",
"contig");
809 spec.attribute(
"oiio:ColorSpace",
colorspace);
812 spec.attribute(
"colorInteropID", interop_id);
815 spec.erase_attribute(
"colorInteropID");
820 spec.erase_attribute(
"Exif:ColorSpace");
821 spec.erase_attribute(
"ICCProfile");
822 spec.erase_attribute(
"CICP");
824 spec.attribute(
"oiio:UnassociatedAlpha", 0);
828 spec.attribute(
"blender:SourceFile", filepath);
829 spec.attribute(
"blender:SourceColorSpace", metadata.
colorspace);
831 spec.attribute(
"blender:XYZToSceneLinear",
836 spec.attribute(
"DocumentName",
837 string_printf(
"%s (colorspace: %s, xyz_to_scene_linear:%s, alpha: %s)",
845 switch (format_type) {
847 spec.attribute(
"textureformat",
"Plain Texture");
848 spec.attribute(
"wrapmodes",
"black,black");
851 spec.attribute(
"textureformat",
"LatLong Environment");
852 spec.attribute(
"wrapmodes",
"periodic,clamp");
854 spec.attribute(
"oiio:updirection",
"y");
855 spec.attribute(
"oiio:sampleborder", 1);
862 spec.attribute(
"openexr:roundingmode", 0 );
863 spec.erase_attribute(
"openexr:levelmode");
866 OIIO::ImageBuf buf(spec, OIIO::InitializePixels::No);
867 std::time_t in_time = OIIO::Filesystem::last_write_time(filepath);
870 LOG_WARNING <<
"Failed to load pixels for " << filepath;
878 if (buf.spec().format != TypeFloat) {
879 spec.set_format(TypeFloat);
880 OIIO::ImageBuf float_buf(spec, OIIO::InitializePixels::No);
881 OIIO::ImageBufAlgo::copy(float_buf, buf, TypeFloat);
882 buf = std::move(float_buf);
886 buf.specmod().attribute(
"DateTime",
datestring(in_time));
897 const char *tmp_extension = (use_openexr) ?
".%%%%%%%%.temp.exr" :
".%%%%%%%%.temp.tif";
898 string tmp_filepath = OIIO::Filesystem::unique_path(out_filepath + tmp_extension);
900 std::unique_ptr<ImageOutput>
out = ImageOutput::create(tmp_filepath);
912 OIIO::Filesystem::last_write_time(tmp_filepath, in_time);
916 std::string rename_err;
917 if (!OIIO::Filesystem::rename(tmp_filepath, out_filepath, rename_err)) {
918 LOG_WARNING <<
"Could not rename file: " << rename_err;
925 LOG_INFO <<
"Wrote tx file: " << out_filepath;
931 const string &out_filepath,
936 LOG_INFO <<
"Generating tx file for: " << filepath;
947 LOG_WARNING <<
"Failed to load metadata for " << filepath;
952 if (!
make_tx(filepath, out_filepath, metadata, alpha_type, format_type, spec)) {
static bool colorspace_is_data(ustring colorspace)
static string get_xyz_to_scene_linear_rgb_string()
static const char * colorspace_interop_id(ustring colorspace)
void append(const uint8_t *data, const int nbytes)
ccl_device float color_srgb_to_linear(const float c)
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
ccl_device float4 color_linear_to_srgb_v4(const float4 c)
ccl_device float color_linear_to_srgb(const float c)
#define CCL_NAMESPACE_END
#define assert(assertion)
constexpr T clamp(T, U, U) RET
static void write_stats_tx(OIIO::ImageBuf &buf, const bool use_openexr)
static std::string unique_filename_tx(const string &filepath, const string &texture_cache_path, ustring colorspace, const ImageAlphaType alpha_type, const ImageFormatType format_type, const bool absolute_texture_cache)
static void interppixel_NDC(const OIIO::ImageBuf &buf, float x, float y, OIIO::span< float > pixel, bool envlatlmode, OIIO::ImageBuf::ConstIterator< SRCTYPE > &it, OIIO::ImageBuf::WrapMode wrapmode)
static bool write_buf_tx(std::unique_ptr< ImageOutput > &out, const string &out_filepath, const TypeDesc out_format, const ImageFormatType format_type, const bool compress_as_srgb, const bool unassociate_alpha, const OIIO::ImageOutput::OpenMode mode, OIIO::ImageBuf &buf)
ustring make_tx_get_file_colorspace(const ImageMetaData &metadata)
static void convert_srgb_tx(OIIO::ImageBuf &buf, const bool from_srgb, const bool unassociate_alpha)
static bool resize_block_2pass(OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi)
static void fix_latl_edges(OIIO::ImageBuf &buf)
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)
static bool resize_block_(OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi, bool envlatlmode)
static std::string format_type_string(const ImageFormatType format_type)
bool make_tx(const string &filepath, const string &out_filepath, ustring colorspace, const ImageAlphaType alpha_type, const ImageFormatType format_type)
static bool write_mipmap_tx(std::unique_ptr< ImageOutput > &out, const string &out_filepath, const TypeDesc out_format, const ImageFormatType format_type, const bool compress_as_srgb, const bool unassociate_alpha, OIIO::ImageBuf &large_buf)
static void clamp_half_tx(OIIO::ImageBuf &buf, const TypeDesc out_format)
static CCL_NAMESPACE_BEGIN std::string datestring(time_t t)
static void halve_scanline(const SRCTYPE *s, const int nchannels, size_t sw, float *dst)
static void parallel_for_pixels(const int64_t num, const Func &func)
static bool texture_cache_file_outdated(const string &filepath, const string &tx_filepath)
static std::string alpha_type_string(const ImageAlphaType alpha_type)
static bool resize_block(OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi, bool envlatlmode)
#define TX_FILE_FORMAT_VERSION
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
string path_make_relative(const string &path_, const string &base_)
string path_dirname(const string &path)
bool path_create_directories(const string &filepath)
bool path_is_relative(const string &path)
string path_join(const string &dir, const string &file)
string path_normalize(const string &path)
bool path_is_file(const string &path)
string path_filename(const string &path)
bool path_remove(const string &path)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
@ IMAGE_ALPHA_CHANNEL_PACKED
@ IMAGE_ALPHA_UNASSOCIATED
@ IMAGE_FORMAT_EQUIANGULAR
ustring u_colorspace_scene_linear_srgb
ustring u_colorspace_scene_linear
ustring u_colorspace_data