- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessRGB.cpp
More file actions
Latest commit
215 lines (190 loc) · 4.93 KB
/
Copy pathProcessRGB.cpp
File metadata and controls
215 lines (190 loc) · 4.93 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
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
// https://bitbucket.org/wolfpld/etcpak
// Bartosz Taudul <wolf.pld@gmail.com>
#include<string.h>
#include"ProcessCommon.hpp"
#include"ProcessRGB.hpp"
#include"TablesETC1.hpp"
#include"VectorETC1.hpp"
#include"ForceInline.h"
static finline v3i Average(constunsignedchar* data)
{
unsignedint r = 0, g = 0, b = 0;
for (int i = 0; i < 8; i++)
{
r += *data++;
g += *data++;
b += *data++;
data++;
}
returnv3i(r / 8, g / 8, b / 8);
}
static finline voidCalcErrorBlock(constunsignedchar* data, unsignedint err[4])
{
for (int i = 0; i < 8; i++)
{
unsignedint d = *data++;
err[0] += d;
err[3] += d*d;
d = *data++;
err[1] += d;
err[3] += d*d;
d = *data++;
err[2] += d;
err[3] += d*d;
data++;
}
}
static finline unsignedintCalcError(constunsignedint block[4], const v3i& average)
{
unsignedint err = block[3];
err -= block[0] * 2 * average.x;
err -= block[1] * 2 * average.y;
err -= block[2] * 2 * average.z;
err += 8 * (sq(average.x) + sq(average.y) + sq(average.z));
return err;
}
static finline voidProcessAverages(v3i* a)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
int c1 = a[i * 2 + 1][j] >> 3;
int c2 = a[i * 2][j] >> 3;
int diff = c2 - c1;
if (diff > 3) diff = 3;
elseif (diff < -4) diff = -4;
int co = c1 + diff;
a[5 + i * 2][j] = (c1 << 3) | (c1 >> 2);
a[4 + i * 2][j] = (co << 3) | (co >> 2);
}
}
for (int i = 0; i < 4; i++)
{
a[i].x = g_avg2[a[i].x >> 4];
a[i].y = g_avg2[a[i].y >> 4];
a[i].z = g_avg2[a[i].z >> 4];
}
}
static finline voidEncodeAverages(unsignedlonglong& _d, const v3i* a, size_t idx)
{
auto d = _d;
d |= (idx << 24);
size_t base = idx << 1;
if ((idx & 0x2) == 0)
{
for (int i = 0; i < 3; i++)
{
d |= (unsignedlonglong)(a[base + 0][i] >> 4) << (i * 8);
d |= (unsignedlonglong)(a[base + 1][i] >> 4) << (i * 8 + 4);
}
}
else
{
for (int i = 0; i < 3; i++)
{
d |= (unsignedlonglong)(a[base + 1][i] & 0xF8) << (i * 8);
int c = ((a[base + 0][i] & 0xF8) - (a[base + 1][i] & 0xF8)) >> 3;
c &= ~0xFFFFFFF8;
d |= ((unsignedlonglong)c) << (i * 8);
}
}
_d = d;
}
static finline unsignedlonglongCheckSolid(constunsignedchar* src)
{
bool solid = true;
constunsignedchar* ptr = src + 4;
for (int i = 1; i < 16; i++)
{
if (memcmp(src, ptr, 4) != 0)
{
return0;
}
ptr += 4;
}
return0x02000000 |
((unsignedint)(src[0] & 0xF8)) |
((unsignedint)(src[1] & 0xF8) << 8) |
((unsignedint)(src[2] & 0xF8) << 16);
}
unsignedlonglongProcessRGB(constunsignedchar* src)
{
unsignedlonglong d = CheckSolid(src);
if (d != 0) return d;
unsignedchar b23[2][32];
constunsignedchar* b[4] = { src + 32, src, b23[0], b23[1] };
for (int i = 0; i < 4; i++)
{
memcpy(b23[1] + i * 8, src + i * 16, 8);
memcpy(b23[0] + i * 8, src + i * 16 + 8, 8);
}
v3i a[8];
for (int i = 0; i < 4; i++)
{
a[i] = Average(b[i]);
}
ProcessAverages(a);
unsignedint err[4] = {};
for (int i = 0; i < 4; i++)
{
unsignedint errblock[4] = {};
CalcErrorBlock(b[i], errblock);
err[i / 2] += CalcError(errblock, a[i]);
err[2 + i / 2] += CalcError(errblock, a[i + 4]);
}
size_t idx = GetLeastError(err, 4);
EncodeAverages(d, a, idx);
unsignedlonglong terr[2][8] = {};
unsignedint tsel[16][8];
auto id = g_id[idx];
constunsignedchar* data = src;
for (size_t i = 0; i < 16; i++)
{
unsignedint* sel = tsel[i];
unsignedint bid = id[i];
unsignedlonglong* ter = terr[bid % 2];
unsignedchar r = *data++;
unsignedchar g = *data++;
unsignedchar b = *data++;
data++;
int dr = a[bid].x - r;
int dg = a[bid].y - g;
int db = a[bid].z - b;
int pix = dr * 77 + dg * 151 + db * 28;
for (int t = 0; t < 8; t++)
{
constlonglong* tab = g_table256[t];
unsignedint idx = 0;
unsignedlonglong err = sq(tab[0] + pix);
for (int j = 1; j < 4; j++)
{
unsignedlonglong local = sq(tab[j] + pix);
if (local < err)
{
err = local;
idx = j;
}
}
*sel++ = idx;
*ter++ += err;
}
}
size_t tidx[2];
tidx[0] = GetLeastError(terr[0], 8);
tidx[1] = GetLeastError(terr[1], 8);
d |= tidx[0] << 26;
d |= tidx[1] << 29;
for (int i = 0; i < 16; i++)
{
unsignedlonglong t = tsel[i][tidx[id[i] % 2]];
d |= (t & 0x1) << (i + 32);
d |= (t & 0x2) << (i + 47);
}
d = ((d & 0x00000000FFFFFFFF)) |
((d & 0xFF00000000000000) >> 24) |
((d & 0x000000FF00000000) << 24) |
((d & 0x00FF000000000000) >> 8) |
((d & 0x0000FF0000000000) << 8);
return d;
}