]> git.saurik.com Git - wxWidgets.git/blame - src/common/bmpbase.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[wxWidgets.git] / src / common / bmpbase.cpp
CommitLineData
a04eec87 1/////////////////////////////////////////////////////////////////////////////
f03e8f9b 2// Name: src/common/bmpbase.cpp
a04eec87
VS
3// Purpose: wxBitmapBase
4// Author: VaclavSlavik
5// Created: 2001/04/11
6// RCS-ID: $Id$
7// Copyright: (c) 2001, Vaclav Slavik
65571936 8// Licence: wxWindows licence
a04eec87
VS
9/////////////////////////////////////////////////////////////////////////////
10
9d4ca3aa
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
da8cebb2
WS
18#include "wx/bitmap.h"
19
20#ifndef WX_PRECOMP
21 #include "wx/colour.h"
d4f6dbf6 22 #include "wx/icon.h"
1deb3af2 23 #include "wx/image.h"
da8cebb2
WS
24#endif // WX_PRECOMP
25
20e6714a
VZ
26#if wxUSE_IMAGE && wxUSE_LIBPNG && wxUSE_STREAMS
27 #define wxHAS_PNG_LOAD
28
29 #include "wx/mstream.h"
30#endif
31
bae328c1
WS
32// ----------------------------------------------------------------------------
33// wxVariant support
34// ----------------------------------------------------------------------------
35
36#if wxUSE_VARIANT
55ccdb93
VZ
37IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxBitmap,WXDLLEXPORT)
38IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxIcon,WXDLLEXPORT)
bae328c1
WS
39#endif
40
e765d7ee
SC
41#if wxUSE_EXTENDED_RTTI
42//WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxBitmap>)
43//WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxIcon>)
44#endif
45
20e6714a
VZ
46// ----------------------------------------------------------------------------
47// wxBitmapHelpers
48// ----------------------------------------------------------------------------
49
50// wxOSX has a native version and doesn't use this one.
51
52#ifndef __WXOSX__
53
54/* static */
55wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size)
56{
57 wxBitmap bitmap;
58
59#ifdef wxHAS_PNG_LOAD
60 wxMemoryInputStream is(data, size);
61 wxImage image(is, wxBITMAP_TYPE_PNG);
62 if ( image.IsOk() )
63 bitmap = wxBitmap(image);
64#endif // wxHAS_PNG_LOAD
65
66 return bitmap;
67}
68
69#endif // !__WXOSX__
70
91885f46
VZ
71// ----------------------------------------------------------------------------
72// wxBitmapBase
73// ----------------------------------------------------------------------------
74
ab00f409
WS
75#if wxUSE_BITMAP_BASE
76
e4db172a
WS
77#ifndef WX_PRECOMP
78 #include "wx/log.h"
de6185e2 79 #include "wx/utils.h"
559a723c 80 #include "wx/palette.h"
02761f6c 81 #include "wx/module.h"
e4db172a
WS
82#endif // WX_PRECOMP
83
6f5d7825 84
a04eec87 85IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase, wxGDIObject)
e86f2cc8 86IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject)
a04eec87
VS
87
88wxList wxBitmapBase::sm_handlers;
89
c822d4ca 90void wxBitmapBase::AddHandler(wxBitmapHandler *handler)
a04eec87
VS
91{
92 sm_handlers.Append(handler);
93}
94
c822d4ca 95void wxBitmapBase::InsertHandler(wxBitmapHandler *handler)
a04eec87
VS
96{
97 sm_handlers.Insert(handler);
98}
99
100bool wxBitmapBase::RemoveHandler(const wxString& name)
101{
102 wxBitmapHandler *handler = FindHandler(name);
103 if ( handler )
104 {
105 sm_handlers.DeleteObject(handler);
c9d59ee7 106 return true;
a04eec87
VS
107 }
108 else
c9d59ee7 109 return false;
a04eec87
VS
110}
111
112wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& name)
113{
ac32ba44 114 wxList::compatibility_iterator node = sm_handlers.GetFirst();
a04eec87
VS
115 while ( node )
116 {
1bc822df 117 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
a04eec87
VS
118 if ( handler->GetName() == name )
119 return handler;
1bc822df 120 node = node->GetNext();
a04eec87
VS
121 }
122 return NULL;
123}
124
125wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& extension, wxBitmapType bitmapType)
126{
ac32ba44 127 wxList::compatibility_iterator node = sm_handlers.GetFirst();
a04eec87
VS
128 while ( node )
129 {
1bc822df 130 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
a04eec87 131 if ( handler->GetExtension() == extension &&
4b232264 132 (bitmapType == wxBITMAP_TYPE_ANY || handler->GetType() == bitmapType) )
a04eec87 133 return handler;
1bc822df 134 node = node->GetNext();
a04eec87
VS
135 }
136 return NULL;
137}
138
139wxBitmapHandler *wxBitmapBase::FindHandler(wxBitmapType bitmapType)
140{
ac32ba44 141 wxList::compatibility_iterator node = sm_handlers.GetFirst();
a04eec87
VS
142 while ( node )
143 {
1bc822df 144 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
a04eec87
VS
145 if (handler->GetType() == bitmapType)
146 return handler;
1bc822df 147 node = node->GetNext();
a04eec87
VS
148 }
149 return NULL;
150}
151
152void wxBitmapBase::CleanUpHandlers()
153{
ac32ba44 154 wxList::compatibility_iterator node = sm_handlers.GetFirst();
a04eec87
VS
155 while ( node )
156 {
1bc822df 157 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
ac32ba44 158 wxList::compatibility_iterator next = node->GetNext();
a04eec87 159 delete handler;
ac32ba44 160 sm_handlers.Erase(node);
a04eec87
VS
161 node = next;
162 }
163}
164
a04eec87
VS
165class wxBitmapBaseModule: public wxModule
166{
167DECLARE_DYNAMIC_CLASS(wxBitmapBaseModule)
168public:
169 wxBitmapBaseModule() {}
98d6e4b4
PC
170 bool OnInit() { wxBitmap::InitStandardHandlers(); return true; }
171 void OnExit() { wxBitmap::CleanUpHandlers(); }
a04eec87
VS
172};
173
174IMPLEMENT_DYNAMIC_CLASS(wxBitmapBaseModule, wxModule)
ee058011 175
91885f46
VZ
176#endif // wxUSE_BITMAP_BASE
177
452418c4
PC
178// ----------------------------------------------------------------------------
179// wxBitmap common
180// ----------------------------------------------------------------------------
181
182#if !(defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXX11__))
183
184wxBitmap::wxBitmap(const char* const* bits)
185{
186 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
187
188#if wxUSE_IMAGE && wxUSE_XPM
189 wxImage image(bits);
a1b806b9 190 wxCHECK2_MSG(image.IsOk(), return, wxT("invalid bitmap data"));
452418c4
PC
191
192 *this = wxBitmap(image);
193#else
9a83f860 194 wxFAIL_MSG(wxT("creating bitmaps from XPMs not supported"));
452418c4
PC
195#endif // wxUSE_IMAGE && wxUSE_XPM
196}
197#endif // !(defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXX11__))
198
87f83ac8
VZ
199// ----------------------------------------------------------------------------
200// wxMaskBase
201// ----------------------------------------------------------------------------
202
203bool wxMaskBase::Create(const wxBitmap& bitmap, const wxColour& colour)
204{
205 FreeData();
206
207 return InitFromColour(bitmap, colour);
208}
209
210#if wxUSE_PALETTE
211
212bool wxMaskBase::Create(const wxBitmap& bitmap, int paletteIndex)
213{
214 wxPalette *pal = bitmap.GetPalette();
215
216 wxCHECK_MSG( pal, false,
217 wxT("Cannot create mask from palette index of a bitmap without palette") );
218
219 unsigned char r,g,b;
220 pal->GetRGB(paletteIndex, &r, &g, &b);
221
222 return Create(bitmap, wxColour(r, g, b));
223}
224
225#endif // wxUSE_PALETTE
226
227bool wxMaskBase::Create(const wxBitmap& bitmap)
228{
229 FreeData();
230
231 return InitFromMonoBitmap(bitmap);
232}