Blender V5.3
blender/image.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 <algorithm>
6
7#include "DNA_image_types.h"
8
9#include "IMB_imbuf_types.hh"
10
11#include "BKE_image.hh"
12
13#include "blender/image.h"
14#include "blender/session.h"
15
16#include "util/half.h"
17#include "util/types_float4.h"
18
20
21/* Packed Images */
22
25 const int frame,
26 const int tile_number,
27 const bool is_preview_render)
30 /* Don't free cache for preview render to avoid race condition from #93560, to be fixed
31 * properly later as we are close to release. */
32 free_cache(!is_preview_render && !BKE_image_has_loaded_ibuf(b_image)),
33 cached_update_count(b_image->runtime->update_count)
34{
35 this->b_iuser.framenr = frame;
36 if (b_image->source != blender::IMA_SRC_TILED) {
37 /* Image sequences currently not supported by this image loader. */
38 assert(b_image->source != blender::IMA_SRC_SEQUENCE);
39 }
40 else {
41 /* Set UDIM tile, each can have different resolution. */
42 this->b_iuser.tile = tile_number;
43 }
44}
45
47 const ImageLoaderParams & /*params*/,
48 Progress & /*progress*/)
49{
50 bool is_float = false;
51 bool is_data = false;
52
53 {
54 void *lock;
55 blender::ImBuf *ibuf = BKE_image_acquire_ibuf(b_image, &b_iuser, &lock);
56 if (ibuf) {
57 is_float = ibuf->float_data() != nullptr;
58 is_data = ibuf->colorspace_is_data();
59 metadata.width = ibuf->x;
60 metadata.height = ibuf->y;
61 metadata.channels = (is_float) ? ibuf->channels : 4;
62 metadata.is_unassociated_alpha = !is_float;
63 }
64 else {
65 metadata.width = 0;
66 metadata.height = 0;
67 metadata.channels = 0;
68 }
69 BKE_image_release_ibuf(b_image, ibuf, lock);
70 }
71
72 if (is_float) {
73 if (metadata.channels == 1) {
74 metadata.type = IMAGE_DATA_TYPE_FLOAT;
75 }
76 else {
77 metadata.channels = 4;
79 }
80
81 /* Float images are already converted on the Blender side,
82 * no need to do anything in Cycles. */
84 }
85 else {
86 /* In some cases (e.g. #94135), the colorspace setting in Blender gets updated as part of the
87 * metadata queries in this function, so update the colorspace setting here. */
88 metadata.colorspace = (is_data) ? u_colorspace_data :
89 ustring(b_image->colorspace_settings.name);
90 metadata.type = IMAGE_DATA_TYPE_BYTE4;
91 }
92
93 return true;
94}
95
96static void load_float_pixels(const blender::ImBuf *ibuf,
97 const ImageMetaData &metadata,
98 float *out_pixels)
99{
100 const size_t num_pixels = ((size_t)metadata.width) * metadata.height;
101 const int out_channels = metadata.channels;
102 const int in_channels = ibuf->channels;
103 const float *in_pixels = ibuf->float_data();
104
105 if (in_pixels && out_channels == in_channels) {
106 /* Straight copy pixel data. */
107 memcpy(out_pixels, in_pixels, num_pixels * out_channels * sizeof(float));
108 }
109 else if (in_pixels && out_channels == 4) {
110 /* Fill channels to 4. */
111 float *out_pixel = out_pixels;
112 const float *in_pixel = in_pixels;
113 for (size_t i = 0; i < num_pixels; i++) {
114 out_pixel[0] = in_pixel[0];
115 out_pixel[1] = (in_channels >= 2) ? in_pixel[1] : 0.0f;
116 out_pixel[2] = (in_channels >= 3) ? in_pixel[2] : 0.0f;
117 out_pixel[3] = (in_channels >= 4) ? in_pixel[3] : 1.0f;
118 out_pixel += out_channels;
119 in_pixel += in_channels;
120 }
121 }
122 else {
123 /* Missing or invalid pixel data. */
124 if (out_channels == 1) {
125 std::fill(out_pixels, out_pixels + num_pixels, 0.0f);
126 }
127 else {
128 std::fill((float4 *)out_pixels,
129 (float4 *)out_pixels + num_pixels,
130 make_float4(1.0f, 0.0f, 1.0f, 1.0f));
131 }
132 }
133}
134
135static void load_half_pixels(const blender::ImBuf *ibuf,
136 const ImageMetaData &metadata,
137 half *out_pixels)
138{
139 /* Half float. Blender does not have a half type, but in some cases
140 * we up-sample byte to half to avoid precision loss for colorspace
141 * conversion. */
142 const size_t num_pixels = ((size_t)metadata.width) * metadata.height;
143 const int out_channels = metadata.channels;
144 const uchar *in_pixels = ibuf->byte_data();
145
146 if (in_pixels) {
147 /* Convert uchar to half. */
148 const uchar *in_pixel = in_pixels;
149 half *out_pixel = out_pixels;
150 for (size_t i = 0; i < num_pixels; i++) {
151 for (int c = 0; c < out_channels; c++, in_pixel++, out_pixel++) {
152 *out_pixel = float_to_half_image(util_image_cast_to_float(*in_pixel));
153 }
154 }
155 }
156 else {
157 /* Missing or invalid pixel data. */
158 if (out_channels == 1) {
159 std::fill(out_pixels, out_pixels + num_pixels, float_to_half_image(0.0f));
160 }
161 else {
162 std::fill((half4 *)out_pixels,
163 (half4 *)out_pixels + num_pixels,
164 float4_to_half4_display(make_float4(1.0f, 0.0f, 1.0f, 1.0f)));
165 }
166 }
167}
168
169static void load_byte_pixels(const blender::ImBuf *ibuf,
170 const ImageMetaData &metadata,
171 uchar *out_pixels)
172{
173 const size_t num_pixels = ((size_t)metadata.width) * metadata.height;
174 const int out_channels = metadata.channels;
175 const int in_channels = 4;
176 const uchar *in_pixels = ibuf->byte_data();
177
178 if (in_pixels) {
179 /* Straight copy pixel data. */
180 memcpy(out_pixels, in_pixels, num_pixels * in_channels * sizeof(unsigned char));
181 }
182 else {
183 /* Missing or invalid pixel data. */
184 if (out_channels == 1) {
185 std::fill(out_pixels, out_pixels + num_pixels, 0.0f);
186 }
187 else {
188 std::fill(
189 (uchar4 *)out_pixels, (uchar4 *)out_pixels + num_pixels, make_uchar4(255, 0, 255, 255));
190 }
191 }
192}
193
194bool BlenderImageLoader::load_pixels(const ImageMetaData &metadata, void *out_pixels)
195{
196 void *lock;
197 blender::ImBuf *ibuf = BKE_image_acquire_ibuf(b_image, &b_iuser, &lock);
198
199 /* Image changed since we requested metadata, assume we'll get a signal to reload it later. */
200 const bool mismatch = (ibuf == nullptr || ibuf->x != metadata.width ||
201 ibuf->y != metadata.height);
202
203 if (!mismatch) {
204 if (metadata.type == IMAGE_DATA_TYPE_FLOAT || metadata.type == IMAGE_DATA_TYPE_FLOAT4) {
205 load_float_pixels(ibuf, metadata, (float *)out_pixels);
206 }
207 else if (metadata.type == IMAGE_DATA_TYPE_HALF || metadata.type == IMAGE_DATA_TYPE_HALF4) {
208 load_half_pixels(ibuf, metadata, (half *)out_pixels);
209 }
210 else {
211 load_byte_pixels(ibuf, metadata, (uchar *)out_pixels);
212 }
213 }
214
215 BKE_image_release_ibuf(b_image, ibuf, lock);
216
217 /* Free image buffers to save memory during render. */
218 if (free_cache) {
219 BKE_image_free_buffers_ex(b_image, true);
220 }
221
222 if (!mismatch) {
223 metadata.conform_pixels(out_pixels);
224 }
225
226 return !mismatch;
227}
228
230{
231 return b_image->id.name + 2;
232}
233
235{
236 const BlenderImageLoader &other_loader = (const BlenderImageLoader &)other;
237 return b_image == other_loader.b_image && b_iuser.framenr == other_loader.b_iuser.framenr &&
238 b_iuser.tile == other_loader.b_iuser.tile &&
240}
241
243{
244 return b_iuser.tile;
245}
246
248{
249 /* Force builtin images to be loaded along with Blender data sync. This
250 * is needed because we may be reading from depsgraph evaluated data which
251 * can be freed by Blender before Cycles reads it.
252 *
253 * TODO: the assumption that no further access to builtin image data will
254 * happen is really weak, and likely to break in the future. We should find
255 * a better solution to hand over the data directly to the image manager
256 * instead of through callbacks whose timing is difficult to control. */
257 ImageManager *manager = session->scene->image_manager.get();
258 Device *device = session->device.get();
259 manager->device_load_builtin(device, session->scene.get(), session->progress);
260}
261
volatile int lock
static void load_float_pixels(const blender::ImBuf *ibuf, const ImageMetaData &metadata, float *out_pixels)
static void load_half_pixels(const blender::ImBuf *ibuf, const ImageMetaData &metadata, half *out_pixels)
static void load_byte_pixels(const blender::ImBuf *ibuf, const ImageMetaData &metadata, uchar *out_pixels)
blender::ImageUser b_iuser
bool load_metadata(ImageMetaData &metadata, const ImageLoaderParams &params, Progress &progress) override
bool equals(const ImageLoader &other) const override
blender::Image * b_image
BlenderImageLoader(blender::Image *b_image, blender::ImageUser *b_iuser, const int frame, const int tile_number, const bool is_preview_render)
bool load_pixels(const ImageMetaData &metadata, void *pixels) override
int get_tile_number() const override
string name() const override
unique_ptr< Session > session
void device_load_builtin(Device *device, Scene *scene, Progress &progress)
ImageDataType type
void conform_pixels(void *pixels) const
bool is_unassociated_alpha
Definition half.h:29
float util_image_cast_to_float(T value)
#define CCL_NAMESPACE_END
ccl_device_forceinline uchar4 make_uchar4(const uchar x, const uchar y, const uchar z, const uchar w)
unsigned int uchar
ccl_device_inline half float_to_half_image(const float f)
Definition half.h:316
ccl_device_inline half4 float4_to_half4_display(const float4 f)
Definition half.h:337
#define make_float4
const uint8_t * byte_data() const
bool colorspace_is_data() const
const float * float_data() const
Definition half.h:53
@ IMAGE_DATA_TYPE_FLOAT
Definition types_image.h:39
@ IMAGE_DATA_TYPE_FLOAT4
Definition types_image.h:36
@ IMAGE_DATA_TYPE_HALF
Definition types_image.h:41
@ IMAGE_DATA_TYPE_BYTE4
Definition types_image.h:37
@ IMAGE_DATA_TYPE_HALF4
Definition types_image.h:38
ustring u_colorspace_scene_linear
ustring u_colorspace_data