Blender  V2.93
COM_ImageOperation.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_ImageOperation.h"
20 
21 #include "BKE_image.h"
22 #include "BKE_scene.h"
23 #include "BLI_listbase.h"
24 #include "BLI_math.h"
25 #include "DNA_image_types.h"
26 
27 #include "IMB_colormanagement.h"
28 #include "IMB_imbuf.h"
29 #include "IMB_imbuf_types.h"
30 
31 #include "RE_pipeline.h"
32 #include "RE_texture.h"
33 
34 namespace blender::compositor {
35 
37 {
38  this->m_image = nullptr;
39  this->m_buffer = nullptr;
40  this->m_imageFloatBuffer = nullptr;
41  this->m_imageByteBuffer = nullptr;
42  this->m_imageUser = nullptr;
43  this->m_imagewidth = 0;
44  this->m_imageheight = 0;
45  this->m_framenumber = 0;
46  this->m_depthBuffer = nullptr;
47  this->m_numberOfChannels = 0;
48  this->m_rd = nullptr;
49  this->m_viewName = nullptr;
50 }
52 {
54 }
56 {
58 }
60 {
62 }
63 
65 {
66  ImBuf *ibuf;
67  ImageUser iuser = *this->m_imageUser;
68 
69  if (this->m_image == nullptr) {
70  return nullptr;
71  }
72 
73  /* local changes to the original ImageUser */
74  if (BKE_image_is_multilayer(this->m_image) == false) {
76  }
77 
78  ibuf = BKE_image_acquire_ibuf(this->m_image, &iuser, nullptr);
79  if (ibuf == nullptr || (ibuf->rect == nullptr && ibuf->rect_float == nullptr)) {
80  BKE_image_release_ibuf(this->m_image, ibuf, nullptr);
81  return nullptr;
82  }
83  return ibuf;
84 }
85 
87 {
88  ImBuf *stackbuf = getImBuf();
89  this->m_buffer = stackbuf;
90  if (stackbuf) {
91  this->m_imageFloatBuffer = stackbuf->rect_float;
92  this->m_imageByteBuffer = stackbuf->rect;
93  this->m_depthBuffer = stackbuf->zbuf_float;
94  this->m_imagewidth = stackbuf->x;
95  this->m_imageheight = stackbuf->y;
96  this->m_numberOfChannels = stackbuf->channels;
97  }
98 }
99 
101 {
102  this->m_imageFloatBuffer = nullptr;
103  this->m_imageByteBuffer = nullptr;
104  BKE_image_release_ibuf(this->m_image, this->m_buffer, nullptr);
105 }
106 
107 void BaseImageOperation::determineResolution(unsigned int resolution[2],
108  unsigned int /*preferredResolution*/[2])
109 {
110  ImBuf *stackbuf = getImBuf();
111 
112  resolution[0] = 0;
113  resolution[1] = 0;
114 
115  if (stackbuf) {
116  resolution[0] = stackbuf->x;
117  resolution[1] = stackbuf->y;
118  }
119 
120  BKE_image_release_ibuf(this->m_image, stackbuf, nullptr);
121 }
122 
124  ImBuf *ibuf, float x, float y, PixelSampler sampler, bool make_linear_rgb, float color[4])
125 {
126  if (ibuf->rect_float) {
127  switch (sampler) {
129  nearest_interpolation_color(ibuf, nullptr, color, x, y);
130  break;
132  bilinear_interpolation_color(ibuf, nullptr, color, x, y);
133  break;
135  bicubic_interpolation_color(ibuf, nullptr, color, x, y);
136  break;
137  }
138  }
139  else {
140  unsigned char byte_color[4];
141  switch (sampler) {
143  nearest_interpolation_color(ibuf, byte_color, nullptr, x, y);
144  break;
146  bilinear_interpolation_color(ibuf, byte_color, nullptr, x, y);
147  break;
149  bicubic_interpolation_color(ibuf, byte_color, nullptr, x, y);
150  break;
151  }
152  rgba_uchar_to_float(color, byte_color);
153  if (make_linear_rgb) {
155  }
156  }
157 }
158 
159 void ImageOperation::executePixelSampled(float output[4], float x, float y, PixelSampler sampler)
160 {
161  int ix = x, iy = y;
162  if (this->m_imageFloatBuffer == nullptr && this->m_imageByteBuffer == nullptr) {
163  zero_v4(output);
164  }
165  else if (ix < 0 || iy < 0 || ix >= this->m_buffer->x || iy >= this->m_buffer->y) {
166  zero_v4(output);
167  }
168  else {
169  sampleImageAtLocation(this->m_buffer, x, y, sampler, true, output);
170  }
171 }
172 
174  float x,
175  float y,
176  PixelSampler sampler)
177 {
178  float tempcolor[4];
179 
180  if (this->m_imageFloatBuffer == nullptr && this->m_imageByteBuffer == nullptr) {
181  output[0] = 0.0f;
182  }
183  else {
184  tempcolor[3] = 1.0f;
185  sampleImageAtLocation(this->m_buffer, x, y, sampler, false, tempcolor);
186  output[0] = tempcolor[3];
187  }
188 }
189 
191  float x,
192  float y,
193  PixelSampler /*sampler*/)
194 {
195  if (this->m_depthBuffer == nullptr) {
196  output[0] = 0.0f;
197  }
198  else {
199  if (x < 0 || y < 0 || x >= this->getWidth() || y >= this->getHeight()) {
200  output[0] = 0.0f;
201  }
202  else {
203  int offset = y * this->m_width + x;
204  output[0] = this->m_depthBuffer[offset];
205  }
206  }
207 }
208 
209 } // namespace blender::compositor
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
Definition: image.c:5113
bool BKE_image_is_multilayer(struct Image *ima)
Definition: image.c:3868
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
Definition: image.c:5100
int BKE_scene_multiview_view_id_get(const struct RenderData *rd, const char *viewname)
void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
Definition: math_color.c:414
MINLINE void zero_v4(float r[4])
_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_colorspace_to_scene_linear_v4(float pixel[4], bool predivide, struct ColorSpace *colorspace)
void bicubic_interpolation_color(struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v)
Definition: imageprocess.c:98
void bilinear_interpolation_color(struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v)
Definition: imageprocess.c:130
void nearest_interpolation_color(struct ImBuf *in, unsigned char outI[4], float outF[4], float u, float v)
Definition: imageprocess.c:242
Contains defines and structs used throughout the imbuf module.
#define output
Base class for all image operations.
void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]) override
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override
calculate a single pixel
void addOutputSocket(DataType datatype)
static void sampleImageAtLocation(ImBuf *ibuf, float x, float y, PixelSampler sampler, bool make_linear_rgb, float color[4])
float * zbuf_float
int channels
struct ColorSpace * rect_colorspace
unsigned int * rect
float * rect_float
short multi_index