Path: blob/master/3rdparty/openexr/IlmImf/ImfConvert.cpp
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////333435//-----------------------------------------------------------------------------36//37// Routines for converting between pixel data types,38// with well-defined behavior for exceptional cases.39//40//-----------------------------------------------------------------------------4142#include <ImfConvert.h>43#include <limits.h>4445namespace Imf {46namespace {4748inline bool49isNegative (float f)50{51union {float f; int i;} u;52u.f = f;53return (u.i & 0x80000000) != 0;54}555657inline bool58isNan (float f)59{60union {float f; int i;} u;61u.f = f;62return (u.i & 0x7fffffff) > 0x7f800000;63}646566inline bool67isInfinity (float f)68{69union {float f; int i;} u;70u.f = f;71return (u.i & 0x7fffffff) == 0x7f800000;72}737475inline bool76isFinite (float f)77{78union {float f; int i;} u;79u.f = f;80return (u.i & 0x7f800000) != 0x7f800000;81}8283} // namespace848586unsigned int87halfToUint (half h)88{89if (h.isNegative() || h.isNan())90return 0;9192if (h.isInfinity())93return UINT_MAX;9495return (unsigned int) h;96}979899unsigned int100floatToUint (float f)101{102if (isNegative (f) || isNan (f))103return 0;104105if (isInfinity (f) || f > UINT_MAX)106return UINT_MAX;107108return (unsigned int) f;109}110111112half113uintToHalf (unsigned int ui)114{115if (ui > HALF_MAX)116return half::posInf();117118return half (ui);119}120121122half123floatToHalf (float f)124{125if (isFinite (f))126{127if (f > HALF_MAX)128return half::posInf();129130if (f < -HALF_MAX)131return half::negInf();132}133134return half (f);135}136137138} // namespace Imf139140141