Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/jxr/image/decode/decode.c
4393 views
1
//*@@@+++@@@@******************************************************************
2
//
3
// Copyright © Microsoft Corp.
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
//
9
// • Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// • Redistributions in binary form must reproduce the above copyright notice,
12
// this list of conditions and the following disclaimer in the documentation
13
// and/or other materials provided with the distribution.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
// POSSIBILITY OF SUCH DAMAGE.
26
//
27
//*@@@---@@@@******************************************************************
28
/******************************************************************************
29
30
Module Name:
31
decode.c
32
33
Abstract:
34
Defines the entry point for the console application.
35
36
Author:
37
38
Revision History:
39
*******************************************************************************/
40
#include "strcodec.h"
41
#include "decode.h"
42
43
#ifdef MEM_TRACE
44
#define TRACE_MALLOC 1
45
#define TRACE_NEW 0
46
#define TRACE_HEAP 0
47
#include "memtrace.h"
48
#endif
49
50
/******************************************************************
51
Free Adaptive Huffman Table
52
******************************************************************/
53
static Void CleanAH(CAdaptiveHuffman **ppAdHuff)
54
{
55
CAdaptiveHuffman *pAdHuff;
56
57
if (NULL != ppAdHuff) {
58
pAdHuff = *ppAdHuff;
59
if (NULL != pAdHuff) {
60
free(pAdHuff);
61
}
62
*ppAdHuff = NULL;
63
}
64
}
65
66
static Void CleanAHDec(CCodingContext * pSC)
67
{
68
Int kk;
69
70
for (kk = 0; kk < NUMVLCTABLES; kk++) {
71
CleanAH(&(pSC->m_pAHexpt[kk]));
72
}
73
CleanAH(&(pSC->m_pAdaptHuffCBPCY));
74
CleanAH(&(pSC->m_pAdaptHuffCBPCY1));
75
}
76
77
/*************************************************************************
78
Initialize an adaptive huffman table
79
*************************************************************************/
80
static Int InitializeAH(CAdaptiveHuffman **ppAdHuff, Int iSym)
81
{
82
Int iMemStatus = 0;
83
84
CAdaptiveHuffman *pAdHuff = Allocate(iSym, DECODER);
85
if (pAdHuff == NULL) {
86
iMemStatus = -1; // out of memory
87
goto ErrorExit;
88
}
89
90
//Adapt(pAdHuff, bFixedTables);
91
//InitHuffman(pAdHuff->m_pHuffman);
92
//if (ICERR_OK != initHuff(pAdHuff->m_pHuffman, 1, pAdHuff->m_pTable, NULL)) {
93
// goto ErrorExit;
94
//}
95
*ppAdHuff = pAdHuff;
96
return ICERR_OK;
97
98
ErrorExit:
99
if (pAdHuff) {
100
free(pAdHuff);
101
}
102
*ppAdHuff = NULL;
103
if (-1 == iMemStatus) {
104
printf("Insufficient memory to init decoder.\n");
105
}
106
return ICERR_ERROR;
107
}
108
109
110
/*************************************************************************
111
Context allocation
112
*************************************************************************/
113
Int AllocateCodingContextDec(CWMImageStrCodec *pSC, Int iNumContexts)
114
{
115
Int i, iCBPSize, k;
116
static const Int aAlphabet[] = {5,4,8,7,7, 12,6,6,12,6,6,7,7, 12,6,6,12,6,6,7,7};
117
118
if (iNumContexts > MAX_TILES || iNumContexts < 1) // only between 1 and MAX_TILES allowed
119
return ICERR_ERROR;
120
121
if (pSC == NULL)
122
return ICERR_ERROR;
123
124
pSC->m_pCodingContext = malloc (iNumContexts * sizeof (CCodingContext));
125
if (pSC->m_pCodingContext == NULL) {
126
pSC->cNumCodingContext = 0;
127
return ICERR_ERROR;
128
}
129
memset (pSC->m_pCodingContext, 0, iNumContexts * sizeof (CCodingContext));
130
131
pSC->cNumCodingContext = iNumContexts;
132
iCBPSize = (pSC->m_param.cfColorFormat == Y_ONLY || pSC->m_param.cfColorFormat == NCOMPONENT
133
|| pSC->m_param.cfColorFormat == CMYK) ? 5 : 9;
134
135
/** allocate / initialize members **/
136
for (i = 0; i < iNumContexts; i++) {
137
CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
138
139
/** allocate adaptive Huffman encoder **/
140
if (InitializeAH(&pContext->m_pAdaptHuffCBPCY, iCBPSize) != ICERR_OK) {
141
return ICERR_ERROR;
142
}
143
if (InitializeAH(&pContext->m_pAdaptHuffCBPCY1, 5) != ICERR_OK) {
144
return ICERR_ERROR;
145
}
146
147
for(k = 0; k < NUMVLCTABLES; k ++){
148
if (InitializeAH(&pContext->m_pAHexpt[k], aAlphabet[k]) != ICERR_OK) {
149
return ICERR_ERROR;
150
}
151
}
152
153
ResetCodingContextDec(pContext);
154
}
155
156
return ICERR_OK;
157
}
158
159
/*************************************************************************
160
Context reset on encoder
161
*************************************************************************/
162
Void ResetCodingContextDec(CCodingContext *pContext)
163
{
164
Int k;
165
/** set flags **/
166
pContext->m_pAdaptHuffCBPCY->m_bInitialize = FALSE;
167
pContext->m_pAdaptHuffCBPCY1->m_bInitialize = FALSE;
168
for(k = 0; k < NUMVLCTABLES; k ++)
169
pContext->m_pAHexpt[k]->m_bInitialize = FALSE;
170
171
// reset VLC tables
172
AdaptLowpassDec (pContext);
173
AdaptHighpassDec (pContext);
174
175
// reset zigzag patterns, totals
176
InitZigzagScan(pContext);
177
// reset bit reduction and cbp models
178
ResetCodingContext(pContext);
179
}
180
181
/*************************************************************************
182
Context deletion
183
*************************************************************************/
184
Void FreeCodingContextDec(CWMImageStrCodec *pSC)
185
{
186
Int iContexts = (Int)(pSC->cNumCodingContext), i, k;
187
188
if (iContexts > 0 && pSC->m_pCodingContext) {
189
190
for (i = 0; i < iContexts; i++) {
191
CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
192
CleanAH (&pContext->m_pAdaptHuffCBPCY);
193
CleanAH (&pContext->m_pAdaptHuffCBPCY1);
194
for (k = 0; k < NUMVLCTABLES; k++)
195
CleanAH (&pContext->m_pAHexpt[k]);
196
}
197
free (pSC->m_pCodingContext);
198
}
199
}
200
201
202