Blender V5.0
Image.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11
12#include <string.h> // for memcpy
13
14#include "MEM_guardedalloc.h"
15
16namespace Freestyle {
17
18//
19// Image base class, for all types of images
20//
22
28class FrsImage {
29 public:
32 {
33 _storedWidth = 0;
34 _storedHeight = 0;
35 _width = 0;
36 _height = 0;
37 _Ox = 0;
38 _Oy = 0;
39 }
40
42 FrsImage(const FrsImage &brother)
43 {
44 _storedWidth = brother._storedWidth;
46 _width = brother._width;
47 _height = brother._height;
48 _Ox = brother._Ox;
49 _Oy = brother._Oy;
50 }
51
56 {
57 _width = w;
58 _height = h;
60 _storedHeight = h;
61 _Ox = 0;
62 _Oy = 0;
63 }
64
79 FrsImage(uint w, uint h, uint sw, uint sh, uint ox, uint oy)
80 {
81 _width = w;
82 _height = h;
83 _storedWidth = sw;
84 _storedHeight = sh;
85 _Ox = ox;
86 _Oy = oy;
87 }
88
90 FrsImage &operator=(const FrsImage &brother)
91 {
92 _width = brother._width;
93 _height = brother._height;
94 _storedWidth = brother._storedWidth;
96 _Ox = brother._Ox;
97 _Oy = brother._Oy;
98 return *this;
99 }
100
102 virtual ~FrsImage() {}
103
105 inline uint width() const
106 {
107 return _width;
108 }
109
111 inline uint height() const
112 {
113 return _height;
114 }
115
117 virtual float pixel(uint x, uint y) const = 0;
118
138 virtual void setArray(float *array,
139 uint width,
140 uint height,
141 uint sw,
142 uint sh,
143 uint x,
144 uint y,
145 bool copy = true) = 0;
146
150 virtual float *getArray() = 0;
151
152 protected:
157 uint _Ox; // origin of the stored part
158 uint _Oy; // origin of the stored part
159
160 MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:FrsImage")
161};
162
163//
164// RGBImage
165//
167class RGBImage : public FrsImage {
168 public:
170 {
171 _rgb = 0;
172 }
173
174 RGBImage(const RGBImage &brother) : FrsImage(brother)
175 {
176 _rgb = new float[3 * _storedWidth * _storedHeight];
177 memcpy(_rgb, brother._rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
178 }
179
181 {
182 _rgb = new float[3 * _width * _height];
183 }
184
185 RGBImage(float *rgb, uint w, uint h) : FrsImage(w, h)
186 {
187 _rgb = new float[3 * _width * _height];
188 memcpy(_rgb, rgb, 3 * _width * _height * sizeof(float));
189 }
190
205 RGBImage(float *rgb, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
206 : FrsImage(w, h, sw, sh, ox, oy)
207 {
208 _rgb = new float[3 * _storedWidth * _storedHeight];
209 memcpy(_rgb, rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
210 }
211
212 RGBImage &operator=(const RGBImage &brother)
213 {
214 dynamic_cast<FrsImage &>(*this) = brother;
215 _rgb = new float[3 * _storedWidth * _storedHeight];
216 memcpy(_rgb, brother._rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
217 return *this;
218 }
219
220 virtual ~RGBImage()
221 {
222 if (_rgb) {
223 delete[] _rgb;
224 }
225 }
226
227 inline float getR(uint x, uint y) const
228 {
229 return _rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3];
230 }
231
232 inline float getG(uint x, uint y) const
233 {
234 return _rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3 + 1];
235 }
236
237 inline float getB(uint x, uint y) const
238 {
239 return _rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3 + 2];
240 }
241
242 virtual void setPixel(uint x, uint y, float r, float g, float b)
243 {
244 float *tmp = &(_rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3]);
245 *tmp = r;
246 tmp++;
247 *tmp = g;
248 tmp++;
249 *tmp = b;
250 }
251
252 virtual float pixel(uint x, uint y) const
253 {
254 float res = 0.0f;
255 float *tmp = &(_rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3]);
256 res += 11.0f * (*tmp);
257 tmp++;
258 res += 16.0f * (*tmp);
259 tmp++;
260 res += 5.0f * (*tmp);
261 return res / 32.0f;
262 }
263
268 virtual void setArray(
269 float *rgb, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy = true)
270 {
271 _width = width;
272 _height = height;
273 _storedWidth = sw;
274 _storedHeight = sh;
275 _Ox = x;
276 _Oy = y;
277 if (!copy) {
278 _rgb = rgb;
279 return;
280 }
281
282 memcpy(_rgb, rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
283 }
284
285 virtual float *getArray()
286 {
287 return _rgb;
288 }
289
290 protected:
291 float *_rgb;
292};
293
294//
295// GrayImage
296//
298
299class GrayImage : public FrsImage {
300 public:
302 {
303 _lvl = 0;
304 }
305
306 GrayImage(const GrayImage &brother) : FrsImage(brother)
307 {
308 _lvl = new float[_storedWidth * _storedHeight];
309 memcpy(_lvl, brother._lvl, _storedWidth * _storedHeight * sizeof(*_lvl));
310 }
311
314 {
315 _lvl = new float[_width * _height];
316 }
317
318 GrayImage(float *lvl, uint w, uint h) : FrsImage(w, h)
319 {
320 _lvl = new float[_width * _height];
321 memcpy(_lvl, lvl, _width * _height * sizeof(*_lvl));
322 }
323
338 GrayImage(float *lvl, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
339 : FrsImage(w, h, sw, sh, ox, oy)
340 {
341 _lvl = new float[_storedWidth * _storedHeight];
342 memcpy(_lvl, lvl, _storedWidth * _storedHeight * sizeof(float));
343 }
344
346 {
347 dynamic_cast<FrsImage &>(*this) = brother;
348 _lvl = new float[_storedWidth * _storedHeight];
349 memcpy(_lvl, brother._lvl, _storedWidth * _storedHeight * sizeof(float));
350 return *this;
351 }
352
353 virtual ~GrayImage()
354 {
355 if (_lvl) {
356 delete[] _lvl;
357 }
358 }
359
360 inline void setPixel(uint x, uint y, float v)
361 {
362 _lvl[(y - _Oy) * _storedWidth + (x - _Ox)] = v;
363 }
364
365 inline float pixel(uint x, uint y) const
366 {
367 return _lvl[(y - _Oy) * _storedWidth + (x - _Ox)];
368 }
369
375 float *lvl, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy = true)
376 {
377 _width = width;
378 _height = height;
379 _storedWidth = sw;
380 _storedHeight = sh;
381 _Ox = x;
382 _Oy = y;
383 if (!copy) {
384 _lvl = lvl;
385 return;
386 }
387
388 memcpy(_lvl, lvl, _storedWidth * _storedHeight * sizeof(float));
389 }
390
392 virtual float *getArray()
393 {
394 return _lvl;
395 }
396
397 protected:
398 float *_lvl;
399};
400
401} /* namespace Freestyle */
unsigned int uint
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
FrsImage(const FrsImage &brother)
Definition Image.h:42
uint height() const
Definition Image.h:111
FrsImage(uint w, uint h, uint sw, uint sh, uint ox, uint oy)
Definition Image.h:79
FrsImage(uint w, uint h)
Definition Image.h:55
uint width() const
Definition Image.h:105
FrsImage & operator=(const FrsImage &brother)
Definition Image.h:90
virtual float pixel(uint x, uint y) const =0
virtual float * getArray()=0
virtual ~FrsImage()
Definition Image.h:102
virtual void setArray(float *array, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy=true)=0
GrayImage(float *lvl, uint w, uint h)
Definition Image.h:318
virtual float * getArray()
Definition Image.h:392
void setPixel(uint x, uint y, float v)
Definition Image.h:360
GrayImage & operator=(const GrayImage &brother)
Definition Image.h:345
virtual ~GrayImage()
Definition Image.h:353
void setArray(float *lvl, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy=true)
Definition Image.h:374
GrayImage(float *lvl, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
Definition Image.h:338
GrayImage(const GrayImage &brother)
Definition Image.h:306
float pixel(uint x, uint y) const
Definition Image.h:365
GrayImage(uint w, uint h)
Definition Image.h:313
virtual ~RGBImage()
Definition Image.h:220
virtual float * getArray()
Definition Image.h:285
virtual void setPixel(uint x, uint y, float r, float g, float b)
Definition Image.h:242
RGBImage & operator=(const RGBImage &brother)
Definition Image.h:212
virtual void setArray(float *rgb, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy=true)
Definition Image.h:268
float getB(uint x, uint y) const
Definition Image.h:237
RGBImage(const RGBImage &brother)
Definition Image.h:174
float getG(uint x, uint y) const
Definition Image.h:232
RGBImage(float *rgb, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
Definition Image.h:205
virtual float pixel(uint x, uint y) const
Definition Image.h:252
float getR(uint x, uint y) const
Definition Image.h:227
RGBImage(float *rgb, uint w, uint h)
Definition Image.h:185
RGBImage(uint w, uint h)
Definition Image.h:180
inherits from class Rep
Definition AppCanvas.cpp:20
static uint x[3]
Definition RandGen.cpp:77
static void copy(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node)