Blender V5.3
cycles/util/image.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/* OpenImageIO is used for all image file reading and writing. */
8
9#include <OpenImageIO/imageio.h>
10
11#include "util/half.h"
12#include "util/vector.h"
13
15
16using OIIO::AutoStride;
17using OIIO::ImageInput;
18using OIIO::ImageOutput;
19using OIIO::ImageSpec;
20
21template<typename T>
22void util_image_resize_pixels(const vector<T> &input_pixels,
23 const int64_t input_width,
24 const int64_t input_height,
25 const int64_t components,
26 vector<T> *output_pixels,
27 int64_t *output_width,
28 int64_t *output_height);
29
30/* Cast input pixel from unknown storage to float. */
31template<typename T> inline float util_image_cast_to_float(T value);
32
33template<> inline float util_image_cast_to_float(const float value)
34{
35 return value;
36}
37template<> inline float util_image_cast_to_float(const uchar value)
38{
39 return (float)value / 255.0f;
40}
41template<> inline float util_image_cast_to_float(const uint16_t value)
42{
43 return (float)value / 65535.0f;
44}
45template<> inline float util_image_cast_to_float(half value)
46{
47 return half_to_float_image(value);
48}
49
50/* Cast float value to output pixel type. */
51template<typename T> inline T util_image_cast_from_float(const float value);
52
53template<> inline float util_image_cast_from_float(const float value)
54{
55 return value;
56}
57template<> inline uchar util_image_cast_from_float(const float value)
58{
59 if (value < 0.0f) {
60 return 0;
61 }
62 if (value > (1.0f - 0.5f / 255.0f)) {
63 return 255;
64 }
65 return (uchar)((255.0f * value) + 0.5f); // NOLINT
66}
67template<> inline uint16_t util_image_cast_from_float(const float value)
68{
69 if (value < 0.0f) {
70 return 0;
71 }
72 if (value > (1.0f - 0.5f / 65535.0f)) {
73 return 65535;
74 }
75 return (uint16_t)((65535.0f * value) + 0.5f); // NOLINT
76}
77template<> inline half util_image_cast_from_float(const float value)
78{
79 return float_to_half_image(value);
80}
81
82/* Multiply image pixels in native data format. */
83template<typename T> inline T util_image_multiply_native(T a, T b);
84
85template<> inline float util_image_multiply_native(const float a, const float b)
86{
87 return a * b;
88}
89template<> inline uchar util_image_multiply_native(const uchar a, const uchar b)
90{
91 return ((uint32_t)a * (uint32_t)b) / 255;
92}
93template<> inline uint16_t util_image_multiply_native(const uint16_t a, const uint16_t b)
94{
95 return ((uint32_t)a * (uint32_t)b) / 65535;
96}
101
102/* Test if value is finite, in native data format. */
103template<typename T> inline T util_image_is_finite(T a);
104
105template<> inline float util_image_is_finite(const float a)
106{
107 return isfinite(a);
108}
109template<> inline uchar util_image_is_finite(const uchar /*a*/)
110{
111 return true;
112}
113template<> inline uint16_t util_image_is_finite(const uint16_t /*a*/)
114{
115 return true;
116}
117template<> inline half util_image_is_finite(half a)
118{
119 return half_is_finite(a);
120}
121
123
124#include "util/image_impl.h" // IWYU pragma: export
long long int int64_t
Definition half.h:29
T util_image_is_finite(T a)
T util_image_cast_from_float(const float value)
T util_image_multiply_native(T a, T b)
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
unsigned int uchar
ccl_device_inline float half_is_finite(const half h)
Definition half.h:343
ccl_device_inline half float_to_half_image(const float f)
Definition half.h:316
ccl_device_inline float half_to_float_image(half h)
Definition half.h:321
#define T