Blender V5.0
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.h"
12
13#include "MEM_guardedalloc.h"
14
15#include "IMB_imbuf.hh"
16#include "IMB_imbuf_types.hh"
17
18template<typename T>
19static void rotate_pixels(const int degrees,
20 const int size_x,
21 const int size_y,
22 const T *src_pixels,
23 T *dst_pixels,
24 const int channels)
25{
26 using namespace blender;
27 threading::parallel_for(IndexRange(size_y), 256, [&](const IndexRange y_range) {
28 const T *src_pixel = src_pixels + y_range.first() * size_x * channels;
29 if (degrees == 90) {
30 for (int y : y_range) {
31 for (int x = 0; x < size_x; x++, src_pixel += channels) {
32 memcpy(&dst_pixels[(y + ((size_x - x - 1) * size_y)) * channels],
33 src_pixel,
34 sizeof(T) * channels);
35 }
36 }
37 }
38 else if (degrees == 180) {
39 for (int y : y_range) {
40 for (int x = 0; x < size_x; x++, src_pixel += channels) {
41 memcpy(&dst_pixels[(((size_y - y - 1) * size_x) + (size_x - x - 1)) * channels],
42 src_pixel,
43 sizeof(T) * channels);
44 }
45 }
46 }
47 else if (degrees == 270) {
48 for (int y : y_range) {
49 for (int x = 0; x < size_x; x++, src_pixel += channels) {
50 memcpy(&dst_pixels[((size_y - y - 1) + (x * size_y)) * channels],
51 src_pixel,
52 sizeof(T) * channels);
53 }
54 }
55 }
56 });
57}
58
60{
61 if (!ELEM(degrees, 90, 180, 270)) {
62 return false;
63 }
64
65 const int size_x = ibuf->x;
66 const int size_y = ibuf->y;
67
68 if (ELEM(degrees, 90, 270)) {
69 std::swap(ibuf->x, ibuf->y);
70 }
71 if (ibuf->float_buffer.data) {
72 const int channels = ibuf->channels;
73 const float *src_pixels = ibuf->float_buffer.data;
74 float *dst_pixels = MEM_malloc_arrayN<float>(
75 size_t(channels) * size_t(size_x) * size_t(size_y), __func__);
76 rotate_pixels<float>(degrees, size_x, size_y, src_pixels, dst_pixels, ibuf->channels);
78 if (ibuf->byte_buffer.data) {
80 }
81 }
82 else if (ibuf->byte_buffer.data) {
83 const uchar *src_pixels = ibuf->byte_buffer.data;
84 uchar *dst_pixels = MEM_malloc_arrayN<uchar>(4 * size_t(size_x) * size_t(size_y), __func__);
85 rotate_pixels<uchar>(degrees, size_x, size_y, src_pixels, dst_pixels, 4);
87 }
88
89 return true;
90}
91
92void IMB_flipy(ImBuf *ibuf)
93{
94 size_t x_size, y_size;
95
96 if (ibuf == nullptr) {
97 return;
98 }
99
100 if (ibuf->byte_buffer.data) {
101 uint *top, *bottom, *line;
102
103 x_size = ibuf->x;
104 y_size = ibuf->y;
105
106 const size_t stride = x_size * sizeof(int);
107
108 top = (uint *)ibuf->byte_buffer.data;
109 bottom = top + ((y_size - 1) * x_size);
110 line = MEM_malloc_arrayN<uint>(x_size, "linebuf");
111
112 y_size >>= 1;
113
114 for (; y_size > 0; y_size--) {
115 memcpy(line, top, stride);
116 memcpy(top, bottom, stride);
117 memcpy(bottom, line, stride);
118 bottom -= x_size;
119 top += x_size;
120 }
121
122 MEM_freeN(line);
123 }
124
125 if (ibuf->float_buffer.data) {
126 float *topf = nullptr, *bottomf = nullptr, *linef = nullptr;
127
128 x_size = ibuf->x;
129 y_size = ibuf->y;
130
131 const size_t stride = x_size * 4 * sizeof(float);
132
133 topf = ibuf->float_buffer.data;
134 bottomf = topf + 4 * ((y_size - 1) * x_size);
135 linef = MEM_malloc_arrayN<float>(4 * x_size, "linebuf");
136
137 y_size >>= 1;
138
139 for (; y_size > 0; y_size--) {
140 memcpy(linef, topf, stride);
141 memcpy(topf, bottomf, stride);
142 memcpy(bottomf, linef, stride);
143 bottomf -= 4 * x_size;
144 topf += 4 * x_size;
145 }
146
147 MEM_freeN(linef);
148 }
149}
150
151void IMB_flipx(ImBuf *ibuf)
152{
153 int x, y, xr, xl, yi;
154 float px_f[4];
155
156 if (ibuf == nullptr) {
157 return;
158 }
159
160 x = ibuf->x;
161 y = ibuf->y;
162
163 if (ibuf->byte_buffer.data) {
164 uint *rect = (uint *)ibuf->byte_buffer.data;
165 for (yi = y - 1; yi >= 0; yi--) {
166 const size_t x_offset = size_t(x) * yi;
167 for (xr = x - 1, xl = 0; xr >= xl; xr--, xl++) {
168 std::swap(rect[x_offset + xr], rect[x_offset + xl]);
169 }
170 }
171 }
172
173 if (ibuf->float_buffer.data) {
174 float *rect_float = ibuf->float_buffer.data;
175 for (yi = y - 1; yi >= 0; yi--) {
176 const size_t x_offset = size_t(x) * yi;
177 for (xr = x - 1, xl = 0; xr >= xl; xr--, xl++) {
178 memcpy(&px_f, &rect_float[(x_offset + xr) * 4], sizeof(float[4]));
179 memcpy(
180 &rect_float[(x_offset + xr) * 4], &rect_float[(x_offset + xl) * 4], sizeof(float[4]));
181 memcpy(&rect_float[(x_offset + xl) * 4], &px_f, sizeof(float[4]));
182 }
183 }
184 }
185}
unsigned char uchar
unsigned int uint
#define ELEM(...)
void IMB_assign_float_buffer(ImBuf *ibuf, float *buffer_data, ImBufOwnership ownership)
void IMB_byte_from_float(ImBuf *ibuf)
void IMB_assign_byte_buffer(ImBuf *ibuf, uint8_t *buffer_data, ImBufOwnership ownership)
@ IB_TAKE_OWNERSHIP
Read Guarded memory(de)allocation.
constexpr int64_t first() const
nullptr float
uint top
constexpr T degrees(T) RET
void IMB_flipy(ImBuf *ibuf)
void IMB_flipx(ImBuf *ibuf)
bool IMB_rotate_orthogonal(ImBuf *ibuf, int degrees)
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)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#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:93
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer