Blender V5.3
conversion.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2024 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later */
5
9
10#include "BLI_array.hh"
11#include "BLI_rect.hh"
12#include "BLI_task.hh"
13
14#include "IMB_filter.hh"
15#include "IMB_imbuf.hh"
16#include "IMB_imbuf_types.hh"
17
19
20#include "MEM_guardedalloc.h"
21
22#include "OCIO_colorspace.hh"
23
24namespace blender {
25
26/* -------------------------------------------------------------------- */
29
30MINLINE uchar ftochar(float value)
31{
32 return unit_float_to_uchar_clamp(value);
33}
34
35MINLINE void float_to_byte_dither_v4(uchar b[4], const float f[4], float dither, int x, int y)
36{
37 float dither_value = dither_random_value(x, y) * 0.0033f * dither;
38
39 b[0] = ftochar(dither_value + f[0]);
40 b[1] = ftochar(dither_value + f[1]);
41 b[2] = ftochar(dither_value + f[2]);
43}
44
46{
47 return ibuf && !flag_is_set(ibuf->flags, ImBufFlags::AlphaChannelPacked);
48}
49
51 const float *src,
52 int src_channels,
53 float dither,
54 bool predivide,
55 int width,
56 int height,
57 int stride,
58 int start_y)
59{
60 for (int y = 0; y < height; y++) {
61 const float *from = src + size_t(stride) * y * src_channels;
62 uchar *to = dest + size_t(stride) * y * 4;
63 if (src_channels == 1) {
64 /* single channel input */
65 for (int x = 0; x < width; x++, from++, to += 4) {
66 to[0] = to[1] = to[2] = to[3] = unit_float_to_uchar_clamp(from[0]);
67 }
68 }
69 else if (src_channels == 3) {
70 /* RGB input */
71 for (int x = 0; x < width; x++, from += 3, to += 4) {
72 rgb_float_to_uchar(to, from);
73 to[3] = 255;
74 }
75 }
76 else if (src_channels == 4) {
77 /* RGBA input */
78 if (dither && predivide) {
79 float straight[4];
80 for (int x = 0; x < width; x++, from += 4, to += 4) {
81 premul_to_straight_v4_v4(straight, from);
82 float_to_byte_dither_v4(to, straight, dither, x, y + start_y);
83 }
84 }
85 else if (dither) {
86 for (int x = 0; x < width; x++, from += 4, to += 4) {
87 float_to_byte_dither_v4(to, from, dither, x, y + start_y);
88 }
89 }
90 else if (predivide) {
91 for (int x = 0; x < width; x++, from += 4, to += 4) {
93 }
94 }
95 else {
96 for (int x = 0; x < width; x++, from += 4, to += 4) {
97 rgba_float_to_uchar(to, from);
98 }
99 }
100 }
101 }
102}
103
105 const float *src,
106 int src_channels,
107 float dither,
108 int width,
109 int height,
110 const char *mask)
111{
112 for (int y = 0; y < height; y++) {
113 const float *from = src + size_t(width) * y * src_channels;
114 uchar *to = dest + size_t(width) * y * 4;
115 if (src_channels == 1) {
116 /* single channel input */
117 for (int x = 0; x < width; x++, from++, to += 4) {
118 if (*mask++ == FILTER_MASK_USED) {
119 to[0] = to[1] = to[2] = to[3] = unit_float_to_uchar_clamp(from[0]);
120 }
121 }
122 }
123 else if (src_channels == 3) {
124 /* RGB input */
125 for (int x = 0; x < width; x++, from += 3, to += 4) {
126 if (*mask++ == FILTER_MASK_USED) {
127 rgb_float_to_uchar(to, from);
128 to[3] = 255;
129 }
130 }
131 }
132 else if (src_channels == 4) {
133 /* RGBA input */
134 if (dither) {
135 for (int x = 0; x < width; x++, from += 4, to += 4) {
136 if (*mask++ == FILTER_MASK_USED) {
137 float_to_byte_dither_v4(to, from, dither, x, y);
138 }
139 }
140 }
141 else {
142 for (int x = 0; x < width; x++, from += 4, to += 4) {
143 if (*mask++ == FILTER_MASK_USED) {
144 rgba_float_to_uchar(to, from);
145 }
146 }
147 }
148 }
149 }
150}
151
153 float *dest, const uchar *src, int width, int height, int dest_stride, int src_stride)
154{
155 for (int y = 0; y < height; y++) {
156 const uchar *from = src + size_t(src_stride) * y * 4;
157 float *to = dest + size_t(dest_stride) * y * 4;
158
159 for (int x = 0; x < width; x++, from += 4, to += 4) {
160 rgba_uchar_to_float(to, from);
161 }
162 }
163}
164
166 float *dest, const float *src, int src_channels, int width, int height)
167{
168 if (src_channels == 1) {
169 /* single channel input */
170 for (int y = 0; y < height; y++) {
171 const float *from = src + size_t(width) * y;
172 float *to = dest + size_t(width) * y * 4;
173
174 for (int x = 0; x < width; x++, from++, to += 4) {
175 to[0] = to[1] = to[2] = to[3] = from[0];
176 }
177 }
178 }
179 else if (src_channels == 3) {
180 /* RGB input */
181 for (int y = 0; y < height; y++) {
182 const float *from = src + size_t(width) * y * 3;
183 float *to = dest + size_t(width) * y * 4;
184
185 for (int x = 0; x < width; x++, from += 3, to += 4) {
186 copy_v3_v3(to, from);
187 to[3] = 1.0f;
188 }
189 }
190 }
191 else if (src_channels == 4) {
192 /* RGBA input */
193 for (int y = 0; y < height; y++) {
194 const float *from = src + size_t(width) * y * 4;
195 float *to = dest + size_t(width) * y * 4;
196 memcpy(to, from, sizeof(float) * size_t(4) * width);
197 }
198 }
199}
200
202 float *dest, const float *src, int src_channels, int width, int height, const char *mask)
203{
204 if (src_channels == 1) {
205 /* single channel input */
206 for (int y = 0; y < height; y++) {
207 const float *from = src + size_t(width) * y;
208 float *to = dest + size_t(width) * y * 4;
209
210 for (int x = 0; x < width; x++, from++, to += 4) {
211 if (*mask++ == FILTER_MASK_USED) {
212 to[0] = to[1] = to[2] = to[3] = from[0];
213 }
214 }
215 }
216 }
217 else if (src_channels == 3) {
218 /* RGB input */
219 for (int y = 0; y < height; y++) {
220 const float *from = src + size_t(width) * y * 3;
221 float *to = dest + size_t(width) * y * 4;
222
223 for (int x = 0; x < width; x++, from += 3, to += 4) {
224 if (*mask++ == FILTER_MASK_USED) {
225 copy_v3_v3(to, from);
226 to[3] = 1.0f;
227 }
228 }
229 }
230 }
231 else if (src_channels == 4) {
232 /* RGBA input */
233 for (int y = 0; y < height; y++) {
234 const float *from = src + size_t(width) * y * 4;
235 float *to = dest + size_t(width) * y * 4;
236
237 for (int x = 0; x < width; x++, from += 4, to += 4) {
238 if (*mask++ == FILTER_MASK_USED) {
239 copy_v4_v4(to, from);
240 }
241 }
242 }
243 }
244}
245
246void IMB_buffer_float_rgba_srgb_to_linear(float *buffer, int width, int height)
247{
248 threading::parallel_for(IndexRange(height), 64, [&](const IndexRange y_range) {
249 for (int y : y_range) {
250 float *ptr = buffer + size_t(width) * y * 4;
251 for (int x = 0; x < width; x++, ptr += 4) {
253 }
254 }
255 });
256}
257
259
260/* -------------------------------------------------------------------- */
263
265{
266 /* Nothing to do if there's no float buffer */
267 const float *float_data = ibuf->float_data();
268 if (float_data == nullptr) {
269 return;
270 }
271
272 /* Allocate byte buffer if needed. */
273 if (ibuf->byte_data() == nullptr) {
274 if (!IMB_alloc_byte_pixels(ibuf, false)) {
275 return;
276 }
277 }
278
279 const char *from_colorspace = IMB_colormanagement_get_float_colorspace(ibuf);
280 const char *to_colorspace = IMB_colormanagement_get_byte_colorspace(ibuf);
281 const bool predivide = IMB_alpha_affects_rgb(ibuf);
283 to_colorspace);
284
285 /* At 4 floats per pixel, this is 32KB of data, and fits into typical CPU L1 cache. */
286 static constexpr int grain_size = 2048;
287 uchar *byte_data = ibuf->byte_data_for_write();
289 IndexRange(IMB_get_pixel_count(ibuf)), grain_size, [&](const IndexRange range) {
290 /* Copy chunk of source float pixels into a local buffer. */
291 Array<float, grain_size * 4> buffer(range.size() * ibuf->channels);
292 buffer.as_mutable_span().copy_from(
293 Span(float_data + range.first() * ibuf->channels, buffer.size()));
294 /* Unpremultiply alpha if needed. */
295 if (predivide) {
296 IMB_unpremultiply_rect_float(buffer.data(), ibuf->channels, range.size(), 1);
297 }
298 /* Convert to byte color space if needed. */
299 if (!processor.is_noop()) {
300 processor.apply(buffer.data(), range.size(), 1, ibuf->channels, false);
301 }
302 /* Convert to bytes. */
303 IMB_buffer_byte_from_float(byte_data + range.first() * 4,
304 buffer.data(),
305 ibuf->channels,
306 ibuf->dither,
307 false,
308 range.size(),
309 1,
310 ibuf->x);
311 });
312}
313
314void IMB_float_from_byte_ex(ImBuf *dst, const ImBuf *src, const rcti *region_to_update)
315{
316 BLI_assert_msg(dst->float_data() != nullptr,
317 "Destination buffer should have a float buffer assigned.");
318 BLI_assert_msg(src->byte_data() != nullptr, "Source buffer should have a byte buffer assigned.");
319 BLI_assert_msg(dst->x == src->x, "Source and destination buffer should have the same dimension");
320 BLI_assert_msg(dst->y == src->y, "Source and destination buffer should have the same dimension");
321 BLI_assert_msg(dst->channels == 4, "Destination buffer should have 4 channels.");
322 BLI_assert_msg(region_to_update->xmin >= 0,
323 "Region to update should be clipped to the given buffers.");
324 BLI_assert_msg(region_to_update->ymin >= 0,
325 "Region to update should be clipped to the given buffers.");
326 BLI_assert_msg(region_to_update->xmax <= dst->x,
327 "Region to update should be clipped to the given buffers.");
328 BLI_assert_msg(region_to_update->ymax <= dst->y,
329 "Region to update should be clipped to the given buffers.");
330
331 const int region_width = BLI_rcti_size_x(region_to_update);
332 const int region_height = BLI_rcti_size_y(region_to_update);
334
335 const uchar *byte_data = src->byte_data();
336 float *float_data = dst->float_data_for_write();
338 IndexRange(region_to_update->ymin, region_height), 64, [&](const IndexRange y_range) {
339 const uchar *src_ptr = byte_data + (region_to_update->xmin + y_range.first() * dst->x) * 4;
340 float *dst_ptr = float_data + (region_to_update->xmin + y_range.first() * dst->x) * 4;
341
342 /* Convert byte -> float without color or alpha conversions. */
343 IMB_buffer_float_from_byte(dst_ptr, src_ptr, region_width, y_range.size(), src->x, dst->x);
344
345 /* Convert to scene linear color space, and premultiply alpha if needed. */
346 float *dst_ptr_line = dst_ptr;
347 for ([[maybe_unused]] const int64_t y : y_range) {
348 IMB_colormanagement_colorspace_to_scene_linear(
349 dst_ptr_line, region_width, 1, dst->channels, src->byte_buffer.colorspace, false);
350 if (premultiply_alpha) {
351 IMB_premultiply_rect_float(dst_ptr_line, dst->channels, region_width, 1);
352 }
353 dst_ptr_line += 4 * dst->x;
354 }
355 });
356}
357
359{
360 /* Nothing to do if there's no byte buffer. */
361 if (ibuf->byte_data() == nullptr) {
362 return;
363 }
364
365 /* Allocate float buffer if needed. */
366 if (ibuf->float_data() == nullptr) {
367 if (!IMB_alloc_float_pixels(ibuf, 4, false)) {
368 return;
369 }
370 }
371
372 /* Pixels get converted to scene linear, so clear colorspace. */
373 ibuf->float_buffer.colorspace = nullptr;
374
375 rcti region_to_update;
376 BLI_rcti_init(&region_to_update, 0, ibuf->x, 0, ibuf->y);
377 IMB_float_from_byte_ex(ibuf, ibuf, &region_to_update);
378}
379
381
382/* -------------------------------------------------------------------- */
385
387{
388 float *rct_fl = ibuf->float_data_for_write();
389 uchar *rct = ibuf->byte_data_for_write();
390 size_t i;
391
392 if (rct_fl) {
393 if (ibuf->channels >= 3) {
394 for (i = IMB_get_pixel_count(ibuf); i > 0; i--, rct_fl += ibuf->channels) {
395 rct_fl[0] = rct_fl[1] = rct_fl[2] = IMB_colormanagement_get_luminance(rct_fl);
396 }
397 }
398 }
399
400 if (rct) {
401 for (i = IMB_get_pixel_count(ibuf); i > 0; i--, rct += 4) {
402 rct[0] = rct[1] = rct[2] = IMB_colormanagement_get_luminance_byte(rct);
403 }
404 }
405}
406
408
409/* -------------------------------------------------------------------- */
412
413void IMB_saturation(ImBuf *ibuf, float sat)
414{
415 const size_t pixel_count = IMB_get_pixel_count(ibuf);
416 if (uchar *byte_data = ibuf->byte_data_for_write()) {
417 threading::parallel_for(IndexRange(pixel_count), 64 * 1024, [&](IndexRange range) {
418 uchar *ptr = byte_data + range.first() * 4;
419 float rgb[3];
420 float hsv[3];
421 for ([[maybe_unused]] const int64_t i : range) {
422 rgb_uchar_to_float(rgb, ptr);
423 rgb_to_hsv_v(rgb, hsv);
424 hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], rgb + 0, rgb + 1, rgb + 2);
425 rgb_float_to_uchar(ptr, rgb);
426 ptr += 4;
427 }
428 });
429 }
430
431 float *float_data = ibuf->float_data_for_write();
432 if (float_data != nullptr && ibuf->channels >= 3) {
433 threading::parallel_for(IndexRange(pixel_count), 64 * 1024, [&](IndexRange range) {
434 const int channels = ibuf->channels;
435 float *ptr = float_data + range.first() * channels;
436 float hsv[3];
437 for ([[maybe_unused]] const int64_t i : range) {
438 rgb_to_hsv_v(ptr, hsv);
439 hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], ptr + 0, ptr + 1, ptr + 2);
440 ptr += channels;
441 }
442 });
443 }
444}
445
447
448} // namespace blender
#define MINLINE
Function declarations for filter.cc.
#define FILTER_MASK_USED
Definition IMB_imbuf.hh:347
Read Blender memory allocation and freeing.
#define BLI_assert_msg(a, msg)
int64_t size() const
Definition BLI_array.hh:267
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T * data() const
Definition BLI_array.hh:323
void apply(float *buffer, int width, int height, int channels, bool predivide) const
static ColormanageProcessor colorspace_processor_new(StringRefNull from_colorspace, StringRefNull to_colorspace)
constexpr int64_t first() const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr Span()=default
ColorSceneLinear4f< eAlpha::Premultiplied > premultiply_alpha(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:93
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
void IMB_buffer_float_rgba_srgb_to_linear(float *buffer, int width, int height)
void IMB_float_from_byte_ex(ImBuf *dst, const ImBuf *src, const rcti *region_to_update)
__int64 int64_t
Definition BKE_text.h:90
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
void IMB_buffer_float_from_byte(float *dest, const unsigned char *src, int width, int height, int dest_stride, int src_stride)
void IMB_saturation(ImBuf *ibuf, float sat)
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition math_color.cc:25
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
bool IMB_alloc_byte_pixels(ImBuf *ibuf, bool initialize_pixels=true)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
BLI_INLINE unsigned char IMB_colormanagement_get_luminance_byte(const unsigned char rgb[3])
const char * IMB_colormanagement_get_byte_colorspace(const ImBuf *ibuf)
void IMB_buffer_byte_from_float(unsigned char *dest, const float *src, int src_channels, float dither, bool predivide, int width, int height, int stride, int start_y=0)
Definition conversion.cc:50
MINLINE void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3])
void IMB_buffer_byte_from_float_mask(unsigned char *dest, const float *src, int src_channels, float dither, int width, int height, const char *mask)
void IMB_float_from_byte(ImBuf *ibuf)
MINLINE float dither_random_value(int x, int y)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
unsigned char uchar
Definition BKE_text.h:58
void IMB_buffer_float_rgba_from_float(float *dest, const float *src, int src_channels, int width, int height)
void IMB_color_to_bw(ImBuf *ibuf)
MINLINE uchar ftochar(float value)
Definition conversion.cc:30
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void IMB_buffer_float_rgba_from_float_mask(float *dest, const float *src, int src_channels, int width, int height, const char *mask)
bool IMB_alpha_affects_rgb(const ImBuf *ibuf)
Definition conversion.cc:45
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4])
void IMB_byte_from_float(ImBuf *ibuf)
MINLINE void float_to_byte_dither_v4(uchar b[4], const float f[4], float dither, int x, int y)
Definition conversion.cc:35
MINLINE void rgb_uchar_to_float(float r_col[3], const unsigned char col_ub[3])
MINLINE void copy_v4_v4(float r[4], const float a[4])
bool IMB_alloc_float_pixels(ImBuf *ibuf, unsigned int channels, bool initialize_pixels=true)
void IMB_unpremultiply_rect_float(float *rect_float, int channels, int w, int h)
Definition filter.cc:426
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
const char * IMB_colormanagement_get_float_colorspace(const ImBuf *ibuf)
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79