Blender  V2.93
COM_PreviewOperation.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2011, Blender Foundation.
17  */
18 
19 #include "COM_PreviewOperation.h"
20 #include "BKE_image.h"
21 #include "BLI_listbase.h"
22 #include "BLI_math.h"
23 #include "BLI_math_color.h"
24 #include "BLI_utildefines.h"
25 #include "COM_defines.h"
26 #include "MEM_guardedalloc.h"
27 #include "PIL_time.h"
28 #include "WM_api.h"
29 #include "WM_types.h"
30 
31 #include "BKE_node.h"
32 #include "IMB_colormanagement.h"
33 #include "IMB_imbuf.h"
34 #include "IMB_imbuf_types.h"
35 
36 namespace blender::compositor {
37 
39  const ColorManagedDisplaySettings *displaySettings,
40  const unsigned int defaultWidth,
41  const unsigned int defaultHeight)
42 
43 {
45  this->m_preview = nullptr;
46  this->m_outputBuffer = nullptr;
47  this->m_input = nullptr;
48  this->m_divider = 1.0f;
49  this->m_viewSettings = viewSettings;
50  this->m_displaySettings = displaySettings;
51  this->m_defaultWidth = defaultWidth;
52  this->m_defaultHeight = defaultHeight;
53  flags.use_viewer_border = true;
55 }
56 
58 {
59  /* Size (0, 0) ensures the preview rect is not allocated in advance,
60  * this is set later in initExecution once the resolution is determined.
61  */
62  this->m_preview = BKE_node_preview_verify(previews, key, 0, 0, true);
63 }
64 
66 {
67  this->m_input = getInputSocketReader(0);
68 
69  if (this->getWidth() == (unsigned int)this->m_preview->xsize &&
70  this->getHeight() == (unsigned int)this->m_preview->ysize) {
71  this->m_outputBuffer = this->m_preview->rect;
72  }
73 
74  if (this->m_outputBuffer == nullptr) {
75  this->m_outputBuffer = (unsigned char *)MEM_callocN(
76  sizeof(unsigned char) * 4 * getWidth() * getHeight(), "PreviewOperation");
77  if (this->m_preview->rect) {
78  MEM_freeN(this->m_preview->rect);
79  }
80  this->m_preview->xsize = getWidth();
81  this->m_preview->ysize = getHeight();
82  this->m_preview->rect = this->m_outputBuffer;
83  }
84 }
85 
87 {
88  this->m_outputBuffer = nullptr;
89  this->m_input = nullptr;
90 }
91 
92 void PreviewOperation::executeRegion(rcti *rect, unsigned int /*tileNumber*/)
93 {
94  int offset;
95  float color[4];
96  struct ColormanageProcessor *cm_processor;
97 
99  this->m_displaySettings);
100 
101  for (int y = rect->ymin; y < rect->ymax; y++) {
102  offset = (y * getWidth() + rect->xmin) * 4;
103  for (int x = rect->xmin; x < rect->xmax; x++) {
104  float rx = floor(x / this->m_divider);
105  float ry = floor(y / this->m_divider);
106 
107  color[0] = 0.0f;
108  color[1] = 0.0f;
109  color[2] = 0.0f;
110  color[3] = 1.0f;
111  this->m_input->readSampled(color, rx, ry, PixelSampler::Nearest);
112  IMB_colormanagement_processor_apply_v4(cm_processor, color);
113  rgba_float_to_uchar(this->m_outputBuffer + offset, color);
114  offset += 4;
115  }
116  }
117 
119 }
121  ReadBufferOperation *readOperation,
122  rcti *output)
123 {
124  rcti newInput;
125 
126  newInput.xmin = input->xmin / this->m_divider;
127  newInput.xmax = input->xmax / this->m_divider;
128  newInput.ymin = input->ymin / this->m_divider;
129  newInput.ymax = input->ymax / this->m_divider;
130 
131  return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
132 }
133 void PreviewOperation::determineResolution(unsigned int resolution[2],
134  unsigned int /*preferredResolution*/[2])
135 {
136  /* Use default preview resolution as preferred ensuring it has size so that
137  * generated inputs (which don't have resolution on their own) are displayed */
138  BLI_assert(this->m_defaultWidth > 0 && this->m_defaultHeight > 0);
139  unsigned int previewPreferredRes[2] = {this->m_defaultWidth, this->m_defaultHeight};
140  NodeOperation::determineResolution(resolution, previewPreferredRes);
141 
142  /* If resolution is 0 there are two possible scenarios:
143  * - Either node is not connected at all
144  * - Or it is connected to an input which has no resolution.
145  *
146  * In the former case we rely on the execution system to not evaluate this node.
147  *
148  * The latter case would only happen if an input doesn't set any resolution ignoring output
149  * preferred resolution. In such case preview size will be 0 too.
150  */
151  int width = resolution[0];
152  int height = resolution[1];
153  this->m_divider = 0.0f;
154  if (width > 0 && height > 0) {
155  if (width > height) {
156  this->m_divider = (float)COM_PREVIEW_SIZE / (width);
157  }
158  else {
159  this->m_divider = (float)COM_PREVIEW_SIZE / (height);
160  }
161  }
162  width = width * this->m_divider;
163  height = height * this->m_divider;
164 
165  resolution[0] = width;
166  resolution[1] = height;
167 }
168 
170 {
172 }
173 
174 } // namespace blender::compositor
typedef float(TangentPoint)[2]
bNodePreview * BKE_node_preview_verify(struct bNodeInstanceHash *previews, bNodeInstanceKey key, int xsize, int ysize, bool create)
Definition: node.cc:2580
#define BLI_assert(a)
Definition: BLI_assert.h:58
void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
Definition: math_color.c:427
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
void IMB_colormanagement_processor_free(struct ColormanageProcessor *cm_processor)
void IMB_colormanagement_processor_apply_v4(struct ColormanageProcessor *cm_processor, float pixel[4])
struct ColormanageProcessor * IMB_colormanagement_display_processor_new(const struct ColorManagedViewSettings *view_settings, const struct ColorManagedDisplaySettings *display_settings)
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
Platform independent time functions.
#define output
void readSampled(float result[4], float x, float y, PixelSampler sampler)
void addInputSocket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
virtual void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
determine the resolution of this node
SocketReader * getInputSocketReader(unsigned int inputSocketindex)
virtual bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
determine the resolution of this node
bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override
PreviewOperation(const ColorManagedViewSettings *viewSettings, const ColorManagedDisplaySettings *displaySettings, unsigned int defaultWidth, unsigned int defaultHeight)
bNodePreview * m_preview
holds reference to the SDNA bNode, where this nodes will render the preview image for
const ColorManagedDisplaySettings * m_displaySettings
void executeRegion(rcti *rect, unsigned int tileNumber) override
when a chunk is executed by a CPUDevice, this method is called
eCompositorPriority getRenderPriority() const override
get the render priority of this node.
const ColorManagedViewSettings * m_viewSettings
void verifyPreview(bNodeInstanceHash *previews, bNodeInstanceKey key)
eCompositorPriority
Possible priority settings.
Definition: COM_Enums.h:45
@ None
The bottom left of the input image is the bottom left of the working area of the node,...
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
constexpr float COM_PREVIEW_SIZE
Definition: COM_defines.h:78
unsigned char * rect
int ymin
Definition: DNA_vec_types.h:80
int ymax
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79
int xmax
Definition: DNA_vec_types.h:79
ccl_device_inline float2 floor(const float2 &a)