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