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