File: | home/HaikuArchives/ArtPaint/addons/AddOns/Emboss/EmbossAddOn.cpp |
Warning: | line 94, column 9 Value stored to 'spare_bits' 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 <Window.h> |
14 | |
15 | #include "AddOns.h" |
16 | #include "EmbossAddOn.h" |
17 | #include "ManipulatorInformer.h" |
18 | #include "Selection.h" |
19 | |
20 | #undef B_TRANSLATION_CONTEXT"AddOns_Emboss" |
21 | #define B_TRANSLATION_CONTEXT"AddOns_Emboss" "AddOns_Emboss" |
22 | |
23 | |
24 | #ifdef __cplusplus201402L |
25 | extern "C" { |
26 | #endif |
27 | char name[255] = B_TRANSLATE_MARK("Emboss")("Emboss"); |
28 | char menu_help_string[255] = B_TRANSLATE_MARK("Creates an emboss effect.")("Creates an emboss effect."); |
29 | int32 add_on_api_version = ADD_ON_API_VERSION; |
30 | add_on_types add_on_type = EFFECT_FILTER_ADD_ON; |
31 | #ifdef __cplusplus201402L |
32 | } |
33 | #endif |
34 | |
35 | |
36 | Manipulator* instantiate_add_on(BBitmap*,ManipulatorInformer *i) |
37 | { |
38 | delete i; |
39 | return new EmbossManipulator(); |
40 | } |
41 | |
42 | |
43 | |
44 | |
45 | EmbossManipulator::EmbossManipulator() |
46 | : Manipulator() |
47 | { |
48 | } |
49 | |
50 | |
51 | EmbossManipulator::~EmbossManipulator() |
52 | { |
53 | } |
54 | |
55 | // these are used when calculating the new pixel-value |
56 | #define LT(*(spare_bits-spare_bpr-1)) (*(spare_bits-spare_bpr-1)) |
57 | #define RB(*(spare_bits+spare_bpr+1)) (*(spare_bits+spare_bpr+1)) |
58 | |
59 | BBitmap* EmbossManipulator::ManipulateBitmap(BBitmap *original,Selection *selection,BStatusBar *progress_view) |
60 | { |
61 | // We may create another bitmap and return it instead of original, but we may |
62 | // also do the manipulation on the original and return it. We Should send messages |
63 | // to pregress_view that contain B_UPDATE_STATUS_BAR as their 'what'. The sum of |
64 | // message deltas should equal 100*prog_step. |
65 | |
66 | // Here manipulate the bitmap in what way is required |
67 | // , but do not delete it. |
68 | /* |
69 | |
70 | this will do a simple emboss-effect by using the following matrix |
71 | |
72 | -1 0 0 |
73 | 0 0 0 |
74 | 0 0 1 |
75 | |
76 | it should also be checked that the pixel values do not go to negative |
77 | , if that happens we should replace them with 0 |
78 | |
79 | */ |
80 | |
81 | original->Lock(); |
82 | |
83 | // first we will create a spare-buffer and copy data from original to it |
84 | // the spare-buffer should be 1 pixel larger in each direction than the |
85 | // actual buffer |
86 | BRect a_rect = original->Bounds(); |
87 | // a_rect.InsetBy(-1,-1); |
88 | // BBitmap *spare_buffer = new BBitmap(a_rect,B_RGB_32_BIT); |
89 | BBitmap *spare_buffer = DuplicateBitmap(original,-1); |
90 | // here we should copy the buffer to spare and pad the edges twice |
91 | int32 *buffer_bits = (int32*)original->Bits(); |
92 | int32 buffer_bpr = original->BytesPerRow()/4; |
93 | |
94 | int32 *spare_bits = (int32*)spare_buffer->Bits(); |
Value stored to 'spare_bits' during its initialization is never read | |
95 | int32 spare_bpr = spare_buffer->BytesPerRow()/4; |
96 | |
97 | // here we can start doing the actual convolution |
98 | buffer_bits = (int32*)original->Bits(); |
99 | spare_bits = (int32*)spare_buffer->Bits(); |
100 | spare_bits += spare_bpr + 1; |
101 | |
102 | int32 target_value; |
103 | |
104 | BMessage progress_message = BMessage(B_UPDATE_STATUS_BAR); |
105 | progress_message.AddFloat("delta",0.0); |
106 | |
107 | union { |
108 | uint8 bytes[4]; |
109 | uint32 word; |
110 | } left_top,right_bottom,result; |
111 | if (selection->IsEmpty() == TRUE1) { |
112 | // here we iterate through each pixel in spare_buffer omitting the border pixels |
113 | for (int32 y=0;y<a_rect.Height()+1;y++) { |
114 | for (int32 x=0;x<a_rect.Width()+1;x++) { |
115 | /* |
116 | as this is a very simple filter we should only look at pixels |
117 | at left top and right bottom and calculate the answer |
118 | |
119 | X1 0 0 |
120 | |
121 | 0 P 0 |
122 | |
123 | 0 0 -X1 |
124 | |
125 | here P == X1 - X2 |
126 | */ |
127 | // target_value = (((LT>>24)&0xFF) > ((RB>>24)&0xFF) ? (((LT>>24)&0xFF)-((RB>>24)&0xFF))<<24 : 0x00000000) |
128 | // | (((LT>>16)&0xFF) > ((RB>>16)&0xFF) ? (((LT>>16)&0xFF)-((RB>>16)&0xFF))<<16 : 0x00000000) |
129 | // | (((LT>>8)&0xFF) > ((RB>>8)&0xFF) ? (((LT>>8)&0xFF)-((RB>>8)&0xFF))<<8 : 0x00000000) |
130 | // | (*spare_bits & 0x000000FF); |
131 | // *buffer_bits++ = target_value; |
132 | left_top.word = LT(*(spare_bits-spare_bpr-1)); |
133 | right_bottom.word = RB(*(spare_bits+spare_bpr+1)); |
134 | result.word = *spare_bits; |
135 | result.bytes[0] = max_c(min_c(255,127+right_bottom.bytes[0] - left_top.bytes[0]),0)((((255)>(127+right_bottom.bytes[0] - left_top.bytes[0])?( 127+right_bottom.bytes[0] - left_top.bytes[0]):(255)))>(0) ?(((255)>(127+right_bottom.bytes[0] - left_top.bytes[0])?( 127+right_bottom.bytes[0] - left_top.bytes[0]):(255))):(0)); |
136 | result.bytes[1] = max_c(min_c(255,127+right_bottom.bytes[1] - left_top.bytes[1]),0)((((255)>(127+right_bottom.bytes[1] - left_top.bytes[1])?( 127+right_bottom.bytes[1] - left_top.bytes[1]):(255)))>(0) ?(((255)>(127+right_bottom.bytes[1] - left_top.bytes[1])?( 127+right_bottom.bytes[1] - left_top.bytes[1]):(255))):(0)); |
137 | result.bytes[2] = max_c(min_c(255,127+right_bottom.bytes[2] - left_top.bytes[2]),0)((((255)>(127+right_bottom.bytes[2] - left_top.bytes[2])?( 127+right_bottom.bytes[2] - left_top.bytes[2]):(255)))>(0) ?(((255)>(127+right_bottom.bytes[2] - left_top.bytes[2])?( 127+right_bottom.bytes[2] - left_top.bytes[2]):(255))):(0)); |
138 | |
139 | *buffer_bits++ = result.word; |
140 | spare_bits++; |
141 | } |
142 | spare_bits+=2; |
143 | if ((progress_view != NULL__null) && ((y % 20) == 0)) { |
144 | progress_message.ReplaceFloat("delta",20*100.0/(a_rect.Height()+1)); |
145 | progress_view->Window()->PostMessage(&progress_message,progress_view); |
146 | } |
147 | } |
148 | } |
149 | else { |
150 | BRect frame = selection->GetBoundingRect(); |
151 | |
152 | int32 left = frame.left; |
153 | int32 right = frame.right; |
154 | int32 top = frame.top; |
155 | int32 bottom = frame.bottom; |
156 | int32 *spare_origin = spare_bits; |
157 | for (int32 y=top;y<=bottom;y++) { |
158 | int32 y_buffer_bpr = y*buffer_bpr; |
159 | for (int32 x=left;x<=right;x++) { |
160 | if (selection->ContainsPoint(x,y) == TRUE1) { |
161 | spare_bits = spare_origin + y*spare_bpr + x; |
162 | // target_value = (((LT>>24)&0xFF) > ((RB>>24)&0xFF) ? (((LT>>24)&0xFF)-((RB>>24)&0xFF))<<24 : 0x00000000) |
163 | // | (((LT>>16)&0xFF) > ((RB>>16)&0xFF) ? (((LT>>16)&0xFF)-((RB>>16)&0xFF))<<16 : 0x00000000) |
164 | // | (((LT>>8)&0xFF) > ((RB>>8)&0xFF) ? (((LT>>8)&0xFF)-((RB>>8)&0xFF))<<8 : 0x00000000) |
165 | // | (*spare_bits & 0x000000FF); |
166 | // *(buffer_bits + y*buffer_bpr + x) = target_value; |
167 | left_top.word = LT(*(spare_bits-spare_bpr-1)); |
168 | right_bottom.word = RB(*(spare_bits+spare_bpr+1)); |
169 | result.word = *spare_bits; |
170 | result.bytes[0] = max_c(min_c(255,127+right_bottom.bytes[0] - left_top.bytes[0]),0)((((255)>(127+right_bottom.bytes[0] - left_top.bytes[0])?( 127+right_bottom.bytes[0] - left_top.bytes[0]):(255)))>(0) ?(((255)>(127+right_bottom.bytes[0] - left_top.bytes[0])?( 127+right_bottom.bytes[0] - left_top.bytes[0]):(255))):(0)); |
171 | result.bytes[1] = max_c(min_c(255,127+right_bottom.bytes[1] - left_top.bytes[1]),0)((((255)>(127+right_bottom.bytes[1] - left_top.bytes[1])?( 127+right_bottom.bytes[1] - left_top.bytes[1]):(255)))>(0) ?(((255)>(127+right_bottom.bytes[1] - left_top.bytes[1])?( 127+right_bottom.bytes[1] - left_top.bytes[1]):(255))):(0)); |
172 | result.bytes[2] = max_c(min_c(255,127+right_bottom.bytes[2] - left_top.bytes[2]),0)((((255)>(127+right_bottom.bytes[2] - left_top.bytes[2])?( 127+right_bottom.bytes[2] - left_top.bytes[2]):(255)))>(0) ?(((255)>(127+right_bottom.bytes[2] - left_top.bytes[2])?( 127+right_bottom.bytes[2] - left_top.bytes[2]):(255))):(0)); |
173 | |
174 | *(buffer_bits + y_buffer_bpr + x) = result.word; |
175 | } |
176 | } |
177 | } |
178 | } |
179 | original->Unlock(); |
180 | |
181 | // we should also delete the spare-bitmap |
182 | delete spare_buffer; |
183 | |
184 | |
185 | return original; |
186 | } |
187 | |
188 | |
189 | const char* EmbossManipulator::ReturnHelpString() |
190 | { |
191 | return B_TRANSLATE("Creates an emboss effect.")BLocaleRoster::Default()->GetCatalog()->GetString(("Creates an emboss effect." ), "AddOns_Emboss"); |
192 | } |
193 | |
194 | |
195 | const char* EmbossManipulator::ReturnName() |
196 | { |
197 | return B_TRANSLATE("Emboss")BLocaleRoster::Default()->GetCatalog()->GetString(("Emboss" ), "AddOns_Emboss"); |
198 | } |