Blender V4.5
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 input_depth,
26 const size_t components,
27 vector<T> *output_pixels,
28 size_t *output_width,
29 size_t *output_height,
30 size_t *output_depth);
31
32/* Cast input pixel from unknown storage to float. */
33template<typename T> inline float util_image_cast_to_float(T value);
34
35template<> inline float util_image_cast_to_float(const float value)
36{
37 return value;
38}
39template<> inline float util_image_cast_to_float(const uchar value)
40{
41 return (float)value / 255.0f;
42}
43template<> inline float util_image_cast_to_float(const uint16_t value)
44{
45 return (float)value / 65535.0f;
46}
47template<> inline float util_image_cast_to_float(half value)
48{
49 return half_to_float_image(value);
50}
51
52/* Cast float value to output pixel type. */
53template<typename T> inline T util_image_cast_from_float(const float value);
54
55template<> inline float util_image_cast_from_float(const float value)
56{
57 return value;
58}
59template<> inline uchar util_image_cast_from_float(const float value)
60{
61 if (value < 0.0f) {
62 return 0;
63 }
64 if (value > (1.0f - 0.5f / 255.0f)) {
65 return 255;
66 }
67 return (uchar)((255.0f * value) + 0.5f); // NOLINT
68}
69template<> inline uint16_t util_image_cast_from_float(const float value)
70{
71 if (value < 0.0f) {
72 return 0;
73 }
74 if (value > (1.0f - 0.5f / 65535.0f)) {
75 return 65535;
76 }
77 return (uint16_t)((65535.0f * value) + 0.5f); // NOLINT
78}
79template<> inline half util_image_cast_from_float(const float value)
80{
81 return float_to_half_image(value);
82}
83
84/* Multiply image pixels in native data format. */
85template<typename T> inline T util_image_multiply_native(T a, T b);
86
87template<> inline float util_image_multiply_native(const float a, const float b)
88{
89 return a * b;
90}
91template<> inline uchar util_image_multiply_native(const uchar a, const uchar b)
92{
93 return ((uint32_t)a * (uint32_t)b) / 255;
94}
95template<> inline uint16_t util_image_multiply_native(const uint16_t a, const uint16_t b)
96{
97 return ((uint32_t)a * (uint32_t)b) / 65535;
98}
103
105
106#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)
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, vector< T > *output_pixels, size_t *output_width, size_t *output_height, size_t *output_depth)
float util_image_cast_to_float(T value)
#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