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