-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorReduction.cpp
More file actions
164 lines (145 loc) · 5.61 KB
/
ColorReduction.cpp
File metadata and controls
164 lines (145 loc) · 5.61 KB
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
#include "ConsoleGraphics.h"
#include "ColorReduction.h"
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
unsigned nAlphaSum; // Sum of alpha components
Node *pChild[16]; // Pointers to child Nodes
Node *pNext; // Pointer to next reducible Node
};
const unsigned nColorBits = 8;
void AddColor (Node**, Pixel p, unsigned, unsigned, unsigned*, Node**);
Node* CreateNode (unsigned, unsigned, unsigned*, Node**);
void ReduceTree (unsigned, unsigned*, Node**);
void DeleteTree (Node**);
void GetPaletteColors (Node*, ref<Palette>, PixelFormat &, unsigned*);
ref<Palette> CreateOctreePalette(ref<Image> img, unsigned maxColors, PixelFormat &pixelFormat) {
Node* pTree;
unsigned nLeafCount, nIndex;
Node* pReducibleNodes[9];
// Initialize octree variables
pTree = NULL;
nLeafCount = 0;
if (nColorBits > 8) // Just in case
return NULL;
for (unsigned i = 0; i <= nColorBits; i++)
pReducibleNodes[i] = NULL;
// Create palette
ref<Palette> result = newref Palette(maxColors, pixelFormat.firstColorTransparent);
maxColors = result->remaningColors();
for (unsigned j = 0; j < img->height; j++) {
for (unsigned i = 0; i < img->width; i++) {
Pixel p = img->getPixel(i, j);
// Ignore fully transparent, others will become fully opaque
if (p.a == 0)
continue;
// Add other colors
AddColor(&pTree, p, nColorBits, 0, &nLeafCount, pReducibleNodes);
while (nLeafCount > maxColors)
ReduceTree (nColorBits, &nLeafCount, pReducibleNodes);
}
}
unsigned colorCount = nLeafCount - 1;
GetPaletteColors (pTree, result, pixelFormat, &nIndex);
DeleteTree (&pTree);
return result;
}
void AddColor (Node** ppNode, Pixel p, 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 = CreateNode (nLevel, nColorBits, pLeafCount,
pReducibleNodes);
// Update color information if it's a leaf Node
if ((*ppNode)->bIsLeaf) {
(*ppNode)->nPixelCount++;
(*ppNode)->nRedSum += p.r;
(*ppNode)->nGreenSum += p.g;
(*ppNode)->nBlueSum += p.b;
(*ppNode)->nAlphaSum += p.a;
}
// Recurse a level deeper if the Node is not a leaf
else {
shift = 7 - nLevel;
nIndex = (((p.r & mask[nLevel]) >> shift) << 3) |
(((p.g & mask[nLevel]) >> shift) << 2) |
(((p.b & mask[nLevel]) >> shift) << 1) |
((p.a & mask[nLevel]) >> shift);
AddColor (&((*ppNode)->pChild[nIndex]), p, nColorBits,
nLevel + 1, pLeafCount, pReducibleNodes);
}
}
Node* CreateNode (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 ReduceTree (unsigned nColorBits, unsigned* pLeafCount, Node** pReducibleNodes) {
int i;
Node* pNode;
unsigned nRedSum, nGreenSum, nBlueSum, nAlphaSum, 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 = nAlphaSum = nChildren = 0;
for (i = 0; i < 16; i++) {
if (pNode->pChild[i] != NULL) {
nRedSum += pNode->pChild[i]->nRedSum;
nGreenSum += pNode->pChild[i]->nGreenSum;
nBlueSum += pNode->pChild[i]->nBlueSum;
nAlphaSum += pNode->pChild[i]->nAlphaSum;
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;
pNode->nAlphaSum = nAlphaSum;
*pLeafCount -= (nChildren - 1);
}
void DeleteTree (Node** ppNode)
{
for (unsigned i = 0; i < 16; i++) {
if ((*ppNode)->pChild[i] != NULL)
DeleteTree (&((*ppNode)->pChild[i]));
}
delete *ppNode;
*ppNode = NULL;
}
void GetPaletteColors (Node* pTree, ref<Palette> destPalette, PixelFormat &pixelFormat, unsigned* pIndex) {
if (pTree->bIsLeaf) {
destPalette->addColor(Pixel(
(uint8_t) ((pTree->nRedSum) / (pTree->nPixelCount)),
(uint8_t) ((pTree->nGreenSum) / (pTree->nPixelCount)),
(uint8_t) ((pTree->nBlueSum) / (pTree->nPixelCount)),
(uint8_t) ((pTree->nAlphaSum) / (pTree->nPixelCount))));
(*pIndex)++;
}
else {
for (int i = 0; i < 16; i++) {
if (pTree->pChild[i] != NULL)
GetPaletteColors (pTree->pChild[i], destPalette, pixelFormat, pIndex);
}
}
}