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