Blender  V2.93
util_image_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_IMAGE_IMPL_H__
18 #define __UTIL_IMAGE_IMPL_H__
19 
20 #include "util/util_algorithm.h"
21 #include "util/util_half.h"
22 #include "util/util_image.h"
23 
25 
26 namespace {
27 
28 template<typename T>
29 const T *util_image_read(const vector<T> &pixels,
30  const size_t width,
31  const size_t height,
32  const size_t /*depth*/,
33  const size_t components,
34  const size_t x,
35  const size_t y,
36  const size_t z)
37 {
38  const size_t index = ((size_t)z * (width * height) + (size_t)y * width + (size_t)x) * components;
39  return &pixels[index];
40 }
41 
42 template<typename T>
44  const size_t width,
45  const size_t height,
46  const size_t depth,
47  const size_t components,
48  const size_t kernel_size,
49  const float x,
50  const float y,
51  const float z,
52  T *result)
53 {
54  assert(components <= 4);
55  const size_t ix = (size_t)x, iy = (size_t)y, iz = (size_t)z;
56  /* TODO(sergey): Support something smarter than box filer. */
57  float accum[4] = {0};
58  size_t count = 0;
59  for (size_t dz = 0; dz < kernel_size; ++dz) {
60  for (size_t dy = 0; dy < kernel_size; ++dy) {
61  for (size_t dx = 0; dx < kernel_size; ++dx) {
62  const size_t nx = ix + dx, ny = iy + dy, nz = iz + dz;
63  if (nx >= width || ny >= height || nz >= depth) {
64  continue;
65  }
66  const T *pixel = util_image_read(pixels, width, height, depth, components, nx, ny, nz);
67  for (size_t k = 0; k < components; ++k) {
68  accum[k] += util_image_cast_to_float(pixel[k]);
69  }
70  ++count;
71  }
72  }
73  }
74  if (count != 0) {
75  const float inv_count = 1.0f / (float)count;
76  for (size_t k = 0; k < components; ++k) {
77  result[k] = util_image_cast_from_float<T>(accum[k] * inv_count);
78  }
79  }
80  else {
81  for (size_t k = 0; k < components; ++k) {
82  result[k] = T(0.0f);
83  }
84  }
85 }
86 
87 template<typename T>
88 void util_image_downscale_pixels(const vector<T> &input_pixels,
89  const size_t input_width,
90  const size_t input_height,
91  const size_t input_depth,
92  const size_t components,
93  const float inv_scale_factor,
94  const size_t output_width,
95  const size_t output_height,
96  const size_t output_depth,
97  vector<T> *output_pixels)
98 {
99  const size_t kernel_size = (size_t)(inv_scale_factor + 0.5f);
100  for (size_t z = 0; z < output_depth; ++z) {
101  for (size_t y = 0; y < output_height; ++y) {
102  for (size_t x = 0; x < output_width; ++x) {
103  const float input_x = (float)x * inv_scale_factor, input_y = (float)y * inv_scale_factor,
104  input_z = (float)z * inv_scale_factor;
105  const size_t output_index = (z * output_width * output_height + y * output_width + x) *
106  components;
107  util_image_downscale_sample(input_pixels,
108  input_width,
109  input_height,
110  input_depth,
111  components,
112  kernel_size,
113  input_x,
114  input_y,
115  input_z,
116  &output_pixels->at(output_index));
117  }
118  }
119  }
120 }
121 
122 } /* namespace */
123 
124 template<typename T>
125 void util_image_resize_pixels(const vector<T> &input_pixels,
126  const size_t input_width,
127  const size_t input_height,
128  const size_t input_depth,
129  const size_t components,
130  const float scale_factor,
131  vector<T> *output_pixels,
132  size_t *output_width,
133  size_t *output_height,
134  size_t *output_depth)
135 {
136  /* Early output for case when no scaling is applied. */
137  if (scale_factor == 1.0f) {
138  *output_width = input_width;
139  *output_height = input_height;
140  *output_depth = input_depth;
141  *output_pixels = input_pixels;
142  return;
143  }
144  /* First of all, we calculate output image dimensions.
145  * We clamp them to be 1 pixel at least so we do not generate degenerate
146  * image.
147  */
148  *output_width = max((size_t)((float)input_width * scale_factor), (size_t)1);
149  *output_height = max((size_t)((float)input_height * scale_factor), (size_t)1);
150  *output_depth = max((size_t)((float)input_depth * scale_factor), (size_t)1);
151  /* Prepare pixel storage for the result. */
152  const size_t num_output_pixels = ((*output_width) * (*output_height) * (*output_depth)) *
153  components;
154  output_pixels->resize(num_output_pixels);
155  if (scale_factor < 1.0f) {
156  const float inv_scale_factor = 1.0f / scale_factor;
157  util_image_downscale_pixels(input_pixels,
158  input_width,
159  input_height,
160  input_depth,
161  components,
162  inv_scale_factor,
163  *output_width,
164  *output_height,
165  *output_depth,
166  output_pixels);
167  }
168  else {
169  /* TODO(sergey): Needs implementation. */
170  }
171 }
172 
174 
175 #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 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
int count
#define CCL_NAMESPACE_END
#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)
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)
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)
float max
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)