Blender V5.3
oiio_output_driver.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7#include "util/colorspace.h"
8#include "util/image.h"
9#include "util/unique_ptr.h"
10
11#include <OpenImageIO/imagebuf.h>
12#include <OpenImageIO/imagebufalgo.h>
13
15
16OIIOOutputDriver::OIIOOutputDriver(const string_view filepath,
17 const string_view pass,
19 : filepath_(filepath), pass_(pass), log_(log)
20{
21}
22
24
26{
27 /* Only write the full buffer, no intermediate tiles. */
28 if (!(tile.size == tile.full_size)) {
29 return;
30 }
31
32 log_(string_printf("Writing image %s", filepath_.c_str()));
33
34 unique_ptr<ImageOutput> image_output(ImageOutput::create(filepath_));
35 if (image_output == nullptr) {
36 log_("Failed to create image file");
37 return;
38 }
39
40 const int width = tile.size.x;
41 const int height = tile.size.y;
42
43 const ImageSpec spec(width, height, 4, TypeDesc::FLOAT);
44 if (!image_output->open(filepath_, spec)) {
45 log_("Failed to create image file");
46 return;
47 }
48
49 vector<float> pixels(width * height * 4);
50 if (!tile.get_pass_pixels(pass_, 4, pixels.data())) {
51 log_("Failed to read render pass pixels");
52 return;
53 }
54
55 /* Manipulate offset and stride to convert from bottom-up to top-down convention. */
56 OIIO::ImageBuf image_buffer(spec,
57 pixels.data() + (height - 1) * width * 4,
58 AutoStride,
59 -width * 4 * sizeof(float),
60 AutoStride);
61
62 /* Apply gamma correction for (some) non-linear file formats.
63 * TODO: use OpenColorIO view transform if available. */
65 u_colorspace_auto, "", image_output->format_name(), true) == u_colorspace_srgb)
66 {
67 const float g = 1.0f / 2.2f;
68 OIIO::ImageBufAlgo::pow(image_buffer, image_buffer, {g, g, g, 1.0f});
69 }
70
71 /* Write to disk and close */
72 image_buffer.set_write_format(TypeDesc::FLOAT);
73 image_buffer.write(image_output.get());
74 image_output->close();
75}
76
static ustring detect_known_colorspace(ustring colorspace, const char *file_colorspace, const char *file_format, bool is_float)
void write_render_tile(const Tile &tile) override
std::function< void(const string &)> LogFunction
~OIIOOutputDriver() override
OIIOOutputDriver(const string_view filepath, const string_view pass, LogFunction log)
CCL_NAMESPACE_BEGIN ustring u_colorspace_auto
#define CCL_NAMESPACE_END
#define log
const ccl_global KernelWorkTile * tile
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
ustring u_colorspace_srgb