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