File: Quantizer.cpp

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (390 lines) | stat: -rw-r--r-- 9,765 bytes parent folder | download
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#include "EmulatorCommon.h"
#include "Quantizer.h"

static long PrvGetRowBytes (LPBITMAPINFO dib);

struct NODE
{
	Bool	bIsLeaf;		// TRUE if node has no children
	uae_u32 nPixelCount;	// Number of pixels represented by this leaf
	uae_u32 nRedSum;		// Sum of red components
	uae_u32 nGreenSum;		// Sum of green components
	uae_u32 nBlueSum;		// Sum of blue components
	NODE*	pChild[8];		// Pointers to child nodes
	NODE*	pNext;			// Pointer to next reducible node
};

CQuantizer::CQuantizer (uae_u32 nMaxColors, uae_u32 nColorBits)
{
	assert (nColorBits <= 8);

	m_pTree = NULL;
	m_nLeafCount = 0;
	for (int i=0; i<=(int) nColorBits; i++)
		m_pReducibleNodes[i] = NULL;
	m_nMaxColors = nMaxColors;
	m_nColorBits = nColorBits;
}

CQuantizer::~CQuantizer ()
{
	if (m_pTree != NULL)
		DeleteTree (&m_pTree);
}

Bool CQuantizer::ProcessImage (/*HANDLE hImage*/LPBITMAPINFO bm, void* bits)
{
	uae_u32 rmask, gmask, bmask;
	int rright, gright, bright;
	int rleft, gleft, bleft;
	uae_u8* pbBits;
	uae_u32* pwBits;
	uae_u32* pdwBits;
	uae_u8 r, g, b;
	uae_u32 wColor;
	uae_u32 dwColor;
	int i, j;
	HDC hdc;
	uae_u8* pBuffer;
	BITMAPINFO bmi;

#if 0
	DIBSECTION ds;
	::GetObject (hImage, sizeof (ds), &ds);
	int nPad = ds.dsBm.bmWidthBytes - (((ds.dsBmih.biWidth *
		ds.dsBmih.biBitCount) + 7) / 8);
#else
	int nPad = ::PrvGetRowBytes (bm) - (((bm->bmiHeader.biWidth *
		bm->bmiHeader.biBitCount) + 7) / 8);
#endif

#if 0
   switch (ds.dsBmih.biBitCount) {
#else
   switch (bm->bmiHeader.biBitCount) {
#endif

#if 0
	case 1: // 1-bit DIB
	case 4: // 4-bit DIB
	case 8: // 8-bit DIB
		//
		// The strategy here is to use ::GetDIBits to convert the
		// image into a 24-bit DIB one scan line at a time. A pleasant
		// side effect of using ::GetDIBits in this manner is that RLE-
		// encoded 4-bit and 8-bit DIBs will be uncompressed.
		//
		hdc = ::GetDC (NULL);
		pBuffer = new uae_u8[ds.dsBmih.biWidth * 3];

		::ZeroMemory (&bmi, sizeof (bmi));
		bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
		bmi.bmiHeader.biWidth = ds.dsBmih.biWidth;
		bmi.bmiHeader.biHeight = ds.dsBmih.biHeight;
		bmi.bmiHeader.biPlanes = 1;
		bmi.bmiHeader.biBitCount = 24;
		bmi.bmiHeader.biCompression = BI_RGB;

		for (i=0; i<ds.dsBmih.biHeight; i++) {
			::GetDIBits (hdc, (HBITMAP) hImage, i, 1, pBuffer, &bmi,
				DIB_RGB_COLORS);
			pbBits = pBuffer;
			for (j=0; j<ds.dsBmih.biWidth; j++) {
				b = *pbBits++;
				g = *pbBits++;
				r = *pbBits++;
				AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
					m_pReducibleNodes);
				while (m_nLeafCount > m_nMaxColors)
					ReduceTree (m_nColorBits, &m_nLeafCount,
						m_pReducibleNodes);
			}
		}

		delete pBuffer;
		::ReleaseDC (NULL, hdc);
		break;

	case 16: // 16-bit DIB
		if (ds.dsBmih.biCompression == BI_BITFIELDS) {
			rmask = ds.dsBitfields[0];
			gmask = ds.dsBitfields[1];
			bmask = ds.dsBitfields[2];
		}
		else {
			rmask = 0x7C00;
			gmask = 0x03E0;
			bmask = 0x001F;
		}

		rright = GetRightShiftCount (rmask);
		gright = GetRightShiftCount (gmask);
		bright = GetRightShiftCount (bmask);

		rleft = GetLeftShiftCount (rmask);
		gleft = GetLeftShiftCount (gmask);
		bleft = GetLeftShiftCount (bmask);

		pwBits = (uae_u32*) ds.dsBm.bmBits;
		for (i=0; i<ds.dsBmih.biHeight; i++) {
			for (j=0; j<ds.dsBmih.biWidth; j++) {
				wColor = *pwBits++;
				b = (uae_u8) (((wColor & (uae_u32) bmask) >> bright) << bleft);
				g = (uae_u8) (((wColor & (uae_u32) gmask) >> gright) << gleft);
				r = (uae_u8) (((wColor & (uae_u32) rmask) >> rright) << rleft);
				AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
					m_pReducibleNodes);
				while (m_nLeafCount > m_nMaxColors)
					ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
			}
			pwBits = (uae_u32*) (((uae_u8*) pwBits) + nPad);
		}
		break;
#endif

	case 24: // 24-bit DIB
#if 0
		pbBits = (uae_u8*) ds.dsBm.bmBits;
#else
		pbBits = (uae_u8*) bits;
#endif
#if 0
		for (i=0; i<ds.dsBmih.biHeight; i++) {
			for (j=0; j<ds.dsBmih.biWidth; j++) {
#else
		for (i=0; i<bm->bmiHeader.biHeight; i++) {
			for (j=0; j<bm->bmiHeader.biWidth; j++) {
#endif
				b = *pbBits++;
				g = *pbBits++;
				r = *pbBits++;
				AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
					m_pReducibleNodes);
				while (m_nLeafCount > m_nMaxColors)
					ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
			}
			pbBits += nPad;
		}
		break;

#if 0
	case 32: // 32-bit DIB
		if (ds.dsBmih.biCompression == BI_BITFIELDS) {
			rmask = ds.dsBitfields[0];
			gmask = ds.dsBitfields[1];
			bmask = ds.dsBitfields[2];
		}
		else {
			rmask = 0x00FF0000;
			gmask = 0x0000FF00;
			bmask = 0x000000FF;
		}

		rright = GetRightShiftCount (rmask);
		gright = GetRightShiftCount (gmask);
		bright = GetRightShiftCount (bmask);

		pdwBits = (uae_u32*) ds.dsBm.bmBits;
		for (i=0; i<ds.dsBmih.biHeight; i++) {
			for (j=0; j<ds.dsBmih.biWidth; j++) {
				dwColor = *pdwBits++;
				b = (uae_u8) ((dwColor & bmask) >> bright);
				g = (uae_u8) ((dwColor & gmask) >> gright);
				r = (uae_u8) ((dwColor & rmask) >> rright);
				AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
					m_pReducibleNodes);
				while (m_nLeafCount > m_nMaxColors)
					ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
			}
			pdwBits = (uae_u32*) (((uae_u8*) pdwBits) + nPad);
		}
		break;
#endif

	default: // Unrecognized color format
		return FALSE;
	}
	return TRUE;
}

