Blender V4.3
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
12#include <string.h> // for memcpy
13
14#ifdef WITH_CXX_GUARDEDALLOC
15# include "MEM_guardedalloc.h"
16#endif
17
18namespace Freestyle {
19
20//
21// Image base class, for all types of images
22//
24
30class FrsImage {
31 public:
34 {
35 _storedWidth = 0;
36 _storedHeight = 0;
37 _width = 0;
38 _height = 0;
39 _Ox = 0;
40 _Oy = 0;
41 }
42
44 FrsImage(const FrsImage &brother)
45 {
46 _storedWidth = brother._storedWidth;
48 _width = brother._width;
49 _height = brother._height;
50 _Ox = brother._Ox;
51 _Oy = brother._Oy;
52 }
53
58 {
59 _width = w;
60 _height = h;
62 _storedHeight = h;
63 _Ox = 0;
64 _Oy = 0;
65 }
66
81 FrsImage(uint w, uint h, uint sw, uint sh, uint ox, uint oy)
82 {
83 _width = w;
84 _height = h;
85 _storedWidth = sw;
86 _storedHeight = sh;
87 _Ox = ox;
88 _Oy = oy;
89 }
90
92 FrsImage &operator=(const FrsImage &brother)
93 {
94 _width = brother._width;
95 _height = brother._height;
96 _storedWidth = brother._storedWidth;
98 _Ox = brother._Ox;
99 _Oy = brother._Oy;
100 return *this;
101 }
102
104 virtual ~FrsImage() {}
105
107 inline uint width() const
108 {
109 return _width;
110 }
111
113 inline uint height() const
114 {
115 return _height;
116 }
117
119 virtual float pixel(uint x, uint y) const = 0;
120
140 virtual void setArray(float *array,
141 uint width,
142 uint height,
143 uint sw,
144 uint sh,
145 uint x,
146 uint y,
147 bool copy = true) = 0;
148
152 virtual float *getArray() = 0;
153
154 protected:
159 uint _Ox; // origin of the stored part
160 uint _Oy; // origin of the stored part
161
162#ifdef WITH_CXX_GUARDEDALLOC
163 MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:FrsImage")
164#endif
165};
166
167//
168// RGBImage
169//
171class RGBImage : public FrsImage {
172 public:
174 {
175 _rgb = 0;
176 }
177
178 RGBImage(const RGBImage &brother) : FrsImage(brother)
179 {
180 _rgb = new float[3 * _storedWidth * _storedHeight];
181 memcpy(_rgb, brother._rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
182 }
183
185 {
186 _rgb = new float[3 * _width * _height];
187 }
188
189 RGBImage(float *rgb, uint w, uint h) : FrsImage(w, h)
190 {
191 _rgb = new float[3 * _width * _height];
192 memcpy(_rgb, rgb, 3 * _width * _height * sizeof(float));
193 }
194
209 RGBImage(float *rgb, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
210 : FrsImage(w, h, sw, sh, ox, oy)
211 {
212 _rgb = new float[3 * _storedWidth * _storedHeight];
213 memcpy(_rgb, rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
214 }
215
216 RGBImage &operator=(const RGBImage &brother)
217 {
218 dynamic_cast<FrsImage &>(*this) = brother;
219 _rgb = new float[3 * _storedWidth * _storedHeight];
220 memcpy(_rgb, brother._rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
221 return *this;
222 }
223
224 virtual ~RGBImage()
225 {
226 if (_rgb) {
227 delete[] _rgb;
228 }
229 }
230
231 inline float getR(uint x, uint y) const
232 {
233 return _rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3];
234 }
235
236 inline float getG(uint x, uint y) const
237 {
238 return _rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3 + 1];
239 }
240
241 inline float getB(uint x, uint y) const
242 {
243 return _rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3 + 2];
244 }
245
246 virtual void setPixel(uint x, uint y, float r, float g, float b)
247 {
248 float *tmp = &(_rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3]);
249 *tmp = r;
250 tmp++;
251 *tmp = g;
252 tmp++;
253 *tmp = b;
254 }
255
256 virtual float pixel(uint x, uint y) const
257 {
258 float res = 0.0f;
259 float *tmp = &(_rgb[3 * (y - _Oy) * _storedWidth + (x - _Ox) * 3]);
260 res += 11.0f * (*tmp);
261 tmp++;
262 res += 16.0f * (*tmp);
263 tmp++;
264 res += 5.0f * (*tmp);
265 return res / 32.0f;
266 }
267
272 virtual void setArray(
273 float *rgb, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy = true)
274 {
275 _width = width;
276 _height = height;
277 _storedWidth = sw;
278 _storedHeight = sh;
279 _Ox = x;
280 _Oy = y;
281 if (!copy) {
282 _rgb = rgb;
283 return;
284 }
285
286 memcpy(_rgb, rgb, 3 * _storedWidth * _storedHeight * sizeof(float));
287 }
288
289 virtual float *getArray()
290 {
291 return _rgb;
292 }
293
294 protected:
295 float *_rgb;
296};
297
298//
299// GrayImage
300//
302
303class GrayImage : public FrsImage {
304 public:
306 {
307 _lvl = 0;
308 }
309
310 GrayImage(const GrayImage &brother) : FrsImage(brother)
311 {
312 _lvl = new float[_storedWidth * _storedHeight];
313 memcpy(_lvl, brother._lvl, _storedWidth * _storedHeight * sizeof(*_lvl));
314 }
315
318 {
319 _lvl = new float[_width * _height];
320 }
321
322 GrayImage(float *lvl, uint w, uint h) : FrsImage(w, h)
323 {
324 _lvl = new float[_width * _height];
325 memcpy(_lvl, lvl, _width * _height * sizeof(*_lvl));
326 }
327
342 GrayImage(float *lvl, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
343 : FrsImage(w, h, sw, sh, ox, oy)
344 {
345 _lvl = new float[_storedWidth * _storedHeight];
346 memcpy(_lvl, lvl, _storedWidth * _storedHeight * sizeof(float));
347 }
348
350 {
351 dynamic_cast<FrsImage &>(*this) = brother;
352 _lvl = new float[_storedWidth * _storedHeight];
353 memcpy(_lvl, brother._lvl, _storedWidth * _storedHeight * sizeof(float));
354 return *this;
355 }
356
357 virtual ~GrayImage()
358 {
359 if (_lvl) {
360 delete[] _lvl;
361 }
362 }
363
364 inline void setPixel(uint x, uint y, float v)
365 {
366 _lvl[(y - _Oy) * _storedWidth + (x - _Ox)] = v;
367 }
368
369 inline float pixel(uint x, uint y) const
370 {
371 return _lvl[(y - _Oy) * _storedWidth + (x - _Ox)];
372 }
373
379 float *lvl, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy = true)
380 {
381 _width = width;
382 _height = height;
383 _storedWidth = sw;
384 _storedHeight = sh;
385 _Ox = x;
386 _Oy = y;
387 if (!copy) {
388 _lvl = lvl;
389 return;
390 }
391
392 memcpy(_lvl, lvl, _storedWidth * _storedHeight * sizeof(float));
393 }
394
396 virtual float *getArray()
397 {
398 return _lvl;
399 }
400
401 protected:
402 float *_lvl;
403};
404
405} /* 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:44
uint height() const
Definition Image.h:113
FrsImage(uint w, uint h, uint sw, uint sh, uint ox, uint oy)
Definition Image.h:81
FrsImage(uint w, uint h)
Definition Image.h:57
uint width() const
Definition Image.h:107
FrsImage & operator=(const FrsImage &brother)
Definition Image.h:92
virtual float pixel(uint x, uint y) const =0
virtual float * getArray()=0
virtual ~FrsImage()
Definition Image.h:104
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:322
virtual float * getArray()
Definition Image.h:396
void setPixel(uint x, uint y, float v)
Definition Image.h:364
GrayImage & operator=(const GrayImage &brother)
Definition Image.h:349
virtual ~GrayImage()
Definition Image.h:357
void setArray(float *lvl, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy=true)
Definition Image.h:378
GrayImage(float *lvl, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
Definition Image.h:342
GrayImage(const GrayImage &brother)
Definition Image.h:310
float pixel(uint x, uint y) const
Definition Image.h:369
GrayImage(uint w, uint h)
Definition Image.h:317
virtual ~RGBImage()
Definition Image.h:224
virtual float * getArray()
Definition Image.h:289
virtual void setPixel(uint x, uint y, float r, float g, float b)
Definition Image.h:246
RGBImage & operator=(const RGBImage &brother)
Definition Image.h:216
virtual void setArray(float *rgb, uint width, uint height, uint sw, uint sh, uint x, uint y, bool copy=true)
Definition Image.h:272
float getB(uint x, uint y) const
Definition Image.h:241
RGBImage(const RGBImage &brother)
Definition Image.h:178
float getG(uint x, uint y) const
Definition Image.h:236
RGBImage(float *rgb, uint w, uint h, uint sw, uint sh, uint ox, uint oy)
Definition Image.h:209
virtual float pixel(uint x, uint y) const
Definition Image.h:256
float getR(uint x, uint y) const
Definition Image.h:231
RGBImage(float *rgb, uint w, uint h)
Definition Image.h:189
RGBImage(uint w, uint h)
Definition Image.h:184
local_group_size(16, 16) .push_constant(Type b
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)