]>
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 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robin Dunn | |
65571936 | 9 | // Licence: wxWindows licence |
1542ea39 RD |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8a16d737 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
1542ea39 RD |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
f38924e8 | 20 | #pragma hdrstop |
1542ea39 RD |
21 | #endif |
22 | ||
23 | #include "wx/region.h" | |
f38924e8 WS |
24 | |
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/dcmemory.h" | |
0bca0373 | 27 | #include "wx/bitmap.h" |
f38924e8 | 28 | #include "wx/image.h" |
155ecd4c | 29 | #endif //WX_PRECOMP |
1542ea39 | 30 | |
8a16d737 VZ |
31 | // ============================================================================ |
32 | // wxRegionBase implementation | |
33 | // ============================================================================ | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // region comparision | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | bool wxRegionBase::IsEqual(const wxRegion& region) const | |
40 | { | |
81e4cb8b | 41 | if ( m_refData == region.GetRefData() ) |
8a16d737 VZ |
42 | { |
43 | // regions are identical, hence equal | |
44 | return true; | |
45 | } | |
46 | ||
81e4cb8b | 47 | if ( !m_refData || !region.GetRefData() ) |
8a16d737 VZ |
48 | { |
49 | // one, but not both, of the regions is invalid | |
50 | return false; | |
51 | } | |
52 | ||
53 | return DoIsEqual(region); | |
54 | } | |
55 | ||
56 | // ---------------------------------------------------------------------------- | |
57 | // region to/from bitmap conversions | |
58 | // ---------------------------------------------------------------------------- | |
1542ea39 | 59 | |
8a16d737 | 60 | wxBitmap wxRegionBase::ConvertToBitmap() const |
1542ea39 RD |
61 | { |
62 | wxRect box = GetBox(); | |
63 | wxBitmap bmp(box.GetRight(), box.GetBottom()); | |
64 | wxMemoryDC dc; | |
65 | dc.SelectObject(bmp); | |
819451b6 | 66 | dc.SetBackground(*wxBLACK_BRUSH); |
1542ea39 | 67 | dc.Clear(); |
8a16d737 | 68 | dc.SetClippingRegion(*wx_static_cast(const wxRegion *, this)); |
819451b6 | 69 | dc.SetBackground(*wxWHITE_BRUSH); |
1542ea39 RD |
70 | dc.Clear(); |
71 | dc.SelectObject(wxNullBitmap); | |
72 | return bmp; | |
73 | } | |
74 | ||
1904aa72 | 75 | #if wxUSE_IMAGE |
8a16d737 VZ |
76 | |
77 | static bool DoRegionUnion(wxRegionBase& region, | |
85f6b408 VS |
78 | const wxImage& image, |
79 | unsigned char loR, | |
80 | unsigned char loG, | |
81 | unsigned char loB, | |
82 | int tolerance) | |
1542ea39 | 83 | { |
1542ea39 RD |
84 | unsigned char hiR, hiG, hiB; |
85 | ||
7ac31c42 WS |
86 | hiR = (unsigned char)wxMin(0xFF, loR + tolerance); |
87 | hiG = (unsigned char)wxMin(0xFF, loG + tolerance); | |
88 | hiB = (unsigned char)wxMin(0xFF, loB + tolerance); | |
1542ea39 RD |
89 | |
90 | // Loop through the image row by row, pixel by pixel, building up | |
91 | // rectangles to add to the region. | |
92 | int width = image.GetWidth(); | |
93 | int height = image.GetHeight(); | |
94 | for (int y=0; y < height; y++) | |
95 | { | |
96 | wxRect rect; | |
97 | rect.y = y; | |
98 | rect.height = 1; | |
99 | ||
100 | for (int x=0; x < width; x++) | |
101 | { | |
102 | // search for a continuous range of non-transparent pixels | |
103 | int x0 = x; | |
104 | while ( x < width) | |
105 | { | |
106 | unsigned char R = image.GetRed(x,y); | |
107 | unsigned char G = image.GetGreen(x,y); | |
108 | unsigned char B = image.GetBlue(x,y); | |
109 | if (( R >= loR && R <= hiR) && | |
110 | ( G >= loG && G <= hiG) && | |
111 | ( B >= loB && B <= hiB)) // It's transparent | |
112 | break; | |
113 | x++; | |
114 | } | |
115 | ||
116 | // Add the run of non-transparent pixels (if any) to the region | |
117 | if (x > x0) { | |
118 | rect.x = x0; | |
119 | rect.width = x - x0; | |
85f6b408 | 120 | region.Union(rect); |
1542ea39 RD |
121 | } |
122 | } | |
123 | } | |
124 | ||
1904aa72 | 125 | return true; |
85f6b408 | 126 | } |
701a0b47 | 127 | |
85f6b408 | 128 | |
8a16d737 | 129 | bool wxRegionBase::Union(const wxBitmap& bmp) |
85f6b408 VS |
130 | { |
131 | if (bmp.GetMask()) | |
132 | { | |
133 | wxImage image = bmp.ConvertToImage(); | |
134 | wxASSERT_MSG( image.HasMask(), _T("wxBitmap::ConvertToImage doesn't preserve mask?") ); | |
135 | return DoRegionUnion(*this, image, | |
136 | image.GetMaskRed(), | |
137 | image.GetMaskGreen(), | |
138 | image.GetMaskBlue(), | |
139 | 0); | |
140 | } | |
141 | else | |
142 | { | |
143 | return Union(0, 0, bmp.GetWidth(), bmp.GetHeight()); | |
144 | } | |
145 | } | |
146 | ||
8a16d737 VZ |
147 | bool wxRegionBase::Union(const wxBitmap& bmp, |
148 | const wxColour& transColour, | |
149 | int tolerance) | |
85f6b408 VS |
150 | { |
151 | wxImage image = bmp.ConvertToImage(); | |
152 | return DoRegionUnion(*this, image, | |
153 | transColour.Red(), | |
154 | transColour.Green(), | |
155 | transColour.Blue(), | |
156 | tolerance); | |
1542ea39 RD |
157 | } |
158 | ||
8a16d737 | 159 | #endif // wxUSE_IMAGE |