File: | home/HaikuArchives/ArtPaint/addons/AddOns/Oil/OilAddOn.cpp |
Warning: | line 93, column 8 Value stored to 'width_times_height' 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 <Bitmap.h> |
10 | #include <Catalog.h> |
11 | #include <Message.h> |
12 | #include <StatusBar.h> |
13 | #include <stdlib.h> |
14 | #include <Window.h> |
15 | |
16 | #include "AddOns.h" |
17 | #include "OilAddOn.h" |
18 | #include "BitmapDrawer.h" |
19 | #include "ManipulatorInformer.h" |
20 | #include "Selection.h" |
21 | |
22 | #undef B_TRANSLATION_CONTEXT"AddOns_Oil" |
23 | #define B_TRANSLATION_CONTEXT"AddOns_Oil" "AddOns_Oil" |
24 | |
25 | |
26 | #ifdef __cplusplus201402L |
27 | extern "C" { |
28 | #endif |
29 | char name[255] = B_TRANSLATE_MARK("Oil")("Oil"); |
30 | char menu_help_string[255] = B_TRANSLATE_MARK("Creates an \"oil\" effect.")("Creates an \"oil\" effect."); |
31 | int32 add_on_api_version = ADD_ON_API_VERSION; |
32 | add_on_types add_on_type = EFFECT_FILTER_ADD_ON; |
33 | #ifdef __cplusplus201402L |
34 | } |
35 | #endif |
36 | |
37 | |
38 | Manipulator* instantiate_add_on(BBitmap *bm,ManipulatorInformer *i) |
39 | { |
40 | delete i; |
41 | return new OilManipulator(bm); |
42 | } |
43 | |
44 | |
45 | OilManipulator::OilManipulator(BBitmap*) |
46 | : Manipulator() |
47 | { |
48 | } |
49 | |
50 | |
51 | OilManipulator::~OilManipulator() |
52 | { |
53 | |
54 | } |
55 | |
56 | BBitmap* OilManipulator::ManipulateBitmap(BBitmap *original,Selection *selection,BStatusBar *status_bar) |
57 | { |
58 | BWindow *status_bar_window = NULL__null; |
59 | if (status_bar != NULL__null) |
60 | status_bar_window = status_bar->Window(); |
61 | |
62 | BRect a_rect = original->Bounds(); |
63 | BBitmap *spare_buffer = DuplicateBitmap(original); |
64 | |
65 | BitmapDrawer *target = new BitmapDrawer(original); |
66 | BitmapDrawer *source = new BitmapDrawer(spare_buffer); |
67 | |
68 | int32 width = original->Bounds().Width() + 1; |
69 | int32 height = original->Bounds().Height() + 1; |
70 | |
71 | float status_bar_update_step = 100.0 / (width*height) * 1000.0; |
72 | |
73 | BMessage progress_message = BMessage(B_UPDATE_STATUS_BAR); |
74 | progress_message.AddFloat("delta",0.0); |
75 | |
76 | // We must select all pixels between 0 and width*height in a |
77 | // pseudo-random order. |
78 | int32 *offsets = new int32[width*height]; |
79 | for (int32 i=0;i<width*height;i++) |
80 | offsets[i] = i; |
81 | |
82 | /* |
83 | We copy each pixel with the following pattern: |
84 | |
85 | O |
86 | OOO |
87 | X -> OXOO |
88 | O |
89 | |
90 | */ |
91 | |
92 | int32 size_of_area = width*height-1; |
93 | int32 width_times_height = width*height; |
Value stored to 'width_times_height' during its initialization is never read | |
94 | uint32 moved_pixel; |
95 | union { |
96 | uint8 bytes[4]; |
97 | uint32 word; |
98 | } color; |
99 | |
100 | int32 random_array_size = 32; |
101 | int32 *random_array = new int32[random_array_size]; |
102 | for (int32 i=0;i<random_array_size;i++) { |
103 | random_array[i] = random()%10 * (random()%2 == 0?-1:1); |
104 | } |
105 | |
106 | if ((selection == NULL__null) || (selection->IsEmpty() == TRUE1)) { |
107 | // for (int32 i=0;i<width_times_height;i++) { |
108 | while (size_of_area > 0) { |
109 | // Select one pixel at random |
110 | int32 new_offset = rand() % size_of_area; |
111 | int32 spare = offsets[new_offset]; |
112 | offsets[new_offset] = offsets[size_of_area]; |
113 | size_of_area--; |
114 | int32 x = spare % width; |
115 | int32 y = spare / width; |
116 | color.word = source->GetPixel(BPoint(x,y)); |
117 | // randomize the color a bit |
118 | color.bytes[0] = min_c(255,max_c(0,(int32)color.bytes[0] + random_array[size_of_area%random_array_size]))((255)>(((0)>((int32)color.bytes[0] + random_array[size_of_area %random_array_size])?(0):((int32)color.bytes[0] + random_array [size_of_area%random_array_size])))?(((0)>((int32)color.bytes [0] + random_array[size_of_area%random_array_size])?(0):((int32 )color.bytes[0] + random_array[size_of_area%random_array_size ]))):(255)); |
119 | color.bytes[1] = min_c(255,max_c(0,(int32)color.bytes[1] + random_array[size_of_area%random_array_size]))((255)>(((0)>((int32)color.bytes[1] + random_array[size_of_area %random_array_size])?(0):((int32)color.bytes[1] + random_array [size_of_area%random_array_size])))?(((0)>((int32)color.bytes [1] + random_array[size_of_area%random_array_size])?(0):((int32 )color.bytes[1] + random_array[size_of_area%random_array_size ]))):(255)); |
120 | color.bytes[2] = min_c(255,max_c(0,(int32)color.bytes[2] + random_array[size_of_area%random_array_size]))((255)>(((0)>((int32)color.bytes[2] + random_array[size_of_area %random_array_size])?(0):((int32)color.bytes[2] + random_array [size_of_area%random_array_size])))?(((0)>((int32)color.bytes [2] + random_array[size_of_area%random_array_size])?(0):((int32 )color.bytes[2] + random_array[size_of_area%random_array_size ]))):(255)); |
121 | |
122 | moved_pixel = color.word; |
123 | target->SetPixel(BPoint(x-2,y-1),moved_pixel); |
124 | target->SetPixel(BPoint(x-1,y-1),moved_pixel); |
125 | target->SetPixel(BPoint(x,y-1),moved_pixel); |
126 | target->SetPixel(BPoint(x-1,y-2),moved_pixel); |
127 | target->SetPixel(BPoint(x-1,y),moved_pixel); |
128 | target->SetPixel(BPoint(x,y),moved_pixel); |
129 | target->SetPixel(BPoint(x+1,y),moved_pixel); |
130 | target->SetPixel(BPoint(x+2,y),moved_pixel); |
131 | target->SetPixel(BPoint(x,y+1),moved_pixel); |
132 | |
133 | if (((size_of_area % 1000) == 0) && (status_bar != NULL__null) && (status_bar_window != NULL__null) && (status_bar_window->LockWithTimeout(0) == B_OK((int)0))) { |
134 | status_bar->Update(status_bar_update_step); |
135 | status_bar_window->Unlock(); |
136 | } |
137 | } |
138 | } |
139 | else { |
140 | while (size_of_area > 0) { |
141 | // Select one pixel at random |
142 | int32 new_offset = rand() % size_of_area; |
143 | int32 spare = offsets[new_offset]; |
144 | offsets[new_offset] = offsets[size_of_area]; |
145 | size_of_area--; |
146 | int32 x = spare % width; |
147 | int32 y = spare / width; |
148 | color.word = source->GetPixel(BPoint(x,y)); |
149 | // randomize the color a bit |
150 | color.bytes[0] = min_c(255,max_c(0,(int32)color.bytes[0] + random_array[size_of_area%random_array_size]))((255)>(((0)>((int32)color.bytes[0] + random_array[size_of_area %random_array_size])?(0):((int32)color.bytes[0] + random_array [size_of_area%random_array_size])))?(((0)>((int32)color.bytes [0] + random_array[size_of_area%random_array_size])?(0):((int32 )color.bytes[0] + random_array[size_of_area%random_array_size ]))):(255)); |
151 | color.bytes[1] = min_c(255,max_c(0,(int32)color.bytes[1] + random_array[size_of_area%random_array_size]))((255)>(((0)>((int32)color.bytes[1] + random_array[size_of_area %random_array_size])?(0):((int32)color.bytes[1] + random_array [size_of_area%random_array_size])))?(((0)>((int32)color.bytes [1] + random_array[size_of_area%random_array_size])?(0):((int32 )color.bytes[1] + random_array[size_of_area%random_array_size ]))):(255)); |
152 | color.bytes[2] = min_c(255,max_c(0,(int32)color.bytes[2] + random_array[size_of_area%random_array_size]))((255)>(((0)>((int32)color.bytes[2] + random_array[size_of_area %random_array_size])?(0):((int32)color.bytes[2] + random_array [size_of_area%random_array_size])))?(((0)>((int32)color.bytes [2] + random_array[size_of_area%random_array_size])?(0):((int32 )color.bytes[2] + random_array[size_of_area%random_array_size ]))):(255)); |
153 | |
154 | moved_pixel = color.word; |
155 | |
156 | target->SetPixel(BPoint(x-2,y-1),moved_pixel,selection); |
157 | target->SetPixel(BPoint(x-1,y-1),moved_pixel,selection); |
158 | target->SetPixel(BPoint(x,y-1),moved_pixel,selection); |
159 | target->SetPixel(BPoint(x-1,y-2),moved_pixel,selection); |
160 | target->SetPixel(BPoint(x-1,y),moved_pixel,selection); |
161 | target->SetPixel(BPoint(x,y),moved_pixel,selection); |
162 | target->SetPixel(BPoint(x+1,y),moved_pixel,selection); |
163 | target->SetPixel(BPoint(x+2,y),moved_pixel,selection); |
164 | target->SetPixel(BPoint(x,y+1),moved_pixel,selection); |
165 | if (((size_of_area % 1000) == 0) && (status_bar != NULL__null) && (status_bar_window != NULL__null) && (status_bar_window->LockWithTimeout(0) == B_OK((int)0))) { |
166 | status_bar->Update(status_bar_update_step); |
167 | status_bar_window->Unlock(); |
168 | } |
169 | } |
170 | } |
171 | // we should also delete the spare-bitmap |
172 | delete spare_buffer; |
173 | delete target; |
174 | delete source; |
175 | delete[] offsets; |
176 | |
177 | return original; |
178 | } |
179 | |
180 | |
181 | const char* OilManipulator::ReturnHelpString() |
182 | { |
183 | return B_TRANSLATE("Creates an \"oil\" effect.")BLocaleRoster::Default()->GetCatalog()->GetString(("Creates an \"oil\" effect." ), "AddOns_Oil"); |
184 | } |
185 | |
186 | |
187 | const char* OilManipulator::ReturnName() |
188 | { |
189 | return B_TRANSLATE("Oil")BLocaleRoster::Default()->GetCatalog()->GetString(("Oil" ), "AddOns_Oil"); |
190 | } |