-
Notifications
You must be signed in to change notification settings - Fork 0
/
VQImage8x8x16x15.cpp
executable file
·313 lines (253 loc) · 8.77 KB
/
VQImage8x8x16x15.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#pragma inline_depth(255)
#pragma warning(disable: 4244 4018)
#include <algorithm>
#include <functional>
#include <utility>
#include <list>
#include <memory>
#include <process.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "VQImage8x8x16x15.h"
#include "GLAQuant.h"
#include "QuantVect.h"
// 128 prime numbers used for pseudorandom traversal of image, needed for Neuquant
static DWORD g_dwPrimes[128] = {
1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027,
2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141,
2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281,
2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389,
2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539,
2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671,
2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767,
2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897,
2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037,
3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319,
3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457,
3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559,
3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691,
3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823,
3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943
};
static CVQImage8x8x16x15 *gpCur;
static CVBitmap *gpCurSrc;
static void (*gpCurOnPass)(int);
int WINAPI MainCompress8x8x16x15(HINSTANCE hInstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow);
CVQImage8x8x16x15::CVQImage8x8x16x15()
:m_pBits(0), m_pCodebook(0),pCurGlaq(0), m_bReadyToDraw(0)
{
}
CVQImage8x8x16x15::~CVQImage8x8x16x15()
{
delete m_pBits;
delete m_pCodebook;
}
HRESULT CVQImage8x8x16x15::Create(
CVBitmap *pSrc,
unsigned char bThreaded,
unsigned long dwCBSize1,
unsigned long dwCBSize2,
unsigned long dwBlockW,
unsigned long dwBlockH,
void (*OnPass)(int))
{
m_dwQuantSizeX = 1;
m_dwQuantSizeY = 1;
m_dwSizeofQuant = 1;
gpCur = this;
gpCur->m_dwCBSize1 = dwCBSize1;
gpCur->m_dwCBSize2 = dwCBSize2;
gpCurSrc = pSrc;
gpCurOnPass = OnPass;
m_dwBlocW = dwBlockW;
m_dwBlocH = dwBlockH;
m_allChopedSrc= new CVBitmap[((pSrc->m_dwWidth+(m_dwBlocW-1)) / m_dwBlocW)*((pSrc->m_dwHeight+(m_dwBlocH-1)) / m_dwBlocH)];
m_allChopedRes= new CVQImage[((pSrc->m_dwWidth+(m_dwBlocW-1)) / m_dwBlocW)*((pSrc->m_dwHeight+(m_dwBlocH-1)) / m_dwBlocH)];
if(bThreaded){
_beginthread((void (__cdecl *)(void *))MainCompress8x8x16x15,0,NULL);
}else{
CreateQuant8x8x16x15(gpCurSrc, gpCurOnPass); //Start With only quantize palettes
}
// MainCompress8x8x16x15(0, 0, 0, 0);
return S_OK;
}
HRESULT CVQImage8x8x16x15::CreateQuant8x8x16x15(const CVBitmap *pSrc, void (*OnPass)(int))
{
int i,j, k, l;
m_dwWidth = pSrc->GetWidth();
m_dwWidthQ = pSrc->GetWidth() / m_dwQuantSizeX;
m_dwHeight = pSrc->GetHeight();
m_dwHeightQ = pSrc->GetHeight() / m_dwQuantSizeY;
m_bReadyToDraw= TRUE;
for(j=0; j < (m_dwHeight/m_dwBlocW); ++j){
for(i=0; i < (m_dwWidth/m_dwBlocW); ++i){
m_allChopedSrc[j*(m_dwWidth/m_dwBlocW)+i].Create(m_dwBlocW, m_dwBlocW);
}
}
for(j=0; j < (m_dwHeight/m_dwBlocH); ++j){
for(i=0; i < (m_dwWidth/m_dwBlocW); ++i){
for(k=0; k < m_dwBlocH; ++k){
for(l=0; l < m_dwBlocW; ++l){
m_allChopedSrc[j*(m_dwWidth/m_dwBlocW)+i].m_pData[k*m_dwBlocW+l]= pSrc->m_pData[((j*m_dwBlocH)+k)*pSrc->m_dwWidth+((i*m_dwBlocW)+l)];
}
}
}
}
for(j=0; j < (m_dwHeight/m_dwBlocH); ++j){
for(i=0; i < (m_dwWidth/m_dwBlocW); ++i){
m_allChopedRes[j*(m_dwWidth/m_dwBlocW)+i].Create(&m_allChopedSrc[j*(m_dwWidth/m_dwBlocW)+i], 0, m_dwCBSize1, OnPass);
while(!m_allChopedRes[j*(m_dwWidth/m_dwBlocW)+i].m_bFinished);
}
}
delete m_pCodebook;
RGBTRIPLE *pCodebook;
m_pCodebook = pCodebook = new RGBTRIPLE[m_dwCBSize1*m_dwCBSize2];
unsigned long x,y;
// get image data into TQuant array for easier pseudorandom walk
TQuant_Screen *pQuants= new TQuant_Screen[(m_dwWidth/m_dwBlocW)*(m_dwHeight/m_dwBlocH)];
TQuant_Screen *pQuant = pQuants;
CVQImage *pImage = m_allChopedRes;
for(y=0; y < (m_dwHeight/m_dwBlocH); y++){
for(x=0; x < (m_dwWidth/m_dwBlocW); x++){
for(i=0; i < m_dwCBSize1; ++i){
pQuant->data[i*3+0] = float(((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtRed);
pQuant->data[i*3+1] = float(((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtGreen);
pQuant->data[i*3+2] = float(((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtBlue);
}
++pQuant;
++pImage;
}
}
unsigned long dwPass;
GLAQuant<TQuant_Screen> glaq(m_dwCBSize2);
pCurGlaq= &glaq;
glaq.Init();
dwPass = 0;
unsigned long dwUpdated = 0;
do {
// feed the entire image through the quantizer
unsigned long dwIdx;
for(dwIdx= 0;dwIdx < (m_dwWidth/m_dwBlocW)*(m_dwHeight/m_dwBlocH); dwIdx++) {
glaq.Feed( pQuants[dwIdx] );
}
// let the quantizer know it's over
glaq.UpdateCB();
// exit conditions
if( glaq.m_dwUpdated == 0 ) {
break;
}
}while( dwPass < 128 );
/*
// read codebook from quantizer
for(unsigned long dwCode = 0; dwCode < dwCBSize; dwCode++) {
QuantToPixels(glaq[dwCode], &pCodebook[dwCode * m_dwQuantSizeX * m_dwQuantSizeY], 1, 1);
}
// quantize image
for(x=0; x<m_dwWidthQ*m_dwHeightQ; x++) {
pBits[x] = glaq.GetIndex(pQuants[x]);
}
*/
// read codebook from quantizer
for(j=0; j < m_dwCBSize2; ++j){
for(i=0; i < m_dwCBSize1; ++i){
pCodebook[j*m_dwCBSize1+i].rgbtRed = glaq[j].data[i*3+0];
pCodebook[j*m_dwCBSize1+i].rgbtGreen= glaq[j].data[i*3+1];
pCodebook[j*m_dwCBSize1+i].rgbtBlue = glaq[j].data[i*3+2];
}
}
pImage = m_allChopedRes;
for(y=0; y < (m_dwHeight/m_dwBlocH); y++){
for(x=0; x < (m_dwWidth/m_dwBlocW); x++){
TQuant_Screen tempVec;
for(i=0; i < m_dwCBSize1; ++i){
tempVec.data[i*3+0] = float(((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtRed);
tempVec.data[i*3+1] = float(((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtGreen);
tempVec.data[i*3+2] = float(((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtBlue);
}
long newIdx= glaq.GetIndex(tempVec);
for(i=0; i < m_dwCBSize1; ++i){
((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtRed =pCodebook[newIdx*m_dwCBSize1+i].rgbtRed;
((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtGreen =pCodebook[newIdx*m_dwCBSize1+i].rgbtGreen;
((RGBTRIPLE*)pImage->m_pCodebook)[i].rgbtBlue =pCodebook[newIdx*m_dwCBSize1+i].rgbtBlue;
}
++pImage;
}
}
pCurGlaq= 0;
return S_OK;
}
HRESULT CVQImage8x8x16x15::Decompress8x8x16x15(CVBitmap *& pDest)
{
pDest = new CVBitmap();
pDest->Create(m_dwWidthQ * m_dwQuantSizeX, m_dwHeightQ * m_dwQuantSizeY);
int x,y;
unsigned char *pBits = (unsigned char *)m_pBits;
RGBTRIPLE *pDestBits = pDest->GetData();
for(y=0; y<m_dwHeightQ; y++) {
for(x=0; x<m_dwWidthQ; x++) {
memcpy(pDestBits, (RGBTRIPLE *)m_pCodebook + (*pBits) * m_dwQuantSizeX * m_dwQuantSizeY, m_dwQuantSizeX * sizeof(RGBTRIPLE));
memcpy(pDestBits+(pDest->GetWidth()), ((RGBTRIPLE *)m_pCodebook + (*pBits) * m_dwQuantSizeX * m_dwQuantSizeY) + m_dwQuantSizeX, m_dwQuantSizeX * sizeof(RGBTRIPLE));
pBits++;
pDestBits += m_dwQuantSizeX;
}
pDestBits += pDest->GetWidth();
}
return S_OK;
}
// ---- load/save stuff
#pragma pack(push)
#pragma pack(4)
// this header potentially supports "future extensions" of the format
struct TVQImage8x8x16x15Hdr {
unsigned long dwMagic;
unsigned long dwCBSize1;
unsigned long dwCBSize2;
unsigned long dwSizeofCode;
unsigned long dwSizeofPixel;
unsigned long dwQuantSizeX, dwQuantSizeY;
unsigned long dwWidth, dwHeight;
unsigned long dwWidthQ, dwHeightQ;
static unsigned long s_dwMagic;
TVQImage8x8x16x15Hdr() {}
TVQImage8x8x16x15Hdr(const CVQImage8x8x16x15 *pBmp) {
dwMagic = TVQImage8x8x16x15Hdr::s_dwMagic;
dwCBSize1 = pBmp->m_dwCBSize1;
dwCBSize2 = pBmp->m_dwCBSize2;
dwSizeofCode = 1;
dwSizeofPixel = 1;
dwWidth = pBmp->m_dwWidth;
dwWidthQ = pBmp->m_dwWidthQ;
dwHeight = pBmp->m_dwHeight;
dwHeightQ = pBmp->m_dwHeightQ;
dwQuantSizeX = pBmp->m_dwQuantSizeX;
dwQuantSizeY = pBmp->m_dwQuantSizeY;
}
};
unsigned long TVQImage8x8x16x15Hdr::s_dwMagic = 'vqbm';
#pragma pack(pop)
// load/save functions from pathname...
HRESULT SaveVQImage(CVFile *pFile, const CVQImage8x8x16x15 * pBmp)
{
return S_OK;
}
HRESULT LoadVQImage(CVFile *pFile, CVQImage8x8x16x15 *& pBmp)
{
return S_OK;
}
HRESULT LoadVQImage(const char *pchFilename, CVQImage8x8x16x15 *& pBmp)
{
return S_OK;
}
HRESULT SaveVQImage(const char *pchFilename, const CVQImage8x8x16x15 * pBmp)
{
return S_OK;
}
// ...and to/from CVFile*
int WINAPI
MainCompress8x8x16x15(HINSTANCE hInstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
gpCur->CreateQuant8x8x16x15(gpCurSrc, gpCurOnPass); //Start With only quantize palettes
return 1;
}