Blender V5.3
image_maketx.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 OpenImageIO project
2 * 2026 Blender Authors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * This is a modified version of maketexture.cpp from OpenImageIO, to add a few
7 * features missing in the native implementation. */
8
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <iostream>
13#include <memory>
14
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>
28
29#include "util/color.h"
30#include "util/colorspace.h"
31#include "util/image.h"
32#include "util/image_maketx.h"
33#include "util/image_metadata.h"
34#include "util/log.h"
35#include "util/md5.h"
36#include "util/path.h"
37#include "util/tbb.h"
38#include "util/types_image.h"
39
41
42static std::string datestring(time_t t)
43{
44 struct tm mytm;
45 OIIO::Sysutil::get_local_time(&t, &mytm);
46 return string_printf("%04d:%02d:%02d %02d:%02d:%02d",
47 mytm.tm_year + 1900,
48 mytm.tm_mon + 1,
49 mytm.tm_mday,
50 mytm.tm_hour,
51 mytm.tm_min,
52 mytm.tm_sec);
53}
54
55template<class SRCTYPE>
56static void interppixel_NDC(const OIIO::ImageBuf &buf,
57 float x,
58 float y,
59 OIIO::span<float> pixel,
60 bool envlatlmode,
61 OIIO::ImageBuf::ConstIterator<SRCTYPE> &it,
62 OIIO::ImageBuf::WrapMode wrapmode)
63{
64 const ImageSpec &spec(buf.spec());
65 int fx = spec.x;
66 int fy = spec.y;
67 int fw = spec.width;
68 int fh = spec.height;
69 x = static_cast<float>(fx) + x * static_cast<float>(fw);
70 y = static_cast<float>(fy) + y * static_cast<float>(fh);
71
72 int n = spec.nchannels;
73 float *p0 = OIIO_ALLOCA(float, 4 * n), *p1 = p0 + n, *p2 = p1 + n, *p3 = p2 + n;
74
75 x -= 0.5f;
76 y -= 0.5f;
77 int xtexel, ytexel;
78 float xfrac, yfrac;
79 xfrac = floorfrac(x, &xtexel);
80 yfrac = floorfrac(y, &ytexel);
81
82 /* Get the four texels */
83 it.rerange(xtexel, xtexel + 2, ytexel, ytexel + 2, 0, 1, wrapmode);
84 for (int c = 0; c < n; ++c) {
85 p0[c] = it[c];
86 }
87 ++it;
88 for (int c = 0; c < n; ++c) {
89 p1[c] = it[c];
90 }
91 ++it;
92 for (int c = 0; c < n; ++c) {
93 p2[c] = it[c];
94 }
95 ++it;
96 for (int c = 0; c < n; ++c) {
97 p3[c] = it[c];
98 }
99
100 if (envlatlmode) {
101 /* For latlong environment maps, in order to conserve energy, we must weight the pixels by
102 * sin(t*PI) because pixels closer to the pole are actually less area on the sphere. Doing
103 * this wrong will tend to over-represent the high latitudes in low-res MIP levels. We fold
104 * the area weighting into our linear interpolation by adjusting yfrac. */
105 int ymin = spec.y;
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);
112 }
113
114 /* Bilinearly interpolate. */
115 OIIO::bilerp(p0, p1, p2, p3, xfrac, yfrac, n, pixel.data());
116}
117
118/* Resize src into dst, relying on the linear interpolation of
119 * interppixel_NDC_full or interppixel_NDC_clamped, for the pixel range. */
120template<class SRCTYPE>
121static bool resize_block_(OIIO::ImageBuf &dst,
122 const OIIO::ImageBuf &src,
123 OIIO::ROI roi,
124 bool envlatlmode)
125{
126 int x0 = roi.xbegin, x1 = roi.xend, y0 = roi.ybegin, y1 = roi.yend;
127
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);
131#else
132 std::vector<float> pel_buf(dstspec.nchannels);
133 OIIO::span<float> pel(pel_buf.data(), dstspec.nchannels);
134#endif
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;
148 interppixel_NDC<SRCTYPE>(src, s, t, pel, envlatlmode, srcit, wrapmode);
149 for (int c = 0; c < nchannels; ++c) {
150 d[c] = pel[c];
151 }
152 }
153 }
154 return true;
155}
156
157/* Helper function to compute the first bilerp pass into a scanline buffer. */
158template<class SRCTYPE>
159static void halve_scanline(const SRCTYPE *s, const int nchannels, size_t sw, float *dst)
160{
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));
164 }
165 }
166}
167
168/* Bilinear resize performed as a 2-pass filter.
169 * Optimized to assume that the images are contiguous. */
170template<class SRCTYPE>
171static bool resize_block_2pass(OIIO::ImageBuf &dst, const OIIO::ImageBuf &src, OIIO::ROI roi)
172{
173 /* Two-pass filtering introduces a half-pixel shift for odd resolutions.
174 * Revert to correct bilerp sampling. */
175 if (src.spec().width % 2 || src.spec().height % 2) {
176 return resize_block_<SRCTYPE>(dst, src, roi, false);
177 }
178
179 assert(roi.ybegin + roi.height() <= dst.spec().height);
180
181 /* Allocate two scanline buffers to hold the result of the first pass. */
182 const int nchannels = dst.nchannels();
183 const size_t row_elem = roi.width() * nchannels; /* # floats in scanline */
184 std::unique_ptr<float[]> S0(new float[row_elem]);
185 std::unique_ptr<float[]> S1(new float[row_elem]);
186
187 /* We know that the buffers created for mipmapping are all contiguous,
188 * so we can skip the iterators for a bilerp resize entirely along with
189 * any NDC -> pixel math, and just directly traverse pixels. */
190 const SRCTYPE *s = (const SRCTYPE *)src.localpixels();
191 SRCTYPE *d = (SRCTYPE *)dst.localpixels();
192 assert(s && d); /* Assume contig bufs */
193 d += int64_t(roi.ybegin) * dst.spec().width * nchannels; /* Top of dst OIIO::ROI */
194 const size_t ystride = src.spec().width * nchannels; /* Scanline offset */
195 s += 2 * roi.ybegin * ystride; /* Top of src OIIO::ROI */
196
197 /* Run through destination rows, doing the two-pass bilerp filter. */
198 const size_t dw = roi.width(), dh = roi.height(); /* Loop invariants */
199 const size_t sw = dw * 2; /* Handle odd res */
200 for (size_t y = 0; y < dh; ++y) { /* For each dst OIIO::ROI row */
201 halve_scanline<SRCTYPE>(s, nchannels, sw, &S0[0]);
202 s += ystride;
203 halve_scanline<SRCTYPE>(s, nchannels, sw, &S1[0]);
204 s += ystride;
205 const float *s0 = &S0[0], *s1 = &S1[0];
206 for (size_t x = 0; x < dw; ++x) { /* For each dst OIIO::ROI col */
207 for (int i = 0; i < nchannels; ++i, ++s0, ++s1, ++d) {
208 *d = (SRCTYPE)(0.5f * (*s0 + *s1)); /* Average vertically */
209 }
210 }
211 }
212
213 return true;
214}
215
216template<typename SRCTYPE>
217static bool resize_block(OIIO::ImageBuf &dst,
218 const OIIO::ImageBuf &src,
219 OIIO::ROI roi,
220 bool envlatlmode)
221{
222 const ImageSpec &srcspec(src.spec());
223 const ImageSpec &dstspec(dst.spec());
224 assert(dstspec.nchannels == srcspec.nchannels);
225 assert(dst.localpixels());
226 bool ok;
227 if (src.localpixels() && /* Not a cached image */
228 !envlatlmode && /* not latlong wrap mode */
229 roi.xbegin == 0 && /* Region x at origin */
230 dstspec.width == roi.width() && /* Full width OIIO::ROI */
231 (2 * dstspec.width) == srcspec.width && /* Src is 2x resize */
232 dstspec.format == srcspec.format && /* Same formats */
233 dstspec.x == 0 && dstspec.y == 0 && /* Not a crop or overscan */
234 srcspec.x == 0 && srcspec.y == 0)
235 {
236 /* If all these conditions are met, we have a special case that
237 * can be more highly optimized. */
238 ok = resize_block_2pass<SRCTYPE>(dst, src, roi);
239 }
240 else {
241 assert(dst.spec().format == TypeFloat);
242 ok = resize_block_<SRCTYPE>(dst, src, roi, envlatlmode);
243 }
244 return ok;
245}
246
247static void fix_latl_edges(OIIO::ImageBuf &buf)
248{
249 int n = buf.nchannels();
250
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);
254#else
255 std::vector<float> left_buf(n);
256 std::vector<float> right_buf(n);
257
258 OIIO::span<float> left(left_buf.data(), n);
259 OIIO::span<float> right(right_buf.data(), n);
260#endif
261
262 /* Make the whole first and last row be solid, since they are exactly on the pole. */
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;
266 /* Use left for the sum, right for each new pixel. */
267 for (int c = 0; c < n; ++c) {
268 left[c] = 0.0f;
269 }
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);
273#else
274 buf.getpixel(x, y, right.data());
275#endif
276 for (int c = 0; c < n; ++c) {
277 left[c] += right[c];
278 }
279 }
280 for (int c = 0; c < n; ++c) {
281 left[c] *= wscale;
282 }
283 for (int x = buf.xbegin(); x < buf.xend(); ++x) {
284 buf.setpixel(x, y, left);
285 }
286 }
287
288 /* Make the left and right match, since they are both right on the prime meridian. */
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);
293#else
294 buf.getpixel(buf.xbegin(), y, left.data());
295 buf.getpixel(buf.xend() - 1, y, right.data());
296#endif
297 for (int c = 0; c < n; ++c) {
298 left[c] = 0.5f * left[c] + 0.5f * right[c];
299 }
300 buf.setpixel(buf.xbegin(), y, left);
301 buf.setpixel(buf.xend() - 1, y, left);
302 }
303}
304
305static bool texture_cache_file_outdated(const string &filepath, const string &tx_filepath)
306{
307 if (!path_is_file(tx_filepath)) {
308 return true;
309 }
310
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);
313
314 if (in_time == out_time) {
315 LOG_INFO << "Using texture cache file: " << tx_filepath;
316 return false;
317 }
318
319 LOG_INFO << "Texture cache file is outdated: " << tx_filepath;
320 return true;
321}
322
323static std::string alpha_type_string(const ImageAlphaType alpha_type)
324{
325 switch (alpha_type) {
327 return "associated";
328 break;
330 return "unassociated";
331 break;
333 return "channel_packed";
334 break;
336 return "ignore";
337 break;
338 case IMAGE_ALPHA_AUTO:
339 return "auto";
341 break;
342 }
343
344 assert(!"Unknown tx alpha type");
345
346 return "auto";
347}
348
349static std::string format_type_string(const ImageFormatType format_type)
350{
351 switch (format_type) {
353 return "plain";
355 return "equiangular";
356 }
357
358 assert(!"Unknown tx format type");
359
360 return "unknown";
361}
362
363static std::string unique_filename_tx(const string &filepath,
364 const string &texture_cache_path,
365 ustring colorspace,
366 const ImageAlphaType alpha_type,
367 const ImageFormatType format_type,
368 const bool absolute_texture_cache)
369{
370 /* NOTE: Be careful not to change existing hashes if at all possible, as this
371 * will cause all texture files to be regenerated with significantly increased
372 * disk usage. */
373 MD5Hash md5;
374
375 /* Scene linear colorspace that we may be converting to. */
377
378 /* Color-space. */
380 u_colorspace_data.string() :
381 colorspace.string()));
382
383 md5.append("alpha:" + alpha_type_string(alpha_type));
384
385 md5.append("format:" + format_type_string(format_type));
386
387 /* For absolute texture cache path, include the full file path. This requires
388 * a matching directory structure though. */
389 /* TODO: Can we use a relative path? The problem is that we can not automatically
390 * distinguish between cases like /tmp/texture_cache and /projects/X/texture_cache
391 * where the former is not helped by a relative path, while the latter can be. */
392 if (absolute_texture_cache) {
393 md5.append("filepath:" + path_normalize(path_make_relative(filepath, texture_cache_path)));
394 }
395
396 const std::string version = string_printf(".%d-", TX_FILE_FORMAT_VERSION);
397
398 return path_filename(filepath) + version + md5.get_hex() + ".tx";
399}
400
401bool resolve_tx(const string &filepath,
402 const string &texture_cache_path,
403 ustring colorspace,
404 const ImageAlphaType alpha_type,
405 const ImageFormatType format_type,
406 string &out_filepath,
407 ImageMetaData &out_metadata)
408{
409
410 /* Nothing to do if file doesn't even exist. */
411 if (!path_is_file(filepath)) {
412 return false;
413 }
414
415 const string filedir = path_dirname(filepath);
416
417 /* Check the specified directory if one is given. */
418 if (!texture_cache_path.empty()) {
419 const bool absolute_cache_path = !path_is_relative(texture_cache_path);
420 const string tx_filename = unique_filename_tx(
421 filepath, texture_cache_path, colorspace, alpha_type, format_type, absolute_cache_path);
422 const string tx_filepath = path_join(
423 absolute_cache_path ? string(texture_cache_path) : path_join(filedir, texture_cache_path),
424 tx_filename);
425
426 out_filepath = tx_filepath;
427
428 if (!texture_cache_file_outdated(filepath, tx_filepath)) {
429 if (out_metadata.oiio_load_metadata(tx_filepath) && out_metadata.is_tx_file) {
430 return true;
431 }
432 }
433 }
434
435 /* Check in the default location. */
436 const char *default_texture_cache_dir = "blender_tx";
437 if (texture_cache_path != default_texture_cache_dir) {
438 const string tx_filename = unique_filename_tx(
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),
441 tx_filename);
442 if (!texture_cache_file_outdated(filepath, tx_default_filepath)) {
443 if (out_metadata.oiio_load_metadata(tx_default_filepath) && out_metadata.is_tx_file) {
444 out_filepath = tx_default_filepath;
445 return true;
446 }
447 }
448
449 if (texture_cache_path.empty()) {
450 out_filepath = tx_default_filepath;
451 }
452 }
453
454 /* If it's already a tx, tiff or exr file, we can use it directly as well. But it's
455 * preferable to use a Cycles native tx file for performance. */
456 if (out_metadata.oiio_load_metadata(filepath) && out_metadata.is_tx_file) {
457 out_filepath = filepath;
458 return true;
459 }
460
461 return false;
462}
463
464static void write_stats_tx(OIIO::ImageBuf &buf, const bool use_openexr)
465{
466 OIIO::ImageBufAlgo::PixelStats pixel_stats = OIIO::ImageBufAlgo::computePixelStats(buf);
467
468 /* Constant color optimization. Creating small image and add metadata. */
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;
473
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);
478 buf.reset(spec);
479 OIIO::ImageBufAlgo::fill(buf, constant_color);
480 LOG_INFO << " Constant color image detected. Creating " << spec.width << "x" << spec.height
481 << " texture instead.";
482 }
483
484 /* TODO: Opaque detection optimization doesn't work currently, because we
485 * expected tile pixels to be conformed and that means 1 or 4 channels. */
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;
489#if 0
490 if (is_opaque) {
491 LOG_INFO << " Alpha==1 image detected. Dropping the alpha channel.";
492 OIIO::ImageBuf new_buf(buf.spec());
493 OIIO::ImageBufAlgo::channels(new_buf,
494 buf,
495 buf.nchannels() - 1,
496 OIIO::cspan<int>(),
497 OIIO::cspan<float>(),
498 OIIO::cspan<std::string>(),
499 true);
500 std::swap(buf, new_buf);
501 }
502#endif
503
504 /* Monochrome detection. */
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))
508 {
509 LOG_INFO << " Monochrome image detected. Converting to single channel texture.";
510 OIIO::ImageBuf new_buf(buf.spec());
511 OIIO::ImageBufAlgo::channels(new_buf,
512 buf,
513 1,
514 OIIO::cspan<int>(),
515 OIIO::cspan<float>(),
516 OIIO::cspan<std::string>(),
517 true);
518 new_buf.specmod().default_channel_names();
519 std::swap(buf, new_buf);
520 }
521
522 /* Image description for tiff, to store arbitrary metadata because it's not natively
523 * supported by this file format. */
524 string desc;
525
526 /* Add hash attribute. */
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()) {
532 if (use_openexr) {
533 buf.specmod().attribute("oiio:SHA-1", hash_digest);
534 }
535 else {
536 if (desc.length()) {
537 desc += " ";
538 }
539 desc += "oiio:SHA-1=";
540 desc += hash_digest;
541 }
542 LOG_DEBUG << " SHA-1: " << hash_digest;
543 }
544
545 /* Add constant color attribute. */
546 if (is_constant_color) {
547 std::string colstr = OIIO::Strutil::join(constant_color, ",", buf.spec().nchannels);
548 if (use_openexr) {
549 buf.specmod().attribute("oiio:ConstantColor", colstr);
550 }
551 else {
552 desc += string_printf("%soiio:ConstantColor=%s", desc.length() ? " " : "", colstr.c_str());
553 }
554 LOG_DEBUG << " ConstantColor: " << colstr;
555 }
556
557 /* Add average color attribute. */
558 std::string avgstr = OIIO::Strutil::join(pixel_stats.avg, ",", buf.spec().nchannels);
559 if (use_openexr) {
560 buf.specmod().attribute("oiio:AverageColor", avgstr);
561 }
562 else {
563 desc += string_printf("%soiio:AverageColor=%s", desc.length() ? " " : "", avgstr.c_str());
564 }
565 LOG_DEBUG << " AverageColor: " << avgstr;
566
567 if (!use_openexr) {
568 buf.specmod().attribute("ImageDescription", desc);
569 }
570}
571
572template<typename Func> static void parallel_for_pixels(const int64_t num, const Func &func)
573{
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++) {
576 func(i);
577 }
578 });
579}
580
581static void clamp_half_tx(OIIO::ImageBuf &buf, const TypeDesc out_format)
582{
583 /* Clamp to half bounds to avoid creating inf when converting from float to half on write. */
584 if (out_format != TypeDesc::HALF) {
585 return;
586 }
587
588 assert(buf.spec().format == TypeFloat);
589
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());
592 parallel_for_pixels(num_values,
593 [&](const int64_t i) { pixels[i] = clamp(pixels[i], -HALF_MAX, HALF_MAX); });
594}
595
596static void convert_srgb_tx(OIIO::ImageBuf &buf,
597 const bool from_srgb,
598 const bool unassociate_alpha)
599{
600 assert(buf.spec().format == TypeFloat);
601 assert(buf.spec().nchannels == 1 || buf.spec().nchannels == 4);
602
603 const int64_t num_pixels = int64_t(buf.spec().width) * buf.spec().height;
604
605 if (buf.spec().nchannels == 1) {
606 float *pixels = static_cast<float *>(buf.localpixels());
607 if (from_srgb) {
608 parallel_for_pixels(num_pixels,
609 [&](const int64_t i) { pixels[i] = color_srgb_to_linear(pixels[i]); });
610 }
611 else {
612 parallel_for_pixels(num_pixels,
613 [&](const int64_t i) { pixels[i] = color_linear_to_srgb(pixels[i]); });
614 }
615 }
616 else {
617 float4 *pixels = static_cast<float4 *>(buf.localpixels());
618 parallel_for_pixels(num_pixels, [&](const int64_t i) {
619 float4 value = pixels[i];
620 const bool has_alpha = unassociate_alpha && !(value.w <= 0.0f || value.w == 1.0f);
621
622 if (has_alpha) {
623 const float inv_alpha = 1.0f / value.w;
624 value.x *= inv_alpha;
625 value.y *= inv_alpha;
626 value.z *= inv_alpha;
627 }
628
629 if (from_srgb) {
630 value = color_srgb_to_linear_v4(value);
631 }
632 else {
633 value = color_linear_to_srgb_v4(value);
634 }
635
636 if (has_alpha) {
637 value.x *= value.w;
638 value.y *= value.w;
639 value.z *= value.w;
640 }
641
642 pixels[i] = value;
643 });
644 }
645}
646
647static bool write_buf_tx(std::unique_ptr<ImageOutput> &out,
648 const string &out_filepath,
649 const TypeDesc out_format,
650 const ImageFormatType format_type,
651 const bool compress_as_srgb,
652 const bool unassociate_alpha,
653 const OIIO::ImageOutput::OpenMode mode,
654 OIIO::ImageBuf &buf)
655{
656 ImageSpec out_spec = buf.spec();
657 out_spec.set_format(out_format);
658 if (format_type == IMAGE_FORMAT_EQUIANGULAR) {
659 fix_latl_edges(buf);
660 }
661 OIIO::ImageBuf srgb_buf;
662 if (compress_as_srgb) {
663 srgb_buf.copy(buf);
664 convert_srgb_tx(srgb_buf, false, unassociate_alpha);
665 }
666 clamp_half_tx(buf, out_format);
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();
670 return false;
671 }
672 if (!write_buf.write(out.get())) {
673 LOG_WARNING << "Write failed: " << write_buf.geterror();
674 out->close();
675 return false;
676 }
677
678 return true;
679}
680
681static bool write_mipmap_tx(std::unique_ptr<ImageOutput> &out,
682 const string &out_filepath,
683 const TypeDesc out_format,
684 const ImageFormatType format_type,
685 const bool compress_as_srgb,
686 const bool unassociate_alpha,
687 OIIO::ImageBuf &large_buf)
688{
689 const bool envlatlmode = format_type == IMAGE_FORMAT_EQUIANGULAR;
690 const ImageOutput::OpenMode append_mode = out->supports("mipmap") ? ImageOutput::AppendMIPLevel :
691 ImageOutput::AppendSubimage;
692
693 /* Write top level. */
695 out_filepath,
696 out_format,
697 format_type,
698 false,
699 false,
700 OIIO::ImageOutput::Create,
701 large_buf);
702
703 /* When using sRGB, convert to linear before resizing, and convert to sRGB again in write_buf_tx.
704 * for each mip level.
705 *
706 * Pixels have associated alpha, which is unassociated for the conversion, matching the image
707 * texture node when the alpha output is used. Except for channel packed alpha, where alpha is
708 * unrelated to color and color can exceed alpha. */
709 if (compress_as_srgb) {
710 convert_srgb_tx(large_buf, true, unassociate_alpha);
711 }
712
713 /* Write mip levels. */
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();
718
719 /* Halve resolution and reallocate small buffer */
720 if (small_spec.width > 1) {
721 small_spec.width /= 2;
722 }
723 if (small_spec.height > 1) {
724 small_spec.height /= 2;
725 }
726 small_buf.reset(small_spec);
727
728 /* TODO: Support other filters than box? If we do this, be sure to include them in the
729 * hash like OIIO maketx does. */
730 /* TODO: Support highlight compensation like OIIO? */
731
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());
736 /* Always float for now. */
737 assert(small_buf.spec().format == TypeFloat);
738 resize_block<float>(small_buf, large_buf, roi, envlatlmode);
739 });
740
742 out_filepath,
743 out_format,
744 format_type,
745 compress_as_srgb,
746 unassociate_alpha,
747 append_mode,
748 small_buf);
749 std::swap(large_buf, small_buf);
750 }
751
752 if (!out->close()) {
753 LOG_WARNING << "Error writing \"" << out_filepath << "\" : " << out->geterror();
754 return false;
755 }
756
757 return true;
758}
759
761{
762 /* Tx files are written in a handful of colorspace that can be directly loaded into
763 * memory without colorspace conversion. */
764 const bool is_data = ColorSpaceManager::colorspace_is_data(metadata.colorspace);
765 const bool compress_as_srgb = metadata.is_compressible_as_srgb;
766 return is_data ? u_colorspace_data :
767 compress_as_srgb ? u_colorspace_scene_linear_srgb :
769}
770
771static bool make_tx(const string &filepath,
772 const string &out_filepath,
773 ImageMetaData &metadata,
774 const ImageAlphaType alpha_type,
775 const ImageFormatType format_type,
776 const ImageSpec &in_spec)
777{
778 const bool use_openexr = metadata.is_float();
779 const TypeDesc out_format = metadata.typedesc();
780
781 ImageSpec spec;
782
783 /* Dimensions. */
784 spec.width = metadata.width;
785 spec.height = metadata.height;
786 spec.depth = 0;
787 spec.nchannels = metadata.is_rgba() ? 4 : 1;
788 spec.set_format(out_format);
789 spec.tile_width = 64;
790 spec.tile_height = 64;
791 spec.tile_depth = 1;
792
793 /* Inherit extra attributes from original image. */
794 spec.extra_attribs = in_spec.extra_attribs;
795
796 /* Software info, version is used to detect if file format is known by old versions. */
797 spec.attribute("Software", string_printf("Blender maketx v%d", TX_FILE_FORMAT_VERSION));
798
799 /* Compression. */
800 std::string in_compression = in_spec.get_string_attribute("compression", "none");
801 if (in_compression == "none") {
802 spec.attribute("compression", "zip");
803 }
804 spec.attribute("planarconfig", "contig");
805
806 /* Always convert to scene linear or data colorspace with associated alpha. */
807 const ustring colorspace = make_tx_get_file_colorspace(metadata);
808
809 spec.attribute("oiio:ColorSpace", colorspace);
811 if (interop_id) {
812 spec.attribute("colorInteropID", interop_id);
813 }
814 else {
815 spec.erase_attribute("colorInteropID");
816 }
817 /* Remove other potentially interfering colorspace metadata, that is no
818 * longer correct after colorspace conversion. Cycles itself does not use this,
819 * but other software could. */
820 spec.erase_attribute("Exif:ColorSpace");
821 spec.erase_attribute("ICCProfile");
822 spec.erase_attribute("CICP");
823
824 spec.attribute("oiio:UnassociatedAlpha", 0);
825
826 /* Source image metadata. */
827 if (use_openexr) {
828 spec.attribute("blender:SourceFile", filepath);
829 spec.attribute("blender:SourceColorSpace", metadata.colorspace);
830 spec.attribute("blender:SourceAlpha", alpha_type_string(alpha_type));
831 spec.attribute("blender:XYZToSceneLinear",
833 }
834 else {
835 /* TIFF does not have arbitrary metadata, so we put it in an existing one. */
836 spec.attribute("DocumentName",
837 string_printf("%s (colorspace: %s, xyz_to_scene_linear:%s, alpha: %s)",
838 filepath.c_str(),
839 metadata.colorspace.c_str(),
841 alpha_type_string(alpha_type).c_str()));
842 }
843
844 /* OIIO texture format metadata. */
845 switch (format_type) {
847 spec.attribute("textureformat", "Plain Texture");
848 spec.attribute("wrapmodes", "black,black");
849 break;
851 spec.attribute("textureformat", "LatLong Environment");
852 spec.attribute("wrapmodes", "periodic,clamp");
853 if (use_openexr) {
854 spec.attribute("oiio:updirection", "y");
855 spec.attribute("oiio:sampleborder", 1);
856 }
857 break;
858 }
859
860 /* OpenEXR mipmap metadata. */
861 if (use_openexr) {
862 spec.attribute("openexr:roundingmode", 0 /* ROUND_DOWN */);
863 spec.erase_attribute("openexr:levelmode");
864 }
865
866 OIIO::ImageBuf buf(spec, OIIO::InitializePixels::No);
867 std::time_t in_time = OIIO::Filesystem::last_write_time(filepath);
868
869 if (!metadata.oiio_load_pixels(filepath, buf.localpixels(), false)) {
870 LOG_WARNING << "Failed to load pixels for " << filepath;
871 return false;
872 }
873
874 metadata.conform_pixels(buf.localpixels());
875
876 /* Convert to float only after conforming, so it matches the regular image
877 * loading code path exactly. */
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);
883 }
884
885 /* Write date and time from input file. */
886 buf.specmod().attribute("DateTime", datestring(in_time));
887
888 /* Write pixel statistics. */
889 write_stats_tx(buf, use_openexr);
890
891 /* Write to temporary file, next to the final filepath for atomic rename. */
892 /* TODO: Can we protect against leaving files on crashes? Two possibilities:
893 * - Write to tmp directory, then move next to target file and rename. This reduces
894 * the probability but does not eliminate it.
895 * - Add an atexit handles that removes any temporary files we are writing. This
896 * does not help with a power outage or maybe some types of process killing. */
897 const char *tmp_extension = (use_openexr) ? ".%%%%%%%%.temp.exr" : ".%%%%%%%%.temp.tif";
898 string tmp_filepath = OIIO::Filesystem::unique_path(out_filepath + tmp_extension);
899
900 std::unique_ptr<ImageOutput> out = ImageOutput::create(tmp_filepath);
901 bool ok = write_mipmap_tx(out,
902 tmp_filepath,
903 out_format,
904 format_type,
906 !metadata.is_channel_packed,
907 buf);
908 out.reset();
909
910 /* Stamp with same time as input image file to detect updates. */
911 if (ok) {
912 OIIO::Filesystem::last_write_time(tmp_filepath, in_time);
913
914 /* Atomic move in case multiple process try to do this, and to avoid writing
915 * incomplete files in case of failure. */
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;
919 ok = false;
920 }
921 }
922
923 path_remove(tmp_filepath);
924 assert(path_is_file(out_filepath));
925 LOG_INFO << "Wrote tx file: " << out_filepath;
926
927 return ok;
928}
929
930bool make_tx(const string &filepath,
931 const string &out_filepath,
932 ustring colorspace,
933 const ImageAlphaType alpha_type,
934 const ImageFormatType format_type)
935{
936 LOG_INFO << "Generating tx file for: " << filepath;
937
938 if (!path_create_directories(out_filepath)) {
939 LOG_WARNING << "Failed to create directory for texture cache: " << path_dirname(out_filepath);
940 return false;
941 }
942
943 ImageMetaData metadata;
944 ImageSpec spec;
945 metadata.colorspace = colorspace;
946 if (!metadata.oiio_load_metadata(filepath, &spec)) {
947 LOG_WARNING << "Failed to load metadata for " << filepath;
948 return false;
949 }
950 metadata.finalize(alpha_type);
951
952 if (!make_tx(filepath, out_filepath, metadata, alpha_type, format_type, spec)) {
953 LOG_WARNING << "Failed to write tx file";
954 return false;
955 }
956
957 return true;
958}
959
#define M_PI
long long int int64_t
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 finalize(const ImageAlphaType alpha_type=IMAGE_ALPHA_AUTO)
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
bool is_float() const
TypeDesc typedesc() const
bool is_rgba() const
bool is_compressible_as_srgb
void conform_pixels(void *pixels) const
Definition md5.h:19
void append(const uint8_t *data, const int nbytes)
Definition md5.cpp:268
string get_hex()
Definition md5.cpp:367
ccl_device float color_srgb_to_linear(const float c)
Definition color.h:51
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:335
ccl_device float4 color_linear_to_srgb_v4(const float4 c)
Definition color.h:329
ccl_device float color_linear_to_srgb(const float c)
Definition color.h:59
#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
#define LOG_DEBUG
Definition log.h:107
#define LOG_WARNING
Definition log.h:103
#define LOG_INFO
Definition log.h:106
ccl_device_inline float floorfrac(const float x, ccl_private int *i)
Definition math_base.h:464
string path_make_relative(const string &path_, const string &base_)
Definition path.cpp:480
string path_dirname(const string &path)
Definition path.cpp:422
bool path_create_directories(const string &filepath)
Definition path.cpp:696
bool path_is_relative(const string &path)
Definition path.cpp:475
string path_join(const string &dir, const string &file)
Definition path.cpp:436
string path_normalize(const string &path)
Definition path.cpp:465
bool path_is_file(const string &path)
Definition path.cpp:631
string path_filename(const string &path)
Definition path.cpp:399
bool path_remove(const string &path)
Definition path.cpp:827
#define sinf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:54
ImageAlphaType
Definition types_image.h:61
@ IMAGE_ALPHA_ASSOCIATED
Definition types_image.h:63
@ IMAGE_ALPHA_CHANNEL_PACKED
Definition types_image.h:64
@ IMAGE_ALPHA_NUM_TYPES
Definition types_image.h:68
@ IMAGE_ALPHA_AUTO
Definition types_image.h:66
@ IMAGE_ALPHA_IGNORE
Definition types_image.h:65
@ IMAGE_ALPHA_UNASSOCIATED
Definition types_image.h:62
ImageFormatType
Definition types_image.h:72
@ IMAGE_FORMAT_PLAIN
Definition types_image.h:73
@ IMAGE_FORMAT_EQUIANGULAR
Definition types_image.h:74
ustring u_colorspace_scene_linear_srgb
ustring u_colorspace_scene_linear
ustring u_colorspace_data