forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_dither.h
More file actions
177 lines (149 loc) · 5.39 KB
/
ordered_dither.h
File metadata and controls
177 lines (149 loc) · 5.39 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
// Aseprite Render Library
// Copyright (c) 2001-2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef RENDER_ORDERED_DITHER_H_INCLUDED
#define RENDER_ORDERED_DITHER_H_INCLUDED
#pragma once
#include "doc/color.h"
#include "doc/image_impl.h"
#include "doc/palette.h"
#include "doc/rgbmap.h"
namespace render {
// Creates a Bayer dither matrix.
template<int N>
class BayerMatrix {
static int D2[4];
int m_matrix[N*N];
public:
int maxValue() const { return N*N; }
BayerMatrix() {
int c = 0;
for (int i=0; i<N; ++i)
for (int j=0; j<N; ++j)
m_matrix[c++] = Dn(i, j, N);
}
int operator()(int i, int j) const {
return m_matrix[(i%N)*N + (j%N)];
}
int operator[](int i) const {
return m_matrix[i];
}
private:
int Dn(int i, int j, int n) const {
ASSERT(i >= 0 && i < n);
ASSERT(j >= 0 && j < n);
if (n == 2)
return D2[i*2 + j];
else
return
+ 4*Dn(i%(n/2), j%(n/2), n/2)
+ Dn(i/(n/2), j/(n/2), 2);
}
};
// Base 2x2 dither matrix, called D(2):
template<int N>
int BayerMatrix<N>::D2[4] = { 0, 2,
3, 1 };
class OrderedDither {
static int colorDistance(int r1, int g1, int b1, int a1,
int r2, int g2, int b2, int a2) {
// The factor for RGB components came from doc::rba_luma()
return int((r1-r2) * (r1-r2) * 21 + // 2126
(g1-g2) * (g1-g2) * 71 + // 7152
(b1-b2) * (b1-b2) * 7 + // 722
(a1-a2) * (a1-a2));
}
public:
OrderedDither(int transparentIndex = -1) : m_transparentIndex(transparentIndex) {
}
template<typename Matrix>
doc::color_t ditherRgbPixelToIndex(
const Matrix& matrix,
doc::color_t color,
int x, int y,
const doc::RgbMap* rgbmap,
const doc::Palette* palette) {
// Alpha=0, output transparent color
if (m_transparentIndex >= 0 && !doc::rgba_geta(color))
return m_transparentIndex;
// Get the nearest color in the palette with the given RGB
// values.
int r = doc::rgba_getr(color);
int g = doc::rgba_getg(color);
int b = doc::rgba_getb(color);
int a = doc::rgba_geta(color);
doc::color_t nearest1idx =
(rgbmap ? rgbmap->mapColor(r, g, b, a):
palette->findBestfit(r, g, b, a, m_transparentIndex));
doc::color_t nearest1rgb = palette->getEntry(nearest1idx);
int r1 = doc::rgba_getr(nearest1rgb);
int g1 = doc::rgba_getg(nearest1rgb);
int b1 = doc::rgba_getb(nearest1rgb);
int a1 = doc::rgba_geta(nearest1rgb);
// Between the original color ('color' parameter) and 'nearest'
// index, we have an error (r1-r, g1-g, b1-b). Here we try to
// find the other nearest color with the same error but with
// different sign.
int r2 = r - (r1-r);
int g2 = g - (g1-g);
int b2 = b - (b1-b);
int a2 = a - (a1-a);
r2 = MID(0, r2, 255);
g2 = MID(0, g2, 255);
b2 = MID(0, b2, 255);
a2 = MID(0, a2, 255);
doc::color_t nearest2idx =
(rgbmap ? rgbmap->mapColor(r2, g2, b2, a2):
palette->findBestfit(r2, g2, b2, a2, m_transparentIndex));
// If both possible RGB colors use the same index, we cannot
// make any dither with these two colors.
if (nearest1idx == nearest2idx)
return nearest1idx;
doc::color_t nearest2rgb = palette->getEntry(nearest2idx);
r2 = doc::rgba_getr(nearest2rgb);
g2 = doc::rgba_getg(nearest2rgb);
b2 = doc::rgba_getb(nearest2rgb);
a2 = doc::rgba_geta(nearest2rgb);
// Here we calculate the distance between the original 'color'
// and 'nearest1rgb'. The maximum possible distance is given by
// the distance between 'nearest1rgb' and 'nearest2rgb'.
int d = colorDistance(r1, g1, b1, a1, r, g, b, a);
int D = colorDistance(r1, g1, b1, a1, r2, g2, b2, a2);
if (D == 0)
return nearest1idx;
// We convert the d/D factor to the matrix range to compare it
// with the threshold. If d > threshold, it means that we're
// closer to 'nearest2rgb' than to 'nearest1rgb'.
d = matrix.maxValue() * d / D;
int threshold = matrix(x, y);
return (d > threshold ? nearest2idx:
nearest1idx);
}
template<typename Matrix>
void ditherRgbImageToIndexed(const Matrix& matrix,
const doc::Image* srcImage,
doc::Image* dstImage,
int u, int v,
const doc::RgbMap* rgbmap,
const doc::Palette* palette) {
const doc::LockImageBits<doc::RgbTraits> srcBits(srcImage);
doc::LockImageBits<doc::IndexedTraits> dstBits(dstImage);
auto srcIt = srcBits.begin();
auto dstIt = dstBits.begin();
int w = srcImage->width();
int h = srcImage->height();
for (int y=0; y<h; ++y) {
for (int x=0; x<w; ++x, ++srcIt, ++dstIt) {
ASSERT(srcIt != srcBits.end());
ASSERT(dstIt != dstBits.end());
*dstIt = ditherRgbPixelToIndex(matrix, *srcIt, x+u, y+v, rgbmap, palette);
}
}
}
private:
int m_transparentIndex;
};
} // namespace render
#endif