2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
|
ImBuf: Refactor pixel interpolation functions
There exist a bunch of "give me a (filtered) image pixel at this location"
functions, some with duplicated functionality, some with almost the same but
not quite, some that look similar but behave slightly differently, etc.
Some of them were in BLI, some were in ImBuf.
This commit tries to improve the situation by:
* Adding low level interpolation functions to `BLI_math_interp.hh`
- With documentation on their behavior,
- And with more unit tests.
* At `ImBuf` level, there are only convenience inline wrappers to the above BLI
functions (split off into a separate header `IMB_interp.hh`). However, since
these wrappers are inline, some things get a tiny bit faster as a side
effect. E.g. VSE image strip, scaling to 4K resolution (Windows/Ryzen5950X):
- Nearest filter: 2.33 -> 1.94ms
- Bilinear filter: 5.83 -> 5.69ms
- Subsampled3x3 filter: 28.6 -> 22.4ms
Details on the functions:
- All of them have `_byte` and `_fl` suffixes.
- They exist in 4-channel byte (uchar4) and float (float4), as well as
explicitly passed amount of channels for other float images.
- New functions in BLI `blender::math` namespace:
- `interpolate_nearest`
- `interpolate_bilinear`
- `interpolate_bilinear_wrap`. Note that unlike previous "wrap" function,
this one no longer requires the caller to do their own wrapping.
- `interpolate_cubic_bspline`. Previous similar function was called just
"bicubic" which could mean many different things.
- Same functions exist in `IMB_interp.hh`, they are just convenience that takes
ImBuf and uses data pointer, width, height from that.
Other bits:
- Renamed `mod_f_positive` to `floored_fmod` (better matches `safe_floored_modf`
and `floored_modulo` that exist elsewhere), made it branchless and added more
unit tests.
- `interpolate_bilinear_wrap_fl` no longer clamps result to 0..1 range. Instead,
moved the clamp to be outside of the call in `paint_image_proj.cc` and
`paint_utils.cc`. Though the need for clamping in there is also questionable.
Pull Request: https://projects.blender.org/blender/blender/pulls/117387
2024-01-25 11:45:24 +01:00
|
|
|
* SPDX-FileCopyrightText: 2024 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-04-30 14:24:11 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup imbuf
|
2011-02-27 20:23:21 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-02-28 14:43:05 +01:00
|
|
|
#include "BLI_math_vector.h"
|
2025-02-28 12:18:45 +01:00
|
|
|
#include "BLI_task.hh"
|
2012-03-08 14:23:34 +00:00
|
|
|
|
2024-01-18 22:50:23 +02:00
|
|
|
#include "IMB_imbuf.hh"
|
|
|
|
|
#include "IMB_imbuf_types.hh"
|
2007-07-10 19:13:03 +00:00
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void IMB_convert_rgba_to_abgr(ImBuf *ibuf)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2015-05-29 13:38:20 +02:00
|
|
|
size_t size;
|
2023-05-18 10:19:01 +02:00
|
|
|
uchar rt, *cp = ibuf->byte_buffer.data;
|
|
|
|
|
float rtf, *cpf = ibuf->float_buffer.data;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-18 10:19:01 +02:00
|
|
|
if (ibuf->byte_buffer.data) {
|
2008-01-20 18:55:56 +00:00
|
|
|
size = ibuf->x * ibuf->y;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 07:52:14 +00:00
|
|
|
while (size-- > 0) {
|
2012-05-13 22:05:51 +00:00
|
|
|
rt = cp[0];
|
|
|
|
|
cp[0] = cp[3];
|
|
|
|
|
cp[3] = rt;
|
|
|
|
|
rt = cp[1];
|
|
|
|
|
cp[1] = cp[2];
|
|
|
|
|
cp[2] = rt;
|
|
|
|
|
cp += 4;
|
2008-01-20 18:55:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-18 10:19:01 +02:00
|
|
|
if (ibuf->float_buffer.data) {
|
2008-01-20 18:55:56 +00:00
|
|
|
size = ibuf->x * ibuf->y;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 07:52:14 +00:00
|
|
|
while (size-- > 0) {
|
2012-05-13 22:05:51 +00:00
|
|
|
rtf = cpf[0];
|
|
|
|
|
cpf[0] = cpf[3];
|
|
|
|
|
cpf[3] = rtf;
|
|
|
|
|
rtf = cpf[1];
|
|
|
|
|
cpf[1] = cpf[2];
|
|
|
|
|
cpf[2] = rtf;
|
|
|
|
|
cpf += 4;
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-14 16:11:13 +10:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2013-01-05 15:33:18 +00:00
|
|
|
/** \name Alpha-under
|
2020-09-14 16:11:13 +10:00
|
|
|
* \{ */
|
2013-01-05 15:33:18 +00:00
|
|
|
|
|
|
|
|
void IMB_alpha_under_color_float(float *rect_float, int x, int y, float backcol[3])
|
|
|
|
|
{
|
2025-02-28 12:18:45 +01:00
|
|
|
using namespace blender;
|
|
|
|
|
threading::parallel_for(IndexRange(int64_t(x) * y), 32 * 1024, [&](const IndexRange i_range) {
|
|
|
|
|
float *pix = rect_float + i_range.first() * 4;
|
|
|
|
|
for ([[maybe_unused]] const int i : i_range) {
|
|
|
|
|
const float mul = 1.0f - pix[3];
|
|
|
|
|
madd_v3_v3fl(pix, backcol, mul);
|
|
|
|
|
pix[3] = 1.0f;
|
|
|
|
|
pix += 4;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-01-05 15:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
2022-09-13 16:29:06 +10:00
|
|
|
void IMB_alpha_under_color_byte(uchar *rect, int x, int y, const float backcol[3])
|
2013-01-05 15:33:18 +00:00
|
|
|
{
|
2025-02-28 12:18:45 +01:00
|
|
|
using namespace blender;
|
|
|
|
|
threading::parallel_for(IndexRange(int64_t(x) * y), 32 * 1024, [&](const IndexRange i_range) {
|
|
|
|
|
uchar *pix = rect + i_range.first() * 4;
|
|
|
|
|
for ([[maybe_unused]] const int i : i_range) {
|
|
|
|
|
if (pix[3] == 255) {
|
|
|
|
|
/* pass */
|
|
|
|
|
}
|
|
|
|
|
else if (pix[3] == 0) {
|
|
|
|
|
pix[0] = backcol[0] * 255;
|
|
|
|
|
pix[1] = backcol[1] * 255;
|
|
|
|
|
pix[2] = backcol[2] * 255;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
float alpha = pix[3] / 255.0;
|
|
|
|
|
float mul = 1.0f - alpha;
|
|
|
|
|
|
|
|
|
|
pix[0] = (pix[0] * alpha) + mul * backcol[0];
|
|
|
|
|
pix[1] = (pix[1] * alpha) + mul * backcol[1];
|
|
|
|
|
pix[2] = (pix[2] * alpha) + mul * backcol[2];
|
|
|
|
|
}
|
|
|
|
|
pix[3] = 255;
|
|
|
|
|
pix += 4;
|
2013-01-05 15:33:18 +00:00
|
|
|
}
|
2025-02-28 12:18:45 +01:00
|
|
|
});
|
2013-01-05 15:33:18 +00:00
|
|
|
}
|
2020-03-09 16:27:24 +01:00
|
|
|
|
2020-09-14 16:11:13 +10:00
|
|
|
/** \} */
|