]> git.saurik.com Git - wxWidgets.git/blame - src/dfb/bitmap.cpp
Update info on wxMiniFrame.
[wxWidgets.git] / src / dfb / bitmap.cpp
CommitLineData
b3c86150
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/dfb/bitmap.cpp
3// Purpose: wxBitmap implementation
4// Author: Vaclav Slavik
5// Created: 2006-08-04
6// RCS-ID: $Id$
7// Copyright: (c) 2006 REA Elektronik GmbH
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/log.h"
21#endif
22
23#include "wx/bitmap.h"
24#include "wx/colour.h"
25#include "wx/image.h"
26
27#warning "move this to common"
28#include "wx/xpmdecod.h"
29
30#include "wx/dfb/private.h"
31
32//-----------------------------------------------------------------------------
33// helpers
34//-----------------------------------------------------------------------------
35
36// Convert wxColour into it's quantized value in lower-precision
37// pixel format (needed for masking by colour).
38static wxColour wxQuantizeColour(const wxColour& clr, const wxBitmap& bmp)
39{
40#if 0
41 pixel_format_t *pf = bmp.GetMGLbitmap_t()->pf;
42
43 if ( pf->redAdjust == 0 && pf->greenAdjust == 0 && pf->blueAdjust == 0 )
44 return clr;
45 else
46 return wxColour((unsigned char)((clr.Red() >> pf->redAdjust) << pf->redAdjust),
47 (unsigned char)((clr.Green() >> pf->greenAdjust) << pf->greenAdjust),
48 (unsigned char)((clr.Blue() >> pf->blueAdjust) << pf->blueAdjust));
49#endif
50}
51
52
53//-----------------------------------------------------------------------------
54// wxMask
55//-----------------------------------------------------------------------------
56
57IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
58
59wxMask::wxMask() : m_bitmap(NULL)
60{
61}
62
63wxMask::wxMask(const wxBitmap& bitmap)
64 : m_bitmap(NULL)
65{
66 Create(bitmap);
67}
68
69wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
70 : m_bitmap(NULL)
71{
72 Create(bitmap, colour);
73}
74
75#if wxUSE_PALETTE
76wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
77 : m_bitmap(NULL)
78{
79 Create(bitmap, paletteIndex);
80}
81#endif // wxUSE_PALETTE
82
83wxMask::wxMask(const wxMask& mask)
84{
85 m_bitmap = mask.m_bitmap ? new wxBitmap(*mask.m_bitmap) : NULL;
86}
87
88wxMask::~wxMask()
89{
90 delete m_bitmap;
91}
92
93#warning "move this to common code"
94bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
95{
96 delete m_bitmap;
97 m_bitmap = NULL;
98
99 wxColour clr(wxQuantizeColour(colour, bitmap));
100
101 wxImage imgSrc(bitmap.ConvertToImage());
102 imgSrc.SetMask(false);
103 wxImage image(imgSrc.ConvertToMono(clr.Red(), clr.Green(), clr.Blue()));
104 if ( !image.Ok() )
105 return false;
106
107 m_bitmap = new wxBitmap(image, 1);
108
109 return m_bitmap->Ok();
110}
111
112#if wxUSE_PALETTE
113bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
114{
115 unsigned char r,g,b;
116 wxPalette *pal = bitmap.GetPalette();
117
118 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
119
120 pal->GetRGB(paletteIndex, &r, &g, &b);
121
122 return Create(bitmap, wxColour(r, g, b));
123}
124#endif // wxUSE_PALETTE
125
126bool wxMask::Create(const wxBitmap& bitmap)
127{
128 delete m_bitmap;
129 m_bitmap = NULL;
130
131 wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap") );
132 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
133
134 m_bitmap = new wxBitmap(bitmap);
135 return true;
136}
137
138const wxBitmap& wxMask::GetBitmap() const
139{
140 return m_bitmap ? *m_bitmap : wxNullBitmap;
141}
142
143
144//-----------------------------------------------------------------------------
145// wxBitmapRefData
146//-----------------------------------------------------------------------------
147
148class wxBitmapRefData: public wxObjectRefData
149{
150public:
151 wxBitmapRefData()
152 {
153 m_mask = NULL;
154#if wxUSE_PALETTE
155 m_palette = NULL;
156#endif
157 }
158
159 wxBitmapRefData(const wxBitmapRefData& data)
160 {
161 m_surface = wxDfbCloneSurface(data.m_surface,
162 wxDfbCloneSurface_NoPixels);
163 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
164#if wxUSE_PALETTE
165 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
166#endif
167 }
168
169 ~wxBitmapRefData()
170 {
171 delete m_mask;
172#if wxUSE_PALETTE
173 delete m_palette;
174#endif
175 }
176
177 IDirectFBSurfacePtr m_surface;
178 wxMask *m_mask;
179#if wxUSE_PALETTE
180 wxPalette *m_palette;
181#endif
182};
183
184#define M_BITMAP ((wxBitmapRefData *)m_refData)
185
186//-----------------------------------------------------------------------------
187// wxBitmap
188//-----------------------------------------------------------------------------
189
190IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject)
191IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
192
193wxBitmap::wxBitmap(int width, int height, int depth)
194{
195 Create(width, height, depth);
196}
197
198bool wxBitmap::Create(int width, int height, int depth)
199{
200 UnRef();
201
202 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
203
204 DFBSurfaceDescription desc;
205 desc.flags = (DFBSurfaceDescriptionFlags)(
206 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
207 desc.caps = DSCAPS_NONE;
208 desc.width = width;
209 desc.height = height;
210
211 IDirectFBSurfacePtr surface;
212 IDirectFBPtr dfb(wxTheApp->GetDirectFBInterface());
213 if ( !DFB_CALL( dfb->CreateSurface(dfb, &desc, &surface) ) )
214 return false;
215
216 m_refData = new wxBitmapRefData();
217 M_BITMAP->m_surface = surface;
218
219 return true;
220}
221
222#warning "FIXME: move this to common code"
223bool wxBitmap::CreateFromXpm(const char **bits)
224{
225 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
226
227#if wxUSE_IMAGE && wxUSE_XPM
228 wxXPMDecoder decoder;
229 wxImage img = decoder.ReadData(bits);
230 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
231
232 *this = wxBitmap(img);
233
234 return true;
235#else
236 wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") );
237 return false;
238#endif // wxUSE_IMAGE && wxUSE_XPM
239}
240
241#if wxUSE_IMAGE
242wxBitmap::wxBitmap(const wxImage& image, int depth)
243{
244 wxCHECK_RET( image.Ok(), wxT("invalid image") );
245}
246
247wxImage wxBitmap::ConvertToImage() const
248{
249 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
250
251 return wxNullImage; // FIXME
252}
253#endif // wxUSE_IMAGE
254
255wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
256{
257 LoadFile(filename, type);
258}
259
260wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
261{
262 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
263}
264
265bool wxBitmap::Ok() const
266{
267 return (m_refData != NULL && M_BITMAP->m_surface);
268}
269
270bool wxBitmap::operator==(const wxBitmap& bmp) const
271{
272 // FIXME: is this the right way to compare bitmaps?
273 return (m_refData == bmp.m_refData);
274}
275
276int wxBitmap::GetHeight() const
277{
278 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
279
280 int h = -1;
281 DFB_CALL( M_BITMAP->m_surface->GetSize(M_BITMAP->m_surface, NULL, &h) );
282 return h;
283}
284
285int wxBitmap::GetWidth() const
286{
287 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
288
289 int w = -1;
290 DFB_CALL( M_BITMAP->m_surface->GetSize(M_BITMAP->m_surface, &w, NULL) );
291 return w;
292}
293
294int wxBitmap::GetDepth() const
295{
296 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
297
298 return wxDfbGetSurfaceDepth(M_BITMAP->m_surface);
299}
300
301wxMask *wxBitmap::GetMask() const
302{
303 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
304
305 return M_BITMAP->m_mask;
306}
307
308void wxBitmap::SetMask(wxMask *mask)
309{
310 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
311
312 delete M_BITMAP->m_mask;
313 M_BITMAP->m_mask = mask;
314}
315
316bool wxBitmap::CopyFromIcon(const wxIcon& icon)
317{
318 *this = *((wxBitmap*)(&icon));
319 return true;
320}
321
322wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
323{
324 wxCHECK_MSG( Ok() &&
325 rect.x >= 0 && rect.y >= 0 &&
326 rect.x+rect.width <= GetWidth() &&
327 rect.y+rect.height <= GetHeight(),
328 wxNullBitmap,
329 wxT("invalid bitmap or bitmap region") );
330
331}
332
333#warning "to common code"
334bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
335{
336 UnRef();
337
338 wxBitmapHandler *handler = FindHandler(type);
339
340 if ( handler == NULL )
341 {
342 wxImage image;
343 if ( !image.LoadFile(name, type) || !image.Ok() )
344 {
345 wxLogError("no bitmap handler for type %d defined.", type);
346 return false;
347 }
348 else
349 {
350 *this = wxBitmap(image);
351 return true;
352 }
353 }
354
355 m_refData = new wxBitmapRefData();
356
357 return handler->LoadFile(this, name, type, -1, -1);
358}
359
360#warning "to common code"
361bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
362{
363 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
364
365 wxBitmapHandler *handler = FindHandler(type);
366
367 if ( handler == NULL )
368 {
369 wxImage image = ConvertToImage();
370#if wxUSE_PALETTE
371 if ( palette )
372 image.SetPalette(*palette);
373#endif // wxUSE_PALETTE
374
375 if ( image.Ok() )
376 return image.SaveFile(filename, type);
377 else
378 {
379 wxLogError("no bitmap handler for type %d defined.", type);
380 return false;
381 }
382 }
383
384 return handler->SaveFile(this, filename, type, palette);
385}
386
387#if wxUSE_PALETTE
388wxPalette *wxBitmap::GetPalette() const
389{
390 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
391
392 return M_BITMAP->m_palette;
393}
394
395void wxBitmap::SetPalette(const wxPalette& palette)
396{
397 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
398 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
399
400 delete M_BITMAP->m_palette;
401 M_BITMAP->m_palette = NULL;
402
403 if ( !palette.Ok() ) return;
404
405 M_BITMAP->m_palette = new wxPalette(palette);
406}
407#endif // wxUSE_PALETTE
408
409void wxBitmap::SetHeight(int height)
410{
411 AllocExclusive();
412#warning "todo"
413}
414
415void wxBitmap::SetWidth(int width)
416{
417 AllocExclusive();
418#warning "todo"
419}
420
421void wxBitmap::SetDepth(int depth)
422{
423 AllocExclusive();
424#warning "todo"
425}
426
427IDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
428{
429 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
430
431 return M_BITMAP->m_surface;
432}
433
434wxObjectRefData *wxBitmap::CreateRefData() const
435{
436 return new wxBitmapRefData;
437}
438
439wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
440{
441 return new wxBitmapRefData(*(wxBitmapRefData *)data);
442}
443
444
445/*static*/
446void wxBitmap::InitStandardHandlers()
447{
448 // not wxBitmap handlers, we rely on wxImage
449}