Blender V4.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 "scene/colorspace.h"
8
9#include <OpenImageIO/imagebuf.h>
10#include <OpenImageIO/imagebufalgo.h>
11
13
14OIIOOutputDriver::OIIOOutputDriver(const string_view filepath,
15 const string_view pass,
17 : filepath_(filepath), pass_(pass), log_(log)
18{
19}
20
22
24{
25 /* Only write the full buffer, no intermediate tiles. */
26 if (!(tile.size == tile.full_size)) {
27 return;
28 }
29
30 log_(string_printf("Writing image %s", filepath_.c_str()));
31
32 unique_ptr<ImageOutput> image_output(ImageOutput::create(filepath_));
33 if (image_output == nullptr) {
34 log_("Failed to create image file");
35 return;
36 }
37
38 const int width = tile.size.x;
39 const int height = tile.size.y;
40
41 ImageSpec spec(width, height, 4, TypeDesc::FLOAT);
42 if (!image_output->open(filepath_, spec)) {
43 log_("Failed to create image file");
44 return;
45 }
46
47 vector<float> pixels(width * height * 4);
48 if (!tile.get_pass_pixels(pass_, 4, pixels.data())) {
49 log_("Failed to read render pass pixels");
50 return;
51 }
52
53 /* Manipulate offset and stride to convert from bottom-up to top-down convention. */
54 ImageBuf image_buffer(spec,
55 pixels.data() + (height - 1) * width * 4,
56 AutoStride,
57 -width * 4 * sizeof(float),
58 AutoStride);
59
60 /* Apply gamma correction for (some) non-linear file formats.
61 * TODO: use OpenColorIO view transform if available. */
63 u_colorspace_auto, "", image_output->format_name(), true) == u_colorspace_srgb)
64 {
65 const float g = 1.0f / 2.2f;
66 ImageBufAlgo::pow(image_buffer, image_buffer, {g, g, g, 1.0f});
67 }
68
69 /* Write to disk and close */
70 image_buffer.set_write_format(TypeDesc::FLOAT);
71 image_buffer.write(image_output.get());
72 image_output->close();
73}
74
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
function< void(const string &)> LogFunction
OIIOOutputDriver(const string_view filepath, const string_view pass, LogFunction log)
CCL_NAMESPACE_BEGIN ustring u_colorspace_auto
ustring u_colorspace_srgb
#define CCL_NAMESPACE_END
ccl_global const KernelWorkTile * tile
ccl_device_inline float3 log(float3 v)
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23