forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctree_map.cpp
311 lines (277 loc) · 10.2 KB
/
octree_map.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
// Aseprite
// Copyright (c) 2020-2024 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/octree_map.h"
#include "doc/palette.h"
#define MIN_LEVEL_OCTREE_DEEP 3
namespace doc {
//////////////////////////////////////////////////////////////////////
// OctreeNode
void OctreeNode::addColor(color_t c, int level, OctreeNode* parent, int paletteIndex, int levelDeep)
{
m_parent = parent;
if (level >= levelDeep) {
m_leafColor.add(c);
m_paletteIndex = paletteIndex;
return;
}
int index = getHextet(c, level);
if (!m_children) {
m_children.reset(new std::array<OctreeNode, 16>());
}
(*m_children)[index].addColor(c, level + 1, this, paletteIndex, levelDeep);
}
int OctreeNode::mapColor(int r,
int g,
int b,
int a,
int mask_index,
const Palette* palette,
int level,
const OctreeMap* octree) const
{
// New behavior: if mapColor do not have an exact rgba match, it must calculate which
// color of the current palette is the bestfit and memorize the index in a octree leaf.
if (level >= 8) {
if (m_paletteIndex == -1)
m_paletteIndex = octree->findBestfit(r, g, b, a, mask_index);
return m_paletteIndex;
}
int index = getHextet(r, g, b, a, level);
if (!m_children)
m_children.reset(new std::array<OctreeNode, 16>());
return (*m_children)[index].mapColor(r, g, b, a, mask_index, palette, level + 1, octree);
}
void OctreeNode::collectLeafNodes(OctreeNodes& leavesVector, int& paletteIndex)
{
for (int i = 0; i < 16; i++) {
OctreeNode& child = (*m_children)[i];
if (child.isLeaf()) {
child.paletteIndex(paletteIndex);
leavesVector.push_back(&child);
paletteIndex++;
}
else if (child.hasChildren()) {
child.collectLeafNodes(leavesVector, paletteIndex);
}
}
}
// removeLeaves(): remove leaves from a common parent
// auxParentVector: i/o addreess of an auxiliary parent leaf Vector from outside this function.
// rootLeavesVector: i/o address of the m_root->m_leavesVector
int OctreeNode::removeLeaves(OctreeNodes& auxParentVector, OctreeNodes& rootLeavesVector)
{
// Apply to OctreeNode which has children which are leaf nodes
int result = 0;
for (int i = 15; i >= 0; i--) {
OctreeNode& child = (*m_children)[i];
if (child.isLeaf()) {
m_leafColor.add(child.leafColor());
result++;
if (rootLeavesVector[rootLeavesVector.size() - 1] == &child)
rootLeavesVector.pop_back();
}
}
auxParentVector.push_back(this);
return result - 1;
}
// static
int OctreeNode::getHextet(color_t c, int level)
{
return ((c & (0x00000080 >> level)) ? 1 : 0) | ((c & (0x00008000 >> level)) ? 2 : 0) |
((c & (0x00800000 >> level)) ? 4 : 0) | ((c & (0x80000000 >> level)) ? 8 : 0);
}
int OctreeNode::getHextet(int r, int g, int b, int a, int level)
{
return ((r & (0x80 >> level)) ? 1 : 0) | ((g & (0x80 >> level)) ? 2 : 0) |
((b & (0x80 >> level)) ? 4 : 0) | ((a & (0x80 >> level)) ? 8 : 0);
}
// static
color_t OctreeNode::hextetToBranchColor(int hextet, int level)
{
return ((hextet & 1) ? 0x00000080 >> level : 0) | ((hextet & 2) ? 0x00008000 >> level : 0) |
((hextet & 4) ? 0x00800000 >> level : 0) | ((hextet & 8) ? 0x80000000 >> level : 0);
}
//////////////////////////////////////////////////////////////////////
// OctreeMap
bool OctreeMap::makePalette(Palette* palette, int colorCount, const int levelDeep)
{
if (m_root.hasChildren()) {
// We create paletteIndex to get a "global like" variable, in collectLeafNodes
// function, the purpose is having a incremental variable in the stack memory
// sharend between all recursive calls of collectLeafNodes.
int paletteIndex = 0;
m_root.collectLeafNodes(m_leavesVector, paletteIndex);
}
if (m_maskColor != DOC_OCTREE_IS_OPAQUE)
colorCount--;
// If we can improve the octree accuracy, makePalette returns false, then
// outside from this function we must re-construct the octreeMap all again with
// deep level equal to 8.
if (levelDeep == 7 && m_leavesVector.size() < colorCount)
return false;
OctreeNodes auxLeavesVector; // auxiliary collapsed node accumulator
bool keepReducingMap = true;
for (int level = levelDeep; level > -1; level--) {
for (int i = m_leavesVector.size() - 1; i >= 0; i--) {
if (m_leavesVector.size() + auxLeavesVector.size() <= colorCount) {
for (int j = 0; j < auxLeavesVector.size(); j++)
m_leavesVector.push_back(auxLeavesVector[auxLeavesVector.size() - 1 - j]);
keepReducingMap = false;
break;
}
else if (m_leavesVector.size() == 0) {
// When colorCount is < 16, auxLeavesVector->size() could reach the 16 size,
// if this is true and we don't stop the regular removeLeaves algorithm,
// the 16 remains colors will collapse in one.
// So, we have to reduce color with other method:
// Sort colors by pixelCount (most pixelCount on front of sortedVector),
// then:
// Blend in pairs from the least pixelCount colors.
if (auxLeavesVector.size() <= 16 && colorCount < 16 && colorCount > 0) {
// Sort colors:
OctreeNodes sortedVector;
int auxVectorSize = auxLeavesVector.size();
for (int k = 0; k < auxVectorSize; k++) {
size_t maximumCount = auxLeavesVector[0]->leafColor().pixelCount();
int maximumIndex = 0;
for (int j = 1; j < auxLeavesVector.size(); j++) {
if (auxLeavesVector[j]->leafColor().pixelCount() > maximumCount) {
maximumCount = auxLeavesVector[j]->leafColor().pixelCount();
maximumIndex = j;
}
}
sortedVector.push_back(auxLeavesVector[maximumIndex]);
auxLeavesVector.erase(auxLeavesVector.begin() + maximumIndex);
}
// End Sort colors.
// Blend colors:
for (;;) {
if (sortedVector.size() <= colorCount) {
for (int k = 0; k < sortedVector.size(); k++)
m_leavesVector.push_back(sortedVector[k]);
break;
}
sortedVector[sortedVector.size() - 2]->leafColor().add(
sortedVector[sortedVector.size() - 1]->leafColor());
sortedVector.pop_back();
}
// End Blend colors:
keepReducingMap = false;
break;
}
else
break;
}
m_leavesVector.back()->parent()->removeLeaves(auxLeavesVector, m_leavesVector);
}
if (keepReducingMap) {
// Copy collapsed leaves to m_leavesVector
int auxLeavesVectorSize = auxLeavesVector.size();
for (int i = 0; i < auxLeavesVectorSize; i++)
m_leavesVector.push_back(auxLeavesVector[auxLeavesVector.size() - 1 - i]);
auxLeavesVector.clear();
}
else
break;
}
int leafCount = m_leavesVector.size();
int aux = 0;
if (m_maskColor == DOC_OCTREE_IS_OPAQUE)
palette->resize(leafCount);
else {
palette->resize(leafCount + 1);
palette->setEntry(0, m_maskColor);
aux = 1;
}
for (int i = 0; i < leafCount; i++)
palette->setEntry(i + aux, m_leavesVector[i]->leafColor().rgbaColor());
return true;
}
void OctreeMap::feedWithImage(const Image* image,
const bool withAlpha,
const color_t maskColor,
const int levelDeep)
{
ASSERT(image);
ASSERT(image->pixelFormat() == IMAGE_RGB || image->pixelFormat() == IMAGE_GRAYSCALE);
color_t forceFullOpacity;
const bool imageIsRGBA = (image->pixelFormat() == IMAGE_RGB);
auto add_color_to_octree = [this, &forceFullOpacity, levelDeep, imageIsRGBA](color_t color) {
const int alpha = (imageIsRGBA ? rgba_geta(color) : graya_geta(color));
if (alpha) {
color |= forceFullOpacity;
color = (imageIsRGBA ? color :
rgba(graya_getv(color), graya_getv(color), graya_getv(color), alpha));
addColor(color, levelDeep);
}
};
switch (image->pixelFormat()) {
case IMAGE_RGB: {
forceFullOpacity = (withAlpha ? 0 : rgba_a_mask);
doc::for_each_pixel<RgbTraits>(image, add_color_to_octree);
break;
}
case IMAGE_GRAYSCALE: {
forceFullOpacity = (withAlpha ? 0 : graya_a_mask);
doc::for_each_pixel<GrayscaleTraits>(image, add_color_to_octree);
break;
}
}
m_maskColor = maskColor;
}
int OctreeMap::mapColor(color_t rgba) const
{
return m_root.mapColor(rgba_getr(rgba),
rgba_getg(rgba),
rgba_getb(rgba),
rgba_geta(rgba),
m_maskIndex,
m_palette,
0,
this);
}
void OctreeMap::regenerateMap(const Palette* palette,
const int maskIndex,
const FitCriteria fitCriteria)
{
ASSERT(palette);
if (!palette)
return;
// Skip useless regenerations
if (m_palette == palette && m_modifications == palette->getModifications() &&
m_maskIndex == maskIndex && m_fitCriteria == fitCriteria)
return;
m_palette = palette;
m_fitCriteria = fitCriteria;
m_root = OctreeNode();
m_leavesVector.clear();
m_maskIndex = maskIndex;
int maskColorBestFitIndex;
if (maskIndex < 0) {
m_maskColor = DOC_OCTREE_IS_OPAQUE;
maskColorBestFitIndex = -1;
}
else {
m_maskColor = palette->getEntry(maskIndex);
maskColorBestFitIndex = findBestfit(rgba_getr(m_maskColor),
rgba_getg(m_maskColor),
rgba_getb(m_maskColor),
rgba_geta(m_maskColor),
maskIndex);
}
for (int i = 0; i < palette->size(); i++) {
if (i == maskIndex) {
m_root.addColor(palette->entry(i), 0, &m_root, maskColorBestFitIndex, 8);
continue;
}
m_root.addColor(palette->entry(i), 0, &m_root, i, 8);
}
m_modifications = palette->getModifications();
}
} // namespace doc