/src/openexr/src/lib/OpenEXR/ImfConvert.cpp
Line | Count | Source |
1 | | // |
2 | | // SPDX-License-Identifier: BSD-3-Clause |
3 | | // Copyright (c) Contributors to the OpenEXR Project. |
4 | | // |
5 | | |
6 | | //----------------------------------------------------------------------------- |
7 | | // |
8 | | // Routines for converting between pixel data types, |
9 | | // with well-defined behavior for exceptional cases. |
10 | | // |
11 | | //----------------------------------------------------------------------------- |
12 | | |
13 | | #include "ImfConvert.h" |
14 | | #include "ImfNamespace.h" |
15 | | |
16 | | #include "halfLimits.h" |
17 | | #include <limits> |
18 | | |
19 | | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER |
20 | | |
21 | | namespace |
22 | | { |
23 | | |
24 | | inline bool |
25 | | isNegative (float f) |
26 | 0 | { |
27 | 0 | union |
28 | 0 | { |
29 | 0 | float f; |
30 | 0 | int i; |
31 | 0 | } u; |
32 | 0 | u.f = f; |
33 | 0 | return (u.i & 0x80000000) != 0; |
34 | 0 | } |
35 | | |
36 | | inline bool |
37 | | isNan (float f) |
38 | 0 | { |
39 | 0 | union |
40 | 0 | { |
41 | 0 | float f; |
42 | 0 | int i; |
43 | 0 | } u; |
44 | 0 | u.f = f; |
45 | 0 | return (u.i & 0x7fffffff) > 0x7f800000; |
46 | 0 | } |
47 | | |
48 | | inline bool |
49 | | isInfinity (float f) |
50 | 0 | { |
51 | 0 | union |
52 | 0 | { |
53 | 0 | float f; |
54 | 0 | int i; |
55 | 0 | } u; |
56 | 0 | u.f = f; |
57 | 0 | return (u.i & 0x7fffffff) == 0x7f800000; |
58 | 0 | } |
59 | | |
60 | | inline bool |
61 | | isFinite (float f) |
62 | 0 | { |
63 | 0 | union |
64 | 0 | { |
65 | 0 | float f; |
66 | 0 | int i; |
67 | 0 | } u; |
68 | 0 | u.f = f; |
69 | 0 | return (u.i & 0x7f800000) != 0x7f800000; |
70 | 0 | } |
71 | | |
72 | | } // namespace |
73 | | |
74 | | unsigned int |
75 | | halfToUint (half h) |
76 | 0 | { |
77 | 0 | if (h.isNegative () || h.isNan ()) return 0; |
78 | | |
79 | 0 | if (h.isInfinity ()) return std::numeric_limits<unsigned int>::max (); |
80 | | |
81 | 0 | return static_cast<unsigned int> (h); |
82 | 0 | } |
83 | | |
84 | | unsigned int |
85 | | floatToUint (float f) |
86 | 0 | { |
87 | 0 | if (isNegative (f) || isNan (f)) return 0; |
88 | | |
89 | 0 | if (isInfinity (f) || |
90 | 0 | f > static_cast<float> (std::numeric_limits<unsigned int>::max ())) |
91 | 0 | return std::numeric_limits<unsigned int>::max (); |
92 | | |
93 | 0 | return static_cast<unsigned int> (f); |
94 | 0 | } |
95 | | |
96 | | half |
97 | | uintToHalf (unsigned int ui) |
98 | 0 | { |
99 | 0 | if (ui > std::numeric_limits<half>::max ()) return half::posInf (); |
100 | | |
101 | 0 | return half ((float) ui); |
102 | 0 | } |
103 | | |
104 | | half |
105 | | floatToHalf (float f) |
106 | 0 | { |
107 | 0 | if (isFinite (f)) |
108 | 0 | { |
109 | 0 | if (f > std::numeric_limits<half>::max ()) return half::posInf (); |
110 | | |
111 | 0 | if (f < std::numeric_limits<half>::lowest ()) return half::negInf (); |
112 | 0 | } |
113 | | |
114 | 0 | return half (f); |
115 | 0 | } |
116 | | |
117 | | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT |