Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/LibJXR/image/sys/strPredQuant.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 "strcodec.h"

#define ORIENT_WEIGHT 4

/* reciprocal (pMantissa, exponent) lookup table */
typedef struct tagQPManExp
{
    int iMan;
    int iExp;
} QPManExp;

static QPManExp gs_QPRecipTable[32] = {
    {0x0, 0}, // 0, invalid
    {0x0, 0}, // 1, lossless
    {0x0, 1}, // 2
    {0xaaaaaaab, 1},
    {0x0, 2}, // 4
    {0xcccccccd, 2},
    {0xaaaaaaab, 2},
    {0x92492493, 2},
    {0x0, 3}, // 8
    {0xe38e38e4, 3},
    {0xcccccccd, 3},
    {0xba2e8ba3, 3},
    {0xaaaaaaab, 3},
    {0x9d89d89e, 3},
    {0x92492493, 3},
    {0x88888889, 3},
    {0x0, 4}, // 16
    {0xf0f0f0f1, 4},
    {0xe38e38e4, 4},
    {0xd79435e6, 4},
    {0xcccccccd, 4},
    {0xc30c30c4, 4},
    {0xba2e8ba3, 4},
    {0xb21642c9, 4},
    {0xaaaaaaab, 4},
    {0xa3d70a3e, 4},
    {0x9d89d89e, 4},
    {0x97b425ee, 4},
    {0x92492493, 4},
    {0x8d3dcb09, 4},
    {0x88888889, 4},
    {0x84210843, 4},
};

/*************************************************************************
    QPRemapping
*************************************************************************/

Void remapQP(CWMIQuantizer * pQP, I32 iShift, Bool bScaledArith)
{
    U8 uiQPIndex = pQP->iIndex;

    if(uiQPIndex == 0) // Lossless mode!
        pQP->iQP = 1, pQP->iMan = pQP->iExp = pQP->iOffset = 0;
    else if (!bScaledArith) {
        I32 man = 0, exp = 0;
        const I32 ciShift = SHIFTZERO - (SHIFTZERO + QPFRACBITS); // == -QPFRACBITS

        if (pQP->iIndex < 32)
            man = (pQP->iIndex + 3) >> 2, exp = ciShift + 2;
        else if (pQP->iIndex < 48)
            man = (16 + (pQP->iIndex & 0xf) + 1) >> 1, exp = ((pQP->iIndex >> 4) - 1) + 1 + ciShift;
        else
            man = 16 + (pQP->iIndex & 0xf), exp = ((pQP->iIndex >> 4) - 1) + ciShift;

        pQP->iQP = man << exp;
        pQP->iMan = gs_QPRecipTable[man].iMan;
        pQP->iExp = gs_QPRecipTable[man].iExp + exp;
        pQP->iOffset = ((pQP->iQP * 3 + 1) >> 3);
#if defined(WMP_OPT_QT)
        pQP->f1_QP = 1.0f / pQP->iQP;
        pQP->d1_QP = 1.0 / pQP->iQP;
#endif    
    }
    else {
        I32 man = 0, exp = 0;

        if(pQP->iIndex < 16)
            man = pQP->iIndex, exp = iShift;
        else
            man = 16 + (pQP->iIndex & 0xf), exp = ((pQP->iIndex >> 4) - 1) + iShift;
        
        pQP->iQP = man << exp;
        pQP->iMan = gs_QPRecipTable[man].iMan;
        pQP->iExp = gs_QPRecipTable[man].iExp + exp;
        pQP->iOffset = ((pQP->iQP * 3 + 1) >> 3);
#if defined(WMP_OPT_QT)
        pQP->f1_QP = 1.0f / pQP->iQP;
        pQP->d1_QP = 1.0 / pQP->iQP;
#endif    
    }
}

/* allocate PredInfo buffers */
Int allocatePredInfo(CWMImageStrCodec *pSC)
{
    size_t i, j;
    // COLORFORMAT cf = pSC->m_param.cfColorFormat;
    const size_t mbWidth = pSC->cmbWidth;
    const size_t iChannels = pSC->m_param.cNumChannels;
    CWMIPredInfo* pMemory;
    Bool b32Bit = sizeof(size_t) == 4;
    
    if(b32Bit) // integer overlow/underflow check for 32-bit system
        if(((mbWidth >> 16) * iChannels * 2 * sizeof(CWMIPredInfo)) & 0xffff0000)
            return ICERR_ERROR;    
    pMemory = (CWMIPredInfo *)malloc(mbWidth * iChannels * 2 * sizeof(CWMIPredInfo));
    if (pMemory == NULL)
        return ICERR_ERROR;

    pSC->pPredInfoMemory = pMemory;
    for(i = 0; i < iChannels; i ++){
        pSC->PredInfo[i] = pMemory;



( run in 0.499 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )