]> git.saurik.com Git - wxWidgets.git/blame - src/common/rgncmn.cpp
Fix memory leak by letting the base class version handle the
[wxWidgets.git] / src / common / rgncmn.cpp
CommitLineData
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
77ffb593 9// Licence: wxWidgets licence
1542ea39
RD
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
1542ea39
RD
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"
4b316329 26#if wxUSE_IMAGE
dd1eb4e3 27#include "wx/image.h"
4b316329 28#endif
1542ea39
RD
29#include "wx/dcmemory.h"
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
1542ea39
RD
54bool wxRegion::Union(const wxBitmap& bmp,
55 const wxColour& transColour,
56 int tolerance)
1904aa72
DS
57#else
58bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp),
59 const wxColour& WXUNUSED(transColour),
60 int WXUNUSED(tolerance))
61#endif
1542ea39 62{
4b316329 63#if wxUSE_IMAGE
1542ea39
RD
64 unsigned char loR, loG, loB;
65 unsigned char hiR, hiG, hiB;
66
67 wxCHECK_MSG((bmp.GetMask() != NULL) || transColour.Ok(),
68 FALSE,
69 wxT("Either the bitmap should have a mask or a colour should be given."));
70
71 wxImage image = bmp.ConvertToImage();
72
73 if (image.HasMask())
74 {
75 loR = image.GetMaskRed();
76 loG = image.GetMaskGreen();
77 loB = image.GetMaskBlue();
78 }
79 else
80 {
81 loR = transColour.Red();
82 loG = transColour.Green();
83 loB = transColour.Blue();
84 }
85
86 hiR = wxMin(0xFF, loR + tolerance);
87 hiG = wxMin(0xFF, loG + tolerance);
88 hiB = wxMin(0xFF, loB + tolerance);
89
90 // Loop through the image row by row, pixel by pixel, building up
91 // rectangles to add to the region.
92 int width = image.GetWidth();
93 int height = image.GetHeight();
94 for (int y=0; y < height; y++)
95 {
96 wxRect rect;
97 rect.y = y;
98 rect.height = 1;
99
100 for (int x=0; x < width; x++)
101 {
102 // search for a continuous range of non-transparent pixels
103 int x0 = x;
104 while ( x < width)
105 {
106 unsigned char R = image.GetRed(x,y);
107 unsigned char G = image.GetGreen(x,y);
108 unsigned char B = image.GetBlue(x,y);
109 if (( R >= loR && R <= hiR) &&
110 ( G >= loG && G <= hiG) &&
111 ( B >= loB && B <= hiB)) // It's transparent
112 break;
113 x++;
114 }
115
116 // Add the run of non-transparent pixels (if any) to the region
117 if (x > x0) {
118 rect.x = x0;
119 rect.width = x - x0;
120 Union(rect);
121 }
122 }
123 }
124
1904aa72 125 return true;
4b316329
JS
126#else
127 // No wxImage support
1904aa72 128 return false;
4b316329 129#endif
1542ea39
RD
130}
131
132//---------------------------------------------------------------------------