forked from HaikuArchives/ArtPaint
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContrast.cpp
More file actions
488 lines (387 loc) · 12.3 KB
/
Copy pathContrast.cpp
File metadata and controls
488 lines (387 loc) · 12.3 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* 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 <LayoutBuilder.h>
#include <Node.h>
#include <Slider.h>
#include <StatusBar.h>
#include <Window.h>
#include <string.h>
#include "AddOns.h"
#include "Contrast.h"
#include "ManipulatorInformer.h"
#include "Selection.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "AddOns_Contrast"
#ifdef __cplusplus
extern "C" {
#endif
char name[255] = B_TRANSLATE_MARK("Contrast" B_UTF8_ELLIPSIS);
char menu_help_string[255] = B_TRANSLATE_MARK("Adjusts the contrast.");
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 ContrastManipulator(bm);
}
ContrastManipulator::ContrastManipulator(BBitmap* bm)
:
WindowGUIManipulator(),
selection(NULL)
{
preview_bitmap = NULL;
config_view = NULL;
copy_of_the_preview_bitmap = NULL;
previous_settings.contrast = settings.contrast + 1;
SetPreviewBitmap(bm);
}
ContrastManipulator::~ContrastManipulator()
{
delete copy_of_the_preview_bitmap;
delete config_view;
}
BBitmap*
ContrastManipulator::ManipulateBitmap(
ManipulatorSettings* set, BBitmap* original, BStatusBar* status_bar)
{
ContrastManipulatorSettings* new_settings = dynamic_cast<ContrastManipulatorSettings*>(set);
if (new_settings == NULL)
return NULL;
if (original == NULL)
return NULL;
if (original == preview_bitmap) {
if ((*new_settings == previous_settings) && (last_calculated_resolution <= 1))
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);
}
current_resolution = 1;
current_settings = *new_settings;
progress_bar = status_bar;
current_average_luminance = CalculateAverageLuminance(source_bitmap);
start_threads();
return target_bitmap;
}
int32
ContrastManipulator::PreviewBitmap(bool full_quality, BRegion* updated_region)
{
progress_bar = NULL;
if (settings == previous_settings) {
if ((last_calculated_resolution != highest_available_quality)
&& (last_calculated_resolution > 0)) {
last_calculated_resolution
= max_c(highest_available_quality, floor(last_calculated_resolution / 2.0));
} else
last_calculated_resolution = 0;
} else
last_calculated_resolution = lowest_available_quality;
if (full_quality)
last_calculated_resolution = min_c(1, last_calculated_resolution);
previous_settings = settings;
if (last_calculated_resolution > 0) {
current_resolution = last_calculated_resolution;
updated_region->Set(preview_bitmap->Bounds());
target_bitmap = preview_bitmap;
source_bitmap = copy_of_the_preview_bitmap;
current_settings = settings;
start_threads();
}
return last_calculated_resolution;
}
uint8
ContrastManipulator::CalculateAverageLuminance(BBitmap* bitmap)
{
uint32* bits = (uint32*)bitmap->Bits();
uint32 bits_length = bitmap->BitsLength() / 4;
double luminance_sum = 0;
union {
uint8 bytes[4];
uint32 word;
} color;
for (uint32 i = 0; i < bits_length; i++) {
color.word = *bits++;
luminance_sum += 0.299 * color.bytes[0] + 0.587 * color.bytes[1] + 0.144 * color.bytes[2];
}
luminance_sum = luminance_sum / bits_length;
uint8 sum = (uint8)luminance_sum;
return sum;
}
void
ContrastManipulator::start_threads()
{
number_of_threads = GetSystemCpuCount();
thread_id* threads = new thread_id[number_of_threads];
for (int32 i = 0; i < number_of_threads; i++) {
threads[i] = spawn_thread(thread_entry, "contrast_thread", B_NORMAL_PRIORITY, this);
resume_thread(threads[i]);
send_data(threads[i], i, NULL, 0);
}
for (int32 i = 0; i < number_of_threads; i++) {
int32 return_value;
wait_for_thread(threads[i], &return_value);
}
delete[] threads;
}
int32
ContrastManipulator::thread_entry(void* data)
{
int32 thread_number;
thread_number = receive_data(NULL, NULL, 0);
ContrastManipulator* this_pointer = (ContrastManipulator*)data;
return this_pointer->thread_function(thread_number);
}
int32
ContrastManipulator::thread_function(int32 thread_number)
{
// This function interpolates the image with a degenerate version,
// which in this case is the average luminance. The luminance image is not actually
// used, but only implied. This function does not touch the alpha-channel.
int32 step = current_resolution;
uint32 contrast = settings.contrast;
BWindow* progress_bar_window = NULL;
if (progress_bar != NULL)
progress_bar_window = progress_bar->Window();
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;
// This union must be used to guarantee endianness compatibility.
union {
uint8 bytes[4];
uint32 word;
} color;
float coeff = current_settings.contrast / 100.0;
float one_minus_coeff = 1.0 - coeff;
int32 luminance_factor = current_average_luminance * one_minus_coeff;
uint8 luminance_values[256];
for (int32 i = 0; i < 256; i++)
luminance_values[i] = max_c(0, min_c(255, i * coeff + luminance_factor));
if (selection->IsEmpty()) {
// Here handle the whole image.
int32 left = target_bitmap->Bounds().left;
int32 right = target_bitmap->Bounds().right;
int32 top = target_bitmap->Bounds().top;
int32 bottom = target_bitmap->Bounds().bottom;
float height = bottom - top;
top = height / number_of_threads * thread_number;
top = ceil(top / (float)step);
top *= step;
bottom = min_c(bottom, top + (height + 1) / number_of_threads);
int32 update_interval = 10;
float update_amount = 100.0 / (bottom - top) * update_interval / (float)number_of_threads;
float missed_update = 0;
// Loop through all pixels in original.
contrast *= 3;
for (int32 y = top; y <= bottom; y += step) {
int32 y_times_source_bpr = y * source_bpr;
int32 y_times_target_bpr = y * target_bpr;
for (int32 x = left; x <= right; x += step) {
color.word = *(source_bits + x + y_times_source_bpr);
color.bytes[0] = luminance_values[color.bytes[0]];
color.bytes[1] = luminance_values[color.bytes[1]];
color.bytes[2] = luminance_values[color.bytes[2]];
*(target_bits + x + y_times_target_bpr) = color.word;
}
// Update the status-bar
if (((y % update_interval) == 0) && (progress_bar_window != NULL)
&& (progress_bar_window->LockWithTimeout(0) == B_OK)) {
progress_bar->Update(update_amount + missed_update);
progress_bar_window->Unlock();
missed_update = 0;
} else if ((y % update_interval) == 0)
missed_update += update_amount;
}
} else {
// Here handle only those pixels for which selection->ContainsPoint(x,y) is true.
BRect rect = selection->GetBoundingRect();
int32 left = rect.left;
int32 right = rect.right;
int32 top = rect.top;
int32 bottom = rect.bottom;
float height = bottom - top;
top += height / number_of_threads * thread_number;
top *= step;
top /= step;
bottom = min_c(bottom, top + (height + 1) / number_of_threads);
int32 update_interval = 10;
float update_amount = 100.0 / (bottom - top) * update_interval / (float)number_of_threads;
// Loop through all pixels in original.
for (int32 y = top; y <= bottom; y += step) {
int32 y_times_source_bpr = y * source_bpr;
int32 y_times_target_bpr = y * target_bpr;
for (int32 x = left; x <= right; x += step) {
if (selection->ContainsPoint(x, y)) {
color.word = *(source_bits + x + y_times_source_bpr);
color.bytes[0] = luminance_values[color.bytes[0]];
color.bytes[1] = luminance_values[color.bytes[1]];
color.bytes[2] = luminance_values[color.bytes[2]];
*(target_bits + x + y_times_target_bpr) = color.word;
}
}
// Update the status-bar
if (((y % update_interval) == 0) && (progress_bar_window != NULL)
&& (progress_bar_window->LockWithTimeout(0) == B_OK)) {
progress_bar->Update(update_amount);
progress_bar_window->Unlock();
}
}
}
return B_OK;
}
void
ContrastManipulator::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;
}
}
if (preview_bitmap != NULL) {
// Let's select a resolution that can handle all the pixels at least
// 10 times in a second while assuming that one pixel calculation takes
// about 50 CPU cycles.
double speed = GetSystemClockSpeed() / (10 * 50);
BRect bounds = preview_bitmap->Bounds();
float num_pixels = (bounds.Width() + 1) * (bounds.Height() + 1);
lowest_available_quality = 1;
while ((num_pixels / lowest_available_quality / lowest_available_quality) > speed)
lowest_available_quality *= 2;
lowest_available_quality = min_c(lowest_available_quality, 16);
highest_available_quality = max_c(lowest_available_quality / 2, 1);
current_average_luminance = CalculateAverageLuminance(preview_bitmap);
} else {
lowest_available_quality = 1;
highest_available_quality = 1;
}
last_calculated_resolution = lowest_available_quality;
}
void
ContrastManipulator::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);
}
}
BView*
ContrastManipulator::MakeConfigurationView(const BMessenger& target)
{
if (config_view == NULL) {
config_view = new ContrastManipulatorView(this, target);
config_view->ChangeSettings(&settings);
}
return config_view;
}
ManipulatorSettings*
ContrastManipulator::ReturnSettings()
{
return new ContrastManipulatorSettings(settings);
}
void
ContrastManipulator::ChangeSettings(ManipulatorSettings* s)
{
ContrastManipulatorSettings* new_settings;
new_settings = dynamic_cast<ContrastManipulatorSettings*>(s);
if (new_settings != NULL)
settings = *new_settings;
}
const char*
ContrastManipulator::ReturnName()
{
return B_TRANSLATE("Contrast");
}
const char*
ContrastManipulator::ReturnHelpString()
{
return B_TRANSLATE("Adjusts the contrast.");
}
ContrastManipulatorView::ContrastManipulatorView(ContrastManipulator* manip, const BMessenger& t)
:
WindowGUIManipulatorView()
{
target = t;
manipulator = manip;
started_adjusting = FALSE;
contrast_slider = new BSlider("contrast_slider", B_TRANSLATE("Contrast:"),
new BMessage(CONTRAST_ADJUSTING_FINISHED), 0, 255, B_HORIZONTAL, B_TRIANGLE_THUMB);
contrast_slider->SetModificationMessage(new BMessage(CONTRAST_ADJUSTED));
contrast_slider->SetLimitLabels(B_TRANSLATE("Low"), B_TRANSLATE("High"));
contrast_slider->SetHashMarks(B_HASH_MARKS_BOTTOM);
contrast_slider->SetHashMarkCount(11);
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(contrast_slider)
.SetInsets(B_USE_SMALL_INSETS)
.End();
}
void
ContrastManipulatorView::AttachedToWindow()
{
WindowGUIManipulatorView::AttachedToWindow();
contrast_slider->SetTarget(BMessenger(this));
}
void
ContrastManipulatorView::AllAttached()
{
contrast_slider->SetValue(settings.contrast);
}
void
ContrastManipulatorView::MessageReceived(BMessage* message)
{
switch (message->what) {
case CONTRAST_ADJUSTED:
{
settings.contrast = contrast_slider->Value();
manipulator->ChangeSettings(&settings);
if (!started_adjusting) {
target.SendMessage(HS_MANIPULATOR_ADJUSTING_STARTED);
started_adjusting = TRUE;
}
} break;
case CONTRAST_ADJUSTING_FINISHED:
{
started_adjusting = FALSE;
settings.contrast = contrast_slider->Value();
manipulator->ChangeSettings(&settings);
target.SendMessage(HS_MANIPULATOR_ADJUSTING_FINISHED);
} break;
default:
WindowGUIManipulatorView::MessageReceived(message);
}
}
void
ContrastManipulatorView::ChangeSettings(ManipulatorSettings* set)
{
ContrastManipulatorSettings* new_settings = dynamic_cast<ContrastManipulatorSettings*>(set);
if (set != NULL) {
settings = *new_settings;
BWindow* window = Window();
if (window != NULL) {
window->Lock();
contrast_slider->SetValue(settings.contrast);
window->Unlock();
}
}
}