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