Blender  V3.3
image_impl.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #ifndef __UTIL_IMAGE_IMPL_H__
5 #define __UTIL_IMAGE_IMPL_H__
6 
7 #include "util/algorithm.h"
8 #include "util/half.h"
9 #include "util/image.h"
10 
12 
13 namespace {
14 
15 template<typename T>
16 const T *util_image_read(const vector<T> &pixels,
17  const size_t width,
18  const size_t height,
19  const size_t /*depth*/,
20  const size_t components,
21  const size_t x,
22  const size_t y,
23  const size_t z)
24 {
25  const size_t index = ((size_t)z * (width * height) + (size_t)y * width + (size_t)x) * components;
26  return &pixels[index];
27 }
28 
29 template<typename T>
31  const size_t width,
32  const size_t height,
33  const size_t depth,
34  const size_t components,
35  const size_t kernel_size,
36  const float x,
37  const float y,
38  const float z,
39  T *result)
40 {
41  assert(components <= 4);
42  const size_t ix = (size_t)x, iy = (size_t)y, iz = (size_t)z;
43  /* TODO(sergey): Support something smarter than box filer. */
44  float accum[4] = {0};
45  size_t count = 0;
46  for (size_t dz = 0; dz < kernel_size; ++dz) {
47  for (size_t dy = 0; dy < kernel_size; ++dy) {
48  for (size_t dx = 0; dx < kernel_size; ++dx) {
49  const size_t nx = ix + dx, ny = iy + dy, nz = iz + dz;
50  if (nx >= width || ny >= height || nz >= depth) {
51  continue;
52  }
53  const T *pixel = util_image_read(pixels, width, height, depth, components, nx, ny, nz);
54  for (size_t k = 0; k < components; ++k) {
55  accum[k] += util_image_cast_to_float(pixel[k]);
56  }
57  ++count;
58  }
59  }
60  }
61  if (count != 0) {
62  const float inv_count = 1.0f / (float)count;
63  for (size_t k = 0; k < components; ++k) {
64  result[k] = util_image_cast_from_float<T>(accum[k] * inv_count);
65  }
66  }
67  else {
68  for (size_t k = 0; k < components; ++k) {
69  result[k] = T(0.0f);
70  }
71  }
72 }
73 
74 template<typename T>
75 void util_image_downscale_pixels(const vector<T> &input_pixels,
76  const size_t input_width,
77  const size_t input_height,
78  const size_t input_depth,
79  const size_t components,
80  const float inv_scale_factor,
81  const size_t output_width,
82  const size_t output_height,
83  const size_t output_depth,
84  vector<T> *output_pixels)
85 {
86  const size_t kernel_size = (size_t)(inv_scale_factor + 0.5f);
87  for (size_t z = 0; z < output_depth; ++z) {
88  for (size_t y = 0; y < output_height; ++y) {
89  for (size_t x = 0; x < output_width; ++x) {
90  const float input_x = (float)x * inv_scale_factor, input_y = (float)y * inv_scale_factor,
91  input_z = (float)z * inv_scale_factor;
92  const size_t output_index = (z * output_width * output_height + y * output_width + x) *
93  components;
94  util_image_downscale_sample(input_pixels,
95  input_width,
96  input_height,
97  input_depth,
98  components,
99  kernel_size,
100  input_x,
101  input_y,
102  input_z,
103  &output_pixels->at(output_index));
104  }
105  }
106  }
107 }
108 
109 } /* namespace */
110 
111 template<typename T>
112 void util_image_resize_pixels(const vector<T> &input_pixels,
113  const size_t input_width,
114  const size_t input_height,
115  const size_t input_depth,
116  const size_t components,
117  const float scale_factor,
118  vector<T> *output_pixels,
119  size_t *output_width,
120  size_t *output_height,
121  size_t *output_depth)
122 {
123  /* Early output for case when no scaling is applied. */
124  if (scale_factor == 1.0f) {
125  *output_width = input_width;
126  *output_height = input_height;
127  *output_depth = input_depth;
128  *output_pixels = input_pixels;
129  return;
130  }
131  /* First of all, we calculate output image dimensions.
132  * We clamp them to be 1 pixel at least so we do not generate degenerate
133  * image.
134  */
135  *output_width = max((size_t)((float)input_width * scale_factor), (size_t)1);
136  *output_height = max((size_t)((float)input_height * scale_factor), (size_t)1);
137  *output_depth = max((size_t)((float)input_depth * scale_factor), (size_t)1);
138  /* Prepare pixel storage for the result. */
139  const size_t num_output_pixels = ((*output_width) * (*output_height) * (*output_depth)) *
140  components;
141  output_pixels->resize(num_output_pixels);
142  if (scale_factor < 1.0f) {
143  const float inv_scale_factor = 1.0f / scale_factor;
144  util_image_downscale_pixels(input_pixels,
145  input_width,
146  input_height,
147  input_depth,
148  components,
149  inv_scale_factor,
150  *output_width,
151  *output_height,
152  *output_depth,
153  output_pixels);
154  }
155  else {
156  /* TODO(sergey): Needs implementation. */
157  }
158 }
159 
161 
162 #endif /* __UTIL_IMAGE_IMPL_H__ */
typedef float(TangentPoint)[2]
_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 GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_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 GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble ny
_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
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
float util_image_cast_to_float(T value)
void util_image_resize_pixels(const vector< T > &input_pixels, const size_t input_width, const size_t input_height, const size_t input_depth, const size_t components, const float scale_factor, vector< T > *output_pixels, size_t *output_width, size_t *output_height, size_t *output_depth)
Definition: image_impl.h:112
int count
#define T
const T * util_image_read(const vector< T > &pixels, const size_t width, const size_t height, const size_t, const size_t components, const size_t x, const size_t y, const size_t z)
Definition: image_impl.h:16
void util_image_downscale_sample(const vector< T > &pixels, const size_t width, const size_t height, const size_t depth, const size_t components, const size_t kernel_size, const float x, const float y, const float z, T *result)
Definition: image_impl.h:30
void util_image_downscale_pixels(const vector< T > &input_pixels, const size_t input_width, const size_t input_height, const size_t input_depth, const size_t components, const float inv_scale_factor, const size_t output_width, const size_t output_height, const size_t output_depth, vector< T > *output_pixels)
Definition: image_impl.h:75
float max