//*@@@+++@@@@******************************************************************1//2// Copyright © Microsoft Corp.3// All rights reserved.4//5// Redistribution and use in source and binary forms, with or without6// modification, are permitted provided that the following conditions are met:7//8// • Redistributions of source code must retain the above copyright notice,9// this list of conditions and the following disclaimer.10// • Redistributions in binary form must reproduce the above copyright notice,11// this list of conditions and the following disclaimer in the documentation12// and/or other materials provided with the distribution.13//14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"15// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE18// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR19// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF20// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN22// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)23// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24// POSSIBILITY OF SUCH DAMAGE.25//26//*@@@---@@@@******************************************************************27/******************************************************************************2829Module Name:30decode.c3132Abstract:33Defines the entry point for the console application.3435Author:3637Revision History:38*******************************************************************************/39#include "strcodec.h"40#include "decode.h"4142#ifdef MEM_TRACE43#define TRACE_MALLOC 144#define TRACE_NEW 045#define TRACE_HEAP 046#include "memtrace.h"47#endif4849/******************************************************************50Free Adaptive Huffman Table51******************************************************************/52static Void CleanAH(CAdaptiveHuffman **ppAdHuff)53{54CAdaptiveHuffman *pAdHuff;5556if (NULL != ppAdHuff) {57pAdHuff = *ppAdHuff;58if (NULL != pAdHuff) {59free(pAdHuff);60}61*ppAdHuff = NULL;62}63}6465static Void CleanAHDec(CCodingContext * pSC)66{67Int kk;6869for (kk = 0; kk < NUMVLCTABLES; kk++) {70CleanAH(&(pSC->m_pAHexpt[kk]));71}72CleanAH(&(pSC->m_pAdaptHuffCBPCY));73CleanAH(&(pSC->m_pAdaptHuffCBPCY1));74}7576/*************************************************************************77Initialize an adaptive huffman table78*************************************************************************/79static Int InitializeAH(CAdaptiveHuffman **ppAdHuff, Int iSym)80{81Int iMemStatus = 0;8283CAdaptiveHuffman *pAdHuff = Allocate(iSym, DECODER);84if (pAdHuff == NULL) {85iMemStatus = -1; // out of memory86goto ErrorExit;87}8889//Adapt(pAdHuff, bFixedTables);90//InitHuffman(pAdHuff->m_pHuffman);91//if (ICERR_OK != initHuff(pAdHuff->m_pHuffman, 1, pAdHuff->m_pTable, NULL)) {92// goto ErrorExit;93//}94*ppAdHuff = pAdHuff;95return ICERR_OK;9697ErrorExit:98if (pAdHuff) {99free(pAdHuff);100}101*ppAdHuff = NULL;102if (-1 == iMemStatus) {103printf("Insufficient memory to init decoder.\n");104}105return ICERR_ERROR;106}107108109/*************************************************************************110Context allocation111*************************************************************************/112Int AllocateCodingContextDec(CWMImageStrCodec *pSC, Int iNumContexts)113{114Int i, iCBPSize, k;115static const Int aAlphabet[] = {5,4,8,7,7, 12,6,6,12,6,6,7,7, 12,6,6,12,6,6,7,7};116117if (iNumContexts > MAX_TILES || iNumContexts < 1) // only between 1 and MAX_TILES allowed118return ICERR_ERROR;119120if (pSC == NULL)121return ICERR_ERROR;122123pSC->m_pCodingContext = malloc (iNumContexts * sizeof (CCodingContext));124if (pSC->m_pCodingContext == NULL) {125pSC->cNumCodingContext = 0;126return ICERR_ERROR;127}128memset (pSC->m_pCodingContext, 0, iNumContexts * sizeof (CCodingContext));129130pSC->cNumCodingContext = iNumContexts;131iCBPSize = (pSC->m_param.cfColorFormat == Y_ONLY || pSC->m_param.cfColorFormat == NCOMPONENT132|| pSC->m_param.cfColorFormat == CMYK) ? 5 : 9;133134/** allocate / initialize members **/135for (i = 0; i < iNumContexts; i++) {136CCodingContext *pContext = &(pSC->m_pCodingContext[i]);137138/** allocate adaptive Huffman encoder **/139if (InitializeAH(&pContext->m_pAdaptHuffCBPCY, iCBPSize) != ICERR_OK) {140return ICERR_ERROR;141}142if (InitializeAH(&pContext->m_pAdaptHuffCBPCY1, 5) != ICERR_OK) {143return ICERR_ERROR;144}145146for(k = 0; k < NUMVLCTABLES; k ++){147if (InitializeAH(&pContext->m_pAHexpt[k], aAlphabet[k]) != ICERR_OK) {148return ICERR_ERROR;149}150}151152ResetCodingContextDec(pContext);153}154155return ICERR_OK;156}157158/*************************************************************************159Context reset on encoder160*************************************************************************/161Void ResetCodingContextDec(CCodingContext *pContext)162{163Int k;164/** set flags **/165pContext->m_pAdaptHuffCBPCY->m_bInitialize = FALSE;166pContext->m_pAdaptHuffCBPCY1->m_bInitialize = FALSE;167for(k = 0; k < NUMVLCTABLES; k ++)168pContext->m_pAHexpt[k]->m_bInitialize = FALSE;169170// reset VLC tables171AdaptLowpassDec (pContext);172AdaptHighpassDec (pContext);173174// reset zigzag patterns, totals175InitZigzagScan(pContext);176// reset bit reduction and cbp models177ResetCodingContext(pContext);178}179180/*************************************************************************181Context deletion182*************************************************************************/183Void FreeCodingContextDec(CWMImageStrCodec *pSC)184{185Int iContexts = (Int)(pSC->cNumCodingContext), i, k;186187if (iContexts > 0 && pSC->m_pCodingContext) {188189for (i = 0; i < iContexts; i++) {190CCodingContext *pContext = &(pSC->m_pCodingContext[i]);191CleanAH (&pContext->m_pAdaptHuffCBPCY);192CleanAH (&pContext->m_pAdaptHuffCBPCY1);193for (k = 0; k < NUMVLCTABLES; k++)194CleanAH (&pContext->m_pAHexpt[k]);195}196free (pSC->m_pCodingContext);197}198}199200201202