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