Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/LibJXR/jxrgluelib/JXRGluePFC.c  view on Meta::CPAN

//*@@@+++@@@@******************************************************************
//
// Copyright © Microsoft Corp.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 
// • Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// • Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//*@@@---@@@@******************************************************************
#include <stdlib.h>

#include <JXRGlue.h>
#include <math.h>

//================================================================
// PKFormatConverter
//================================================================
#define HLF_MIN 0.00006103515625f
#define HLF_MAX 65504.0f

#define HLF_MIN_BITS 0x0400
#define HLF_MAX_BITS 0x7bff

#define HLF_MIN_BITS_NEG (HLF_MIN_BITS | 0x8000)
#define HLF_MAX_BITS_NEG (HLF_MAX_BITS | 0x8000)

#define HLF_QNaN_BITZS 0x7fff

// simple and slow implementation of half <-> float conversion
static U32 Convert_Half_To_Float(U16 u16)
{
    // 1s5e10m -> 1s8e23m
    const U32 s = (u16 >> 15) & 0x0001;
    const U32 e = (u16 >> 10) & 0x001f;
    const U32 m = (u16 >>  0) & 0x03ff;

    if (0 == e) // 0, denorm
    {
        return s << 31;
    }
    else if (~(~0 << 5) == e) // inf, snan, qnan
    {
        return (s << 31) | ~(~0 << 8) << 23| (m << 13);
    }

    return (s << 31) | ((e - 15 + 127) << 23) | (m << 13); // norm
}


static U16 Convert_Float_To_Half(float f)
{
    // 1s5e10m -> 1s8e23m
    const U32 iFloat = *(U32*)&f; // Convert float to U32

    if (f != f)
    {
        return (U16)(iFloat | HLF_QNaN_BITZS); // +QNaN, -QNaN
    }
    else if (f < -HLF_MAX)
    {
        return HLF_MAX_BITS_NEG;
    }
    else if (HLF_MAX < f)
    {
        return HLF_MAX_BITS;
    }
    else if (-HLF_MIN < f && f < HLF_MIN)
    {
        return (U16)((iFloat >> 16) & 0x8000); // +0, -0
    }

    // Cut-and-paste from C++, introduce scope so we can decl more vars
    {
    const U32 s = (iFloat >> 31) & 0x00000001;
    const U32 e = (iFloat >> 23) & 0x000000ff;
    const U32 m = (iFloat >>  0) & 0x007fffff;

    return (U16) ((s << 15) | ((e - 127 + 15) << 10) | (m >> 13));
    }
}


static U8 Convert_Float_To_U8(float f)
{
    // convert from linear scRGB to non-linear sRGB
    if (f <= 0)
    {
        return 0;
    }
    else if (f <= 0.0031308f)
    {
        return (U8)((255.0f * f * 12.92f) + 0.5f);
    }
    else if (f < 1.0f)
    {
        return (U8)((255.0f * ((1.055f * (float)pow(f, 1.0f / 2.4f)) - 0.055f)) + 0.5f);
    }
    else
    {
        return 255;
    }
}

static U8 Convert_AlphaFloat_To_U8(float f)
{
    // alpha is converted differently than RGB in scRGB
    if (f <= 0)
    {
        return 0;
    }
    else if (f < 1.0f)
    {
        return (U8)((255.0f * f) + 0.5f);
    }
    else
    {
        return 255;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.504 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )