/src/FreeImage/Source/OpenEXR/IlmImf/ImfConvert.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 139 lines · 71 code · 29 blank · 39 comment · 14 complexity · 2a952084e70d7e2c461d5128f785f74f MD5 · raw file

  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. // Routines for converting between pixel data types,
  37. // with well-defined behavior for exceptional cases.
  38. //
  39. //-----------------------------------------------------------------------------
  40. #include <ImfConvert.h>
  41. #include <limits.h>
  42. namespace Imf {
  43. namespace {
  44. inline bool
  45. isNegative (float f)
  46. {
  47. union {float f; int i;} u;
  48. u.f = f;
  49. return (u.i & 0x80000000) != 0;
  50. }
  51. inline bool
  52. isNan (float f)
  53. {
  54. union {float f; int i;} u;
  55. u.f = f;
  56. return (u.i & 0x7fffffff) > 0x7f800000;
  57. }
  58. inline bool
  59. isInfinity (float f)
  60. {
  61. union {float f; int i;} u;
  62. u.f = f;
  63. return (u.i & 0x7fffffff) == 0x7f800000;
  64. }
  65. inline bool
  66. isFinite (float f)
  67. {
  68. union {float f; int i;} u;
  69. u.f = f;
  70. return (u.i & 0x7f800000) != 0x7f800000;
  71. }
  72. } // namespace
  73. unsigned int
  74. halfToUint (half h)
  75. {
  76. if (h.isNegative() || h.isNan())
  77. return 0;
  78. if (h.isInfinity())
  79. return UINT_MAX;
  80. return (unsigned int) h;
  81. }
  82. unsigned int
  83. floatToUint (float f)
  84. {
  85. if (isNegative (f) || isNan (f))
  86. return 0;
  87. if (isInfinity (f) || f > UINT_MAX)
  88. return UINT_MAX;
  89. return (unsigned int) f;
  90. }
  91. half
  92. uintToHalf (unsigned int ui)
  93. {
  94. if (ui > HALF_MAX)
  95. return half::posInf();
  96. return half (ui);
  97. }
  98. half
  99. floatToHalf (float f)
  100. {
  101. if (isFinite (f))
  102. {
  103. if (f > HALF_MAX)
  104. return half::posInf();
  105. if (f < -HALF_MAX)
  106. return half::negInf();
  107. }
  108. return half (f);
  109. }
  110. } // namespace Imf