1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_IMAGE_H__
18 #  define __UTIL_IMAGE_H__
19 
20 /* OpenImageIO is used for all image file reading and writing. */
21 
22 #  include <OpenImageIO/imageio.h>
23 
24 #  include "util/util_half.h"
25 #  include "util/util_vector.h"
26 
27 CCL_NAMESPACE_BEGIN
28 
29 OIIO_NAMESPACE_USING
30 
31 template<typename T>
32 void util_image_resize_pixels(const vector<T> &input_pixels,
33                               const size_t input_width,
34                               const size_t input_height,
35                               const size_t input_depth,
36                               const size_t components,
37                               vector<T> *output_pixels,
38                               size_t *output_width,
39                               size_t *output_height,
40                               size_t *output_depth);
41 
42 /* Cast input pixel from unknown storage to float. */
43 template<typename T> inline float util_image_cast_to_float(T value);
44 
util_image_cast_to_float(float value)45 template<> inline float util_image_cast_to_float(float value)
46 {
47   return value;
48 }
util_image_cast_to_float(uchar value)49 template<> inline float util_image_cast_to_float(uchar value)
50 {
51   return (float)value / 255.0f;
52 }
util_image_cast_to_float(uint16_t value)53 template<> inline float util_image_cast_to_float(uint16_t value)
54 {
55   return (float)value / 65535.0f;
56 }
util_image_cast_to_float(half value)57 template<> inline float util_image_cast_to_float(half value)
58 {
59   return half_to_float(value);
60 }
61 
62 /* Cast float value to output pixel type. */
63 template<typename T> inline T util_image_cast_from_float(float value);
64 
util_image_cast_from_float(float value)65 template<> inline float util_image_cast_from_float(float value)
66 {
67   return value;
68 }
util_image_cast_from_float(float value)69 template<> inline uchar util_image_cast_from_float(float value)
70 {
71   if (value < 0.0f) {
72     return 0;
73   }
74   else if (value > (1.0f - 0.5f / 255.0f)) {
75     return 255;
76   }
77   return (uchar)((255.0f * value) + 0.5f);
78 }
util_image_cast_from_float(float value)79 template<> inline uint16_t util_image_cast_from_float(float value)
80 {
81   if (value < 0.0f) {
82     return 0;
83   }
84   else if (value > (1.0f - 0.5f / 65535.0f)) {
85     return 65535;
86   }
87   return (uint16_t)((65535.0f * value) + 0.5f);
88 }
util_image_cast_from_float(float value)89 template<> inline half util_image_cast_from_float(float value)
90 {
91   return float_to_half(value);
92 }
93 
94 CCL_NAMESPACE_END
95 
96 #endif /* __UTIL_IMAGE_H__ */
97 
98 #include "util/util_image_impl.h"
99