-
Notifications
You must be signed in to change notification settings - Fork 0
/
xReduc.hpp
235 lines (199 loc) · 7.42 KB
/
xReduc.hpp
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
/***********************************************
XREDUCTION
************************************************/
struct Node {
bool bIsLeaf; // true if Node has no children
unsigned nPixelCount; // Number of pixels represented by this leaf
unsigned nRedSum; // Sum of red components
unsigned nGreenSum; // Sum of green components
unsigned nBlueSum; // Sum of blue components
Node *pChild[8]; // Pointers to child Nodes
Node *pNext; // Pointer to next reducible Node
};
unsigned nColorBits = 8;
unsigned xCreateOctreePalette (ref<Tile> tile, xPixelType *destPalette, unsigned nMaxColors, unsigned nColorBits);
void xAddColor(Node**, uint8_t, uint8_t, uint8_t, unsigned, unsigned, unsigned*, Node**);
Node* xCreateNode (unsigned, unsigned, unsigned*, Node**);
void xReduceTree (unsigned, unsigned*, Node**);
void xDeleteTree (Node**);
void xGetPaletteColors (Node*, xPixelType*, unsigned*);
unsigned xCreateOctreePalette (ref<Tile> tile, xPixelType *destPalette, unsigned nMaxColors) {
unsigned l = tile->width, h = tile->height;
Node* pTree;
unsigned nLeafCount, nIndex;
Node* pReducibleNodes[9];
// Initialize octree variables
pTree = NULL;
nLeafCount = 0;
for (unsigned i=0; i<=(int) nColorBits; i++)
pReducibleNodes[i] = NULL;
for (unsigned j=0; j<h; j++) {
for (unsigned i=0; i<l; i++) {
Pixel p = tile->getPixel(i, j);
if (p.a == 0)
continue;
xAddColor (&pTree, p.r, p.g, p.b, nColorBits, 0, &nLeafCount,
pReducibleNodes);
while (nLeafCount > nMaxColors)
xReduceTree (nColorBits, &nLeafCount, pReducibleNodes);
}
}
if (nLeafCount > nMaxColors) { // Sanity check
xDeleteTree (&pTree);
return 0;
}
nIndex = 0;
if (!pTree)
return 0;
xGetPaletteColors (pTree, destPalette, &nIndex);
xDeleteTree (&pTree);
return nLeafCount;
}
unsigned xCreateOctreePaletteMultiple (XPALETTE *pal, ref<Tileset> tileset, xPixelType *destPalette, unsigned nMaxColors) {
unsigned l = tileset->getTile(0)->width, h = tileset->getTile(0)->height;
Node* pTree;
unsigned nLeafCount, nIndex;
Node* pReducibleNodes[9];
// Initialize octree variables
pTree = NULL;
nLeafCount = 0;
for (unsigned i=0; i<=(int) nColorBits; i++)
pReducibleNodes[i] = NULL;
for (unsigned p=0;p<pal->nbAssocTiles;p++) {
ref<Tile> t = tileset->getTile(pal->associatedTiles[p]);
for (unsigned j=0; j<h; j++) {
for (unsigned i=0; i<l; i++) {
Pixel p = t->getPixel(i, j);
if (p.a == 0)
continue;
xAddColor (&pTree, p.r, p.g, p.b, nColorBits, 0, &nLeafCount,
pReducibleNodes);
while (nLeafCount > nMaxColors)
xReduceTree (nColorBits, &nLeafCount, pReducibleNodes);
}
}
}
if (nLeafCount > nMaxColors) { // Sanity check
xDeleteTree (&pTree);
return 0;
}
if (!pTree)
return 0;
nIndex = 0;
xGetPaletteColors (pTree, destPalette, &nIndex);
xDeleteTree (&pTree);
return nLeafCount;
}
void xAddColor (Node** ppNode, uint8_t r, uint8_t g, uint8_t b, unsigned nColorBits,
unsigned nLevel, unsigned* pLeafCount, Node** pReducibleNodes)
{
int nIndex, shift;
static uint8_t mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
// If the node doesn't exist, create it
if (*ppNode == NULL)
*ppNode = xCreateNode (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 {
shift = 7 - nLevel;
nIndex = (((r & mask[nLevel]) >> shift) << 2) |
(((g & mask[nLevel]) >> shift) << 1) |
((b & mask[nLevel]) >> shift);
xAddColor (&((*ppNode)->pChild[nIndex]), r, g, b, nColorBits,
nLevel + 1, pLeafCount, pReducibleNodes);
}
}
Node* xCreateNode (unsigned nLevel, unsigned nColorBits, unsigned* pLeafCount,
Node** pReducibleNodes)
{
Node* pNode = new Node;
memset(pNode, 0, sizeof(Node));
pNode->bIsLeaf = (nLevel == nColorBits) ? true : false;
if (pNode->bIsLeaf)
(*pLeafCount)++;
else { // Add the node to the reducible list for this level
pNode->pNext = pReducibleNodes[nLevel];
pReducibleNodes[nLevel] = pNode;
}
return pNode;
}
void xReduceTree (unsigned nColorBits, unsigned* pLeafCount, Node** pReducibleNodes)
{
int i;
Node* pNode;
unsigned nRedSum, nGreenSum, nBlueSum, nChildren;
// Find the deepest level containing at least one reducible node
for (i=nColorBits - 1; (i>0) && (pReducibleNodes[i] == NULL); i--);
// Reduce the node most recently added to the list at level i
pNode = pReducibleNodes[i];
pReducibleNodes[i] = pNode->pNext;
nRedSum = nGreenSum = nBlueSum = 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;
delete pNode->pChild[i];
pNode->pChild[i] = NULL;
nChildren++;
}
}
pNode->bIsLeaf = true;
pNode->nRedSum = nRedSum;
pNode->nGreenSum = nGreenSum;
pNode->nBlueSum = nBlueSum;
*pLeafCount -= (nChildren - 1);
}
void xDeleteTree (Node** ppNode)
{
int i;
for (i=0; i<8; i++) {
if ((*ppNode)->pChild[i] != NULL)
xDeleteTree (&((*ppNode)->pChild[i]));
}
delete *ppNode;
*ppNode = NULL;
}
void xGetPaletteColors (Node* pTree, xPixelType* pPalEntries, unsigned* pIndex)
{
int i;
if (pTree->bIsLeaf) {
pPalEntries[*pIndex] = Pixel(
(pTree->nRedSum) / (pTree->nPixelCount),
(pTree->nGreenSum) / (pTree->nPixelCount),
(pTree->nBlueSum) / (pTree->nPixelCount), 0).value;
(*pIndex)++;
}
else {
for (i=0; i<8; i++) {
if (pTree->pChild[i] != NULL)
xGetPaletteColors (pTree->pChild[i], pPalEntries, pIndex);
}
}
}
int xReductionOctree(ref<Tile> tile, xPixelType *indexedTile, xPixelType *palette, unsigned nCoulInitiales, unsigned nCoul) {
if (nCoul-nCoulInitiales<=1)
return 0;
unsigned nbreCouleursPalette = xCreateOctreePalette(tile, palette+nCoulInitiales, nCoul-nCoulInitiales);
if (nbreCouleursPalette <= 1)
return 0;
xApplyOptimalTilesetPalData(tile, indexedTile, palette, 250000, nbreCouleursPalette);
return nbreCouleursPalette+nCoulInitiales;
}
int xReductionOctreeMultiple(XPALETTE *p, ref<Tileset> tileset, xPixelType *indexedTile, xPixelType *palette, int nCoulInitiales, int nCoul) {
if (nCoul-nCoulInitiales<=1)
return 0;
unsigned nbreCouleursPalette = xCreateOctreePaletteMultiple(p, tileset, palette+nCoulInitiales, nCoul-nCoulInitiales);
if (nbreCouleursPalette<=1)
return 0;
xApplyOptimalTilesetPalDataMultiple(p, tileset, indexedTile, palette, 250000, nbreCouleursPalette);
return nbreCouleursPalette+nCoulInitiales;
}