Blender V5.3
imbuf/intern/rotate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 * SPDX-FileCopyrightText: 2025 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later */
5
9
10#include "BLI_task.hh"
11#include "BLI_utildefines.hh"
12
13#include "MEM_guardedalloc.h"
14
15#include "IMB_imbuf.hh"
16#include "IMB_imbuf_types.hh"
17
18namespace blender {
19
20template<typename T>
21static void rotate_pixels(const int degrees,
22 const int size_x,
23 const int size_y,
24 const T *src_pixels,
25 T *dst_pixels,
26 const int channels)
27{
28 threading::parallel_for(IndexRange(size_y), 256, [&](const IndexRange y_range) {
29 const T *src_pixel = src_pixels + y_range.first() * size_x * channels;
30 if (degrees == 90) {
31 for (int y : y_range) {
32 for (int x = 0; x < size_x; x++, src_pixel += channels) {
33 memcpy(&dst_pixels[(y + ((size_x - x - 1) * size_y)) * channels],
34 src_pixel,
35 sizeof(T) * channels);
36 }
37 }
38 }
39 else if (degrees == 180) {
40 for (int y : y_range) {
41 for (int x = 0; x < size_x; x++, src_pixel += channels) {
42 memcpy(&dst_pixels[(((size_y - y - 1) * size_x) + (size_x - x - 1)) * channels],
43 src_pixel,
44 sizeof(T) * channels);
45 }
46 }
47 }
48 else if (degrees == 270) {
49 for (int y : y_range) {
50 for (int x = 0; x < size_x; x++, src_pixel += channels) {
51 memcpy(&dst_pixels[((size_y - y - 1) + (x * size_y)) * channels],
52 src_pixel,
53 sizeof(T) * channels);
54 }
55 }
56 }
57 });
58}
59
61{
62 if (!ELEM(degrees, 90, 180, 270)) {
63 return false;
64 }
65
66 const int size_x = ibuf->x;
67 const int size_y = ibuf->y;
68
69 if (ELEM(degrees, 90, 270)) {
70 std::swap(ibuf->x, ibuf->y);
71 }
72 if (ibuf->float_data()) {
73 const int channels = ibuf->channels;
74 const float *src_pixels = ibuf->float_data();
75 float *dst_pixels = MEM_new_array_uninitialized<float>(
76 size_t(channels) * size_t(size_x) * size_t(size_y), __func__);
77 rotate_pixels<float>(degrees, size_x, size_y, src_pixels, dst_pixels, ibuf->channels);
78 ibuf->assign_float_data(dst_pixels);
79 if (ibuf->byte_data()) {
81 }
82 }
83 else if (ibuf->byte_data()) {
84 const uchar *src_pixels = ibuf->byte_data();
85 uchar *dst_pixels = MEM_new_array_uninitialized<uchar>(4 * size_t(size_x) * size_t(size_y),
86 __func__);
87 rotate_pixels<uchar>(degrees, size_x, size_y, src_pixels, dst_pixels, 4);
88 ibuf->assign_byte_data(dst_pixels);
89 }
90
91 return true;
92}
93
94void IMB_flipy(ImBuf *ibuf)
95{
96 size_t x_size, y_size;
97
98 if (ibuf == nullptr) {
99 return;
100 }
101
102 if (ibuf->byte_data()) {
103 uint *top, *bottom, *line;
104
105 x_size = ibuf->x;
106 y_size = ibuf->y;
107
108 const size_t stride = x_size * sizeof(int);
109
110 top = reinterpret_cast<uint *>(ibuf->byte_data_for_write());
111 bottom = top + ((y_size - 1) * x_size);
112 line = MEM_new_array_uninitialized<uint>(x_size, "linebuf");
113
114 y_size >>= 1;
115
116 for (; y_size > 0; y_size--) {
117 memcpy(line, top, stride);
118 memcpy(top, bottom, stride);
119 memcpy(bottom, line, stride);
120 bottom -= x_size;
121 top += x_size;
122 }
123
124 MEM_delete(line);
125 }
126
127 if (ibuf->float_data()) {
128 float *topf = nullptr, *bottomf = nullptr, *linef = nullptr;
129
130 x_size = ibuf->x;
131 y_size = ibuf->y;
132
133 const size_t stride = x_size * 4 * sizeof(float);
134
135 topf = ibuf->float_data_for_write();
136 bottomf = topf + 4 * ((y_size - 1) * x_size);
137 linef = MEM_new_array_uninitialized<float>(4 * x_size, "linebuf");
138
139 y_size >>= 1;
140
141 for (; y_size > 0; y_size--) {
142 memcpy(linef, topf, stride);
143 memcpy(topf, bottomf, stride);
144 memcpy(bottomf, linef, stride);
145 bottomf -= 4 * x_size;
146 topf += 4 * x_size;
147 }
148
149 MEM_delete(linef);
150 }
151}
152
153void IMB_flipx(ImBuf *ibuf)
154{
155 int x, y, xr, xl, yi;
156 float px_f[4];
157
158 if (ibuf == nullptr) {
159 return;
160 }
161
162 x = ibuf->x;
163 y = ibuf->y;
164
165 if (ibuf->byte_data()) {
166 uint *rect = reinterpret_cast<uint *>(ibuf->byte_data_for_write());
167 for (yi = y - 1; yi >= 0; yi--) {
168 const size_t x_offset = size_t(x) * yi;
169 for (xr = x - 1, xl = 0; xr >= xl; xr--, xl++) {
170 std::swap(rect[x_offset + xr], rect[x_offset + xl]);
171 }
172 }
173 }
174
175 if (ibuf->float_data()) {
176 float *rect_float = ibuf->float_data_for_write();
177 for (yi = y - 1; yi >= 0; yi--) {
178 const size_t x_offset = size_t(x) * yi;
179 for (xr = x - 1, xl = 0; xr >= xl; xr--, xl++) {
180 memcpy(&px_f, &rect_float[(x_offset + xr) * 4], sizeof(float[4]));
181 memcpy(
182 &rect_float[(x_offset + xr) * 4], &rect_float[(x_offset + xl) * 4], sizeof(float[4]));
183 memcpy(&rect_float[(x_offset + xl) * 4], &px_f, sizeof(float[4]));
184 }
185 }
186 }
187}
188
189} // namespace blender
Read Blender memory allocation and freeing.
#define ELEM(...)
constexpr int64_t first() const
constexpr IndexRange()=default
constexpr T degrees(T) RET
#define T
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.
bool IMB_rotate_orthogonal(ImBuf *ibuf, int degrees)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
unsigned char uchar
Definition BKE_text.h:58
void IMB_flipx(ImBuf *ibuf)
void IMB_flipy(ImBuf *ibuf)
void IMB_byte_from_float(ImBuf *ibuf)
static void rotate_pixels(const int degrees, const int size_x, const int size_y, const T *src_pixels, T *dst_pixels, const int channels)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
void assign_byte_data(uint8_t *data)
float * float_data_for_write()
Definition allocimbuf.cc:79
void assign_float_data(float *data)