#ifndef Magnum_Math_Packing_h #define Magnum_Math_Packing_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Functions @ref Magnum::Math::pack(), @ref Magnum::Math::unpack(), @ref Magnum::Math::packHalf(), @ref Magnum::Math::unpackHalf() */ #include "Magnum/Math/Functions.h" namespace Magnum { namespace Math { namespace Implementation { template inline constexpr T bitMax() { return T(typename std::make_unsigned::type(~T{}) >> (sizeof(T)*8 - (std::is_signed::value ? bits - 1 : bits))); } } #ifdef DOXYGEN_GENERATING_OUTPUT /** @brief Unpack integral value into a floating-point representation Converts integral value from full range of given *unsigned* integral type to value in range @f$ [0, 1] @f$ or from *signed* integral to range @f$ [-1, 1] @f$. @note For best precision, resulting `FloatingPoint` type should be always larger that `Integral` type (e.g. @ref Magnum::Float "Float" from @ref Magnum::Short "Short", @ref Magnum::Double "Double" from @ref Magnum::Int "Int" and similarly for vector types). @attention To ensure the integral type is correctly detected when using literals, this function should be called with both template parameters explicit, e.g.: @attention @code{.cpp} // Literal type is (signed) char, but we assumed unsigned char, a != 1.0f Float a = Math::unpack('\xFF'); // b = 1.0f Float b = Math::unpack('\xFF'); @endcode @see @ref pack() */ template inline FloatingPoint unpack(const Integral& value); /** @brief Unpack integer bits into a floating-point representation Alternative to the above with ability to specify how many bits of the integral representation to use. Example usage: @code{.cpp} Float a = Math::unpack(8191); // 0.124987f Float b = Math::unpack(8191); // 0.499969f Float b = Math::unpack(8191u); // 0.499969f Float b = Math::unpack(8191); // 1.0f @endcode */ template inline FloatingPoint unpack(const Integral& value); #else template inline typename std::enable_if::value && std::is_unsigned::value, FloatingPoint>::type unpack(Integral value) { static_assert(std::is_floating_point::value && std::is_integral::value, "unpacking must be done from integral to floating-point type"); static_assert(bits <= sizeof(Integral)*8, "bit count larger than size of the integral type"); return value/FloatingPoint(Implementation::bitMax()); } template inline typename std::enable_if::value && std::is_signed::value, FloatingPoint>::type unpack(Integral value) { static_assert(std::is_floating_point::value && std::is_integral::value, "unpacking must be done from integral to floating-point type"); static_assert(bits <= sizeof(Integral)*8, "bit count larger than size of the integral type"); /* According to https://www.opengl.org/registry/specs/EXT/texture_snorm.txt */ return Math::max(value/FloatingPoint(Implementation::bitMax()), FloatingPoint(-1.0)); } template FloatingPoint unpack(const Vector& value) { static_assert(FloatingPoint::Size == size, "return vector type should have the same size as input vector type"); FloatingPoint out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = unpack(value[i]); return out; } #endif /** @overload */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline FloatingPoint unpack(const Integral& value); #else template inline typename std::enable_if::value, FloatingPoint>::type unpack(const Integral& value) { return unpack(value); } template inline FloatingPoint unpack(const Vector& value) { return unpack(value); } #endif #ifdef MAGNUM_BUILD_DEPRECATED /** @copybrief unpack() * @deprecated Use @ref unpack() instead. */ template CORRADE_DEPRECATED("use unpack() instead") inline FloatingPoint normalize(const Integral& value) { return unpack(value); } #endif /** @brief Pack floating-point value into an integer representation Converts floating-point value in range @f$ [0, 1] @f$ to full range of given *unsigned* integral type or range @f$ [-1, 1] @f$ to full range of given *signed* integral type. @note For best precision, `FloatingPoint` type should be always larger that resulting `Integral` type (e.g. @ref Magnum::Float "Float" to @ref Magnum::Short "Short", @ref Magnum::Double "Double" to @ref Magnum::Int "Int" and similarly for vector types). @attention Return value for floating point numbers outside the normalized range is undefined. @see @ref unpack() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline Integral pack(const FloatingPoint& value); #else template inline typename std::enable_if::value, Integral>::type pack(FloatingPoint value) { static_assert(std::is_floating_point::value && std::is_integral::value, "packing must be done from floating-point to integral type"); static_assert(bits <= sizeof(Integral)*8, "bit count larger than size of the integral type"); return Integral(value*Implementation::bitMax()); } template Integral pack(const Vector& value) { static_assert(Integral::Size == size, "return vector type should have the same size as input vector type"); Integral out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = pack(value[i]); return out; } #endif /** @brief Pack floating-point value into integer bits Alternative to the above with ability to specify how many bits of the integral representation to use. Example usage: @code{.cpp} auto a = Math::pack(0.5f); // 32767 auto b = Math::pack(0.5f); // 8191 @endcode */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline Integral pack(FloatingPoint value); #else template inline typename std::enable_if::value, Integral>::type pack(FloatingPoint value) { return pack(value); } template inline Integral pack(const Vector& value) { return pack(value); } #endif #ifdef MAGNUM_BUILD_DEPRECATED /** @copybrief pack() * @deprecated Use @ref pack() instead. */ template CORRADE_DEPRECATED("use pack() instead") inline Integral denormalize(const FloatingPoint& value) { return pack(value); } #endif /** @brief Pack 32-bit float value into 16-bit half-float representation See [Wikipedia](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) for more information about half floats. NaNs are converted to NaNs and infinities to infinities, though their exact bit pattern is not preserved. Note that rounding mode is unspecified in order to save some cycles. Implementation based on CC0 / public domain code by *Fabian Giesen*, https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/ . @see @ref unpackHalf(), @ref Half */ MAGNUM_EXPORT UnsignedShort packHalf(Float value); /** @overload */ template Vector packHalf(const Vector& value) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = packHalf(value[i]); return out; } /** @brief Unpack 16-bit half-float value into 32-bit float representation See [Wikipedia](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) for more information about half floats. NaNs are converted to NaNs and infinities to infinities, though their exact bit pattern is not preserved. Implementation based on CC0 / public domain code by *Fabian Giesen*, https://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/ . @see @ref packHalf() */ MAGNUM_EXPORT Float unpackHalf(UnsignedShort value); /** @overload */ template Vector unpackHalf(const Vector& value) { Vector out{NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = unpackHalf(value[i]); return out; } }} #endif