int CQuantizer::GetLeftShiftCount (uae_u32 dwVal)
{
	int nCount = 0;
	for (int i=0; i<sizeof (uae_u32) * 8; i++) {
		if (dwVal & 1)
			nCount++;
		dwVal >>= 1;
	}
	return (8 - nCount);
}

int CQuantizer::GetRightShiftCount (uae_u32 dwVal)
{
	for (int i=0; i<sizeof (uae_u32) * 8; i++) {
		if (dwVal & 1)
			return i;
		dwVal >>= 1;
	}
	return -1;
}

void CQuantizer::AddColor (NODE** ppNode, uae_u8 r, uae_u8 g, uae_u8 b,
	uae_u32 nColorBits, uae_u32 nLevel, uae_u32* pLeafCount, NODE** pReducibleNodes)
{
	static uae_u8 mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

	//
	// If the node doesn't exist, create it.
	//
	if (*ppNode == NULL)
		*ppNode = CreateNode (nLevel, nColorBits, pLeafCount,
			pReducibleNodes);

	//
	// Update color information if it's a leaf node.
	//
	if ((*ppNode)->bIsLeaf) {
		(*ppNode)->nPixelCount++;
		(*ppNode)->nRedSum += r;
		(*ppNode)->nGreenSum += g;
		(*ppNode)->nBlueSum += b;
	}

	//
	// Recurse a level deeper if the node is not a leaf.
	//
	else {
		int shift = 7 - nLevel;
		int nIndex = (((r & mask[nLevel]) >> shift) << 2) |
			(((g & mask[nLevel]) >> shift) << 1) |
			((b & mask[nLevel]) >> shift);
		AddColor (&((*ppNode)->pChild[nIndex]), r, g, b, nColorBits,
			nLevel + 1, pLeafCount, pReducibleNodes);
	}
}

