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