Files
test/source/blender/imbuf/intern/imageprocess.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.2 KiB
C++
Raw Normal View History

/* 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
*
* SPDX-License-Identifier: GPL-2.0-or-later */
2012-04-30 14:24:11 +00:00
/** \file
* \ingroup imbuf
2011-02-27 20:23:21 +00:00
*/
#include "BLI_math_vector.h"
#include "BLI_task.hh"
2024-01-18 22:50:23 +02:00
#include "IMB_imbuf.hh"
#include "IMB_imbuf_types.hh"
void IMB_convert_rgba_to_abgr(ImBuf *ibuf)
2002-10-12 11:37:38 +00:00
{
size_t size;
uchar rt, *cp = ibuf->byte_buffer.data;
float rtf, *cpf = ibuf->float_buffer.data;
if (ibuf->byte_buffer.data) {
size = ibuf->x * ibuf->y;
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;
}
}
if (ibuf->float_buffer.data) {
size = ibuf->x * ibuf->y;
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;
}
2002-10-12 11:37:38 +00:00
}
}
2020-09-14 16:11:13 +10:00
/* -------------------------------------------------------------------- */
/** \name Alpha-under
2020-09-14 16:11:13 +10:00
* \{ */
void IMB_alpha_under_color_float(float *rect_float, int x, int y, float backcol[3])
{
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;
}
});
}
void IMB_alpha_under_color_byte(uchar *rect, int x, int y, const float backcol[3])
{
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;
}
});
}
2020-09-14 16:11:13 +10:00
/** \} */