]>
Commit | Line | Data |
---|---|---|
1542ea39 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f38924e8 | 2 | // Name: src/common/rgncmn.cpp |
1542ea39 RD |
3 | // Purpose: Methods of wxRegion that have a generic implementation |
4 | // Author: Robin Dunn | |
5 | // Modified by: | |
6 | // Created: 27-Mar-2003 | |
1542ea39 | 7 | // Copyright: (c) Robin Dunn |
65571936 | 8 | // Licence: wxWindows licence |
1542ea39 RD |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
8a16d737 VZ |
11 | // ============================================================================ |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
1542ea39 RD |
15 | // For compilers that support precompilation, includes "wx.h". |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
f38924e8 | 19 | #pragma hdrstop |
1542ea39 RD |
20 | #endif |
21 | ||
22 | #include "wx/region.h" | |
f38924e8 WS |
23 | |
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/dcmemory.h" | |
0bca0373 | 26 | #include "wx/bitmap.h" |
f38924e8 | 27 | #include "wx/image.h" |
155ecd4c | 28 | #endif //WX_PRECOMP |
1542ea39 | 29 | |
8a16d737 VZ |
30 | // ============================================================================ |
31 | // wxRegionBase implementation | |
32 | // ============================================================================ | |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
37424888 | 35 | // region comparison |
8a16d737 VZ |
36 | // ---------------------------------------------------------------------------- |
37 | ||
38 | bool wxRegionBase::IsEqual(const wxRegion& region) const | |
39 | { | |
81e4cb8b | 40 | if ( m_refData == region.GetRefData() ) |
8a16d737 VZ |
41 | { |
42 | // regions are identical, hence equal | |
43 | return true; | |
44 | } | |
45 | ||
81e4cb8b | 46 | if ( !m_refData || !region.GetRefData() ) |
8a16d737 VZ |
47 | { |
48 | // one, but not both, of the regions is invalid | |
49 | return false; | |
50 | } | |
51 | ||
52 | return DoIsEqual(region); | |
53 | } | |
54 | ||
55 | // ---------------------------------------------------------------------------- | |
56 | // region to/from bitmap conversions | |
57 | // ---------------------------------------------------------------------------- | |
1542ea39 | 58 | |
8a16d737 | 59 | wxBitmap wxRegionBase::ConvertToBitmap() const |
1542ea39 RD |
60 | { |
61 | wxRect box = GetBox(); | |
4d9d127f | 62 | wxBitmap bmp(box.GetRight() + 1, box.GetBottom() + 1); |
1542ea39 RD |
63 | wxMemoryDC dc; |
64 | dc.SelectObject(bmp); | |
819451b6 | 65 | dc.SetBackground(*wxBLACK_BRUSH); |
1542ea39 | 66 | dc.Clear(); |
5c33522f | 67 | dc.SetDeviceClippingRegion(*static_cast<const wxRegion *>(this)); |
819451b6 | 68 | dc.SetBackground(*wxWHITE_BRUSH); |
1542ea39 RD |
69 | dc.Clear(); |
70 | dc.SelectObject(wxNullBitmap); | |
71 | return bmp; | |
72 | } | |
73 | ||
1904aa72 | 74 | #if wxUSE_IMAGE |
8a16d737 VZ |
75 | |
76 | static bool DoRegionUnion(wxRegionBase& region, | |
85f6b408 VS |
77 | const wxImage& image, |
78 | unsigned char loR, | |
79 | unsigned char loG, | |
80 | unsigned char loB, | |
81 | int tolerance) | |
1542ea39 | 82 | { |
1542ea39 RD |
83 | unsigned char hiR, hiG, hiB; |
84 | ||
7ac31c42 WS |
85 | hiR = (unsigned char)wxMin(0xFF, loR + tolerance); |
86 | hiG = (unsigned char)wxMin(0xFF, loG + tolerance); | |
87 | hiB = (unsigned char)wxMin(0xFF, loB + tolerance); | |
1542ea39 RD |
88 | |
89 | // Loop through the image row by row, pixel by pixel, building up | |
90 | // rectangles to add to the region. | |
91 | int width = image.GetWidth(); | |
92 | int height = image.GetHeight(); | |
93 | for (int y=0; y < height; y++) | |
94 | { | |
95 | wxRect rect; | |
96 | rect.y = y; | |
97 | rect.height = 1; | |
98 | ||
99 | for (int x=0; x < width; x++) | |
100 | { | |
101 | // search for a continuous range of non-transparent pixels | |
102 | int x0 = x; | |
103 | while ( x < width) | |
104 | { | |
105 | unsigned char R = image.GetRed(x,y); | |
106 | unsigned char G = image.GetGreen(x,y); | |
107 | unsigned char B = image.GetBlue(x,y); | |
108 | if (( R >= loR && R <= hiR) && | |
109 | ( G >= loG && G <= hiG) && | |
110 | ( B >= loB && B <= hiB)) // It's transparent | |
111 | break; | |
112 | x++; | |
113 | } | |
114 | ||
115 | // Add the run of non-transparent pixels (if any) to the region | |
116 | if (x > x0) { | |
117 | rect.x = x0; | |
118 | rect.width = x - x0; | |
85f6b408 | 119 | region.Union(rect); |
1542ea39 RD |
120 | } |
121 | } | |
122 | } | |
123 | ||
1904aa72 | 124 | return true; |
85f6b408 | 125 | } |
701a0b47 | 126 | |
85f6b408 | 127 | |
8a16d737 | 128 | bool wxRegionBase::Union(const wxBitmap& bmp) |
85f6b408 VS |
129 | { |
130 | if (bmp.GetMask()) | |
131 | { | |
132 | wxImage image = bmp.ConvertToImage(); | |
9a83f860 | 133 | wxASSERT_MSG( image.HasMask(), wxT("wxBitmap::ConvertToImage doesn't preserve mask?") ); |
85f6b408 VS |
134 | return DoRegionUnion(*this, image, |
135 | image.GetMaskRed(), | |
136 | image.GetMaskGreen(), | |
137 | image.GetMaskBlue(), | |
138 | 0); | |
139 | } | |
140 | else | |
141 | { | |
142 | return Union(0, 0, bmp.GetWidth(), bmp.GetHeight()); | |
143 | } | |
144 | } | |
145 | ||
8a16d737 VZ |
146 | bool wxRegionBase::Union(const wxBitmap& bmp, |
147 | const wxColour& transColour, | |
148 | int tolerance) | |
85f6b408 VS |
149 | { |
150 | wxImage image = bmp.ConvertToImage(); | |
151 | return DoRegionUnion(*this, image, | |
152 | transColour.Red(), | |
153 | transColour.Green(), | |
154 | transColour.Blue(), | |
155 | tolerance); | |
1542ea39 RD |
156 | } |
157 | ||
8a16d737 | 158 | #endif // wxUSE_IMAGE |
c16b9bfe PC |
159 | |
160 | #ifdef wxHAS_REGION_COMBINE | |
161 | // ============================================================================ | |
162 | // wxRegionWithCombine | |
163 | // ============================================================================ | |
164 | ||
165 | // implement some wxRegionBase pure virtuals in terms of Combine() | |
166 | bool wxRegionWithCombine::DoUnionWithRect(const wxRect& rect) | |
167 | { | |
168 | return Combine(rect, wxRGN_OR); | |
169 | } | |
170 | ||
171 | bool wxRegionWithCombine::DoUnionWithRegion(const wxRegion& region) | |
172 | { | |
173 | return DoCombine(region, wxRGN_OR); | |
174 | } | |
175 | ||
176 | bool wxRegionWithCombine::DoIntersect(const wxRegion& region) | |
177 | { | |
178 | return DoCombine(region, wxRGN_AND); | |
179 | } | |
180 | ||
181 | bool wxRegionWithCombine::DoSubtract(const wxRegion& region) | |
182 | { | |
183 | return DoCombine(region, wxRGN_DIFF); | |
184 | } | |
185 | ||
186 | bool wxRegionWithCombine::DoXor(const wxRegion& region) | |
187 | { | |
188 | return DoCombine(region, wxRGN_XOR); | |
189 | } | |
190 | ||
191 | #endif // wxHAS_REGION_COMBINE |