File: | home/HaikuArchives/ArtPaint/addons/AddOns/Grayscale/GrayscaleAddOn.cpp |
Warning: | line 76, column 30 Called C++ object pointer is null |
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 "ManipulatorInformer.h" | |||
17 | #include "GrayscaleAddOn.h" | |||
18 | #include "Selection.h" | |||
19 | ||||
20 | #undef B_TRANSLATION_CONTEXT"AddOns_Grayscale" | |||
21 | #define B_TRANSLATION_CONTEXT"AddOns_Grayscale" "AddOns_Grayscale" | |||
22 | ||||
23 | ||||
24 | #ifdef __cplusplus201402L | |||
25 | extern "C" { | |||
26 | #endif | |||
27 | char name[255] = B_TRANSLATE_MARK("Grayscale")("Grayscale"); | |||
28 | char menu_help_string[255] = B_TRANSLATE_MARK("Converts the active layer to grayscale.")("Converts the active layer to grayscale."); | |||
29 | int32 add_on_api_version = ADD_ON_API_VERSION; | |||
30 | add_on_types add_on_type = COLOR_ADD_ON; | |||
31 | #ifdef __cplusplus201402L | |||
32 | } | |||
33 | #endif | |||
34 | ||||
35 | ||||
36 | Manipulator* instantiate_add_on(BBitmap *bm,ManipulatorInformer *i) | |||
37 | { | |||
38 | delete i; | |||
39 | return new GrayscaleAddOnManipulator(bm); | |||
40 | } | |||
41 | ||||
42 | ||||
43 | ||||
44 | GrayscaleAddOnManipulator::GrayscaleAddOnManipulator(BBitmap*) | |||
45 | : Manipulator() | |||
46 | { | |||
47 | } | |||
48 | ||||
49 | ||||
50 | GrayscaleAddOnManipulator::~GrayscaleAddOnManipulator() | |||
51 | { | |||
52 | ||||
53 | } | |||
54 | ||||
55 | ||||
56 | BBitmap* GrayscaleAddOnManipulator::ManipulateBitmap(BBitmap *original,Selection *selection,BStatusBar *status_bar) | |||
57 | { | |||
58 | // We may create another bitmap and return it instead of original, but we may | |||
59 | // also do the manipulation on the original and return it. We Should send messages | |||
60 | // to progress_view that contain B_UPDATE_STATUS_BAR as their 'what'. The sum of | |||
61 | // message deltas should equal 100*prog_step. | |||
62 | BMessage progress_message = BMessage(B_UPDATE_STATUS_BAR); | |||
63 | progress_message.AddFloat("delta",0.0); | |||
64 | if (status_bar != NULL__null) { | |||
| ||||
65 | progress_message.ReplaceFloat("delta",100); | |||
66 | status_bar->Window()->PostMessage(&progress_message,status_bar); | |||
67 | } | |||
68 | ||||
69 | int32 bits_length = original->BitsLength()/4; | |||
70 | uint32 *bits = (uint32*)original->Bits(); | |||
71 | float blue,red,green; | |||
72 | union { | |||
73 | uint8 bytes[4]; | |||
74 | uint32 word; | |||
75 | } color; | |||
76 | if ((selection == NULL__null) && (selection->IsEmpty() == TRUE1)) { | |||
| ||||
77 | for (int32 i=0;i<bits_length;i++) { | |||
78 | color.word = *bits; | |||
79 | blue = color.bytes[0] * 0.114; | |||
80 | green = color.bytes[1] * 0.587; | |||
81 | red = color.bytes[2] * 0.299; | |||
82 | float sum = blue + red + green; | |||
83 | color.bytes[0] = sum; | |||
84 | color.bytes[1] = sum; | |||
85 | color.bytes[2] = sum; | |||
86 | *bits++ = color.word; | |||
87 | } | |||
88 | } | |||
89 | else { | |||
90 | int32 width = original->Bounds().Width(); | |||
91 | int32 height = original->Bounds().Height(); | |||
92 | for (int32 y=0;y<=height;y++) { | |||
93 | for (int32 x=0;x<=width;x++) { | |||
94 | if (selection->ContainsPoint(x,y)) { | |||
95 | color.word = *bits; | |||
96 | blue = color.bytes[0] * 0.114; | |||
97 | green = color.bytes[1] * 0.587; | |||
98 | red = color.bytes[2] * 0.299; | |||
99 | float sum = blue + red + green; | |||
100 | color.bytes[0] = sum; | |||
101 | color.bytes[1] = sum; | |||
102 | color.bytes[2] = sum; | |||
103 | *bits++ = color.word; | |||
104 | } | |||
105 | else { | |||
106 | ++bits; | |||
107 | } | |||
108 | } | |||
109 | } | |||
110 | } | |||
111 | return original; | |||
112 | } | |||
113 | ||||
114 | ||||
115 | const char* GrayscaleAddOnManipulator::ReturnHelpString() | |||
116 | { | |||
117 | return B_TRANSLATE("Converts the active layer to grayscale.")BLocaleRoster::Default()->GetCatalog()->GetString(("Converts the active layer to grayscale." ), "AddOns_Grayscale"); | |||
118 | } | |||
119 | ||||
120 | ||||
121 | const char* GrayscaleAddOnManipulator::ReturnName() | |||
122 | { | |||
123 | return B_TRANSLATE("Grayscale")BLocaleRoster::Default()->GetCatalog()->GetString(("Grayscale" ), "AddOns_Grayscale"); | |||
124 | } |