Fix Ok/IsOk() mess in wxGDIObject-derived classes; also added
[wxWidgets.git] / src / dfb / bitmap.cpp
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 #include "wx/rawbmp.h"
27
28 #include "wx/dfb/private.h"
29
30 //-----------------------------------------------------------------------------
31 // helpers
32 //-----------------------------------------------------------------------------
33
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
40 // pitch = stride = # of bytes between the start of N-th line and (N+1)-th line
41 // {Src,Dst}PixSize = # of bytes used to represent one pixel
42 template<int SrcPixSize, int DstPixSize>
43 static void CopyPixelsAndSwapRGB(unsigned w, unsigned h,
44 const unsigned char *src,
45 unsigned src_pitch,
46 unsigned char *dst,
47 unsigned dst_pitch)
48 {
49 unsigned src_advance = src_pitch - SrcPixSize * w;
50 unsigned dst_advance = dst_pitch - DstPixSize * w;
51 for ( unsigned y = 0; y < h; y++, src += src_advance, dst += dst_advance )
52 {
53 for ( unsigned x = 0; x < w; x++, src += SrcPixSize, dst += DstPixSize )
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
63 static void CopySurfaceToImage(const wxIDirectFBSurfacePtr& surface,
64 wxImage& image)
65 {
66 wxIDirectFBSurface::Locked locked(surface, DSLF_READ);
67 wxCHECK_RET( locked.ptr, "failed to lock surface" );
68
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 }
116 }
117
118 static void CopyImageToSurface(const wxImage& image,
119 const wxIDirectFBSurfacePtr& surface)
120 {
121 wxIDirectFBSurface::Locked locked(surface, DSLF_WRITE);
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" );
159
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 }
169 }
170
171 static wxIDirectFBSurfacePtr
172 CreateSurfaceWithFormat(int w, int h, DFBSurfacePixelFormat format)
173 {
174 DFBSurfaceDescription desc;
175 desc.flags = (DFBSurfaceDescriptionFlags)
176 (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
177 desc.caps = DSCAPS_NONE;
178 desc.width = w;
179 desc.height = h;
180
181 if ( format != DSPF_UNKNOWN )
182 {
183 desc.flags = (DFBSurfaceDescriptionFlags)(
184 desc.flags | DSDESC_PIXELFORMAT);
185 desc.pixelformat = format;
186 }
187
188 return wxIDirectFB::Get()->CreateSurface(&desc);
189 }
190
191 // Creates a surface that will use wxImage's pixel data (RGB only)
192 static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image)
193 {
194 wxCHECK_MSG( image.Ok(), NULL, "invalid image" );
195 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
196 // into a temporary RGBA surface
197 wxCHECK_MSG( !image.HasAlpha(), NULL, "alpha channel not supported" );
198
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
202 return CreateSurfaceWithFormat(image.GetWidth(), image.GetHeight(),
203 DSPF_RGB24);
204 }
205
206 static 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
227 static 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
245 //-----------------------------------------------------------------------------
246 // wxBitmapRefData
247 //-----------------------------------------------------------------------------
248
249 class wxBitmapRefData: public wxGDIRefData
250 {
251 public:
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 {
262 m_surface = data.m_surface ? data.m_surface->Clone() : NULL;
263
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
270 virtual ~wxBitmapRefData()
271 {
272 delete m_mask;
273 #if wxUSE_PALETTE
274 delete m_palette;
275 #endif
276 }
277
278 virtual bool IsOk() const { return m_surface; }
279
280 wxIDirectFBSurfacePtr m_surface;
281 wxMask *m_mask;
282 #if wxUSE_PALETTE
283 wxPalette *m_palette;
284 #endif
285 };
286
287 #define M_BITMAP ((wxBitmapRefData *)m_refData)
288
289 //-----------------------------------------------------------------------------
290 // wxBitmap
291 //-----------------------------------------------------------------------------
292
293 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
294 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
295
296 wxBitmap::wxBitmap(int width, int height, int depth)
297 {
298 Create(width, height, depth);
299 }
300
301 bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface)
302 {
303 UnRef();
304
305 wxCHECK_MSG( surface, false, "invalid surface" );
306
307 m_refData = new wxBitmapRefData();
308 M_BITMAP->m_surface = surface;
309 return true;
310 }
311
312 bool wxBitmap::Create(int width, int height, int depth)
313 {
314 return CreateWithFormat(width, height, DepthToFormat(depth));
315 }
316
317 bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat)
318 {
319 UnRef();
320
321 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
322
323 return Create(CreateSurfaceWithFormat(width, height,
324 DFBSurfacePixelFormat(dfbFormat)));
325 }
326
327 #if wxUSE_IMAGE
328 wxBitmap::wxBitmap(const wxImage& image, int depth)
329 {
330 wxCHECK_RET( image.Ok(), wxT("invalid image") );
331
332 DFBSurfacePixelFormat format = DepthToFormat(depth);
333 if ( format == DSPF_UNKNOWN && image.HasAlpha() )
334 format = DSPF_ARGB;
335
336 // create surface in screen's format (unless we need alpha channel,
337 // in which case use ARGB):
338 if ( !CreateWithFormat(image.GetWidth(), image.GetHeight(), format) )
339 return;
340
341 // then copy the image to it:
342 wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface;
343
344 switch ( dst->GetPixelFormat() )
345 {
346 case DSPF_RGB24:
347 case DSPF_RGB32:
348 case DSPF_ARGB:
349 CopyImageToSurface(image, dst);
350 break;
351
352 default:
353 {
354 // wxBitmap uses different pixel format, so we have to use a
355 // temporary surface and blit to the bitmap via it:
356 wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image));
357 CopyImageToSurface(image, src);
358
359 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
360 return;
361 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
362 return;
363 }
364 }
365
366 // FIXME: implement mask creation from image's mask (or alpha channel?)
367 wxASSERT_MSG( !image.HasMask(), "image masks are ignored for now" );
368 }
369
370 wxImage wxBitmap::ConvertToImage() const
371 {
372 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
373
374 wxImage img(GetWidth(), GetHeight());
375 wxIDirectFBSurfacePtr src = M_BITMAP->m_surface;
376
377 switch ( src->GetPixelFormat() )
378 {
379 case DSPF_RGB24:
380 case DSPF_RGB32:
381 case DSPF_ARGB:
382 CopySurfaceToImage(src, img);
383 break;
384 default:
385 {
386 // wxBitmap uses different pixel format, so we have to use a
387 // temporary surface and blit to the bitmap via it:
388 wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img));
389
390 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
391 return wxNullImage;
392 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
393 return wxNullImage;
394
395 CopySurfaceToImage(dst, img);
396 }
397 }
398
399 // FIXME: implement mask setting in the image
400 wxASSERT_MSG( GetMask() == NULL, "bitmap masks are ignored for now" );
401
402 return img;
403 }
404 #endif // wxUSE_IMAGE
405
406 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
407 {
408 wxCHECK_MSG( Ok(), NULL, "invalid bitmap" );
409
410 AllocExclusive();
411
412 DFBSurfacePixelFormat format;
413 if ( bpp == 32 )
414 format = DSPF_ARGB;
415 else
416 format = DSPF_RGB24;
417
418 // convert the bitmap into format compatible with requested raw access;
419 // note that we don't bother converting the bitmap back in UngetRawData(),
420 // as unpacked formats (RGB24, RGB32) are the common case and converting
421 // between them while blitting is fast enough (FIXME?)
422 if ( !ConvertSurfaceToFormat(M_BITMAP->m_surface, format) )
423 return NULL;
424
425 void *bits = NULL;
426 if ( !M_BITMAP->m_surface->Lock
427 (
428 (DFBSurfaceLockFlags)(DSLF_READ | DSLF_WRITE),
429 &bits,
430 &data.m_stride
431 ) )
432 return NULL;
433
434 M_BITMAP->m_surface->GetSize(&data.m_width, &data.m_height);
435
436 return bits;
437 }
438
439 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
440 {
441 M_BITMAP->m_surface->Unlock();
442 }
443
444 bool wxBitmap::HasAlpha() const
445 {
446 wxCHECK_MSG( Ok(), false, "invalid bitmap" );
447
448 return M_BITMAP->m_surface->GetPixelFormat() == DSPF_ARGB;
449 }
450
451 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
452 {
453 LoadFile(filename, type);
454 }
455
456 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
457 {
458 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
459
460 wxFAIL_MSG( "not implemented" );
461 }
462
463 int wxBitmap::GetHeight() const
464 {
465 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
466
467 int h = -1;
468 M_BITMAP->m_surface->GetSize(NULL, &h);
469 return h;
470 }
471
472 int wxBitmap::GetWidth() const
473 {
474 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
475
476 int w = -1;
477 M_BITMAP->m_surface->GetSize(&w, NULL);
478 return w;
479 }
480
481 int wxBitmap::GetDepth() const
482 {
483 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
484
485 return M_BITMAP->m_surface->GetDepth();
486 }
487
488 wxMask *wxBitmap::GetMask() const
489 {
490 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
491
492 return M_BITMAP->m_mask;
493 }
494
495 void wxBitmap::SetMask(wxMask *mask)
496 {
497 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
498
499 AllocExclusive();
500 delete M_BITMAP->m_mask;
501 M_BITMAP->m_mask = mask;
502 }
503
504 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
505 {
506 *this = *((wxBitmap*)(&icon));
507 return true;
508 }
509
510 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
511 {
512 wxCHECK_MSG( Ok() &&
513 rect.x >= 0 && rect.y >= 0 &&
514 rect.x+rect.width <= GetWidth() &&
515 rect.y+rect.height <= GetHeight(),
516 wxNullBitmap,
517 wxT("invalid bitmap or bitmap region") );
518
519 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
520 // clone the obtained subsurface
521 DFBRectangle r = { rect.x, rect.y, rect.width, rect.height };
522 return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone());
523 }
524
525 #warning "to common code"
526 bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
527 {
528 UnRef();
529
530 wxBitmapHandler *handler = FindHandler(type);
531
532 if ( handler == NULL )
533 {
534 wxImage image;
535 if ( !image.LoadFile(name, type) || !image.Ok() )
536 {
537 wxLogError(_("No bitmap handler for type %d defined."), type);
538 return false;
539 }
540 else
541 {
542 *this = wxBitmap(image);
543 return true;
544 }
545 }
546
547 m_refData = new wxBitmapRefData();
548
549 return handler->LoadFile(this, name, type, -1, -1);
550 }
551
552 #warning "to common code"
553 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
554 {
555 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
556
557 wxBitmapHandler *handler = FindHandler(type);
558
559 if ( handler == NULL )
560 {
561 wxImage image = ConvertToImage();
562 #if wxUSE_PALETTE
563 if ( palette )
564 image.SetPalette(*palette);
565 #endif // wxUSE_PALETTE
566
567 if ( image.Ok() )
568 return image.SaveFile(filename, type);
569 else
570 {
571 wxLogError(_("No bitmap handler for type %d defined."), type);
572 return false;
573 }
574 }
575
576 return handler->SaveFile(this, filename, type, palette);
577 }
578
579 #if wxUSE_PALETTE
580 wxPalette *wxBitmap::GetPalette() const
581 {
582 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
583
584 return M_BITMAP->m_palette;
585 }
586
587 void wxBitmap::SetPalette(const wxPalette& palette)
588 {
589 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
590 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
591
592 AllocExclusive();
593 delete M_BITMAP->m_palette;
594 M_BITMAP->m_palette = NULL;
595
596 if ( !palette.Ok() ) return;
597
598 M_BITMAP->m_palette = new wxPalette(palette);
599 }
600 #endif // wxUSE_PALETTE
601
602 void wxBitmap::SetHeight(int height)
603 {
604 AllocExclusive();
605
606 wxFAIL_MSG( "SetHeight not implemented" );
607 }
608
609 void wxBitmap::SetWidth(int width)
610 {
611 AllocExclusive();
612
613 wxFAIL_MSG( "SetWidth not implemented" );
614 }
615
616 void wxBitmap::SetDepth(int depth)
617 {
618 DFBSurfacePixelFormat format = DepthToFormat(depth);
619 if ( M_BITMAP->m_surface->GetPixelFormat() == format )
620 return;
621
622 AllocExclusive();
623
624 int w, h;
625 M_BITMAP->m_surface->GetSize(&w, &h);
626 wxIDirectFBSurfacePtr s = CreateSurfaceWithFormat(w, h, format);
627 if ( !s )
628 return;
629 if ( !s->SetBlittingFlags(DSBLIT_NOFX) )
630 return;
631 if ( !s->Blit(M_BITMAP->m_surface->GetRaw(), NULL, 0, 0) )
632 return;
633
634 M_BITMAP->m_surface = s;
635 }
636
637 wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
638 {
639 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
640
641 return M_BITMAP->m_surface;
642 }
643
644 wxGDIRefData *wxBitmap::CreateGDIRefData() const
645 {
646 return new wxBitmapRefData;
647 }
648
649 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
650 {
651 return new wxBitmapRefData(*(wxBitmapRefData *)data);
652 }
653
654
655 /*static*/
656 void wxBitmap::InitStandardHandlers()
657 {
658 // not wxBitmap handlers, we rely on wxImage
659 }