Blender V4.3
imbuf/intern/rotate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "BLI_utildefines.h"
10
11#include "MEM_guardedalloc.h"
12
13#include "IMB_imbuf.hh"
14#include "IMB_imbuf_types.hh"
15#include "imbuf.hh"
16
17bool IMB_rotate_orthogonal(ImBuf *ibuf, int degrees)
18{
19 if (!ELEM(degrees, 90, 180, 270)) {
20 return false;
21 }
22
23 const int size_x = ibuf->x;
24 const int size_y = ibuf->y;
25
26 if (ibuf->float_buffer.data) {
27 float *float_pixels = ibuf->float_buffer.data;
28 float *orig_float_pixels = static_cast<float *>(MEM_dupallocN(float_pixels));
29 const int channels = ibuf->channels;
30 if (degrees == 90) {
31 std::swap(ibuf->x, ibuf->y);
32 for (int y = 0; y < size_y; y++) {
33 for (int x = 0; x < size_x; x++) {
34 const float *source_pixel = &orig_float_pixels[(y * size_x + x) * channels];
35 memcpy(&float_pixels[(y + ((size_x - x - 1) * size_y)) * channels],
36 source_pixel,
37 sizeof(float) * channels);
38 }
39 }
40 }
41 else if (degrees == 180) {
42 for (int y = 0; y < size_y; y++) {
43 for (int x = 0; x < size_x; x++) {
44 const float *source_pixel = &orig_float_pixels[(y * size_x + x) * channels];
45 memcpy(&float_pixels[(((size_y - y - 1) * size_x) + (size_x - x - 1)) * channels],
46 source_pixel,
47 sizeof(float) * channels);
48 }
49 }
50 }
51 else if (degrees == 270) {
52 std::swap(ibuf->x, ibuf->y);
53 for (int y = 0; y < size_y; y++) {
54 for (int x = 0; x < size_x; x++) {
55 const float *source_pixel = &orig_float_pixels[(y * size_x + x) * channels];
56 memcpy(&float_pixels[((size_y - y - 1) + (x * size_y)) * channels],
57 source_pixel,
58 sizeof(float) * channels);
59 }
60 }
61 }
62 MEM_freeN(orig_float_pixels);
63 if (ibuf->byte_buffer.data) {
65 }
66 }
67 else if (ibuf->byte_buffer.data) {
68 uchar *char_pixels = ibuf->byte_buffer.data;
69 uchar *orig_char_pixels = static_cast<uchar *>(MEM_dupallocN(char_pixels));
70 if (degrees == 90) {
71 std::swap(ibuf->x, ibuf->y);
72 for (int y = 0; y < size_y; y++) {
73 for (int x = 0; x < size_x; x++) {
74 const uchar *source_pixel = &orig_char_pixels[(y * size_x + x) * 4];
75 memcpy(
76 &char_pixels[(y + ((size_x - x - 1) * size_y)) * 4], source_pixel, sizeof(uchar[4]));
77 }
78 }
79 }
80 else if (degrees == 180) {
81 for (int y = 0; y < size_y; y++) {
82 for (int x = 0; x < size_x; x++) {
83 const uchar *source_pixel = &orig_char_pixels[(y * size_x + x) * 4];
84 memcpy(&char_pixels[(((size_y - y - 1) * size_x) + (size_x - x - 1)) * 4],
85 source_pixel,
86 sizeof(uchar[4]));
87 }
88 }
89 }
90 else if (degrees == 270) {
91 std::swap(ibuf->x, ibuf->y);
92 for (int y = 0; y < size_y; y++) {
93 for (int x = 0; x < size_x; x++) {
94 const uchar *source_pixel = &orig_char_pixels[(y * size_x + x) * 4];
95 memcpy(
96 &char_pixels[((size_y - y - 1) + (x * size_y)) * 4], source_pixel, sizeof(uchar[4]));
97 }
98 }
99 }
100 MEM_freeN(orig_char_pixels);
101 }
102
103 return true;
104}
105
106void IMB_flipy(ImBuf *ibuf)
107{
108 size_t x_size, y_size;
109
110 if (ibuf == nullptr) {
111 return;
112 }
113
114 if (ibuf->byte_buffer.data) {
115 uint *top, *bottom, *line;
116
117 x_size = ibuf->x;
118 y_size = ibuf->y;
119
120 const size_t stride = x_size * sizeof(int);
121
122 top = (uint *)ibuf->byte_buffer.data;
123 bottom = top + ((y_size - 1) * x_size);
124 line = static_cast<uint *>(MEM_mallocN(stride, "linebuf"));
125
126 y_size >>= 1;
127
128 for (; y_size > 0; y_size--) {
129 memcpy(line, top, stride);
130 memcpy(top, bottom, stride);
131 memcpy(bottom, line, stride);
132 bottom -= x_size;
133 top += x_size;
134 }
135
136 MEM_freeN(line);
137 }
138
139 if (ibuf->float_buffer.data) {
140 float *topf = nullptr, *bottomf = nullptr, *linef = nullptr;
141
142 x_size = ibuf->x;
143 y_size = ibuf->y;
144
145 const size_t stride = x_size * 4 * sizeof(float);
146
147 topf = ibuf->float_buffer.data;
148 bottomf = topf + 4 * ((y_size - 1) * x_size);
149 linef = static_cast<float *>(MEM_mallocN(stride, "linebuf"));
150
151 y_size >>= 1;
152
153 for (; y_size > 0; y_size--) {
154 memcpy(linef, topf, stride);
155 memcpy(topf, bottomf, stride);
156 memcpy(bottomf, linef, stride);
157 bottomf -= 4 * x_size;
158 topf += 4 * x_size;
159 }
160
161 MEM_freeN(linef);
162 }
163}
164
165void IMB_flipx(ImBuf *ibuf)
166{
167 int x, y, xr, xl, yi;
168 float px_f[4];
169
170 if (ibuf == nullptr) {
171 return;
172 }
173
174 x = ibuf->x;
175 y = ibuf->y;
176
177 if (ibuf->byte_buffer.data) {
178 uint *rect = (uint *)ibuf->byte_buffer.data;
179 for (yi = y - 1; yi >= 0; yi--) {
180 const size_t x_offset = size_t(x) * yi;
181 for (xr = x - 1, xl = 0; xr >= xl; xr--, xl++) {
182 std::swap(rect[x_offset + xr], rect[x_offset + xl]);
183 }
184 }
185 }
186
187 if (ibuf->float_buffer.data) {
188 float *rect_float = ibuf->float_buffer.data;
189 for (yi = y - 1; yi >= 0; yi--) {
190 const size_t x_offset = size_t(x) * yi;
191 for (xr = x - 1, xl = 0; xr >= xl; xr--, xl++) {
192 memcpy(&px_f, &rect_float[(x_offset + xr) * 4], sizeof(float[4]));
193 memcpy(
194 &rect_float[(x_offset + xr) * 4], &rect_float[(x_offset + xl) * 4], sizeof(float[4]));
195 memcpy(&rect_float[(x_offset + xl) * 4], &px_f, sizeof(float[4]));
196 }
197 }
198 }
199}
unsigned char uchar
unsigned int uint
#define ELEM(...)
void IMB_rect_from_float(ImBuf *ibuf)
Definition divers.cc:694
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
draw_view in_light_buf[] float
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
uint top
void IMB_flipy(ImBuf *ibuf)
void IMB_flipx(ImBuf *ibuf)
bool IMB_rotate_orthogonal(ImBuf *ibuf, int degrees)
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer