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