Blender  V3.3
intern/libmv/intern/image.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. All rights reserved. */
3 
4 #include "intern/image.h"
5 #include "intern/utildefines.h"
7 
8 #include <png.h>
9 #include <cassert>
10 
11 using libmv::FloatImage;
13 
15  delete[] image->buffer;
16 }
17 
18 /* Image <-> buffers conversion */
19 
20 void libmv_byteBufferToFloatImage(const unsigned char* buffer,
21  int width,
22  int height,
23  int channels,
24  FloatImage* image) {
25  image->Resize(height, width, channels);
26  for (int y = 0, a = 0; y < height; y++) {
27  for (int x = 0; x < width; x++) {
28  for (int k = 0; k < channels; k++) {
29  (*image)(y, x, k) = (float)buffer[a++] / 255.0f;
30  }
31  }
32  }
33 }
34 
36  int width,
37  int height,
38  int channels,
39  FloatImage* image) {
40  image->Resize(height, width, channels);
41  for (int y = 0, a = 0; y < height; y++) {
42  for (int x = 0; x < width; x++) {
43  for (int k = 0; k < channels; k++) {
44  (*image)(y, x, k) = buffer[a++];
45  }
46  }
47  }
48 }
49 
51  for (int y = 0, a = 0; y < image.Height(); y++) {
52  for (int x = 0; x < image.Width(); x++) {
53  for (int k = 0; k < image.Depth(); k++) {
54  buffer[a++] = image(y, x, k);
55  }
56  }
57  }
58 }
59 
61  unsigned char* buffer) {
62  for (int y = 0, a = 0; y < image.Height(); y++) {
63  for (int x = 0; x < image.Width(); x++) {
64  for (int k = 0; k < image.Depth(); k++) {
65  buffer[a++] = image(y, x, k) * 255.0f;
66  }
67  }
68  }
69 }
70 
71 static bool savePNGImage(png_bytep* row_pointers,
72  int width,
73  int height,
74  int depth,
75  int color_type,
76  const char* file_name) {
77  png_infop info_ptr;
78  png_structp png_ptr;
79  FILE* fp = fopen(file_name, "wb");
80 
81  if (fp == NULL) {
82  return false;
83  }
84 
85  /* Initialize stuff */
86  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
87  info_ptr = png_create_info_struct(png_ptr);
88 
89  if (setjmp(png_jmpbuf(png_ptr))) {
90  fclose(fp);
91  return false;
92  }
93 
94  png_init_io(png_ptr, fp);
95 
96  /* Write PNG header. */
97  if (setjmp(png_jmpbuf(png_ptr))) {
98  fclose(fp);
99  return false;
100  }
101 
102  png_set_IHDR(png_ptr,
103  info_ptr,
104  width,
105  height,
106  depth,
107  color_type,
108  PNG_INTERLACE_NONE,
109  PNG_COMPRESSION_TYPE_BASE,
110  PNG_FILTER_TYPE_BASE);
111 
112  png_write_info(png_ptr, info_ptr);
113 
114  /* Write bytes/ */
115  if (setjmp(png_jmpbuf(png_ptr))) {
116  fclose(fp);
117  return false;
118  }
119 
120  png_write_image(png_ptr, row_pointers);
121 
122  /* End write/ */
123  if (setjmp(png_jmpbuf(png_ptr))) {
124  fclose(fp);
125  return false;
126  }
127 
128  png_write_end(png_ptr, NULL);
129  fclose(fp);
130 
131  return true;
132 }
133 
135  const char* prefix,
136  int x0,
137  int y0) {
138  int x, y;
139  png_bytep* row_pointers;
140 
141  assert(image.Depth() == 1);
142 
143  row_pointers = new png_bytep[image.Height()];
144 
145  for (y = 0; y < image.Height(); y++) {
146  row_pointers[y] = new png_byte[4 * image.Width()];
147 
148  for (x = 0; x < image.Width(); x++) {
149  if (x0 == x && image.Height() - y0 - 1 == y) {
150  row_pointers[y][x * 4 + 0] = 255;
151  row_pointers[y][x * 4 + 1] = 0;
152  row_pointers[y][x * 4 + 2] = 0;
153  row_pointers[y][x * 4 + 3] = 255;
154  } else {
155  float pixel = image(image.Height() - y - 1, x, 0);
156  row_pointers[y][x * 4 + 0] = pixel * 255;
157  row_pointers[y][x * 4 + 1] = pixel * 255;
158  row_pointers[y][x * 4 + 2] = pixel * 255;
159  row_pointers[y][x * 4 + 3] = 255;
160  }
161  }
162  }
163 
164  static int image_counter = 0;
165  char file_name[128];
166  snprintf(
167  file_name, sizeof(file_name), "%s_%02d.png", prefix, ++image_counter);
168  bool result = savePNGImage(row_pointers,
169  image.Width(),
170  image.Height(),
171  8,
172  PNG_COLOR_TYPE_RGBA,
173  file_name);
174 
175  for (y = 0; y < image.Height(); y++) {
176  delete[] row_pointers[y];
177  }
178  delete[] row_pointers;
179 
180  return result;
181 }
182 
184  int width,
185  int height,
186  int channels,
187  const double* xs,
188  const double* ys,
189  int num_samples_x,
190  int num_samples_y,
191  const float* mask,
192  float* patch,
193  double* warped_position_x,
194  double* warped_position_y) {
195  FloatImage libmv_image, libmv_patch, libmv_mask;
196  FloatImage* libmv_mask_for_sample = NULL;
197 
199 
200  if (mask) {
201  libmv_floatBufferToFloatImage(mask, width, height, 1, &libmv_mask);
202  libmv_mask_for_sample = &libmv_mask;
203  }
204 
205  SamplePlanarPatch(libmv_image,
206  xs,
207  ys,
208  num_samples_x,
209  num_samples_y,
210  libmv_mask_for_sample,
211  &libmv_patch,
212  warped_position_x,
213  warped_position_y);
214 
215  libmv_floatImageToFloatBuffer(libmv_patch, patch);
216 }
217 
218 void libmv_samplePlanarPatchByte(const unsigned char* image,
219  int width,
220  int height,
221  int channels,
222  const double* xs,
223  const double* ys,
224  int num_samples_x,
225  int num_samples_y,
226  const float* mask,
227  unsigned char* patch,
228  double* warped_position_x,
229  double* warped_position_y) {
230  libmv::FloatImage libmv_image, libmv_patch, libmv_mask;
231  libmv::FloatImage* libmv_mask_for_sample = NULL;
232 
234 
235  if (mask) {
236  libmv_floatBufferToFloatImage(mask, width, height, 1, &libmv_mask);
237  libmv_mask_for_sample = &libmv_mask;
238  }
239 
240  libmv::SamplePlanarPatch(libmv_image,
241  xs,
242  ys,
243  num_samples_x,
244  num_samples_y,
245  libmv_mask_for_sample,
246  &libmv_patch,
247  warped_position_x,
248  warped_position_y);
249 
250  libmv_floatImageToByteBuffer(libmv_patch, patch);
251 }
#define snprintf
Definition: BLI_winstuff.h:53
_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
_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
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a producing a negative Combine Generate a color from its and blue channels(Deprecated)") DefNode(ShaderNode
3D array (row, column, channel).
Definition: array_nd.h:325
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
bool libmv_saveImage(const FloatImage &image, const char *prefix, int x0, int y0)
static bool savePNGImage(png_bytep *row_pointers, int width, int height, int depth, int color_type, const char *file_name)
void libmv_floatBufferToFloatImage(const float *buffer, int width, int height, int channels, FloatImage *image)
void libmv_samplePlanarPatchFloat(const float *image, int width, int height, int channels, const double *xs, const double *ys, int num_samples_x, int num_samples_y, const float *mask, float *patch, double *warped_position_x, double *warped_position_y)
void libmv_samplePlanarPatchByte(const unsigned char *image, int width, int height, int channels, const double *xs, const double *ys, int num_samples_x, int num_samples_y, const float *mask, unsigned char *patch, double *warped_position_x, double *warped_position_y)
void libmv_floatImageToFloatBuffer(const FloatImage &image, float *buffer)
void libmv_floatImageDestroy(libmv_FloatImage *image)
void libmv_floatImageToByteBuffer(const libmv::FloatImage &image, unsigned char *buffer)
void libmv_byteBufferToFloatImage(const unsigned char *buffer, int width, int height, int channels, FloatImage *image)
ccl_global float * buffer
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static unsigned a[3]
Definition: RandGen.cpp:78
Array3Df FloatImage
bool SamplePlanarPatch(const FloatImage &image, const double *xs, const double *ys, int num_samples_x, int num_samples_y, FloatImage *mask, FloatImage *patch, double *warped_position_x, double *warped_position_y)