Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / common / rgncmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/rgncmn.cpp
3 // Purpose: Methods of wxRegion that have a generic implementation
4 // Author: Robin Dunn
5 // Modified by:
6 // Created: 27-Mar-2003
7 // Copyright: (c) Robin Dunn
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/region.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/dcmemory.h"
26 #include "wx/bitmap.h"
27 #include "wx/image.h"
28 #endif //WX_PRECOMP
29
30 // ============================================================================
31 // wxRegionBase implementation
32 // ============================================================================
33
34 // ----------------------------------------------------------------------------
35 // region comparison
36 // ----------------------------------------------------------------------------
37
38 bool wxRegionBase::IsEqual(const wxRegion& region) const
39 {
40 if ( m_refData == region.GetRefData() )
41 {
42 // regions are identical, hence equal
43 return true;
44 }
45
46 if ( !m_refData || !region.GetRefData() )
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 // ----------------------------------------------------------------------------
58
59 wxBitmap wxRegionBase::ConvertToBitmap() const
60 {
61 wxRect box = GetBox();
62 wxBitmap bmp(box.GetRight() + 1, box.GetBottom() + 1);
63 wxMemoryDC dc;
64 dc.SelectObject(bmp);
65 dc.SetBackground(*wxBLACK_BRUSH);
66 dc.Clear();
67 dc.SetDeviceClippingRegion(*static_cast<const wxRegion *>(this));
68 dc.SetBackground(*wxWHITE_BRUSH);
69 dc.Clear();
70 dc.SelectObject(wxNullBitmap);
71 return bmp;
72 }
73
74 #if wxUSE_IMAGE
75
76 static bool DoRegionUnion(wxRegionBase& region,
77 const wxImage& image,
78 unsigned char loR,
79 unsigned char loG,
80 unsigned char loB,
81 int tolerance)
82 {
83 unsigned char hiR, hiG, hiB;
84
85 hiR = (unsigned char)wxMin(0xFF, loR + tolerance);
86 hiG = (unsigned char)wxMin(0xFF, loG + tolerance);
87 hiB = (unsigned char)wxMin(0xFF, loB + tolerance);
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;
119 region.Union(rect);
120 }
121 }
122 }
123
124 return true;
125 }
126
127
128 bool wxRegionBase::Union(const wxBitmap& bmp)
129 {
130 if (bmp.GetMask())
131 {
132 wxImage image = bmp.ConvertToImage();
133 wxASSERT_MSG( image.HasMask(), wxT("wxBitmap::ConvertToImage doesn't preserve mask?") );
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
146 bool wxRegionBase::Union(const wxBitmap& bmp,
147 const wxColour& transColour,
148 int tolerance)
149 {
150 wxImage image = bmp.ConvertToImage();
151 return DoRegionUnion(*this, image,
152 transColour.Red(),
153 transColour.Green(),
154 transColour.Blue(),
155 tolerance);
156 }
157
158 #endif // wxUSE_IMAGE
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