]> git.saurik.com Git - wxWidgets.git/blob - src/common/rgncmn.cpp
No significant change
[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 #ifdef __GNUG__
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 #include "wx/image.h"
27 #include "wx/dcmemory.h"
28
29
30 //---------------------------------------------------------------------------
31
32
33
34 wxBitmap wxRegion::ConvertToBitmap() const
35 {
36 wxRect box = GetBox();
37 wxBitmap bmp(box.GetRight(), box.GetBottom());
38 wxMemoryDC dc;
39 dc.SelectObject(bmp);
40 dc.SetBackground(*wxBLACK_BRUSH);
41 dc.Clear();
42 dc.SetClippingRegion(*this);
43 dc.SetBackground(*wxWHITE_BRUSH);
44 dc.Clear();
45 dc.SelectObject(wxNullBitmap);
46 return bmp;
47 }
48
49 //---------------------------------------------------------------------------
50
51 bool wxRegion::Union(const wxBitmap& bmp,
52 const wxColour& transColour,
53 int tolerance)
54 {
55 unsigned char loR, loG, loB;
56 unsigned char hiR, hiG, hiB;
57
58 wxCHECK_MSG((bmp.GetMask() != NULL) || transColour.Ok(),
59 FALSE,
60 wxT("Either the bitmap should have a mask or a colour should be given."));
61
62 wxImage image = bmp.ConvertToImage();
63
64 if (image.HasMask())
65 {
66 loR = image.GetMaskRed();
67 loG = image.GetMaskGreen();
68 loB = image.GetMaskBlue();
69 }
70 else
71 {
72 loR = transColour.Red();
73 loG = transColour.Green();
74 loB = transColour.Blue();
75 }
76
77 hiR = wxMin(0xFF, loR + tolerance);
78 hiG = wxMin(0xFF, loG + tolerance);
79 hiB = wxMin(0xFF, loB + tolerance);
80
81 // Loop through the image row by row, pixel by pixel, building up
82 // rectangles to add to the region.
83 int width = image.GetWidth();
84 int height = image.GetHeight();
85 for (int y=0; y < height; y++)
86 {
87 wxRect rect;
88 rect.y = y;
89 rect.height = 1;
90
91 for (int x=0; x < width; x++)
92 {
93 // search for a continuous range of non-transparent pixels
94 int x0 = x;
95 while ( x < width)
96 {
97 unsigned char R = image.GetRed(x,y);
98 unsigned char G = image.GetGreen(x,y);
99 unsigned char B = image.GetBlue(x,y);
100 if (( R >= loR && R <= hiR) &&
101 ( G >= loG && G <= hiG) &&
102 ( B >= loB && B <= hiB)) // It's transparent
103 break;
104 x++;
105 }
106
107 // Add the run of non-transparent pixels (if any) to the region
108 if (x > x0) {
109 rect.x = x0;
110 rect.width = x - x0;
111 Union(rect);
112 }
113 }
114 }
115
116 return TRUE;
117 }
118
119 //---------------------------------------------------------------------------