Blender V4.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#ifndef __UTIL_IMAGE_H__
6# define __UTIL_IMAGE_H__
7
8/* OpenImageIO is used for all image file reading and writing. */
9
10# include <OpenImageIO/imageio.h>
11
12# include "util/half.h"
13# include "util/vector.h"
14
16
17OIIO_NAMESPACE_USING
18
19template<typename T>
20void util_image_resize_pixels(const vector<T> &input_pixels,
21 const size_t input_width,
22 const size_t input_height,
23 const size_t input_depth,
24 const size_t components,
25 vector<T> *output_pixels,
26 size_t *output_width,
27 size_t *output_height,
28 size_t *output_depth);
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(float value)
34{
35 return value;
36}
37template<> inline float util_image_cast_to_float(uchar value)
38{
39 return (float)value / 255.0f;
40}
41template<> inline float util_image_cast_to_float(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(float value);
52
53template<> inline float util_image_cast_from_float(float value)
54{
55 return value;
56}
57template<> inline uchar util_image_cast_from_float(float value)
58{
59 if (value < 0.0f) {
60 return 0;
61 }
62 else if (value > (1.0f - 0.5f / 255.0f)) {
63 return 255;
64 }
65 return (uchar)((255.0f * value) + 0.5f);
66}
67template<> inline uint16_t util_image_cast_from_float(float value)
68{
69 if (value < 0.0f) {
70 return 0;
71 }
72 else if (value > (1.0f - 0.5f / 65535.0f)) {
73 return 65535;
74 }
75 return (uint16_t)((65535.0f * value) + 0.5f);
76}
77template<> inline half util_image_cast_from_float(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(float a, float b)
86{
87 return a * b;
88}
90{
91 return ((uint32_t)a * (uint32_t)b) / 255;
92}
94{
95 return ((uint32_t)a * (uint32_t)b) / 65535;
96}
101
103
104#endif /* __UTIL_IMAGE_H__ */
105
106#include "util/image_impl.h"
unsigned char uchar
Definition half.h:42
local_group_size(16, 16) .push_constant(Type b
T util_image_multiply_native(T a, T b)
float util_image_cast_to_float(T value)
T util_image_cast_from_float(float value)
CCL_NAMESPACE_BEGIN OIIO_NAMESPACE_USING 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)
#define CCL_NAMESPACE_END
ccl_device_inline half float_to_half_image(float f)
Definition half.h:71
ccl_device_inline float half_to_float_image(half h)
Definition half.h:99
unsigned short uint16_t
Definition stdint.h:79
unsigned int uint32_t
Definition stdint.h:80