]> git.saurik.com Git - wxWidgets.git/blob - src/common/rgncmn.cpp
make construct simpler
[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 bool wxRegion::Union(const wxBitmap& bmp,
55 const wxColour& transColour,
56 int tolerance)
57 #else
58 bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp),
59 const wxColour& WXUNUSED(transColour),
60 int WXUNUSED(tolerance))
61 #endif
62 {
63 #if wxUSE_IMAGE
64 unsigned char loR, loG, loB;
65 unsigned char hiR, hiG, hiB;
66
67 wxCHECK_MSG((bmp.GetMask() != NULL) || transColour.Ok(),
68 FALSE,
69 wxT("Either the bitmap should have a mask or a colour should be given."));
70
71 wxImage image = bmp.ConvertToImage();
72
73 if (image.HasMask())
74 {
75 loR = image.GetMaskRed();
76 loG = image.GetMaskGreen();
77 loB = image.GetMaskBlue();
78 }
79 else
80 {
81 loR = transColour.Red();
82 loG = transColour.Green();
83 loB = transColour.Blue();
84 }
85
86 hiR = wxMin(0xFF, loR + tolerance);
87 hiG = wxMin(0xFF, loG + tolerance);
88 hiB = wxMin(0xFF, loB + tolerance);
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;
120 Union(rect);
121 }
122 }
123 }
124
125 return true;
126 #else
127 // No wxImage support
128 return false;
129 #endif
130 }
131
132 //---------------------------------------------------------------------------