/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/IlmImf/ImfConvert.cpp
Line  | Count  | Source  | 
1  |  | ///////////////////////////////////////////////////////////////////////////  | 
2  |  | //  | 
3  |  | // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas  | 
4  |  | // Digital Ltd. LLC  | 
5  |  | //   | 
6  |  | // All rights reserved.  | 
7  |  | //   | 
8  |  | // Redistribution and use in source and binary forms, with or without  | 
9  |  | // modification, are permitted provided that the following conditions are  | 
10  |  | // met:  | 
11  |  | // *       Redistributions of source code must retain the above copyright  | 
12  |  | // notice, this list of conditions and the following disclaimer.  | 
13  |  | // *       Redistributions in binary form must reproduce the above  | 
14  |  | // copyright notice, this list of conditions and the following disclaimer  | 
15  |  | // in the documentation and/or other materials provided with the  | 
16  |  | // distribution.  | 
17  |  | // *       Neither the name of Industrial Light & Magic nor the names of  | 
18  |  | // its contributors may be used to endorse or promote products derived  | 
19  |  | // from this software without specific prior written permission.   | 
20  |  | //   | 
21  |  | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  | 
22  |  | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  | 
23  |  | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  | 
24  |  | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  | 
25  |  | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  | 
26  |  | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  | 
27  |  | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  | 
28  |  | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  | 
29  |  | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  | 
30  |  | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  | 
31  |  | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  | 
32  |  | //  | 
33  |  | ///////////////////////////////////////////////////////////////////////////  | 
34  |  |  | 
35  |  |  | 
36  |  | //-----------------------------------------------------------------------------  | 
37  |  | //  | 
38  |  | //  Routines for converting between pixel data types,  | 
39  |  | //  with well-defined behavior for exceptional cases.  | 
40  |  | //  | 
41  |  | //-----------------------------------------------------------------------------  | 
42  |  |  | 
43  |  | #include "ImfConvert.h"  | 
44  |  | #include "ImfNamespace.h"  | 
45  |  |  | 
46  |  | #include <limits.h>  | 
47  |  |  | 
48  |  |  | 
49  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER  | 
50  |  |  | 
51  |  | namespace { | 
52  |  |  | 
53  |  | inline bool  | 
54  |  | isNegative (float f)  | 
55  | 0  | { | 
56  | 0  |     union {float f; int i;} u; | 
57  | 0  |     u.f = f;  | 
58  | 0  |     return (u.i & 0x80000000) != 0;  | 
59  | 0  | }  | 
60  |  |  | 
61  |  |  | 
62  |  | inline bool  | 
63  |  | isNan (float f)  | 
64  | 0  | { | 
65  | 0  |     union {float f; int i;} u; | 
66  | 0  |     u.f = f;  | 
67  | 0  |     return (u.i & 0x7fffffff) > 0x7f800000;  | 
68  | 0  | }  | 
69  |  |  | 
70  |  |  | 
71  |  | inline bool  | 
72  |  | isInfinity (float f)  | 
73  | 0  | { | 
74  | 0  |     union {float f; int i;} u; | 
75  | 0  |     u.f = f;  | 
76  | 0  |     return (u.i & 0x7fffffff) == 0x7f800000;  | 
77  | 0  | }  | 
78  |  |  | 
79  |  |  | 
80  |  | inline bool  | 
81  |  | isFinite (float f)  | 
82  | 0  | { | 
83  | 0  |     union {float f; int i;} u; | 
84  | 0  |     u.f = f;  | 
85  | 0  |     return (u.i & 0x7f800000) != 0x7f800000;  | 
86  | 0  | }  | 
87  |  |  | 
88  |  | } // namespace  | 
89  |  |  | 
90  |  |  | 
91  |  | unsigned int  | 
92  |  | halfToUint (half h)  | 
93  | 0  | { | 
94  | 0  |     if (h.isNegative() || h.isNan())  | 
95  | 0  |   return 0;  | 
96  |  |  | 
97  | 0  |     if (h.isInfinity())  | 
98  | 0  |   return UINT_MAX;  | 
99  |  |  | 
100  | 0  |     return (unsigned int) h;  | 
101  | 0  | }  | 
102  |  |  | 
103  |  |  | 
104  |  | unsigned int  | 
105  |  | floatToUint (float f)  | 
106  | 0  | { | 
107  | 0  |     if (isNegative (f) || isNan (f))  | 
108  | 0  |   return 0;  | 
109  |  |  | 
110  | 0  |     if (isInfinity (f) || f > UINT_MAX)  | 
111  | 0  |   return UINT_MAX;  | 
112  |  |  | 
113  | 0  |     return (unsigned int) f;  | 
114  | 0  | }  | 
115  |  |  | 
116  |  |  | 
117  |  | half    | 
118  |  | uintToHalf (unsigned int ui)  | 
119  | 0  | { | 
120  | 0  |     if (ui >  HALF_MAX)  | 
121  | 0  |   return half::posInf();  | 
122  |  |  | 
123  | 0  |     return half ((float) ui);  | 
124  | 0  | }  | 
125  |  |  | 
126  |  |  | 
127  |  | half    | 
128  |  | floatToHalf (float f)  | 
129  | 0  | { | 
130  | 0  |     if (isFinite (f))  | 
131  | 0  |     { | 
132  | 0  |   if (f >  HALF_MAX)  | 
133  | 0  |       return half::posInf();  | 
134  |  |  | 
135  | 0  |   if (f < -HALF_MAX)  | 
136  | 0  |       return half::negInf();  | 
137  | 0  |     }  | 
138  |  |  | 
139  | 0  |     return half (f);  | 
140  | 0  | }  | 
141  |  |  | 
142  |  |  | 
143  |  | OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT  |