-
Notifications
You must be signed in to change notification settings - Fork 18
/
AntiDitherer.cpp
351 lines (278 loc) · 7.92 KB
/
AntiDitherer.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright 2003, Heikki Suhonen
* Distributed under the terms of the MIT License.
*
* Authors:
* Heikki Suhonen <heikki.suhonen@gmail.com>
*
*/
#include <Bitmap.h>
#include <Catalog.h>
#include <CheckBox.h>
#include <ClassInfo.h>
#include <LayoutBuilder.h>
#include <Spinner.h>
#include <StatusBar.h>
#include <Window.h>
#include <string.h>
#include "AddOns.h"
#include "AntiDitherer.h"
#include "ManipulatorInformer.h"
#include "Selection.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "AddOns_AntiDither"
#ifdef __cplusplus
extern "C" {
#endif
char name[255] = B_TRANSLATE_MARK("Anti-Dither" B_UTF8_ELLIPSIS);
char menu_help_string[255] = B_TRANSLATE_MARK("Attempts to reverse the effects of dithering.");
int32 add_on_api_version = ADD_ON_API_VERSION;
add_on_types add_on_type = COLOR_ADD_ON;
#ifdef __cplusplus
}
#endif
Manipulator*
instantiate_add_on(BBitmap* bm, ManipulatorInformer* i)
{
delete i;
return new AntiDithererManipulator(bm);
}
AntiDithererManipulator::AntiDithererManipulator(BBitmap* bm)
:
WindowGUIManipulator(),
selection(NULL)
{
preview_bitmap = NULL;
config_view = NULL;
copy_of_the_preview_bitmap = NULL;
SetPreviewBitmap(bm);
}
AntiDithererManipulator::~AntiDithererManipulator()
{
delete copy_of_the_preview_bitmap;
delete config_view;
}
BBitmap*
AntiDithererManipulator::ManipulateBitmap(
ManipulatorSettings* set, BBitmap* original, BStatusBar* status_bar)
{
AntiDithererManipulatorSettings* new_settings = cast_as(set, AntiDithererManipulatorSettings);
if (new_settings == NULL)
return NULL;
if (original == NULL)
return NULL;
if (original == preview_bitmap) {
if (*new_settings == previous_settings)
return original;
source_bitmap = copy_of_the_preview_bitmap;
target_bitmap = original;
} else {
source_bitmap = original;
target_bitmap = new BBitmap(original->Bounds(), B_RGB32, FALSE);
}
settings = *new_settings;
anti_dither();
return target_bitmap;
}
int32
AntiDithererManipulator::PreviewBitmap(bool full_quality, BRegion* updated_region)
{
updated_region->Set(preview_bitmap->Bounds());
target_bitmap = preview_bitmap;
source_bitmap = copy_of_the_preview_bitmap;
anti_dither();
return 1;
}
void
AntiDithererManipulator::SetPreviewBitmap(BBitmap* bm)
{
if (preview_bitmap != bm) {
delete copy_of_the_preview_bitmap;
if (bm != NULL) {
preview_bitmap = bm;
copy_of_the_preview_bitmap = DuplicateBitmap(bm, 0);
} else {
preview_bitmap = NULL;
copy_of_the_preview_bitmap = NULL;
}
}
}
void
AntiDithererManipulator::Reset()
{
if (copy_of_the_preview_bitmap != NULL) {
// memcpy seems to be about 10-15% faster that copying with a loop.
uint32* source = (uint32*)copy_of_the_preview_bitmap->Bits();
uint32* target = (uint32*)preview_bitmap->Bits();
uint32 bits_length = preview_bitmap->BitsLength();
memcpy(target, source, bits_length);
}
}
void
AntiDithererManipulator::anti_dither()
{
uint32* source_bits = (uint32*)source_bitmap->Bits();
uint32* target_bits = (uint32*)target_bitmap->Bits();
int32 source_bpr = source_bitmap->BytesPerRow() / 4;
int32 target_bpr = target_bitmap->BytesPerRow() / 4;
BRect bounds = source_bitmap->Bounds();
if (selection != NULL && selection->IsEmpty() == false)
bounds = selection->GetBoundingRect();
int32 width = bounds.IntegerWidth();
int32 height = bounds.IntegerHeight();
float bounds_left = bounds.left;
float bounds_right = bounds.right;
float bounds_top = bounds.top;
float bounds_bottom = bounds.bottom;
int32 block_size = settings.block_size;
union {
uint8 bytes[4];
uint32 word;
} color;
int32 step_size = 1;
if (settings.reduce_resolution)
step_size = block_size;
for (int32 y = bounds_top; y <= bounds_bottom; y += step_size) {
for (int32 x = bounds_left; x <= bounds_right; x += step_size) {
int32 right = min_c(x + block_size - 1, bounds_right);
int32 bottom = min_c(y + block_size - 1, bounds_bottom);
float red = 0;
float green = 0;
float blue = 0;
float alpha = 0;
float divider = (right - x + 1) * (bottom - y + 1);
for (int32 dy = y; dy <= bottom; dy++) {
for (int32 dx = x; dx <= right; dx++) {
color.word = *(source_bits + dy * source_bpr + dx);
red += color.bytes[2];
green += color.bytes[1];
blue += color.bytes[0];
alpha += color.bytes[3];
}
}
red /= divider;
green /= divider;
blue /= divider;
alpha /= divider;
color.bytes[2] = red;
color.bytes[1] = green;
color.bytes[0] = blue;
color.bytes[3] = alpha;
if (settings.reduce_resolution) {
for (int32 dy = y; dy <= bottom; dy++) {
for (int32 dx = x; dx <= right; dx++) {
if (selection == NULL || selection->IsEmpty() == true
|| selection->ContainsPoint(dx, dy))
*(target_bits + dy * target_bpr + dx) = color.word;
}
}
} else if (selection == NULL || selection->IsEmpty() == true
|| selection->ContainsPoint(x, y))
*(target_bits + y * target_bpr + x) = color.word;
}
}
}
BView*
AntiDithererManipulator::MakeConfigurationView(const BMessenger& target)
{
if (config_view == NULL) {
config_view = new AntiDithererManipulatorView(this, target);
config_view->ChangeSettings(&settings);
}
return config_view;
}
ManipulatorSettings*
AntiDithererManipulator::ReturnSettings()
{
return new AntiDithererManipulatorSettings(settings);
}
void
AntiDithererManipulator::ChangeSettings(ManipulatorSettings* s)
{
AntiDithererManipulatorSettings* new_settings;
new_settings = cast_as(s, AntiDithererManipulatorSettings);
if (new_settings != NULL)
settings = *new_settings;
}
const char*
AntiDithererManipulator::ReturnName()
{
return B_TRANSLATE("Anti-Dither");
}
const char*
AntiDithererManipulator::ReturnHelpString()
{
return B_TRANSLATE("Attempts to reverse the effects of dithering.");
}
AntiDithererManipulatorView::AntiDithererManipulatorView(
AntiDithererManipulator* manip, const BMessenger& t)
:
WindowGUIManipulatorView()
{
target = t;
manipulator = manip;
started_adjusting = FALSE;
block_size_control
= new BSpinner("blocksize", B_TRANSLATE("Block size:"), new BMessage(BLOCK_SIZE_ADJUSTED));
block_size_control->SetMinValue(1);
block_size_control->SetValue(1);
reduce_resolution_box = new BCheckBox("reduce_resolution", B_TRANSLATE("Reduce resolution"),
new BMessage(REDUCE_RESOLUTION_ADJUSTED));
reduce_resolution_box->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_ITEM_SPACING)
.AddGroup(B_HORIZONTAL)
.Add(block_size_control)
.AddGlue()
.End()
.Add(reduce_resolution_box)
.SetInsets(B_USE_SMALL_INSETS)
.End();
}
void
AntiDithererManipulatorView::AttachedToWindow()
{
WindowGUIManipulatorView::AttachedToWindow();
block_size_control->SetTarget(BMessenger(this));
reduce_resolution_box->SetTarget(BMessenger(this));
}
void
AntiDithererManipulatorView::AllAttached()
{
block_size_control->SetValue(settings.block_size);
reduce_resolution_box->SetValue(settings.reduce_resolution);
}
void
AntiDithererManipulatorView::MessageReceived(BMessage* message)
{
switch (message->what) {
case REDUCE_RESOLUTION_ADJUSTED:
{
settings.reduce_resolution = reduce_resolution_box->Value();
manipulator->ChangeSettings(&settings);
target.SendMessage(HS_MANIPULATOR_ADJUSTING_FINISHED);
} break;
case BLOCK_SIZE_ADJUSTED:
{
settings.block_size = block_size_control->Value();
manipulator->ChangeSettings(&settings);
target.SendMessage(HS_MANIPULATOR_ADJUSTING_FINISHED);
} break;
default:
WindowGUIManipulatorView::MessageReceived(message);
}
}
void
AntiDithererManipulatorView::ChangeSettings(ManipulatorSettings* set)
{
AntiDithererManipulatorSettings* new_settings = cast_as(set, AntiDithererManipulatorSettings);
if (set != NULL) {
settings = *new_settings;
BWindow* window = Window();
if (window != NULL) {
window->Lock();
block_size_control->SetValue(settings.block_size);
reduce_resolution_box->SetValue(settings.reduce_resolution);
window->Unlock();
}
}
}