NODE* CQuantizer::CreateNode (uae_u32 nLevel, uae_u32 nColorBits, uae_u32* pLeafCount,
	NODE** pReducibleNodes)
{
	NODE* pNode;

	if ((pNode = (NODE*) HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY,
		sizeof (NODE))) == NULL)
		return NULL;

	pNode->bIsLeaf = (nLevel == nColorBits) ? TRUE : FALSE;
	if (pNode->bIsLeaf)
		(*pLeafCount)++;
	else {
		pNode->pNext = pReducibleNodes[nLevel];
		pReducibleNodes[nLevel] = pNode;
	}
	return pNode;
}

void CQuantizer::ReduceTree (uae_u32 nColorBits, uae_u32* pLeafCount,
	NODE** pReducibleNodes)
{
	//
	// Find the deepest level containing at least one reducible node.
	//
	for (int i=nColorBits - 1; (i>0) && (pReducibleNodes[i] == NULL); i--);

	//
	// Reduce the node most recently added to the list at level i.
	//
	NODE* pNode = pReducibleNodes[i];
	pReducibleNodes[i] = pNode->pNext;

	uae_u32 nRedSum = 0;
	uae_u32 nGreenSum = 0;
	uae_u32 nBlueSum = 0;
	uae_u32 nChildren = 0;

	for (i=0; i<8; i++) {
		if (pNode->pChild[i] != NULL) {
			nRedSum += pNode->pChild[i]->nRedSum;
			nGreenSum += pNode->pChild[i]->nGreenSum;
			nBlueSum += pNode->pChild[i]->nBlueSum;
			pNode->nPixelCount += pNode->pChild[i]->nPixelCount;
			HeapFree (GetProcessHeap (), 0, pNode->pChild[i]);
			pNode->pChild[i] = NULL;
			nChildren++;
		}
	}

	pNode->bIsLeaf = TRUE;
	pNode->nRedSum = nRedSum;
	pNode->nGreenSum = nGreenSum;
	pNode->nBlueSum = nBlueSum;
	*pLeafCount -= (nChildren - 1);
}

void CQuantizer::DeleteTree (NODE** ppNode)
{
	for (int i=0; i<8; i++) {
		if ((*ppNode)->pChild[i] != NULL)
			DeleteTree (&((*ppNode)->pChild[i]));
	}
	HeapFree (GetProcessHeap (), 0, *ppNode);
	*ppNode = NULL;
}

void CQuantizer::GetPaletteColors (NODE* pTree, RGBQUAD* prgb, uae_u32* pIndex)
{
	if (pTree->bIsLeaf) {
		prgb[*pIndex].rgbRed =
			(uae_u8) ((pTree->nRedSum) / (pTree->nPixelCount));
		prgb[*pIndex].rgbGreen =
			(uae_u8) ((pTree->nGreenSum) / (pTree->nPixelCount));
		prgb[*pIndex].rgbBlue =
			(uae_u8) ((pTree->nBlueSum) / (pTree->nPixelCount));
		prgb[*pIndex].rgbReserved = 0;
		(*pIndex)++;
	}
	else {
		for (int i=0; i<8; i++) {
			if (pTree->pChild[i] != NULL)
				GetPaletteColors (pTree->pChild[i], prgb, pIndex);
		}
	}
}

uae_u32 CQuantizer::GetColorCount ()
{
	return m_nLeafCount;
}

void CQuantizer::GetColorTable (RGBQUAD* prgb)
{
	uae_u32 nIndex = 0;
	GetPaletteColors (m_pTree, prgb, &nIndex);
}


/***********************************************************************
 *
 * FUNCTION:	PrvGetRowBytes
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	none
 *
 * RETURNED:	nothing
 *
 ***********************************************************************/

long PrvGetRowBytes (LPBITMAPINFO dib)
{
	uae_s32	biWidth = dib->bmiHeader.biWidth;
	uae_s16	biBitCount = dib->bmiHeader.biBitCount;

	return ((biWidth * biBitCount + 31) & ~31) / 8;
}