File: | home/HaikuArchives/ArtPaint/addons/AddOns/Brightness/Brightness.cpp |
Warning: | line 210, column 3 Value stored to 'brightness' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* |
2 | * Copyright 2003, Heikki Suhonen |
3 | * Distributed under the terms of the MIT License. |
4 | * |
5 | * Authors: |
6 | * Heikki Suhonen <heikki.suhonen@gmail.com> |
7 | * |
8 | */ |
9 | #include <Bitmap.h> |
10 | #include <Catalog.h> |
11 | #include <LayoutBuilder.h> |
12 | #include <Node.h> |
13 | #include <StatusBar.h> |
14 | #include <Slider.h> |
15 | #include <string.h> |
16 | #include <Window.h> |
17 | |
18 | #include "AddOns.h" |
19 | #include "Brightness.h" |
20 | #include "ManipulatorInformer.h" |
21 | #include "Selection.h" |
22 | |
23 | #undef B_TRANSLATION_CONTEXT"AddOns_Brightness" |
24 | #define B_TRANSLATION_CONTEXT"AddOns_Brightness" "AddOns_Brightness" |
25 | |
26 | |
27 | #ifdef __cplusplus201402L |
28 | extern "C" { |
29 | #endif |
30 | char name[255] = B_TRANSLATE_MARK("Brightness" B_UTF8_ELLIPSIS)("Brightness" "\xE2\x80\xA6"); |
31 | char menu_help_string[255] = B_TRANSLATE_MARK("Adjusts the brightness.")("Adjusts the brightness."); |
32 | int32 add_on_api_version = ADD_ON_API_VERSION; |
33 | add_on_types add_on_type = COLOR_ADD_ON; |
34 | #ifdef __cplusplus201402L |
35 | } |
36 | #endif |
37 | |
38 | |
39 | Manipulator* instantiate_add_on(BBitmap *bm,ManipulatorInformer *i) |
40 | { |
41 | delete i; |
42 | return new BrightnessManipulator(bm); |
43 | } |
44 | |
45 | |
46 | |
47 | BrightnessManipulator::BrightnessManipulator(BBitmap *bm) |
48 | : WindowGUIManipulator() |
49 | { |
50 | preview_bitmap = NULL__null; |
51 | config_view = NULL__null; |
52 | copy_of_the_preview_bitmap = NULL__null; |
53 | |
54 | previous_settings.brightness = settings.brightness + 1; |
55 | |
56 | SetPreviewBitmap(bm); |
57 | } |
58 | |
59 | |
60 | BrightnessManipulator::~BrightnessManipulator() |
61 | { |
62 | delete copy_of_the_preview_bitmap; |
63 | delete config_view; |
64 | } |
65 | |
66 | |
67 | BBitmap* BrightnessManipulator::ManipulateBitmap(ManipulatorSettings *set,BBitmap *original,Selection *selection,BStatusBar *status_bar) |
68 | { |
69 | BrightnessManipulatorSettings *new_settings = dynamic_cast<BrightnessManipulatorSettings*>(set); |
70 | |
71 | if (new_settings == NULL__null) |
72 | return NULL__null; |
73 | |
74 | if (original == NULL__null) |
75 | return NULL__null; |
76 | |
77 | if (original == preview_bitmap) { |
78 | if ((*new_settings == previous_settings) && (last_calculated_resolution <= 1)) |
79 | return original; |
80 | |
81 | source_bitmap = copy_of_the_preview_bitmap; |
82 | target_bitmap = original; |
83 | } |
84 | else { |
85 | source_bitmap = original; |
86 | target_bitmap = new BBitmap(original->Bounds(),B_RGB32,FALSE0); |
87 | } |
88 | |
89 | |
90 | current_resolution = 1; |
91 | current_selection = selection; |
92 | current_settings = *new_settings; |
93 | progress_bar = status_bar; |
94 | |
95 | start_threads(); |
96 | |
97 | return target_bitmap; |
98 | } |
99 | |
100 | int32 BrightnessManipulator::PreviewBitmap(Selection *selection,bool full_quality,BRegion *updated_region) |
101 | { |
102 | progress_bar = NULL__null; |
103 | current_selection = selection; |
104 | if (settings == previous_settings ) { |
105 | if ((last_calculated_resolution != highest_available_quality) && (last_calculated_resolution > 0)) |
106 | last_calculated_resolution = max_c(highest_available_quality,floor(last_calculated_resolution/2.0))((highest_available_quality)>(floor(last_calculated_resolution /2.0))?(highest_available_quality):(floor(last_calculated_resolution /2.0))); |
107 | else |
108 | last_calculated_resolution = 0; |
109 | } |
110 | else |
111 | last_calculated_resolution = lowest_available_quality; |
112 | |
113 | if (full_quality) { |
114 | last_calculated_resolution = min_c(1,last_calculated_resolution)((1)>(last_calculated_resolution)?(last_calculated_resolution ):(1)); |
115 | } |
116 | previous_settings = settings; |
117 | |
118 | if (last_calculated_resolution > 0) { |
119 | current_resolution = last_calculated_resolution; |
120 | updated_region->Set(preview_bitmap->Bounds()); |
121 | |
122 | target_bitmap = preview_bitmap; |
123 | source_bitmap = copy_of_the_preview_bitmap; |
124 | current_settings = settings; |
125 | |
126 | start_threads(); |
127 | } |
128 | |
129 | return last_calculated_resolution; |
130 | } |
131 | |
132 | |
133 | void BrightnessManipulator::start_threads() |
134 | { |
135 | number_of_threads = GetSystemCpuCount(); |
136 | |
137 | thread_id *threads = new thread_id[number_of_threads]; |
138 | |
139 | for (int32 i=0;i<number_of_threads;i++) { |
140 | threads[i] = spawn_thread(thread_entrythread_func,"brightness_thread",B_NORMAL_PRIORITY10,this); |
141 | resume_thread(threads[i]); |
142 | send_data(threads[i],i,NULL__null,0); |
143 | } |
144 | |
145 | for (int32 i=0;i<number_of_threads;i++) { |
146 | int32 return_value; |
147 | wait_for_thread(threads[i],&return_value); |
148 | } |
149 | |
150 | delete[] threads; |
151 | } |
152 | |
153 | int32 BrightnessManipulator::thread_entrythread_func(void *data) |
154 | { |
155 | int32 thread_number; |
156 | thread_number = receive_data(NULL__null,NULL__null,0); |
157 | |
158 | BrightnessManipulator *this_pointer = (BrightnessManipulator*)data; |
159 | |
160 | return this_pointer->thread_function(thread_number); |
161 | } |
162 | |
163 | |
164 | int32 BrightnessManipulator::thread_function(int32 thread_number) |
165 | { |
166 | // This function interpolates the image with a degenerate version, |
167 | // which in this case is black. The black image is not actually |
168 | // used, but the idea a*X + (1-a)*0 = a*X is used instead. This function |
169 | // does not touch the alpha-channel. |
170 | |
171 | int32 step = current_resolution; |
172 | uint32 brightness = settings.brightness; |
173 | |
174 | BWindow *progress_bar_window = NULL__null; |
175 | if (progress_bar != NULL__null) |
176 | progress_bar_window = progress_bar->Window(); |
177 | |
178 | |
179 | uint32 *source_bits = (uint32*)source_bitmap->Bits(); |
180 | uint32 *target_bits = (uint32*)target_bitmap->Bits(); |
181 | int32 source_bpr = source_bitmap->BytesPerRow()/4; |
182 | int32 target_bpr = target_bitmap->BytesPerRow()/4; |
183 | |
184 | // This union must be used to guarantee endianness compatibility. |
185 | union { |
186 | uint8 bytes[4]; |
187 | uint32 word; |
188 | } color; |
189 | |
190 | float coeff = current_settings.brightness / 100.0; |
191 | |
192 | if (current_selection->IsEmpty()) { |
193 | // Here handle the whole image. |
194 | int32 left = target_bitmap->Bounds().left; |
195 | int32 right = target_bitmap->Bounds().right; |
196 | int32 top = target_bitmap->Bounds().top; |
197 | int32 bottom = target_bitmap->Bounds().bottom; |
198 | |
199 | float height = bottom - top; |
200 | top = height/number_of_threads*thread_number; |
201 | top = ceil(top/(float)step); |
202 | top *= step; |
203 | bottom = min_c(bottom,top + (height+1)/number_of_threads)((bottom)>(top + (height+1)/number_of_threads)?(top + (height +1)/number_of_threads):(bottom)); |
204 | int32 update_interval = 10; |
205 | float update_amount = 100.0/(bottom-top)*update_interval/(float)number_of_threads; |
206 | float missed_update = 0; |
207 | |
208 | // Loop through all pixels in original. |
209 | uint32 sum; |
210 | brightness *= 3; |
Value stored to 'brightness' is never read | |
211 | uint8 brighness_array[256]; |
212 | for (int32 i=0;i<256;i++) { |
213 | brighness_array[i] = min_c(255,i * coeff)((255)>(i * coeff)?(i * coeff):(255)); |
214 | } |
215 | for (int32 y=top;y<=bottom;y+=step) { |
216 | int32 y_times_source_bpr = y*source_bpr; |
217 | int32 y_times_target_bpr = y*target_bpr; |
218 | for (int32 x=left;x<=right;x+=step) { |
219 | color.word = *(source_bits + x + y_times_source_bpr); |
220 | color.bytes[0] = brighness_array[color.bytes[0]]; |
221 | color.bytes[1] = brighness_array[color.bytes[1]]; |
222 | color.bytes[2] = brighness_array[color.bytes[2]]; |
223 | *(target_bits + x + y_times_target_bpr) = color.word; |
224 | } |
225 | |
226 | // Update the status-bar |
227 | if ( ((y % update_interval) == 0) && (progress_bar_window != NULL__null) && (progress_bar_window->LockWithTimeout(0) == B_OK((int)0)) ) { |
228 | progress_bar->Update(update_amount+missed_update); |
229 | progress_bar_window->Unlock(); |
230 | missed_update = 0; |
231 | } |
232 | else if ((y % update_interval) == 0) { |
233 | missed_update += update_amount; |
234 | } |
235 | } |
236 | } |
237 | else { |
238 | // Here handle only those pixels for which selection->ContainsPoint(x,y) is true. |
239 | BRect rect = current_selection->GetBoundingRect(); |
240 | |
241 | int32 left = rect.left; |
242 | int32 right = rect.right; |
243 | int32 top = rect.top; |
244 | int32 bottom = rect.bottom; |
245 | |
246 | float height = bottom - top; |
247 | top += height/number_of_threads*thread_number; |
248 | top *= step; |
249 | top /= step; |
250 | |
251 | bottom = min_c(bottom,top + (height+1)/number_of_threads)((bottom)>(top + (height+1)/number_of_threads)?(top + (height +1)/number_of_threads):(bottom)); |
252 | |
253 | int32 update_interval = 10; |
254 | float update_amount = 100.0/(bottom-top)*update_interval/(float)number_of_threads; |
255 | uint8 brighness_array[256]; |
256 | for (int32 i=0;i<256;i++) { |
257 | brighness_array[i] = min_c(255,i * coeff)((255)>(i * coeff)?(i * coeff):(255)); |
258 | } |
259 | |
260 | // Loop through all pixels in original. |
261 | for (int32 y=top;y<=bottom;y+=step) { |
262 | int32 y_times_source_bpr = y*source_bpr; |
263 | int32 y_times_target_bpr = y*target_bpr; |
264 | for (int32 x=left;x<=right;x+=step) { |
265 | if (current_selection->ContainsPoint(x,y)) { |
266 | color.word = *(source_bits + x + y_times_source_bpr); |
267 | color.bytes[0] = brighness_array[color.bytes[0]]; |
268 | color.bytes[1] = brighness_array[color.bytes[1]]; |
269 | color.bytes[2] = brighness_array[color.bytes[2]]; |
270 | *(target_bits + x + y_times_target_bpr) = color.word; |
271 | } |
272 | } |
273 | |
274 | // Update the status-bar |
275 | if ( ((y % update_interval) == 0) && (progress_bar_window != NULL__null) && (progress_bar_window->LockWithTimeout(0) == B_OK((int)0)) ) { |
276 | progress_bar->Update(update_amount); |
277 | progress_bar_window->Unlock(); |
278 | } |
279 | } |
280 | } |
281 | |
282 | return B_OK((int)0); |
283 | } |
284 | |
285 | |
286 | void BrightnessManipulator::MouseDown(BPoint point,uint32,BView*,bool first_click) |
287 | { |
288 | // This function does nothing in BrightnessManipulator. |
289 | } |
290 | |
291 | |
292 | void BrightnessManipulator::SetPreviewBitmap(BBitmap *bm) |
293 | { |
294 | if (preview_bitmap != bm) { |
295 | delete copy_of_the_preview_bitmap; |
296 | if (bm != NULL__null) { |
297 | preview_bitmap = bm; |
298 | copy_of_the_preview_bitmap = DuplicateBitmap(bm,0); |
299 | } |
300 | else { |
301 | preview_bitmap = NULL__null; |
302 | copy_of_the_preview_bitmap = NULL__null; |
303 | } |
304 | } |
305 | |
306 | if (preview_bitmap != NULL__null) { |
307 | // Let's select a resolution that can handle all the pixels at least |
308 | // 10 times in a second while assuming that one pixel calculation takes |
309 | // about 50 CPU cycles. |
310 | double speed = GetSystemClockSpeed() / (10*50); |
311 | BRect bounds = preview_bitmap->Bounds(); |
312 | float num_pixels = (bounds.Width()+1) * (bounds.Height() + 1); |
313 | lowest_available_quality = 1; |
314 | while ((num_pixels/lowest_available_quality/lowest_available_quality) > speed) |
315 | lowest_available_quality *= 2; |
316 | |
317 | lowest_available_quality = min_c(lowest_available_quality,16)((lowest_available_quality)>(16)?(16):(lowest_available_quality )); |
318 | highest_available_quality = max_c(lowest_available_quality/2,1)((lowest_available_quality/2)>(1)?(lowest_available_quality /2):(1)); |
319 | } |
320 | else { |
321 | lowest_available_quality = 1; |
322 | highest_available_quality = 1; |
323 | } |
324 | last_calculated_resolution = lowest_available_quality; |
325 | } |
326 | |
327 | |
328 | void BrightnessManipulator::Reset(Selection*) |
329 | { |
330 | if (copy_of_the_preview_bitmap != NULL__null) { |
331 | // memcpy seems to be about 10-15% faster that copying with a loop. |
332 | uint32 *source = (uint32*)copy_of_the_preview_bitmap->Bits(); |
333 | uint32 *target = (uint32*)preview_bitmap->Bits(); |
334 | uint32 bits_length = preview_bitmap->BitsLength(); |
335 | |
336 | memcpy(target,source,bits_length); |
337 | } |
338 | } |
339 | |
340 | BView* BrightnessManipulator::MakeConfigurationView(const BMessenger& target) |
341 | { |
342 | if (config_view == NULL__null) { |
343 | config_view = new BrightnessManipulatorView(this,target); |
344 | config_view->ChangeSettings(&settings); |
345 | } |
346 | |
347 | return config_view; |
348 | } |
349 | |
350 | |
351 | ManipulatorSettings* BrightnessManipulator::ReturnSettings() |
352 | { |
353 | return new BrightnessManipulatorSettings(settings); |
354 | } |
355 | |
356 | void BrightnessManipulator::ChangeSettings(ManipulatorSettings *s) |
357 | { |
358 | BrightnessManipulatorSettings *new_settings; |
359 | new_settings = dynamic_cast<BrightnessManipulatorSettings*>(s); |
360 | if (new_settings != NULL__null) { |
361 | settings = *new_settings; |
362 | } |
363 | } |
364 | |
365 | const char* BrightnessManipulator::ReturnName() |
366 | { |
367 | return B_TRANSLATE("Brightness")BLocaleRoster::Default()->GetCatalog()->GetString(("Brightness" ), "AddOns_Brightness"); |
368 | } |
369 | |
370 | const char* BrightnessManipulator::ReturnHelpString() |
371 | { |
372 | return B_TRANSLATE("Adjusts the brightness.")BLocaleRoster::Default()->GetCatalog()->GetString(("Adjusts the brightness." ), "AddOns_Brightness"); |
373 | } |
374 | |
375 | |
376 | |
377 | |
378 | // ------------------------------------- |
379 | BrightnessManipulatorView::BrightnessManipulatorView(BrightnessManipulator *manip, |
380 | const BMessenger& t) |
381 | : WindowGUIManipulatorView() |
382 | { |
383 | target = t; |
384 | manipulator = manip; |
385 | started_adjusting = FALSE0; |
386 | |
387 | brightness_slider = new BSlider("brightness_slider", |
388 | B_TRANSLATE("Brightness:")BLocaleRoster::Default()->GetCatalog()->GetString(("Brightness:" ), "AddOns_Brightness"), new BMessage(BRIGHTNESS_ADJUSTING_FINISHED'Thaf'), 0, 255, |
389 | B_HORIZONTAL, B_TRIANGLE_THUMB); |
390 | brightness_slider->SetModificationMessage(new BMessage(BRIGHTNESS_ADJUSTED'Thad')); |
391 | brightness_slider->SetLimitLabels(B_TRANSLATE("Low")BLocaleRoster::Default()->GetCatalog()->GetString(("Low" ), "AddOns_Brightness"), B_TRANSLATE("High")BLocaleRoster::Default()->GetCatalog()->GetString(("High" ), "AddOns_Brightness")); |
392 | brightness_slider->SetHashMarks(B_HASH_MARKS_BOTTOM); |
393 | brightness_slider->SetHashMarkCount(11); |
394 | |
395 | BLayoutBuilder::Group<>(this, B_VERTICAL) |
396 | .Add(brightness_slider) |
397 | .SetInsets(B_USE_SMALL_INSETS) |
398 | .End(); |
399 | } |
400 | |
401 | |
402 | BrightnessManipulatorView::~BrightnessManipulatorView() |
403 | { |
404 | } |
405 | |
406 | |
407 | void BrightnessManipulatorView::AttachedToWindow() |
408 | { |
409 | WindowGUIManipulatorView::AttachedToWindow(); |
410 | brightness_slider->SetTarget(BMessenger(this)); |
411 | } |
412 | |
413 | void BrightnessManipulatorView::AllAttached() |
414 | { |
415 | brightness_slider->SetValue(settings.brightness); |
416 | } |
417 | |
418 | |
419 | void BrightnessManipulatorView::MessageReceived(BMessage *message) |
420 | { |
421 | switch (message->what) { |
422 | case BRIGHTNESS_ADJUSTED'Thad': |
423 | settings.brightness = brightness_slider->Value(); |
424 | manipulator->ChangeSettings(&settings); |
425 | if (!started_adjusting) { |
426 | target.SendMessage(HS_MANIPULATOR_ADJUSTING_STARTED'Mast'); |
427 | started_adjusting = TRUE1; |
428 | } |
429 | break; |
430 | |
431 | case BRIGHTNESS_ADJUSTING_FINISHED'Thaf': |
432 | started_adjusting = FALSE0; |
433 | settings.brightness = brightness_slider->Value(); |
434 | manipulator->ChangeSettings(&settings); |
435 | target.SendMessage(HS_MANIPULATOR_ADJUSTING_FINISHED'Mafi'); |
436 | break; |
437 | |
438 | default: |
439 | WindowGUIManipulatorView::MessageReceived(message); |
440 | break; |
441 | } |
442 | } |
443 | |
444 | |
445 | void BrightnessManipulatorView::ChangeSettings(ManipulatorSettings *set) |
446 | { |
447 | BrightnessManipulatorSettings *new_settings = dynamic_cast<BrightnessManipulatorSettings*>(set); |
448 | |
449 | if (set != NULL__null) { |
450 | settings = *new_settings; |
451 | |
452 | BWindow *window = Window(); |
453 | if (window != NULL__null) { |
454 | window->Lock(); |
455 | brightness_slider->SetValue(settings.brightness); |
456 | window->Unlock(); |
457 | } |
458 | } |
459 | } |