]> git.saurik.com Git - wxWidgets.git/blob - src/common/rgncmn.cpp
switched to using a real class instead of a namespace because of Mac base/GUI problems
[wxWidgets.git] / src / common / rgncmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "rgncmn.h"
14 #endif
15
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #include "wx/region.h"
25 #include "wx/bitmap.h"
26 #if wxUSE_IMAGE
27 #include "wx/image.h"
28 #endif
29 #include "wx/dcmemory.h"
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 (bmp.GetMask())
109 {
110 wxImage image = bmp.ConvertToImage();
111 wxASSERT_MSG( image.HasMask(), _T("wxBitmap::ConvertToImage doesn't preserve mask?") );
112 return DoRegionUnion(*this, image,
113 image.GetMaskRed(),
114 image.GetMaskGreen(),
115 image.GetMaskBlue(),
116 0);
117 }
118 else
119 {
120 return Union(0, 0, bmp.GetWidth(), bmp.GetHeight());
121 }
122 }
123
124 bool wxRegion::Union(const wxBitmap& bmp,
125 const wxColour& transColour,
126 int tolerance)
127 {
128 wxImage image = bmp.ConvertToImage();
129 return DoRegionUnion(*this, image,
130 transColour.Red(),
131 transColour.Green(),
132 transColour.Blue(),
133 tolerance);
134 }
135
136 #else
137
138 bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp))
139 {
140 // No wxImage support
141 return false;
142 }
143
144 bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp),
145 const wxColour& WXUNUSED(transColour),
146 int WXUNUSED(tolerance))
147 {
148 // No wxImage support
149 return false;
150 }
151
152 #endif
153
154 //---------------------------------------------------------------------------