Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/jxr/image/encode/encode.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
#include <stdio.h>
30
#include <stdlib.h>
31
#include "encode.h"
32
#include "strcodec.h"
33
#include "common.h"
34
35
#ifdef MEM_TRACE
36
#define TRACE_MALLOC 1
37
#define TRACE_NEW 0
38
#define TRACE_HEAP 0
39
#include "memtrace.h"
40
#endif
41
42
/*************************************************************************
43
Context allocation
44
In theory it is possible to independently set uiTrimFlexBits for
45
each tile, but for now we assume only one user specified value is
46
used for the entire image
47
*************************************************************************/
48
Int AllocateCodingContextEnc(CWMImageStrCodec *pSC, Int iNumContexts, Int iTrimFlexBits)
49
{
50
Int i, iCBPSize, k;
51
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};
52
53
if (iTrimFlexBits < 0)
54
iTrimFlexBits = 0;
55
else if (iTrimFlexBits > 15)
56
iTrimFlexBits = 15;
57
pSC->m_param.bTrimFlexbitsFlag = (iTrimFlexBits > 0);
58
59
if (iNumContexts < 1 || iNumContexts > MAX_TILES) // only between 1 and 256 allowed
60
return ICERR_ERROR;
61
62
if (pSC == NULL)
63
return ICERR_ERROR;
64
65
pSC->m_pCodingContext = malloc (iNumContexts * sizeof (CCodingContext));
66
if (pSC->m_pCodingContext == NULL) {
67
pSC->cNumCodingContext = 0;
68
return ICERR_ERROR;
69
}
70
memset (pSC->m_pCodingContext, 0, iNumContexts * sizeof (CCodingContext));
71
72
pSC->cNumCodingContext = iNumContexts;
73
iCBPSize = (pSC->m_param.cfColorFormat == Y_ONLY || pSC->m_param.cfColorFormat == NCOMPONENT
74
|| pSC->m_param.cfColorFormat == CMYK) ? 5 : 9;
75
76
/** allocate / initialize members **/
77
for (i = 0; i < iNumContexts; i++) {
78
CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
79
80
/** allocate adaptive Huffman encoder **/
81
pContext->m_pAdaptHuffCBPCY = Allocate (iCBPSize, ENCODER);
82
if(pContext->m_pAdaptHuffCBPCY == NULL) {
83
return ICERR_ERROR;
84
}
85
pContext->m_pAdaptHuffCBPCY1 = Allocate(5, ENCODER);
86
if(pContext->m_pAdaptHuffCBPCY1 == NULL){
87
return ICERR_ERROR;
88
}
89
90
for(k = 0; k < NUMVLCTABLES; k ++){
91
pContext->m_pAHexpt[k] = Allocate(aAlphabet[k], ENCODER);
92
if(pContext->m_pAHexpt[k] == NULL){
93
return ICERR_ERROR;
94
}
95
}
96
97
ResetCodingContextEnc(pContext);
98
pContext->m_iTrimFlexBits = iTrimFlexBits;
99
}
100
101
return ICERR_OK;
102
}
103
104
/*************************************************************************
105
Context reset on encoder
106
*************************************************************************/
107
Void ResetCodingContextEnc(CCodingContext *pContext)
108
{
109
Int k;
110
/** set flags **/
111
pContext->m_pAdaptHuffCBPCY->m_bInitialize = FALSE;
112
pContext->m_pAdaptHuffCBPCY1->m_bInitialize = FALSE;
113
for(k = 0; k < NUMVLCTABLES; k ++)
114
pContext->m_pAHexpt[k]->m_bInitialize = FALSE;
115
116
// reset VLC tables
117
AdaptLowpassEnc (pContext);
118
AdaptHighpassEnc (pContext);
119
120
// reset zigzag patterns, totals
121
InitZigzagScan(pContext);
122
// reset bit reduction and cbp models
123
ResetCodingContext(pContext);
124
}
125
126
/*************************************************************************
127
Context deletion
128
*************************************************************************/
129
Void FreeCodingContextEnc(CWMImageStrCodec *pSC)
130
{
131
Int iContexts = (Int)(pSC->cNumCodingContext), i, k;
132
if (iContexts > 0 && pSC->m_pCodingContext) {
133
134
for (i = 0; i < iContexts; i++) {
135
CCodingContext *pContext = &(pSC->m_pCodingContext[i]);
136
Clean (pContext->m_pAdaptHuffCBPCY);
137
Clean (pContext->m_pAdaptHuffCBPCY1);
138
for (k = 0; k < NUMVLCTABLES; k++)
139
Clean (pContext->m_pAHexpt[k]);
140
}
141
free (pSC->m_pCodingContext);
142
}
143
}
144
145
146