-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDitherer.cpp
More file actions
250 lines (222 loc) · 9.44 KB
/
Copy pathDitherer.cpp
File metadata and controls
250 lines (222 loc) · 9.44 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
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
#include <cmath>
#include <omp.h>
#include <vector>
#include "Ditherer.h"
namespace
{
__forceinline _NODISCARD uint8_t clamp_color(int val)
{
auto result = (val < 0) ? 0 : val;
result = (result > 255) ? 255 : result;
return result;
}
}
namespace Dithering
{
const std::string& IDitherer::get_name() const
{
return name_;
}
int IDitherer::get_palettes_count() const
{
return (int)palette_handlers_.size();
}
const std::vector<std::string>& IDitherer::get_palettes_names() const
{
return palette_handlers_names_;
}
JJNDitherer::JJNDitherer(bool use_speed_up_structures) :use_speed_up_structures_(use_speed_up_structures)
{
name_ = "Jarvis-Judice-Ninke ditherer";
palette_handlers_.resize((int)Palette::COUNT);
palette_handlers_[(int)Palette::BLACK_AND_WHITE].reset(new BlackAndWhiteHandler());
palette_handlers_[(int)Palette::BLACK_GRAY_AND_WHITE].reset(new BlackGrayAndWhiteHandler());
palette_handlers_[(int)Palette::TWO_BIT_GRAYSCALE].reset(new TwoBitGrayscaleHandler());
palette_handlers_[(int)Palette::FOUR_BIT_GRAYSCALE].reset(new FourBitGrayscaleHandler());
palette_handlers_[(int)Palette::BLACK_RED_GREEN_BLUE].reset(new BlackRedGreenBlueHandler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_1].reset(new Palette1Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_2].reset(new Palette2Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_3].reset(new Palette3Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_6].reset(new Palette6Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_7].reset(new Palette7Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_8].reset(new Palette8Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_9].reset(new Palette9Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_10].reset(new Palette10Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_11].reset(new Palette11Handler());
palette_handlers_[(int)Palette::BK_0011_PALETTE_12].reset(new Palette12Handler());
palette_handlers_[(int)Palette::EGA_16_COLORS].reset(new Ega16ColorsHandler());
palette_handlers_[(int)Palette::VGA_256_COLORS].reset(new Vga256ColorsHandler());
palette_handlers_names_.resize((int)Palette::COUNT);
for (int i = 0; i < palette_handlers_.size(); ++i)
{
palette_handlers_names_[i] = palette_handlers_[i]->get_name();
}
}
bool JJNDitherer::process(Image& input_image, Image& output_image, int palette, int threads_count) const
{
if (threads_count == 1)
{
processSingleThread(input_image, output_image, palette);
}
else if (threads_count > 1)
{
processMultiThread(input_image, output_image, palette, threads_count);
}
return true;
}
void JJNDitherer::processSingleThread(Image& input_image, Image& output_image, int palette) const
{
const auto* color_selector = palette_handlers_[palette].get();
int rows_count = 0;
int columns_count = 0;
input_image.getSize(rows_count, columns_count);
const auto row_coeff = 1.0f / 3.0f;
const auto iterations_count = (rows_count - 1) * 3 + columns_count;
for (int i = 0; i < iterations_count; ++i)
{
int row = (int)ceilf(std::max(i - columns_count + 1.0f, 0.0f) * row_coeff);
int column = i - row * 3;
const auto elements_count = std::min(1 + column / 3, rows_count - row);
for (int j = 0; j < elements_count; ++j)
{
processPixel(input_image, output_image, color_selector, row, column);
++row;
column -= 3;
}
}
}
void JJNDitherer::processMultiThread(Image& input_image, Image& output_image, int palette, int threads_count) const
{
omp_set_num_threads(threads_count);
const auto* color_selector = palette_handlers_[palette].get();
int rows_count = 0;
int columns_count = 0;
input_image.getSize(rows_count, columns_count);
const auto row_coeff = 1.0f / 3.0f;
const auto iterations_count = (rows_count - 1) * 3 + columns_count;
for (int i = 0; i < iterations_count; ++i)
{
int row = (int)ceilf(std::max(i - columns_count + 1.0f, 0.0f) * row_coeff);
int column = i - row * 3;
const auto elements_count = std::min(1 + column / 3, rows_count - row);
#pragma omp parallel for default(shared)
for (int j = 0; j < elements_count; ++j)
{
processPixel(input_image, output_image, color_selector, row + j, column - 3 * j);
}
}
}
void JJNDitherer::processPixel(Image& input_image, Image& output_image, const IPaletteHandler* color_selector, int row, int column) const
{
auto optimal_color = 0;
std::array<int, 3> mistake;
const auto color = color_selector->findOptimalColor(input_image, row, column, mistake, use_speed_up_structures_);
output_image.setColor(color, row, column);
auto rows_count = 0;
auto columns_count = 0;
input_image.getSize(rows_count, columns_count);
const std::array<int, 3> mistake_7_48 = { (int)(mistake[0] * coeff_7_48_), (int)(mistake[1] * coeff_7_48_), (int)(mistake[2] * coeff_7_48_) };
const std::array<int, 3> mistake_5_48 = { (int)(mistake[0] * coeff_5_48_), (int)(mistake[1] * coeff_5_48_), (int)(mistake[2] * coeff_5_48_) };
const std::array<int, 3> mistake_3_48 = { (int)(mistake[0] * coeff_3_48_), (int)(mistake[1] * coeff_3_48_), (int)(mistake[2] * coeff_3_48_) };
const std::array<int, 3> mistake_1_48 = { (int)(mistake[0] * coeff_1_48_), (int)(mistake[1] * coeff_1_48_), (int)(mistake[2] * coeff_1_48_) };
Color tmp_color = { 0 };
if (column + 1 < columns_count)
{
tmp_color = input_image.getColor(row, column + 1);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_7_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_7_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_7_48[2]);
input_image.setColor(tmp_color, row, column + 1);
}
if (column + 2 < columns_count)
{
tmp_color = input_image.getColor(row, column + 2);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_5_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_5_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_5_48[2]);
input_image.setColor(tmp_color, row, column + 2);
}
++row;
if (row < rows_count)
{
if (column - 2 > 0)
{
tmp_color = input_image.getColor(row, column - 2);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_3_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_3_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_3_48[2]);
input_image.setColor(tmp_color, row, column - 2);
}
if (column - 1 > 0)
{
tmp_color = input_image.getColor(row, column - 1);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_5_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_5_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_5_48[2]);
input_image.setColor(tmp_color, row, column - 1);
}
tmp_color = input_image.getColor(row, column);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_7_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_7_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_7_48[2]);
input_image.setColor(tmp_color, row, column);
if (column + 1 < columns_count)
{
tmp_color = input_image.getColor(row, column + 1);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_5_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_5_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_5_48[2]);
input_image.setColor(tmp_color, row, column + 1);
}
if (column + 2 < columns_count)
{
tmp_color = input_image.getColor(row, column + 2);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_3_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_3_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_3_48[2]);
input_image.setColor(tmp_color, row, column + 2);
}
}
++row;
if (row < rows_count)
{
if (column - 2 > 0)
{
tmp_color = input_image.getColor(row, column - 2);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_1_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_1_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_1_48[2]);
input_image.setColor(tmp_color, row, column - 2);
}
if (column - 1 > 0)
{
tmp_color = input_image.getColor(row, column - 1);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_3_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_3_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_3_48[2]);
input_image.setColor(tmp_color, row, column - 1);
}
tmp_color = input_image.getColor(row, column);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_5_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_5_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_5_48[2]);
input_image.setColor(tmp_color, row, column);
if (column + 1 < columns_count)
{
tmp_color = input_image.getColor(row, column + 1);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_3_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_3_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_3_48[2]);
input_image.setColor(tmp_color, row, column + 1);
}
if (column + 2 < columns_count)
{
tmp_color = input_image.getColor(row, column + 2);
tmp_color[0] = clamp_color((int)tmp_color[0] + mistake_1_48[0]);
tmp_color[1] = clamp_color((int)tmp_color[1] + mistake_1_48[1]);
tmp_color[2] = clamp_color((int)tmp_color[2] + mistake_1_48[2]);
input_image.setColor(tmp_color, row, column + 2);
}
}
}
}