File: | home/HaikuArchives/ArtPaint/addons/AddOns/ColorBalance/ColorBalance.cpp |
Warning: | line 99, column 8 Value stored to 'target_bpr' during its initialization 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 "AddOns.h" |
10 | #include "ColorBalance.h" |
11 | #include "ManipulatorInformer.h" |
12 | #include "Selection.h" |
13 | |
14 | #include <Bitmap.h> |
15 | #include <Catalog.h> |
16 | #include <LayoutBuilder.h> |
17 | #include <Slider.h> |
18 | |
19 | #undef B_TRANSLATION_CONTEXT"AddOns_ColorBalance" |
20 | #define B_TRANSLATION_CONTEXT"AddOns_ColorBalance" "AddOns_ColorBalance" |
21 | |
22 | |
23 | #ifdef __cplusplus201402L |
24 | extern "C" { |
25 | #endif |
26 | char name[255] = B_TRANSLATE_MARK("Color balance" B_UTF8_ELLIPSIS)("Color balance" "\xE2\x80\xA6"); |
27 | char menu_help_string[255] = B_TRANSLATE_MARK("Adjusts the color balance.")("Adjusts the color balance."); |
28 | int32 add_on_api_version = ADD_ON_API_VERSION; |
29 | add_on_types add_on_type = COLOR_ADD_ON; |
30 | #ifdef __cplusplus201402L |
31 | } |
32 | #endif |
33 | |
34 | |
35 | Manipulator* instantiate_add_on(BBitmap *bm,ManipulatorInformer *i) |
36 | { |
37 | delete i; |
38 | return new ColorBalanceManipulator(bm); |
39 | } |
40 | |
41 | |
42 | |
43 | |
44 | ColorBalanceManipulator::ColorBalanceManipulator(BBitmap *bm) |
45 | : WindowGUIManipulator() |
46 | { |
47 | preview_bitmap = NULL__null; |
48 | copy_of_the_preview_bitmap = NULL__null; |
49 | config_view = NULL__null; |
50 | |
51 | SetPreviewBitmap(bm); |
52 | lowest_allowed_quality = 2; |
53 | last_used_quality = 2; |
54 | } |
55 | |
56 | |
57 | ColorBalanceManipulator::~ColorBalanceManipulator() |
58 | { |
59 | delete copy_of_the_preview_bitmap; |
60 | |
61 | if (config_view != NULL__null) { |
62 | config_view->RemoveSelf(); |
63 | delete config_view; |
64 | config_view = NULL__null; |
65 | } |
66 | } |
67 | |
68 | BBitmap* ColorBalanceManipulator::ManipulateBitmap(ManipulatorSettings *set,BBitmap *original,Selection *selection,BStatusBar *status_bar) |
69 | { |
70 | ColorBalanceManipulatorSettings *new_settings = dynamic_cast<ColorBalanceManipulatorSettings*>(set); |
71 | |
72 | if (new_settings == NULL__null) |
73 | return NULL__null; |
74 | |
75 | if (original == NULL__null) |
76 | return NULL__null; |
77 | |
78 | if ((new_settings->red_difference == 0) &&(new_settings->green_difference == 0) && (new_settings->blue_difference == 0)) |
79 | return NULL__null; |
80 | |
81 | BBitmap *source_bitmap; |
82 | BBitmap *target_bitmap; |
83 | BBitmap *new_bitmap = NULL__null; |
84 | |
85 | if (original == preview_bitmap) { |
86 | target_bitmap = original; |
87 | source_bitmap = copy_of_the_preview_bitmap; |
88 | } |
89 | else { |
90 | target_bitmap = original; |
91 | new_bitmap = DuplicateBitmap(original,0); |
92 | source_bitmap = new_bitmap; |
93 | } |
94 | |
95 | |
96 | uint32 *source_bits = (uint32*)source_bitmap->Bits(); |
97 | uint32 *target_bits = (uint32*)target_bitmap->Bits(); |
98 | int32 source_bpr = source_bitmap->BytesPerRow()/4; |
99 | int32 target_bpr = target_bitmap->BytesPerRow()/4; |
Value stored to 'target_bpr' during its initialization is never read | |
100 | |
101 | |
102 | int32 left = target_bitmap->Bounds().left; |
103 | int32 right = target_bitmap->Bounds().right; |
104 | int32 top = target_bitmap->Bounds().top; |
105 | int32 bottom = target_bitmap->Bounds().bottom; |
106 | |
107 | union { |
108 | uint8 bytes[4]; |
109 | uint32 word; |
110 | } color; |
111 | |
112 | if (selection->IsEmpty() == TRUE1) { |
113 | for (int32 y=top;y<=bottom;y++) { |
114 | for (int32 x=left;x<=right;x++) { |
115 | color.word = *source_bits++; |
116 | color.bytes[0] = max_c(min_c(255,color.bytes[0]+new_settings->blue_difference),0)((((255)>(color.bytes[0]+new_settings->blue_difference) ?(color.bytes[0]+new_settings->blue_difference):(255)))> (0)?(((255)>(color.bytes[0]+new_settings->blue_difference )?(color.bytes[0]+new_settings->blue_difference):(255))):( 0)); |
117 | color.bytes[1] = max_c(min_c(255,color.bytes[1]+new_settings->green_difference),0)((((255)>(color.bytes[1]+new_settings->green_difference )?(color.bytes[1]+new_settings->green_difference):(255)))> (0)?(((255)>(color.bytes[1]+new_settings->green_difference )?(color.bytes[1]+new_settings->green_difference):(255))): (0)); |
118 | color.bytes[2] = max_c(min_c(255,color.bytes[2]+new_settings->red_difference),0)((((255)>(color.bytes[2]+new_settings->red_difference)? (color.bytes[2]+new_settings->red_difference):(255)))>( 0)?(((255)>(color.bytes[2]+new_settings->red_difference )?(color.bytes[2]+new_settings->red_difference):(255))):(0 )); |
119 | *target_bits++ = color.word; |
120 | } |
121 | } |
122 | } |
123 | else { |
124 | for (int32 y=top;y<=bottom;y++) { |
125 | for (int32 x=left;x<=right;x++) { |
126 | if (selection->ContainsPoint(x,y)) { |
127 | color.word = *source_bits++; |
128 | color.bytes[0] = max_c(min_c(255,color.bytes[0]+new_settings->blue_difference),0)((((255)>(color.bytes[0]+new_settings->blue_difference) ?(color.bytes[0]+new_settings->blue_difference):(255)))> (0)?(((255)>(color.bytes[0]+new_settings->blue_difference )?(color.bytes[0]+new_settings->blue_difference):(255))):( 0)); |
129 | color.bytes[1] = max_c(min_c(255,color.bytes[1]+new_settings->green_difference),0)((((255)>(color.bytes[1]+new_settings->green_difference )?(color.bytes[1]+new_settings->green_difference):(255)))> (0)?(((255)>(color.bytes[1]+new_settings->green_difference )?(color.bytes[1]+new_settings->green_difference):(255))): (0)); |
130 | color.bytes[2] = max_c(min_c(255,color.bytes[2]+new_settings->red_difference),0)((((255)>(color.bytes[2]+new_settings->red_difference)? (color.bytes[2]+new_settings->red_difference):(255)))>( 0)?(((255)>(color.bytes[2]+new_settings->red_difference )?(color.bytes[2]+new_settings->red_difference):(255))):(0 )); |
131 | *target_bits++ = color.word; |
132 | } |
133 | else { |
134 | *target_bits++ = *source_bits++; |
135 | } |
136 | } |
137 | } |
138 | } |
139 | if (new_bitmap != NULL__null) { |
140 | delete new_bitmap; |
141 | } |
142 | |
143 | return original; |
144 | } |
145 | |
146 | |
147 | int32 ColorBalanceManipulator::PreviewBitmap(Selection *selection,bool full_quality,BRegion *updated_region) |
148 | { |
149 | if ((settings == previous_settings) == FALSE0) { |
150 | previous_settings = settings; |
151 | last_used_quality = lowest_allowed_quality; |
152 | } |
153 | else { |
154 | last_used_quality = floor(last_used_quality/2.0); |
155 | } |
156 | if (full_quality == TRUE1) |
157 | last_used_quality = min_c(last_used_quality,1)((last_used_quality)>(1)?(1):(last_used_quality)); |
158 | |
159 | uint32 *source_bits = (uint32*)copy_of_the_preview_bitmap->Bits(); |
160 | uint32 *target_bits = (uint32*)preview_bitmap->Bits(); |
161 | int32 source_bpr = copy_of_the_preview_bitmap->BytesPerRow()/4; |
162 | int32 target_bpr = preview_bitmap->BytesPerRow()/4; |
163 | |
164 | int32 left = preview_bitmap->Bounds().left; |
165 | int32 right = preview_bitmap->Bounds().right; |
166 | int32 top = preview_bitmap->Bounds().top; |
167 | int32 bottom = preview_bitmap->Bounds().bottom; |
168 | |
169 | |
170 | if (last_used_quality > 0) { |
171 | union { |
172 | uint8 bytes[4]; |
173 | uint32 word; |
174 | } color; |
175 | uint8 blue_array[256]; |
176 | uint8 green_array[256]; |
177 | uint8 red_array[256]; |
178 | |
179 | for (int32 i=0;i<256;i++) { |
180 | blue_array[i] = max_c(min_c(255,i+settings.blue_difference),0)((((255)>(i+settings.blue_difference)?(i+settings.blue_difference ):(255)))>(0)?(((255)>(i+settings.blue_difference)?(i+settings .blue_difference):(255))):(0)); |
181 | green_array[i] = max_c(min_c(255,i+settings.green_difference),0)((((255)>(i+settings.green_difference)?(i+settings.green_difference ):(255)))>(0)?(((255)>(i+settings.green_difference)?(i+ settings.green_difference):(255))):(0)); |
182 | red_array[i] = max_c(min_c(255,i+settings.red_difference),0)((((255)>(i+settings.red_difference)?(i+settings.red_difference ):(255)))>(0)?(((255)>(i+settings.red_difference)?(i+settings .red_difference):(255))):(0)); |
183 | } |
184 | |
185 | if (selection->IsEmpty() == TRUE1) { |
186 | for (int32 y=top;y<=bottom;y += last_used_quality) { |
187 | for (int32 x=left;x<=right;x += last_used_quality) { |
188 | color.word = *(source_bits + x + y*source_bpr); |
189 | color.bytes[0] = blue_array[color.bytes[0]]; |
190 | color.bytes[1] = green_array[color.bytes[1]]; |
191 | color.bytes[2] = red_array[color.bytes[2]]; |
192 | *(target_bits + x + y*target_bpr) = color.word; |
193 | } |
194 | } |
195 | } |
196 | else { |
197 | for (int32 y=top;y<=bottom;y += last_used_quality) { |
198 | for (int32 x=left;x<=right;x += last_used_quality) { |
199 | if (selection->ContainsPoint(x,y)) { |
200 | color.word = *(source_bits + x + y*source_bpr); |
201 | color.bytes[0] = blue_array[color.bytes[0]]; |
202 | color.bytes[1] = green_array[color.bytes[1]]; |
203 | color.bytes[2] = red_array[color.bytes[2]]; |
204 | *(target_bits + x + y*target_bpr) = color.word; |
205 | } |
206 | else { |
207 | *(target_bits + x + y*target_bpr) = *(source_bits + x + y*source_bpr); |
208 | } |
209 | } |
210 | } |
211 | } |
212 | } |
213 | |
214 | updated_region->Set(preview_bitmap->Bounds()); |
215 | return last_used_quality; |
216 | } |
217 | |
218 | void ColorBalanceManipulator::SetPreviewBitmap(BBitmap *bm) |
219 | { |
220 | if (preview_bitmap != bm) { |
221 | delete copy_of_the_preview_bitmap; |
222 | if (bm != NULL__null) { |
223 | preview_bitmap = bm; |
224 | copy_of_the_preview_bitmap = DuplicateBitmap(bm,0); |
225 | } |
226 | else { |
227 | preview_bitmap = NULL__null; |
228 | copy_of_the_preview_bitmap = NULL__null; |
229 | } |
230 | } |
231 | } |
232 | |
233 | const char* ColorBalanceManipulator::ReturnHelpString() |
234 | { |
235 | return B_TRANSLATE("Adjusts the color balance.")BLocaleRoster::Default()->GetCatalog()->GetString(("Adjusts the color balance." ), "AddOns_ColorBalance"); |
236 | } |
237 | |
238 | const char* ColorBalanceManipulator::ReturnName() |
239 | { |
240 | return B_TRANSLATE("Color balance")BLocaleRoster::Default()->GetCatalog()->GetString(("Color balance" ), "AddOns_ColorBalance"); |
241 | } |
242 | |
243 | void ColorBalanceManipulator::Reset(Selection*) |
244 | { |
245 | if (preview_bitmap != NULL__null) { |
246 | uint32 *source_bits = (uint32*)copy_of_the_preview_bitmap->Bits(); |
247 | uint32 *target_bits = (uint32*)preview_bitmap->Bits(); |
248 | |
249 | int32 bits_length = preview_bitmap->BitsLength()/4; |
250 | |
251 | for (int32 i=0;i<bits_length;i++) |
252 | *target_bits++ = *source_bits++; |
253 | } |
254 | } |
255 | |
256 | |
257 | BView* ColorBalanceManipulator::MakeConfigurationView(const BMessenger& target) |
258 | { |
259 | config_view = new ColorBalanceManipulatorView(BRect(0,0,0,0),this,target); |
260 | config_view->ChangeSettings(settings); |
261 | |
262 | return config_view; |
263 | } |
264 | |
265 | |
266 | ManipulatorSettings* ColorBalanceManipulator::ReturnSettings() |
267 | { |
268 | return new ColorBalanceManipulatorSettings(&settings); |
269 | } |
270 | |
271 | void ColorBalanceManipulator::ChangeSettings(ManipulatorSettings *s) |
272 | { |
273 | ColorBalanceManipulatorSettings *new_settings = dynamic_cast<ColorBalanceManipulatorSettings*>(s); |
274 | |
275 | if (new_settings != NULL__null) { |
276 | previous_settings = settings; |
277 | settings = *new_settings; |
278 | } |
279 | } |
280 | |
281 | |
282 | |
283 | ColorBalanceManipulatorView::ColorBalanceManipulatorView(BRect rect, |
284 | ColorBalanceManipulator *manip, const BMessenger& t) |
285 | : WindowGUIManipulatorView() |
286 | { |
287 | target = new BMessenger(t); |
288 | manipulator = manip; |
289 | preview_started = FALSE0; |
290 | rgb_color color; |
291 | |
292 | red_slider = new BSlider("red_slider", NULL__null, |
293 | new BMessage(HS_MANIPULATOR_ADJUSTING_FINISHED'Mafi'), -255, 255, |
294 | B_HORIZONTAL, B_TRIANGLE_THUMB); |
295 | // red_slider->SetLimitLabels(B_TRANSLATE("Less"), B_TRANSLATE("More")); |
296 | red_slider->SetHashMarks(B_HASH_MARKS_BOTTOM); |
297 | red_slider->SetHashMarkCount(11); |
298 | red_slider->SetModificationMessage(new BMessage(HS_MANIPULATOR_ADJUSTING_STARTED'Mast')); |
299 | color.red = 255; |
300 | color.blue = 0; |
301 | color.green = 0; |
302 | color.alpha = 255; |
303 | red_slider->SetBarColor(color); |
304 | red_slider->SetBarThickness(8); |
305 | |
306 | green_slider = new BSlider("green_slider", NULL__null, |
307 | new BMessage(HS_MANIPULATOR_ADJUSTING_FINISHED'Mafi'), -255, 255, |
308 | B_HORIZONTAL, B_TRIANGLE_THUMB); |
309 | // green_slider->SetLimitLabels(B_TRANSLATE("Less"), B_TRANSLATE("More")); |
310 | green_slider->SetHashMarks(B_HASH_MARKS_BOTTOM); |
311 | green_slider->SetHashMarkCount(11); |
312 | green_slider->SetModificationMessage(new BMessage(HS_MANIPULATOR_ADJUSTING_STARTED'Mast')); |
313 | color.red = 0; |
314 | color.blue = 0; |
315 | color.green = 255; |
316 | color.alpha = 255; |
317 | green_slider->SetBarColor(color); |
318 | green_slider->SetBarThickness(8); |
319 | |
320 | blue_slider = new BSlider("blue_slider", NULL__null, |
321 | new BMessage(HS_MANIPULATOR_ADJUSTING_FINISHED'Mafi'), -255, 255, |
322 | B_HORIZONTAL, B_TRIANGLE_THUMB); |
323 | blue_slider->SetLimitLabels(B_TRANSLATE("Less")BLocaleRoster::Default()->GetCatalog()->GetString(("Less" ), "AddOns_ColorBalance"), B_TRANSLATE("More")BLocaleRoster::Default()->GetCatalog()->GetString(("More" ), "AddOns_ColorBalance")); |
324 | blue_slider->SetHashMarks(B_HASH_MARKS_BOTTOM); |
325 | blue_slider->SetHashMarkCount(11); |
326 | blue_slider->SetModificationMessage(new BMessage(HS_MANIPULATOR_ADJUSTING_STARTED'Mast')); |
327 | color.red = 0; |
328 | color.blue = 255; |
329 | color.green = 0; |
330 | color.alpha = 255; |
331 | blue_slider->SetBarColor(color); |
332 | blue_slider->SetBarThickness(8); |
333 | |
334 | BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_ITEM_SPACING) |
335 | .Add(red_slider) |
336 | .Add(green_slider) |
337 | .Add(blue_slider) |
338 | .SetInsets(B_USE_SMALL_INSETS) |
339 | .End(); |
340 | } |
341 | |
342 | ColorBalanceManipulatorView::~ColorBalanceManipulatorView() |
343 | { |
344 | delete target; |
345 | } |
346 | |
347 | void ColorBalanceManipulatorView::AttachedToWindow() |
348 | { |
349 | WindowGUIManipulatorView::AttachedToWindow(); |
350 | |
351 | red_slider->SetTarget(BMessenger(this)); |
352 | blue_slider->SetTarget(BMessenger(this)); |
353 | green_slider->SetTarget(BMessenger(this)); |
354 | } |
355 | |
356 | |
357 | |
358 | void ColorBalanceManipulatorView::MessageReceived(BMessage *message) |
359 | { |
360 | switch (message->what) { |
361 | case HS_MANIPULATOR_ADJUSTING_FINISHED'Mafi': |
362 | preview_started = FALSE0; |
363 | settings.red_difference = red_slider->Value(); |
364 | settings.blue_difference = blue_slider->Value(); |
365 | settings.green_difference = green_slider->Value(); |
366 | manipulator->ChangeSettings(&settings); |
367 | target->SendMessage(HS_MANIPULATOR_ADJUSTING_FINISHED'Mafi'); |
368 | break; |
369 | |
370 | case HS_MANIPULATOR_ADJUSTING_STARTED'Mast': |
371 | settings.red_difference = red_slider->Value(); |
372 | settings.blue_difference = blue_slider->Value(); |
373 | settings.green_difference = green_slider->Value(); |
374 | manipulator->ChangeSettings(&settings); |
375 | if (preview_started == FALSE0) { |
376 | preview_started = TRUE1; |
377 | target->SendMessage(HS_MANIPULATOR_ADJUSTING_STARTED'Mast'); |
378 | } |
379 | break; |
380 | |
381 | default: |
382 | WindowGUIManipulatorView::MessageReceived(message); |
383 | break; |
384 | } |
385 | } |
386 | |
387 | |
388 | |
389 | void ColorBalanceManipulatorView::ChangeSettings(ColorBalanceManipulatorSettings s) |
390 | { |
391 | settings = s; |
392 | } |