Blender V5.0
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 size_t input_width,
24 const size_t input_height,
25 const size_t components,
26 vector<T> *output_pixels,
27 size_t *output_width,
28 size_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
103
104#include "util/image_impl.h" // IWYU pragma: export
unsigned char uchar
Definition half.h:41
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 size_t input_width, const size_t input_height, const size_t components, vector< T > *output_pixels, size_t *output_width, size_t *output_height)
#define CCL_NAMESPACE_END
ccl_device_inline half float_to_half_image(const float f)
Definition half.h:70
ccl_device_inline float half_to_float_image(half h)
Definition half.h:98
#define T