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