Blender V4.5
image_oiio.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/image_oiio.h"
6
7#include "util/image.h"
8#include "util/log.h"
9#include "util/path.h"
10#include "util/unique_ptr.h"
11
13
15
17
19 ImageMetaData &metadata)
20{
21 /* Perform preliminary checks, with meaningful logging. */
22 if (!path_exists(filepath.string())) {
23 VLOG_WARNING << "File '" << filepath.string() << "' does not exist.";
24 return false;
25 }
26 if (path_is_directory(filepath.string())) {
27 VLOG_WARNING << "File '" << filepath.string() << "' is a directory, can't use as image.";
28 return false;
29 }
30
31 unique_ptr<ImageInput> in(ImageInput::create(filepath.string()));
32
33 if (!in) {
34 return false;
35 }
36
37 ImageSpec spec;
38 if (!in->open(filepath.string(), spec)) {
39 return false;
40 }
41
42 metadata.width = spec.width;
43 metadata.height = spec.height;
44 metadata.depth = spec.depth;
45 metadata.compress_as_srgb = false;
46
47 /* Check the main format, and channel formats. */
48 size_t channel_size = spec.format.basesize();
49
50 bool is_float = false;
51 bool is_half = false;
52
53 if (spec.format.is_floating_point()) {
54 is_float = true;
55 }
56
57 for (size_t channel = 0; channel < spec.channelformats.size(); channel++) {
58 channel_size = max(channel_size, spec.channelformats[channel].basesize());
59 if (spec.channelformats[channel].is_floating_point()) {
60 is_float = true;
61 }
62 }
63
64 /* check if it's half float */
65 if (spec.format == TypeDesc::HALF) {
66 is_half = true;
67 }
68
69 /* set type and channels */
70 metadata.channels = spec.nchannels;
71
72 if (is_half) {
73 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF;
74 }
75 else if (is_float) {
76 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
77 }
78 else if (spec.format == TypeDesc::USHORT) {
79 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_USHORT4 : IMAGE_DATA_TYPE_USHORT;
80 }
81 else {
82 metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
83 }
84
85 metadata.colorspace_file_format = in->format_name();
86 metadata.colorspace_file_hint = spec.get_string_attribute("oiio:ColorSpace");
87
88 in->close();
89
90 return true;
91}
92
93template<TypeDesc::BASETYPE FileFormat, typename StorageType>
94static void oiio_load_pixels(const ImageMetaData &metadata,
96 const bool associate_alpha,
97 StorageType *pixels)
98{
99 const size_t width = metadata.width;
100 const size_t height = metadata.height;
101 const int depth = metadata.depth;
102 const int components = metadata.channels;
103
104 /* Read pixels through OpenImageIO. */
105 StorageType *readpixels = pixels;
106 vector<StorageType> tmppixels;
107 if (components > 4) {
108 tmppixels.resize(width * height * components);
109 readpixels = &tmppixels[0];
110 }
111
112 if (depth <= 1) {
113 const size_t scanlinesize = width * components * sizeof(StorageType);
114 in->read_image(0,
115 0,
116 0,
117 components,
118 FileFormat,
119 (uchar *)readpixels + (height - 1) * scanlinesize,
120 AutoStride,
121 -scanlinesize,
122 AutoStride);
123 }
124 else {
125 in->read_image(0, 0, 0, components, FileFormat, (uchar *)readpixels);
126 }
127
128 if (components > 4) {
129 const size_t dimensions = width * height;
130 for (size_t i = dimensions - 1, pixel = 0; pixel < dimensions; pixel++, i--) {
131 pixels[i * 4 + 3] = tmppixels[i * components + 3];
132 pixels[i * 4 + 2] = tmppixels[i * components + 2];
133 pixels[i * 4 + 1] = tmppixels[i * components + 1];
134 pixels[i * 4 + 0] = tmppixels[i * components + 0];
135 }
136 tmppixels.clear();
137 }
138
139 /* CMYK to RGBA. */
140 const bool cmyk = strcmp(in->format_name(), "jpeg") == 0 && components == 4;
141 if (cmyk) {
142 const StorageType one = util_image_cast_from_float<StorageType>(1.0f);
143
144 const size_t num_pixels = width * height * depth;
145 for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
146 const float c = util_image_cast_to_float(pixels[i * 4 + 0]);
147 const float m = util_image_cast_to_float(pixels[i * 4 + 1]);
148 const float y = util_image_cast_to_float(pixels[i * 4 + 2]);
149 const float k = util_image_cast_to_float(pixels[i * 4 + 3]);
150 pixels[i * 4 + 0] = util_image_cast_from_float<StorageType>((1.0f - c) * (1.0f - k));
151 pixels[i * 4 + 1] = util_image_cast_from_float<StorageType>((1.0f - m) * (1.0f - k));
152 pixels[i * 4 + 2] = util_image_cast_from_float<StorageType>((1.0f - y) * (1.0f - k));
153 pixels[i * 4 + 3] = one;
154 }
155 }
156
157 if (components == 4 && associate_alpha) {
158 const size_t dimensions = width * height;
159 for (size_t i = dimensions - 1, pixel = 0; pixel < dimensions; pixel++, i--) {
160 const StorageType alpha = pixels[i * 4 + 3];
161 pixels[i * 4 + 0] = util_image_multiply_native(pixels[i * 4 + 0], alpha);
162 pixels[i * 4 + 1] = util_image_multiply_native(pixels[i * 4 + 1], alpha);
163 pixels[i * 4 + 2] = util_image_multiply_native(pixels[i * 4 + 2], alpha);
164 }
165 }
166}
167
169 void *pixels,
170 const size_t /*pixels_size*/,
171 const bool associate_alpha)
172{
173 unique_ptr<ImageInput> in = nullptr;
174
175 /* NOTE: Error logging is done in meta data acquisition. */
176 if (!path_exists(filepath.string()) || path_is_directory(filepath.string())) {
177 return false;
178 }
179
180 /* load image from file through OIIO */
181 in = unique_ptr<ImageInput>(ImageInput::create(filepath.string()));
182 if (!in) {
183 return false;
184 }
185
186 ImageSpec spec = ImageSpec();
187 ImageSpec config = ImageSpec();
188
189 /* Load without automatic OIIO alpha conversion, we do it ourselves. OIIO
190 * will associate alpha in the 8bit buffer for PNGs, which leads to too
191 * much precision loss when we load it as half float to do a color-space transform. */
192 config.attribute("oiio:UnassociatedAlpha", 1);
193
194 if (!in->open(filepath.string(), spec, config)) {
195 return false;
196 }
197
198 bool do_associate_alpha = false;
199 if (associate_alpha) {
200 do_associate_alpha = spec.get_int_attribute("oiio:UnassociatedAlpha", 0);
201
202 if (!do_associate_alpha && spec.alpha_channel != -1) {
203 /* Workaround OIIO not detecting TGA file alpha the same as Blender (since #3019).
204 * We want anything not marked as premultiplied alpha to get associated. */
205 if (strcmp(in->format_name(), "targa") == 0) {
206 do_associate_alpha = spec.get_int_attribute("targa:alpha_type", -1) != 4;
207 }
208 /* OIIO DDS reader never sets UnassociatedAlpha attribute. */
209 if (strcmp(in->format_name(), "dds") == 0) {
210 do_associate_alpha = true;
211 }
212 /* Workaround OIIO bug that sets oiio:UnassociatedAlpha on the last layer
213 * but not composite image that we read. */
214 if (strcmp(in->format_name(), "psd") == 0) {
215 do_associate_alpha = true;
216 }
217 }
218 }
219
220 switch (metadata.type) {
223 oiio_load_pixels<TypeDesc::UINT8, uchar>(metadata, in, do_associate_alpha, (uchar *)pixels);
224 break;
228 metadata, in, do_associate_alpha, (uint16_t *)pixels);
229 break;
232 oiio_load_pixels<TypeDesc::HALF, half>(metadata, in, do_associate_alpha, (half *)pixels);
233 break;
236 oiio_load_pixels<TypeDesc::FLOAT, float>(metadata, in, do_associate_alpha, (float *)pixels);
237 break;
243 break;
244 }
245
246 in->close();
247 return true;
248}
249
251{
252 return path_filename(filepath.string());
253}
254
256{
257 return filepath;
258}
259
260bool OIIOImageLoader::equals(const ImageLoader &other) const
261{
262 const OIIOImageLoader &other_loader = (const OIIOImageLoader &)other;
263 return filepath == other_loader.filepath;
264}
265
unsigned char uchar
ImageDataType type
const char * colorspace_file_format
ustring osl_filepath() const override
bool equals(const ImageLoader &other) const override
bool load_pixels(const ImageMetaData &metadata, void *pixels, const size_t pixels_size, const bool associate_alpha) override
ustring filepath
Definition image_oiio.h:30
~OIIOImageLoader() override
OIIOImageLoader(const string &filepath)
bool load_metadata(const ImageDeviceFeatures &features, ImageMetaData &metadata) override
string name() const override
Definition half.h:41
T util_image_cast_from_float(const float value)
T util_image_multiply_native(T a, T b)
float util_image_cast_to_float(T value)
#define CCL_NAMESPACE_END
#define in
static void oiio_load_pixels(const ImageMetaData &metadata, const unique_ptr< ImageInput > &in, const bool associate_alpha, StorageType *pixels)
#define VLOG_WARNING
Definition log.h:69
bool path_is_directory(const string &path)
Definition path.cpp:582
bool path_exists(const string &path)
Definition path.cpp:563
string path_filename(const string &path)
Definition path.cpp:378
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
@ IMAGE_DATA_NUM_TYPES
@ IMAGE_DATA_TYPE_BYTE
@ IMAGE_DATA_TYPE_FLOAT
@ IMAGE_DATA_TYPE_NANOVDB_FP16
@ IMAGE_DATA_TYPE_FLOAT4
@ IMAGE_DATA_TYPE_USHORT4
@ IMAGE_DATA_TYPE_USHORT
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT
@ IMAGE_DATA_TYPE_NANOVDB_FLOAT3
@ IMAGE_DATA_TYPE_HALF
@ IMAGE_DATA_TYPE_BYTE4
@ IMAGE_DATA_TYPE_HALF4
@ IMAGE_DATA_TYPE_NANOVDB_FPN