]> git.saurik.com Git - wxWidgets.git/blob - src/common/rgncmn.cpp
call wxApp::OnUnhandledException()
[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 bool wxRegion::Union(const wxBitmap& bmp,
54 const wxColour& transColour,
55 int tolerance)
56 {
57 #if wxUSE_IMAGE
58 unsigned char loR, loG, loB;
59 unsigned char hiR, hiG, hiB;
60
61 wxCHECK_MSG((bmp.GetMask() != NULL) || transColour.Ok(),
62 FALSE,
63 wxT("Either the bitmap should have a mask or a colour should be given."));
64
65 wxImage image = bmp.ConvertToImage();
66
67 if (image.HasMask())
68 {
69 loR = image.GetMaskRed();
70 loG = image.GetMaskGreen();
71 loB = image.GetMaskBlue();
72 }
73 else
74 {
75 loR = transColour.Red();
76 loG = transColour.Green();
77 loB = transColour.Blue();
78 }
79
80 hiR = wxMin(0xFF, loR + tolerance);
81 hiG = wxMin(0xFF, loG + tolerance);
82 hiB = wxMin(0xFF, loB + tolerance);
83
84 // Loop through the image row by row, pixel by pixel, building up
85 // rectangles to add to the region.
86 int width = image.GetWidth();
87 int height = image.GetHeight();
88 for (int y=0; y < height; y++)
89 {
90 wxRect rect;
91 rect.y = y;
92 rect.height = 1;
93
94 for (int x=0; x < width; x++)
95 {
96 // search for a continuous range of non-transparent pixels
97 int x0 = x;
98 while ( x < width)
99 {
100 unsigned char R = image.GetRed(x,y);
101 unsigned char G = image.GetGreen(x,y);
102 unsigned char B = image.GetBlue(x,y);
103 if (( R >= loR && R <= hiR) &&
104 ( G >= loG && G <= hiG) &&
105 ( B >= loB && B <= hiB)) // It's transparent
106 break;
107 x++;
108 }
109
110 // Add the run of non-transparent pixels (if any) to the region
111 if (x > x0) {
112 rect.x = x0;
113 rect.width = x - x0;
114 Union(rect);
115 }
116 }
117 }
118
119 return TRUE;
120 #else
121 // No wxImage support
122 return FALSE;
123 #endif
124 }
125
126 //---------------------------------------------------------------------------