Blender V5.3
Canvas.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <sstream>
11#include <vector>
12
13#include "Canvas.h"
14#include "StrokeRenderer.h"
15#include "StyleModule.h"
16
18#include "../image/Image.h"
20
23#include "../system/TimeStamp.h"
24
26
27#include "BLI_sys_types.hh"
28
29#include "BKE_global.hh"
30
31// soc #include <qimage.h>
32// soc #include <QString>
33
34#include "IMB_imbuf.hh"
35#include "IMB_imbuf_types.hh"
36
37using namespace std;
38
39namespace Freestyle {
40
41Canvas *Canvas::_pInstance = nullptr;
42
43const char *Canvas::_MapsPath = nullptr;
44
46{
47 _SelectedFEdge = nullptr;
48 _pInstance = this;
50 _Renderer = nullptr;
51 _current_sm = nullptr;
53 _basic = false;
54}
55
56Canvas::Canvas(const Canvas &iBrother)
57{
59 _pInstance = this;
61 _Renderer = iBrother._Renderer;
62 _current_sm = iBrother._current_sm;
64 _basic = iBrother._basic;
65}
66
68{
69 _pInstance = nullptr;
70
71 Clear();
72 if (_Renderer) {
73 delete _Renderer;
74 _Renderer = nullptr;
75 }
76 // FIXME: think about an easy control for the maps memory management...
77 if (!_maps.empty()) {
78 for (mapsMap::iterator m = _maps.begin(), mend = _maps.end(); m != mend; ++m) {
79 delete ((*m).second);
80 }
81 _maps.clear();
82 }
83 delete _steerableViewMap;
84}
85
87
89{
90 if (_StyleModules.empty()) {
91 return;
92 }
93 preDraw();
94 TimeStamp *timestamp = TimeStamp::instance();
95
96 for (uint i = 0; i < _StyleModules.size(); ++i) {
98
99 if (i < _Layers.size() && _Layers[i]) {
100 delete _Layers[i];
101 }
102
103 _Layers[i] = _StyleModules[i]->execute();
104 if (!_Layers[i]) {
105 continue;
106 }
107
108 stroke_count += _Layers[i]->strokes_size();
109
110 timestamp->increment();
111 }
112 postDraw();
113}
114
116{
117 update();
118}
119
121{
122 if (!_Layers.empty()) {
123 for (deque<StrokeLayer *>::iterator sl = _Layers.begin(), slend = _Layers.end(); sl != slend;
124 ++sl)
125 {
126 if (*sl) {
127 delete (*sl);
128 }
129 }
130 _Layers.clear();
131 }
132
133 if (!_StyleModules.empty()) {
134 for (deque<StyleModule *>::iterator s = _StyleModules.begin(), send = _StyleModules.end();
135 s != send;
136 ++s)
137 {
138 if (*s) {
139 delete (*s);
140 }
141 }
142 _StyleModules.clear();
143 }
144 if (_steerableViewMap) {
146 }
147
148 stroke_count = 0;
149}
150
152{
153 if (!_Layers.empty()) {
154 for (deque<StrokeLayer *>::iterator sl = _Layers.begin(), slend = _Layers.end(); sl != slend;
155 ++sl)
156 {
157 if (*sl) {
158 (*sl)->clear();
159 }
160 }
161 }
162 if (_steerableViewMap) {
164 }
165 update();
166
167 stroke_count = 0;
168}
169
171{
173 _StyleModules.push_back(iStyleModule);
174 _Layers.push_back(layer);
175}
176
178{
179 uint size = _StyleModules.size();
181 if (_StyleModules.empty() || (index == size)) {
182 _StyleModules.push_back(iStyleModule);
183 _Layers.push_back(layer);
184 return;
185 }
186 _StyleModules.insert(_StyleModules.begin() + index, iStyleModule);
187 _Layers.insert(_Layers.begin() + index, layer);
188}
189
191{
192 uint i = 0;
193 if (!_StyleModules.empty()) {
194 for (deque<StyleModule *>::iterator s = _StyleModules.begin(), send = _StyleModules.end();
195 s != send;
196 ++s, ++i)
197 {
198 if (i == index) {
199 // remove shader
200 if (*s) {
201 delete *s;
202 }
203 _StyleModules.erase(s);
204 break;
205 }
206 }
207 }
208
209 if (!_Layers.empty()) {
210 i = 0;
211 for (deque<StrokeLayer *>::iterator sl = _Layers.begin(), slend = _Layers.end(); sl != slend;
212 ++sl, ++i)
213 {
214 if (i == index) {
215 // remove layer
216 if (*sl) {
217 delete *sl;
218 }
219 _Layers.erase(sl);
220 break;
221 }
222 }
223 }
224}
225
226void Canvas::SwapStyleModules(uint i1, uint i2)
227{
228 StyleModule *tmp;
229 tmp = _StyleModules[i1];
231 _StyleModules[i2] = tmp;
232
233 StrokeLayer *tmp2;
234 tmp2 = _Layers[i1];
235 _Layers[i1] = _Layers[i2];
236 _Layers[i2] = tmp2;
237}
238
240{
241 uint i = 0;
242 for (deque<StyleModule *>::iterator s = _StyleModules.begin(), send = _StyleModules.end();
243 s != send;
244 ++s, ++i)
245 {
246 if (i == index) {
247 if (*s) {
248 delete *s;
249 }
250 *s = iStyleModule;
251 break;
252 }
253 }
254}
255
256void Canvas::setVisible(uint index, bool iVisible)
257{
258 _StyleModules[index]->setDisplayed(iVisible);
259}
260
261void Canvas::setModified(uint index, bool iMod)
262{
263 _StyleModules[index]->setModified(iMod);
264}
265
266void Canvas::resetModified(bool iMod /* = false */)
267{
268 uint size = _StyleModules.size();
269 for (uint i = 0; i < size; ++i) {
270 setModified(i, iMod);
271 }
272}
273
274void Canvas::causalStyleModules(vector<uint> &vec, uint index)
275{
276 uint size = _StyleModules.size();
277
278 for (uint i = index; i < size; ++i) {
279 if (_StyleModules[i]->getCausal()) {
280 vec.push_back(i);
281 }
282 }
283}
284
285void Canvas::Render(const StrokeRenderer *iRenderer)
286{
287 for (uint i = 0; i < _StyleModules.size(); ++i) {
288 if (!_StyleModules[i]->getDisplayed() || !_Layers[i]) {
289 continue;
290 }
291 _Layers[i]->Render(iRenderer);
292 }
293}
294
296{
297 for (uint i = 0; i < _StyleModules.size(); ++i) {
298 if (!_StyleModules[i]->getDisplayed() || !_Layers[i]) {
299 continue;
300 }
301 _Layers[i]->RenderBasic(iRenderer);
302 }
303}
304
305void Canvas::loadMap(const char *iFileName, const char *iMapName, uint iNbLevels, float iSigma)
306{
307 // check whether this map was already loaded:
308 if (!_maps.empty()) {
309 mapsMap::iterator m = _maps.find(iMapName);
310 if (m != _maps.end()) {
311 // lazy check for size changes
312 ImagePyramid *pyramid = (*m).second;
313 if ((pyramid->width() != width()) || (pyramid->height() != height())) {
314 delete pyramid;
315 }
316 else {
317 return;
318 }
319 }
320 }
321
322 string filePath;
323 if (_MapsPath) {
324 filePath = _MapsPath;
325 filePath += iFileName;
326 }
327 else {
328 filePath = iFileName;
329 }
330
331#if 0 // soc
332 QImage *qimg;
333 QImage newMap(filePath.c_str());
334 if (newMap.isNull()) {
335 cerr << "Could not load image file " << filePath << endl;
336 return;
337 }
338 qimg = &newMap;
339#endif
340 /* OCIO_TODO: support different input color space */
343 if (qimg == nullptr) {
344 cerr << "Could not load image file " << filePath << endl;
345 return;
346 }
347
348#if 0 // soc
349 // resize
350 QImage scaledImg;
351 if ((newMap.width() != width()) || (newMap.height() != height())) {
352 scaledImg = newMap.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
353 qimg = &scaledImg;
354 }
355#endif
356 blender::ImBuf *scaledImg;
357 if ((qimg->x != width()) || (qimg->y != height())) {
358 scaledImg = IMB_dupImBuf(qimg);
360 }
361
362 // deal with color image
363#if 0
364 if (newMap->depth() != 8) {
365 int w = newMap->width();
366 int h = newMap->height();
367 QImage *tmp = new QImage(w, h, 8);
368 for (uint y = 0; y < h; ++y) {
369 for (uint x = 0; x < w; ++x) {
370 int c = qGray(newMap->pixel(x, y));
371 tmp->setPixel(x, y, c);
372 }
373 }
374 delete newMap;
375 newMap = tmp;
376 }
377#endif
378
379 int x, y;
380 int w = qimg->x;
381 int h = qimg->y;
382 int rowbytes = w * 4;
383 GrayImage tmp(w, h);
384 uchar *pix;
385
386 uchar *qimg_byte_data = qimg->byte_data_for_write();
387 for (y = 0; y < h; ++y) {
388 for (x = 0; x < w; ++x) {
389 pix = qimg_byte_data + y * rowbytes + x * 4;
390 float c = (pix[0] * 11 + pix[1] * 16 + pix[2] * 5) / 32;
391 tmp.setPixel(x, y, c);
392 }
393 }
394
395#if 0
396 GrayImage blur(w, h);
397 GaussianFilter gf(4.0f);
398 // int bound = gf.getBound();
399 for (y = 0; y < h; ++y) {
400 for (x = 0; x < w; ++x) {
401 int c = gf.getSmoothedPixel<GrayImage>(&tmp, x, y);
402 blur.setPixel(x, y, c);
403 }
404 }
405#endif
406
407 GaussianPyramid *pyramid = new GaussianPyramid(tmp, iNbLevels, iSigma);
408 int ow = pyramid->width(0);
409 int oh = pyramid->height(0);
410 string base(iMapName); // soc
411 for (int i = 0; i < pyramid->getNumberOfLevels(); ++i) {
412 // save each image:
413#if 0
414 w = pyramid.width(i);
415 h = pyramid.height(i);
416#endif
417
418 // soc QImage qtmp(ow, oh, QImage::Format_RGB32);
419 blender::ImBuf *qtmp = IMB_allocImBuf(ow, oh, blender::ImBufFlags::ByteData);
420
421 // int k = (1 << i);
422 uchar *qtmp_byte_data = qtmp->byte_data_for_write();
423 for (y = 0; y < oh; ++y) {
424 for (x = 0; x < ow; ++x) {
425 int c = pyramid->pixel(x, y, i); // 255 * pyramid->pixel(x, y, i);
426 // soc qtmp.setPixel(x, y, qRgb(c, c, c));
427 pix = qtmp_byte_data + y * rowbytes + x * 4;
428 pix[0] = pix[1] = pix[2] = c;
429 }
430 }
431 // soc qtmp.save(base + QString::number(i) + ".bmp", "BMP");
432 stringstream filepath;
433 filepath << base;
434 filepath << i << ".bmp";
436 IMB_save_image(qtmp, const_cast<char *>(filepath.str().c_str()), blender::ImBufFlags::Zero);
437 }
438
439#if 0
440 QImage *qtmp = new QImage(w, h, 32);
441 for (y = 0; y < h; ++y) {
442 for (x = 0; x < w; ++x) {
443 int c = int(blur.pixel(x, y));
444 qtmp->setPixel(x, y, qRgb(c, c, c));
445 }
446 }
447 delete newMap;
448 newMap = qtmp;
449#endif
450
451 _maps[iMapName] = pyramid;
452 // newMap->save("toto.bmp", "BMP");
453}
454
455float Canvas::readMapPixel(const char *iMapName, int level, int x, int y)
456{
457 if (_maps.empty()) {
459 cout << "readMapPixel warning: no map was loaded " << endl;
460 }
461 return -1;
462 }
463 mapsMap::iterator m = _maps.find(iMapName);
464 if (m == _maps.end()) {
466 cout << "readMapPixel warning: no map was loaded with the name " << iMapName << endl;
467 }
468 return -1;
469 }
470 ImagePyramid *pyramid = (*m).second;
471 if ((x < 0) || (x >= pyramid->width()) || (y < 0) || (y >= pyramid->height())) {
472 return 0;
473 }
474
475 return pyramid->pixel(x, height() - 1 - y, level);
476}
477
478} /* namespace Freestyle */
Class to define a canvas designed to draw style modules.
Configuration definitions.
Class to perform gaussian filtering operations on an image.
Class to represent a pyramid of images.
Class to encapsulate an array of RGB or Gray level values.
Class to define a pseudo Perlin noise.
Convenient access to the steerable ViewMap to which any element of the ViewMap belongs to.
Classes to render a stroke with OpenGL.
Class representing a style module.
Class defining a singleton used as timestamp.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
virtual void Erase()
Definition Canvas.cpp:151
virtual void postDraw()
Definition Canvas.cpp:115
void setVisible(uint index, bool iVisible)
Definition Canvas.cpp:256
virtual void preDraw()
Definition Canvas.cpp:86
virtual void Draw()
Definition Canvas.cpp:88
FEdge * _SelectedFEdge
Definition Canvas.h:67
std::deque< StrokeLayer * > _Layers
Definition Canvas.h:65
SteerableViewMap * _steerableViewMap
Definition Canvas.h:73
void setModified(uint index, bool iMod)
Definition Canvas.cpp:261
void causalStyleModules(std::vector< uint > &vec, uint index=0)
Definition Canvas.cpp:274
void RemoveStyleModule(uint index)
Definition Canvas.cpp:190
void InsertStyleModule(uint index, StyleModule *iStyleModule)
Definition Canvas.cpp:177
void SwapStyleModules(uint i1, uint i2)
Definition Canvas.cpp:226
float readMapPixel(const char *iMapName, int level, int x, int y)
Definition Canvas.cpp:455
virtual void Render(const StrokeRenderer *iRenderer)
Definition Canvas.cpp:285
void PushBackStyleModule(StyleModule *iStyleModule)
Definition Canvas.cpp:170
virtual void RenderBasic(const StrokeRenderer *iRenderer)
Definition Canvas.cpp:295
void loadMap(const char *iFileName, const char *iMapName, uint iNbLevels=4, float iSigma=1.0f)
Definition Canvas.cpp:305
virtual int height() const =0
StyleModule * _current_sm
Definition Canvas.h:70
virtual int width() const =0
void resetModified(bool iMod=false)
Definition Canvas.cpp:266
std::deque< StyleModule * > _StyleModules
Definition Canvas.h:66
void ReplaceStyleModule(uint index, StyleModule *iStyleModule)
Definition Canvas.cpp:239
static const int NB_STEERABLE_VIEWMAP
Definition Canvas.h:61
virtual ~Canvas()
Definition Canvas.cpp:67
static const char * _MapsPath
Definition Canvas.h:72
StrokeRenderer * _Renderer
Definition Canvas.h:69
static Canvas * _pInstance
Definition Canvas.h:64
mapsMap _maps
Definition Canvas.h:71
virtual void update()=0
float getSmoothedPixel(Map *map, int x, int y)
GaussianPyramid(float iSigma=1.0f)
void setPixel(uint x, uint y, float v)
Definition Image.h:360
float pixel(uint x, uint y) const
Definition Image.h:365
virtual float pixel(int x, int y, int level=0)
int getNumberOfLevels() const
virtual int height(int level=0)
virtual int width(int level=0)
static void init(long seed)
SteerableViewMap(uint nbOrientations=4)
static TimeStamp * instance()
Definition TimeStamp.h:20
#define index(i)
#define layer
unsigned int uchar
inherits from class Rep
Definition AppCanvas.cpp:20
@ G_DEBUG_FREESTYLE
Global G
Definition blender.cc:57
ImBuf * IMB_load_image_from_filepath(const char *filepath, ImBufFlags flags, ColorManagedColorspaceSettings *colorspace_settings=nullptr)
Definition readimage.cc:201
bool IMB_scale(ImBuf *ibuf, int2 new_size, IMBScaleFilter filter, bool threaded=true)
Definition scaling.cc:467
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
eImbFileType ftype