forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremap.cpp
173 lines (143 loc) · 3.57 KB
/
remap.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
// Aseprite Document Library
// Copyright (c) 2020 Igara Studio S.A.
// Copyright (c) 2001-2016 David Capello
//
// 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/remap.h"
#include "base/base.h"
#include "doc/palette.h"
#include "doc/palette_picks.h"
#include <algorithm>
namespace doc {
Remap create_remap_to_move_picks(const PalettePicks& picks, int beforeIndex)
{
Remap map(picks.size());
int selectedTotal = 0;
int selectedBeforeIndex = 0;
for (int i=0; i<map.size(); ++i) {
if (picks[i]) {
++selectedTotal;
if (i < beforeIndex)
++selectedBeforeIndex;
}
}
for (int i=0, j=0, k=0; i<map.size(); ++i) {
if (k == beforeIndex - selectedBeforeIndex)
k += selectedTotal;
if (picks[i]) {
map.map(i, beforeIndex - selectedBeforeIndex + j);
++j;
}
else {
map.map(i, k++);
}
}
return map;
}
Remap create_remap_to_expand_palette(int size, int count, int beforeIndex)
{
Remap map(size);
int j, k = 0;
for (int i=0; i<size; ++i) {
if (i < beforeIndex)
j = i;
else if (i + count < size)
j = i + count;
else
j = beforeIndex + (k++);
map.map(i, j);
}
return map;
}
Remap create_remap_to_change_palette(
const Palette* oldPalette, const Palette* newPalette,
const int oldMaskIndex,
const bool remapMaskIndex)
{
Remap remap(std::max(oldPalette->size(), newPalette->size()));
int maskIndex = oldMaskIndex;
if (maskIndex >= 0) {
if (remapMaskIndex &&
oldPalette->getEntry(maskIndex) !=
newPalette->getEntry(maskIndex)) {
color_t maskColor = oldPalette->getEntry(maskIndex);
int r = rgba_getr(maskColor);
int g = rgba_getg(maskColor);
int b = rgba_getb(maskColor);
int a = rgba_geta(maskColor);
// Find the new mask color
maskIndex = newPalette->findExactMatch(r, g, b, a, -1);
if (maskIndex >= 0)
remap.map(oldMaskIndex, maskIndex);
}
else {
remap.map(maskIndex, maskIndex);
}
}
for (int i=0; i<oldPalette->size(); ++i) {
if (i == oldMaskIndex)
continue;
const color_t color = oldPalette->getEntry(i);
// If in both palettes, it's the same color, we don't need to
// remap this entry.
if (color == newPalette->getEntry(i)) {
remap.map(i, i);
continue;
}
int j = newPalette->findExactMatch(
rgba_getr(color),
rgba_getg(color),
rgba_getb(color),
rgba_geta(color), maskIndex);
if (j < 0)
j = newPalette->findBestfit(
rgba_getr(color),
rgba_getg(color),
rgba_getb(color),
rgba_geta(color), maskIndex);
remap.map(i, j);
}
return remap;
}
void Remap::merge(const Remap& other)
{
for (int i=0; i<size(); ++i) {
m_map[i] = other[m_map[i]];
}
}
Remap Remap::invert() const
{
Remap inv(size());
for (int i=0; i<size(); ++i)
inv.map(operator[](i), i);
return inv;
}
bool Remap::isFor8bit() const
{
for (int i=0; i<size(); ++i) {
// Moving entries between [0,255] range to or from [256,+inf)
// range are invalid for 8-bit images.
if ((i < 256 && m_map[i] >= 256) ||
(i >= 256 && m_map[i] < 256))
return false;
}
return true;
}
bool Remap::isInvertible(const PalettePicks& usedEntries) const
{
PalettePicks picks(size());
for (int i=0; i<size(); ++i) {
if (!usedEntries[i])
continue;
int j = m_map[i];
if (picks[j])
return false;
picks[j] = true;
}
return true;
}
} // namespace doc