]> git.saurik.com Git - wxWidgets.git/blame - src/dfb/bitmap.cpp
* When hiding a wxWindow make sure that none of its subviews are the first
[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"
c3ee7025 26#include "wx/rawbmp.h"
b3c86150 27
b3c86150
VS
28#include "wx/dfb/private.h"
29
30//-----------------------------------------------------------------------------
31// helpers
32//-----------------------------------------------------------------------------
33
e2badebb
VS
34// NB: Most of this conversion code is needed because of differences between
35// wxImage and wxDFB's wxBitmap representations:
36// (1) wxImage uses RGB order, while DirectFB uses BGR
37// (2) wxImage has alpha channel in a separate plane, while DirectFB puts
38// all components into single BGRA plane
39
b39fc8d7 40// pitch = stride = # of bytes between the start of N-th line and (N+1)-th line
e2badebb
VS
41// {Src,Dst}PixSize = # of bytes used to represent one pixel
42template<int SrcPixSize, int DstPixSize>
b39fc8d7
VS
43static void CopyPixelsAndSwapRGB(unsigned w, unsigned h,
44 const unsigned char *src,
45 unsigned src_pitch,
46 unsigned char *dst,
47 unsigned dst_pitch)
48{
e2badebb
VS
49 unsigned src_advance = src_pitch - SrcPixSize * w;
50 unsigned dst_advance = dst_pitch - DstPixSize * w;
b39fc8d7
VS
51 for ( unsigned y = 0; y < h; y++, src += src_advance, dst += dst_advance )
52 {
e2badebb 53 for ( unsigned x = 0; x < w; x++, src += SrcPixSize, dst += DstPixSize )
b39fc8d7
VS
54 {
55 // copy with RGB -> BGR translation:
56 dst[0] = src[2];
57 dst[1] = src[1];
58 dst[2] = src[0];
59 }
60 }
61}
62
63static void CopySurfaceToImage(const wxIDirectFBSurfacePtr& surface,
e2badebb 64 wxImage& image)
b39fc8d7 65{
b39fc8d7 66 wxIDirectFBSurface::Locked locked(surface, DSLF_READ);
a5001e93 67 wxCHECK_RET( locked.ptr, "failed to lock surface" );
b39fc8d7 68
e2badebb
VS
69 const unsigned width = image.GetWidth();
70 const unsigned height = image.GetHeight();
71 const DFBSurfacePixelFormat format = surface->GetPixelFormat();
72
73 // copy RGB data from the surface:
74 switch ( format )
75 {
76 case DSPF_RGB24:
77 CopyPixelsAndSwapRGB<3,3>
78 (
79 width, height,
80 (unsigned char*)locked.ptr, locked.pitch,
81 image.GetData(), width * 3
82 );
83 break;
84
85 case DSPF_RGB32:
86 case DSPF_ARGB:
87 CopyPixelsAndSwapRGB<4,3>
88 (
89 width, height,
90 (unsigned char*)locked.ptr, locked.pitch,
91 image.GetData(), width * 3
92 );
93 break;
94
95 default:
96 wxFAIL_MSG( "unexpected pixel format" );
97 return;
98 }
99
100 // extract alpha channel if the bitmap has it:
101 if ( format == DSPF_ARGB )
102 {
103 // create alpha plane:
104 image.SetAlpha();
105
106 // and copy alpha data to it:
107 const unsigned advance = locked.pitch - 4 * width;
108 unsigned char *alpha = image.GetAlpha();
109 // NB: "+3" is to get pointer to alpha component
110 const unsigned char *src = ((unsigned char*)locked.ptr) + 3;
111
112 for ( unsigned y = 0; y < height; y++, src += advance )
113 for ( unsigned x = 0; x < width; x++, src += 4 )
114 *(alpha++) = *src;
115 }
b39fc8d7
VS
116}
117
118static void CopyImageToSurface(const wxImage& image,
119 const wxIDirectFBSurfacePtr& surface)
120{
b39fc8d7 121 wxIDirectFBSurface::Locked locked(surface, DSLF_WRITE);
e2badebb
VS
122 wxCHECK_RET( locked.ptr, "failed to lock surface" );
123
124 const unsigned width = image.GetWidth();
125 const unsigned height = image.GetHeight();
126 const DFBSurfacePixelFormat format = surface->GetPixelFormat();
127
128 // copy RGB data to the surface:
129 switch ( format )
130 {
131 case DSPF_RGB24:
132 CopyPixelsAndSwapRGB<3,3>
133 (
134 width, height,
135 image.GetData(), width * 3,
136 (unsigned char*)locked.ptr, locked.pitch
137 );
138 break;
139
140 case DSPF_RGB32:
141 case DSPF_ARGB:
142 CopyPixelsAndSwapRGB<3,4>
143 (
144 width, height,
145 image.GetData(), width * 3,
146 (unsigned char*)locked.ptr, locked.pitch
147 );
148 break;
149
150 default:
151 wxFAIL_MSG( "unexpected pixel format" );
152 return;
153 }
154
155 // if the image has alpha channel, merge it in:
156 if ( format == DSPF_ARGB )
157 {
158 wxCHECK_RET( image.HasAlpha(), "logic error - ARGB, but no alpha" );
b39fc8d7 159
e2badebb
VS
160 const unsigned advance = locked.pitch - 4 * width;
161 const unsigned char *alpha = image.GetAlpha();
162 // NB: "+3" is to get pointer to alpha component
163 unsigned char *dest = ((unsigned char*)locked.ptr) + 3;
164
165 for ( unsigned y = 0; y < height; y++, dest += advance )
166 for ( unsigned x = 0; x < width; x++, dest += 4 )
167 *dest = *(alpha++);
168 }
b39fc8d7
VS
169}
170
c3ee7025
VS
171static wxIDirectFBSurfacePtr
172CreateSurfaceWithFormat(int w, int h, DFBSurfacePixelFormat format)
173{
174 DFBSurfaceDescription desc;
175 desc.flags = (DFBSurfaceDescriptionFlags)
5fe050c3 176 (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
c3ee7025
VS
177 desc.caps = DSCAPS_NONE;
178 desc.width = w;
179 desc.height = h;
5fe050c3
VS
180
181 if ( format != DSPF_UNKNOWN )
182 {
183 desc.flags = (DFBSurfaceDescriptionFlags)(
184 desc.flags | DSDESC_PIXELFORMAT);
185 desc.pixelformat = format;
186 }
c3ee7025
VS
187
188 return wxIDirectFB::Get()->CreateSurface(&desc);
189}
190
a46d4814
VS
191// Creates a surface that will use wxImage's pixel data (RGB only)
192static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image)
193{
a5001e93 194 wxCHECK_MSG( image.Ok(), NULL, "invalid image" );
a46d4814
VS
195 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
196 // into a temporary RGBA surface
a5001e93 197 wxCHECK_MSG( !image.HasAlpha(), NULL, "alpha channel not supported" );
a46d4814 198
b39fc8d7
VS
199 // NB: wxImage uses RGB order of bytes while DirectFB uses BGR, so we
200 // cannot use preallocated surface that shares data with wxImage, we
201 // have to copy the data to temporary surface instead
c3ee7025
VS
202 return CreateSurfaceWithFormat(image.GetWidth(), image.GetHeight(),
203 DSPF_RGB24);
a46d4814 204}
b3c86150 205
0c8ae720
VS
206static bool ConvertSurfaceToFormat(wxIDirectFBSurfacePtr& surface,
207 DFBSurfacePixelFormat format)
208{
209 if ( surface->GetPixelFormat() == format )
210 return true;
211
212 int w, h;
213 surface->GetSize(&w, &h);
214 wxIDirectFBSurfacePtr s = CreateSurfaceWithFormat(w, h, format);
215 if ( !s )
216 return false;
217
218 if ( !s->SetBlittingFlags(DSBLIT_NOFX) )
219 return false;
220 if ( !s->Blit(surface->GetRaw(), NULL, 0, 0) )
221 return false;
222
223 surface = s;
224 return true;
225}
226
5fe050c3
VS
227static DFBSurfacePixelFormat DepthToFormat(int depth)
228{
229 switch ( depth )
230 {
231 case 24:
232 return DSPF_RGB24;
233 case 32:
234 // NB: we treat depth=32 as requesting ARGB for consistency with
235 // other ports
236 return DSPF_ARGB;
237 default:
238 wxFAIL_MSG( "unsupported depth requested" );
239 // fall through
240 case -1:
241 return DSPF_UNKNOWN;
242 }
243}
244
b3c86150
VS
245//-----------------------------------------------------------------------------
246// wxBitmapRefData
247//-----------------------------------------------------------------------------
248
8f884a0d 249class wxBitmapRefData: public wxGDIRefData
b3c86150
VS
250{
251public:
252 wxBitmapRefData()
253 {
254 m_mask = NULL;
255#if wxUSE_PALETTE
256 m_palette = NULL;
257#endif
258 }
259
260 wxBitmapRefData(const wxBitmapRefData& data)
261 {
2ee16da2 262 m_surface = data.m_surface ? data.m_surface->Clone() : NULL;
a5b31f4e 263
b3c86150
VS
264 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
265#if wxUSE_PALETTE
266 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
267#endif
268 }
269
2ee16da2 270 virtual ~wxBitmapRefData()
b3c86150
VS
271 {
272 delete m_mask;
273#if wxUSE_PALETTE
274 delete m_palette;
275#endif
276 }
277
8f884a0d
VZ
278 virtual bool IsOk() const { return m_surface; }
279
52c8d32a
VS
280 wxIDirectFBSurfacePtr m_surface;
281 wxMask *m_mask;
b3c86150 282#if wxUSE_PALETTE
52c8d32a 283 wxPalette *m_palette;
b3c86150
VS
284#endif
285};
286
287#define M_BITMAP ((wxBitmapRefData *)m_refData)
288
289//-----------------------------------------------------------------------------
290// wxBitmap
291//-----------------------------------------------------------------------------
292
452418c4 293IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
b3c86150
VS
294IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
295
296wxBitmap::wxBitmap(int width, int height, int depth)
297{
298 Create(width, height, depth);
299}
300
4562386d
VS
301bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface)
302{
303 UnRef();
304
a5001e93 305 wxCHECK_MSG( surface, false, "invalid surface" );
4562386d
VS
306
307 m_refData = new wxBitmapRefData();
308 M_BITMAP->m_surface = surface;
309 return true;
310}
311
b3c86150 312bool wxBitmap::Create(int width, int height, int depth)
e2badebb 313{
5fe050c3 314 return CreateWithFormat(width, height, DepthToFormat(depth));
e2badebb
VS
315}
316
317bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat)
b3c86150
VS
318{
319 UnRef();
320
321 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
322
5fe050c3
VS
323 return Create(CreateSurfaceWithFormat(width, height,
324 DFBSurfacePixelFormat(dfbFormat)));
b3c86150
VS
325}
326
b3c86150 327#if wxUSE_IMAGE
82452a17 328wxBitmap::wxBitmap(const wxImage& imageOrig, int depth)
b3c86150 329{
82452a17
VS
330 wxCHECK_RET( imageOrig.Ok(), wxT("invalid image") );
331
332 wxImage image(imageOrig);
333
334 // convert mask to alpha channel, because wxMask isn't implemented yet
335 // FIXME: don't do this, implement proper wxMask support
336 if ( image.HasMask() )
337 image.InitAlpha();
5fe050c3
VS
338
339 DFBSurfacePixelFormat format = DepthToFormat(depth);
340 if ( format == DSPF_UNKNOWN && image.HasAlpha() )
341 format = DSPF_ARGB;
a46d4814 342
e2badebb
VS
343 // create surface in screen's format (unless we need alpha channel,
344 // in which case use ARGB):
5fe050c3 345 if ( !CreateWithFormat(image.GetWidth(), image.GetHeight(), format) )
a46d4814
VS
346 return;
347
348 // then copy the image to it:
a46d4814
VS
349 wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface;
350
e2badebb 351 switch ( dst->GetPixelFormat() )
b39fc8d7 352 {
e2badebb
VS
353 case DSPF_RGB24:
354 case DSPF_RGB32:
355 case DSPF_ARGB:
356 CopyImageToSurface(image, dst);
357 break;
b39fc8d7 358
e2badebb
VS
359 default:
360 {
361 // wxBitmap uses different pixel format, so we have to use a
362 // temporary surface and blit to the bitmap via it:
363 wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image));
364 CopyImageToSurface(image, src);
365
366 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
367 return;
368 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
369 return;
370 }
b39fc8d7 371 }
b3c86150
VS
372}
373
374wxImage wxBitmap::ConvertToImage() const
375{
376 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
377
a46d4814 378 wxImage img(GetWidth(), GetHeight());
a46d4814
VS
379 wxIDirectFBSurfacePtr src = M_BITMAP->m_surface;
380
e2badebb 381 switch ( src->GetPixelFormat() )
b39fc8d7 382 {
e2badebb
VS
383 case DSPF_RGB24:
384 case DSPF_RGB32:
385 case DSPF_ARGB:
386 CopySurfaceToImage(src, img);
387 break;
388 default:
389 {
390 // wxBitmap uses different pixel format, so we have to use a
391 // temporary surface and blit to the bitmap via it:
392 wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img));
b39fc8d7 393
e2badebb
VS
394 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
395 return wxNullImage;
396 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
397 return wxNullImage;
b39fc8d7 398
e2badebb
VS
399 CopySurfaceToImage(dst, img);
400 }
b39fc8d7 401 }
a46d4814
VS
402
403 // FIXME: implement mask setting in the image
a5001e93 404 wxASSERT_MSG( GetMask() == NULL, "bitmap masks are ignored for now" );
a46d4814
VS
405
406 return img;
b3c86150
VS
407}
408#endif // wxUSE_IMAGE
409
c3ee7025
VS
410void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
411{
412 wxCHECK_MSG( Ok(), NULL, "invalid bitmap" );
413
414 AllocExclusive();
415
416 DFBSurfacePixelFormat format;
417 if ( bpp == 32 )
418 format = DSPF_ARGB;
419 else
420 format = DSPF_RGB24;
421
0c8ae720
VS
422 // convert the bitmap into format compatible with requested raw access;
423 // note that we don't bother converting the bitmap back in UngetRawData(),
424 // as unpacked formats (RGB24, RGB32) are the common case and converting
425 // between them while blitting is fast enough (FIXME?)
426 if ( !ConvertSurfaceToFormat(M_BITMAP->m_surface, format) )
c3ee7025
VS
427 return NULL;
428
429 void *bits = NULL;
430 if ( !M_BITMAP->m_surface->Lock
431 (
432 (DFBSurfaceLockFlags)(DSLF_READ | DSLF_WRITE),
433 &bits,
434 &data.m_stride
435 ) )
436 return NULL;
437
438 M_BITMAP->m_surface->GetSize(&data.m_width, &data.m_height);
439
440 return bits;
441}
442
443void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
444{
445 M_BITMAP->m_surface->Unlock();
446}
447
448bool wxBitmap::HasAlpha() const
449{
450 wxCHECK_MSG( Ok(), false, "invalid bitmap" );
451
452 return M_BITMAP->m_surface->GetPixelFormat() == DSPF_ARGB;
453}
454
b3c86150
VS
455wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
456{
457 LoadFile(filename, type);
458}
459
460wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
461{
462 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
949e0a38 463
a5001e93 464 wxFAIL_MSG( "not implemented" );
b3c86150
VS
465}
466
b3c86150
VS
467int wxBitmap::GetHeight() const
468{
469 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
470
471 int h = -1;
52c8d32a 472 M_BITMAP->m_surface->GetSize(NULL, &h);
b3c86150
VS
473 return h;
474}
475
476int wxBitmap::GetWidth() const
477{
478 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
479
480 int w = -1;
52c8d32a 481 M_BITMAP->m_surface->GetSize(&w, NULL);
b3c86150
VS
482 return w;
483}
484
485int wxBitmap::GetDepth() const
486{
487 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
488
a5b31f4e 489 return M_BITMAP->m_surface->GetDepth();
b3c86150
VS
490}
491
492wxMask *wxBitmap::GetMask() const
493{
494 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
495
496 return M_BITMAP->m_mask;
497}
498
499void wxBitmap::SetMask(wxMask *mask)
500{
501 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
502
55ccdb93 503 AllocExclusive();
b3c86150
VS
504 delete M_BITMAP->m_mask;
505 M_BITMAP->m_mask = mask;
506}
507
508bool wxBitmap::CopyFromIcon(const wxIcon& icon)
509{
510 *this = *((wxBitmap*)(&icon));
511 return true;
512}
513
514wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
515{
516 wxCHECK_MSG( Ok() &&
517 rect.x >= 0 && rect.y >= 0 &&
518 rect.x+rect.width <= GetWidth() &&
519 rect.y+rect.height <= GetHeight(),
520 wxNullBitmap,
521 wxT("invalid bitmap or bitmap region") );
522
4562386d
VS
523 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
524 // clone the obtained subsurface
525 DFBRectangle r = { rect.x, rect.y, rect.width, rect.height };
526 return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone());
b3c86150
VS
527}
528
529#warning "to common code"
530bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
531{
532 UnRef();
533
534 wxBitmapHandler *handler = FindHandler(type);
535
536 if ( handler == NULL )
537 {
538 wxImage image;
539 if ( !image.LoadFile(name, type) || !image.Ok() )
540 {
ec5006bd 541 wxLogError(_("No bitmap handler for type %d defined."), type);
b3c86150
VS
542 return false;
543 }
544 else
545 {
546 *this = wxBitmap(image);
547 return true;
548 }
549 }
550
551 m_refData = new wxBitmapRefData();
552
553 return handler->LoadFile(this, name, type, -1, -1);
554}
555
556#warning "to common code"
557bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
558{
559 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
560
561 wxBitmapHandler *handler = FindHandler(type);
562
563 if ( handler == NULL )
564 {
565 wxImage image = ConvertToImage();
566#if wxUSE_PALETTE
567 if ( palette )
568 image.SetPalette(*palette);
569#endif // wxUSE_PALETTE
570
571 if ( image.Ok() )
572 return image.SaveFile(filename, type);
573 else
574 {
ec5006bd 575 wxLogError(_("No bitmap handler for type %d defined."), type);
b3c86150
VS
576 return false;
577 }
578 }
579
580 return handler->SaveFile(this, filename, type, palette);
581}
582
583#if wxUSE_PALETTE
584wxPalette *wxBitmap::GetPalette() const
585{
586 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
587
588 return M_BITMAP->m_palette;
589}
590
591void wxBitmap::SetPalette(const wxPalette& palette)
592{
593 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
594 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
595
55ccdb93 596 AllocExclusive();
b3c86150
VS
597 delete M_BITMAP->m_palette;
598 M_BITMAP->m_palette = NULL;
599
600 if ( !palette.Ok() ) return;
601
602 M_BITMAP->m_palette = new wxPalette(palette);
603}
604#endif // wxUSE_PALETTE
605
606void wxBitmap::SetHeight(int height)
607{
608 AllocExclusive();
3278b06e 609
a5001e93 610 wxFAIL_MSG( "SetHeight not implemented" );
b3c86150
VS
611}
612
613void wxBitmap::SetWidth(int width)
614{
615 AllocExclusive();
3278b06e 616
a5001e93 617 wxFAIL_MSG( "SetWidth not implemented" );
b3c86150
VS
618}
619
620void wxBitmap::SetDepth(int depth)
621{
5fe050c3
VS
622 DFBSurfacePixelFormat format = DepthToFormat(depth);
623 if ( M_BITMAP->m_surface->GetPixelFormat() == format )
624 return;
625
b3c86150 626 AllocExclusive();
3278b06e 627
5fe050c3
VS
628 int w, h;
629 M_BITMAP->m_surface->GetSize(&w, &h);
630 wxIDirectFBSurfacePtr s = CreateSurfaceWithFormat(w, h, format);
631 if ( !s )
632 return;
633 if ( !s->SetBlittingFlags(DSBLIT_NOFX) )
634 return;
635 if ( !s->Blit(M_BITMAP->m_surface->GetRaw(), NULL, 0, 0) )
636 return;
637
638 M_BITMAP->m_surface = s;
b3c86150
VS
639}
640
52c8d32a 641wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
b3c86150
VS
642{
643 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
644
645 return M_BITMAP->m_surface;
646}
647
8f884a0d 648wxGDIRefData *wxBitmap::CreateGDIRefData() const
b3c86150
VS
649{
650 return new wxBitmapRefData;
651}
652
8f884a0d 653wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
b3c86150
VS
654{
655 return new wxBitmapRefData(*(wxBitmapRefData *)data);
656}
657
658
659/*static*/
660void wxBitmap::InitStandardHandlers()
661{
662 // not wxBitmap handlers, we rely on wxImage
663}