]> git.saurik.com Git - wxWidgets.git/blame - src/dfb/bitmap.cpp
STL compilation fix
[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
b3c86150
VS
27#include "wx/dfb/private.h"
28
29//-----------------------------------------------------------------------------
30// helpers
31//-----------------------------------------------------------------------------
32
b39fc8d7
VS
33// pitch = stride = # of bytes between the start of N-th line and (N+1)-th line
34static void CopyPixelsAndSwapRGB(unsigned w, unsigned h,
35 const unsigned char *src,
36 unsigned src_pitch,
37 unsigned char *dst,
38 unsigned dst_pitch)
39{
40 unsigned src_advance = src_pitch - 3 * w;
41 unsigned dst_advance = dst_pitch - 3 * w;
42 for ( unsigned y = 0; y < h; y++, src += src_advance, dst += dst_advance )
43 {
44 for ( unsigned x = 0; x < w; x++, src += 3, dst += 3 )
45 {
46 // copy with RGB -> BGR translation:
47 dst[0] = src[2];
48 dst[1] = src[1];
49 dst[2] = src[0];
50 }
51 }
52}
53
54static void CopySurfaceToImage(const wxIDirectFBSurfacePtr& surface,
55 const wxImage& image)
56{
57 wxCHECK_RET( surface->GetPixelFormat() == DSPF_RGB24,
58 _T("unexpected pixel format") );
59
60 wxIDirectFBSurface::Locked locked(surface, DSLF_READ);
61 wxCHECK_RET( locked.ptr, _T("failed to lock surface") );
62
63 CopyPixelsAndSwapRGB(image.GetWidth(), image.GetHeight(),
64 (unsigned char*)locked.ptr, locked.pitch,
65 image.GetData(), image.GetWidth() * 3);
66}
67
68static void CopyImageToSurface(const wxImage& image,
69 const wxIDirectFBSurfacePtr& surface)
70{
71 wxCHECK_RET( surface->GetPixelFormat() == DSPF_RGB24,
72 _T("unexpected pixel format") );
73
74 wxIDirectFBSurface::Locked locked(surface, DSLF_WRITE);
75 wxCHECK_RET( locked.ptr, _T("failed to lock surface") );
76
77 CopyPixelsAndSwapRGB(image.GetWidth(), image.GetHeight(),
78 image.GetData(), image.GetWidth() * 3,
79 (unsigned char*)locked.ptr, locked.pitch);
80}
81
a46d4814
VS
82// Creates a surface that will use wxImage's pixel data (RGB only)
83static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image)
84{
85 wxCHECK_MSG( image.Ok(), NULL, _T("invalid image") );
86 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
87 // into a temporary RGBA surface
88 wxCHECK_MSG( !image.HasAlpha(), NULL, _T("alpha channel not supported") );
89
b39fc8d7
VS
90 // NB: wxImage uses RGB order of bytes while DirectFB uses BGR, so we
91 // cannot use preallocated surface that shares data with wxImage, we
92 // have to copy the data to temporary surface instead
a46d4814
VS
93 DFBSurfaceDescription desc;
94 desc.flags = (DFBSurfaceDescriptionFlags)
b39fc8d7 95 (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT);
a46d4814
VS
96 desc.caps = DSCAPS_NONE;
97 desc.width = image.GetWidth();
98 desc.height = image.GetHeight();
99 desc.pixelformat = DSPF_RGB24;
a46d4814
VS
100
101 return wxIDirectFB::Get()->CreateSurface(&desc);
102}
b3c86150 103
b3c86150
VS
104//-----------------------------------------------------------------------------
105// wxBitmapRefData
106//-----------------------------------------------------------------------------
107
108class wxBitmapRefData: public wxObjectRefData
109{
110public:
111 wxBitmapRefData()
112 {
113 m_mask = NULL;
114#if wxUSE_PALETTE
115 m_palette = NULL;
116#endif
117 }
118
119 wxBitmapRefData(const wxBitmapRefData& data)
120 {
2ee16da2 121 m_surface = data.m_surface ? data.m_surface->Clone() : NULL;
a5b31f4e 122
b3c86150
VS
123 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
124#if wxUSE_PALETTE
125 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
126#endif
127 }
128
2ee16da2 129 virtual ~wxBitmapRefData()
b3c86150
VS
130 {
131 delete m_mask;
132#if wxUSE_PALETTE
133 delete m_palette;
134#endif
135 }
136
52c8d32a
VS
137 wxIDirectFBSurfacePtr m_surface;
138 wxMask *m_mask;
b3c86150 139#if wxUSE_PALETTE
52c8d32a 140 wxPalette *m_palette;
b3c86150
VS
141#endif
142};
143
144#define M_BITMAP ((wxBitmapRefData *)m_refData)
145
146//-----------------------------------------------------------------------------
147// wxBitmap
148//-----------------------------------------------------------------------------
149
452418c4 150IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
b3c86150
VS
151IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
152
153wxBitmap::wxBitmap(int width, int height, int depth)
154{
155 Create(width, height, depth);
156}
157
4562386d
VS
158bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface)
159{
160 UnRef();
161
162 wxCHECK_MSG( surface, false, _T("invalid surface") );
163
164 m_refData = new wxBitmapRefData();
165 M_BITMAP->m_surface = surface;
166 return true;
167}
168
b3c86150
VS
169bool wxBitmap::Create(int width, int height, int depth)
170{
171 UnRef();
172
173 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
949e0a38 174 wxCHECK_MSG( depth == -1, false, wxT("only default depth supported now") );
b3c86150
VS
175
176 DFBSurfaceDescription desc;
177 desc.flags = (DFBSurfaceDescriptionFlags)(
178 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
179 desc.caps = DSCAPS_NONE;
180 desc.width = width;
181 desc.height = height;
182
4562386d 183 return Create(wxIDirectFB::Get()->CreateSurface(&desc));
b3c86150
VS
184}
185
b3c86150
VS
186#if wxUSE_IMAGE
187wxBitmap::wxBitmap(const wxImage& image, int depth)
188{
189 wxCHECK_RET( image.Ok(), wxT("invalid image") );
a46d4814
VS
190
191 // create surface in screen's format:
192 if ( !Create(image.GetWidth(), image.GetHeight(), depth) )
193 return;
194
195 // then copy the image to it:
a46d4814
VS
196 wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface;
197
b39fc8d7
VS
198 if ( dst->GetPixelFormat() == DSPF_RGB24 )
199 {
200 CopyImageToSurface(image, dst);
201 }
202 else
203 {
204 // wxBitmap uses different pixel format, so we have to use a temporary
205 // surface and blit to the bitmap via it:
206 wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image));
207 CopyImageToSurface(image, src);
208
209 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
210 return;
211 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
212 return;
213 }
a46d4814
VS
214
215 // FIXME: implement mask creation from image's mask (or alpha channel?)
216 wxASSERT_MSG( !image.HasMask(), _T("image masks are ignored for now") );
b3c86150
VS
217}
218
219wxImage wxBitmap::ConvertToImage() const
220{
221 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
222
a46d4814 223 wxImage img(GetWidth(), GetHeight());
a46d4814
VS
224 wxIDirectFBSurfacePtr src = M_BITMAP->m_surface;
225
b39fc8d7
VS
226 if ( src->GetPixelFormat() == DSPF_RGB24 )
227 {
228 CopySurfaceToImage(src, img);
229 }
230 else
231 {
232 // wxBitmap uses different pixel format, so we have to use a temporary
233 // surface and blit to the bitmap via it:
234 wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img));
235
236 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
237 return wxNullImage;
238 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
239 return wxNullImage;
240
241 CopySurfaceToImage(dst, img);
242 }
a46d4814
VS
243
244 // FIXME: implement mask setting in the image
245 wxASSERT_MSG( GetMask() == NULL, _T("bitmap masks are ignored for now") );
246
247 return img;
b3c86150
VS
248}
249#endif // wxUSE_IMAGE
250
251wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
252{
253 LoadFile(filename, type);
254}
255
256wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
257{
258 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
949e0a38
VS
259
260 wxFAIL_MSG( _T("not implemented") );
b3c86150
VS
261}
262
b7cacb43 263bool wxBitmap::IsOk() const
b3c86150
VS
264{
265 return (m_refData != NULL && M_BITMAP->m_surface);
266}
267
b3c86150
VS
268int wxBitmap::GetHeight() const
269{
270 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
271
272 int h = -1;
52c8d32a 273 M_BITMAP->m_surface->GetSize(NULL, &h);
b3c86150
VS
274 return h;
275}
276
277int wxBitmap::GetWidth() const
278{
279 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
280
281 int w = -1;
52c8d32a 282 M_BITMAP->m_surface->GetSize(&w, NULL);
b3c86150
VS
283 return w;
284}
285
286int wxBitmap::GetDepth() const
287{
288 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
289
a5b31f4e 290 return M_BITMAP->m_surface->GetDepth();
b3c86150
VS
291}
292
293wxMask *wxBitmap::GetMask() const
294{
295 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
296
297 return M_BITMAP->m_mask;
298}
299
300void wxBitmap::SetMask(wxMask *mask)
301{
302 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
303
55ccdb93 304 AllocExclusive();
b3c86150
VS
305 delete M_BITMAP->m_mask;
306 M_BITMAP->m_mask = mask;
307}
308
309bool wxBitmap::CopyFromIcon(const wxIcon& icon)
310{
311 *this = *((wxBitmap*)(&icon));
312 return true;
313}
314
315wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
316{
317 wxCHECK_MSG( Ok() &&
318 rect.x >= 0 && rect.y >= 0 &&
319 rect.x+rect.width <= GetWidth() &&
320 rect.y+rect.height <= GetHeight(),
321 wxNullBitmap,
322 wxT("invalid bitmap or bitmap region") );
323
4562386d
VS
324 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
325 // clone the obtained subsurface
326 DFBRectangle r = { rect.x, rect.y, rect.width, rect.height };
327 return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone());
b3c86150
VS
328}
329
330#warning "to common code"
331bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
332{
333 UnRef();
334
335 wxBitmapHandler *handler = FindHandler(type);
336
337 if ( handler == NULL )
338 {
339 wxImage image;
340 if ( !image.LoadFile(name, type) || !image.Ok() )
341 {
ec5006bd 342 wxLogError(_("No bitmap handler for type %d defined."), type);
b3c86150
VS
343 return false;
344 }
345 else
346 {
347 *this = wxBitmap(image);
348 return true;
349 }
350 }
351
352 m_refData = new wxBitmapRefData();
353
354 return handler->LoadFile(this, name, type, -1, -1);
355}
356
357#warning "to common code"
358bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
359{
360 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
361
362 wxBitmapHandler *handler = FindHandler(type);
363
364 if ( handler == NULL )
365 {
366 wxImage image = ConvertToImage();
367#if wxUSE_PALETTE
368 if ( palette )
369 image.SetPalette(*palette);
370#endif // wxUSE_PALETTE
371
372 if ( image.Ok() )
373 return image.SaveFile(filename, type);
374 else
375 {
ec5006bd 376 wxLogError(_("No bitmap handler for type %d defined."), type);
b3c86150
VS
377 return false;
378 }
379 }
380
381 return handler->SaveFile(this, filename, type, palette);
382}
383
384#if wxUSE_PALETTE
385wxPalette *wxBitmap::GetPalette() const
386{
387 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
388
389 return M_BITMAP->m_palette;
390}
391
392void wxBitmap::SetPalette(const wxPalette& palette)
393{
394 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
395 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
396
55ccdb93 397 AllocExclusive();
b3c86150
VS
398 delete M_BITMAP->m_palette;
399 M_BITMAP->m_palette = NULL;
400
401 if ( !palette.Ok() ) return;
402
403 M_BITMAP->m_palette = new wxPalette(palette);
404}
405#endif // wxUSE_PALETTE
406
407void wxBitmap::SetHeight(int height)
408{
409 AllocExclusive();
3278b06e
VS
410
411 wxFAIL_MSG( _T("SetHeight not implemented") );
b3c86150
VS
412}
413
414void wxBitmap::SetWidth(int width)
415{
416 AllocExclusive();
3278b06e
VS
417
418 wxFAIL_MSG( _T("SetWidth not implemented") );
b3c86150
VS
419}
420
421void wxBitmap::SetDepth(int depth)
422{
423 AllocExclusive();
3278b06e
VS
424
425 wxFAIL_MSG( _T("SetDepth not implemented") );
b3c86150
VS
426}
427
52c8d32a 428wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
b3c86150
VS
429{
430 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
431
432 return M_BITMAP->m_surface;
433}
434
435wxObjectRefData *wxBitmap::CreateRefData() const
436{
437 return new wxBitmapRefData;
438}
439
440wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
441{
442 return new wxBitmapRefData(*(wxBitmapRefData *)data);
443}
444
445
446/*static*/
447void wxBitmap::InitStandardHandlers()
448{
449 // not wxBitmap handlers, we rely on wxImage
450}