-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDither.cpp
More file actions
64 lines (57 loc) · 1.48 KB
/
Dither.cpp
File metadata and controls
64 lines (57 loc) · 1.48 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
// https://bitbucket.org/wolfpld/etcpak
// Bartosz Taudul <wolf.pld@gmail.com>
#include <algorithm>
#include <string.h>
#include "Dither.hpp"
#include "MathETC1.hpp"
static uint8_t e5[32];
static uint8_t e6[64];
static uint8_t qrb[256 + 16];
static uint8_t qg[256 + 16];
void InitDither()
{
for (int i = 0; i < 32; i++)
{
e5[i] = (i << 3) | (i >> 2);
}
for (int i = 0; i < 64; i++)
{
e6[i] = (i << 2) | (i >> 4);
}
for (int i = 0; i < 256 + 16; i++)
{
int v = std::min(std::max(0, i - 8), 255);
qrb[i] = e5[mul8bit(v, 31)];
qg[i] = e6[mul8bit(v, 63)];
}
}
void Dither(uint8_t* data)
{
int err[8];
int* ep1 = err;
int* ep2 = err + 4;
for (int ch = 0; ch < 3; ch++)
{
uint8_t* ptr = data + ch;
uint8_t* quant = (ch == 1) ? qg + 8 : qrb + 8;
memset(err, 0, sizeof(err));
for (int y = 0; y < 4; y++)
{
uint8_t tmp;
tmp = quant[ptr[0] + ((3 * ep2[1] + 5 * ep2[0]) >> 4)];
ep1[0] = ptr[0] - tmp;
ptr[0] = tmp;
tmp = quant[ptr[4] + ((7 * ep1[0] + 3 * ep2[2] + 5 * ep2[1] + ep2[0]) >> 4)];
ep1[1] = ptr[4] - tmp;
ptr[4] = tmp;
tmp = quant[ptr[8] + ((7 * ep1[1] + 3 * ep2[3] + 5 * ep2[2] + ep2[1]) >> 4)];
ep1[2] = ptr[8] - tmp;
ptr[8] = tmp;
tmp = quant[ptr[12] + ((7 * ep1[2] + 5 * ep2[3] + ep2[2]) >> 4)];
ep1[3] = ptr[12] - tmp;
ptr[12] = tmp;
ptr += 16;
std::swap(ep1, ep2);
}
}
}