implemented raw bitmap access for wxDFB
[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, _T("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 | DSDESC_PIXELFORMAT);
177 desc.caps = DSCAPS_NONE;
178 desc.width = w;
179 desc.height = h;
180 desc.pixelformat = format;
181
182 return wxIDirectFB::Get()->CreateSurface(&desc);
183 }
184
185 // Creates a surface that will use wxImage's pixel data (RGB only)
186 static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image)
187 {
188 wxCHECK_MSG( image.Ok(), NULL, _T("invalid image") );
189 // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB
190 // into a temporary RGBA surface
191 wxCHECK_MSG( !image.HasAlpha(), NULL, _T("alpha channel not supported") );
192
193 // NB: wxImage uses RGB order of bytes while DirectFB uses BGR, so we
194 // cannot use preallocated surface that shares data with wxImage, we
195 // have to copy the data to temporary surface instead
196 return CreateSurfaceWithFormat(image.GetWidth(), image.GetHeight(),
197 DSPF_RGB24);
198 }
199
200 //-----------------------------------------------------------------------------
201 // wxBitmapRefData
202 //-----------------------------------------------------------------------------
203
204 class wxBitmapRefData: public wxObjectRefData
205 {
206 public:
207 wxBitmapRefData()
208 {
209 m_mask = NULL;
210 #if wxUSE_PALETTE
211 m_palette = NULL;
212 #endif
213 }
214
215 wxBitmapRefData(const wxBitmapRefData& data)
216 {
217 m_surface = data.m_surface ? data.m_surface->Clone() : NULL;
218
219 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
220 #if wxUSE_PALETTE
221 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
222 #endif
223 }
224
225 virtual ~wxBitmapRefData()
226 {
227 delete m_mask;
228 #if wxUSE_PALETTE
229 delete m_palette;
230 #endif
231 }
232
233 wxIDirectFBSurfacePtr m_surface;
234 wxMask *m_mask;
235 #if wxUSE_PALETTE
236 wxPalette *m_palette;
237 #endif
238 };
239
240 #define M_BITMAP ((wxBitmapRefData *)m_refData)
241
242 //-----------------------------------------------------------------------------
243 // wxBitmap
244 //-----------------------------------------------------------------------------
245
246 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
247 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
248
249 wxBitmap::wxBitmap(int width, int height, int depth)
250 {
251 Create(width, height, depth);
252 }
253
254 bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface)
255 {
256 UnRef();
257
258 wxCHECK_MSG( surface, false, _T("invalid surface") );
259
260 m_refData = new wxBitmapRefData();
261 M_BITMAP->m_surface = surface;
262 return true;
263 }
264
265 bool wxBitmap::Create(int width, int height, int depth)
266 {
267 wxCHECK_MSG( depth == -1, false, wxT("only default depth supported now") );
268
269 return CreateWithFormat(width, height, -1);
270 }
271
272 bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat)
273 {
274 UnRef();
275
276 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
277
278 DFBSurfaceDescription desc;
279 desc.flags = (DFBSurfaceDescriptionFlags)(
280 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
281 desc.caps = DSCAPS_NONE;
282 desc.width = width;
283 desc.height = height;
284
285 if ( dfbFormat != -1 )
286 {
287 desc.flags = (DFBSurfaceDescriptionFlags)(
288 desc.flags | DSDESC_PIXELFORMAT);
289 desc.pixelformat = (DFBSurfacePixelFormat)dfbFormat;
290 }
291
292 return Create(wxIDirectFB::Get()->CreateSurface(&desc));
293 }
294
295 #if wxUSE_IMAGE
296 wxBitmap::wxBitmap(const wxImage& image, int depth)
297 {
298 wxCHECK_RET( image.Ok(), wxT("invalid image") );
299 wxCHECK_RET( depth == -1, wxT("only default depth supported now") );
300
301 // create surface in screen's format (unless we need alpha channel,
302 // in which case use ARGB):
303 if ( !CreateWithFormat(image.GetWidth(), image.GetHeight(),
304 image.HasAlpha() ? DSPF_ARGB : -1) )
305 return;
306
307 // then copy the image to it:
308 wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface;
309
310 switch ( dst->GetPixelFormat() )
311 {
312 case DSPF_RGB24:
313 case DSPF_RGB32:
314 case DSPF_ARGB:
315 CopyImageToSurface(image, dst);
316 break;
317
318 default:
319 {
320 // wxBitmap uses different pixel format, so we have to use a
321 // temporary surface and blit to the bitmap via it:
322 wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image));
323 CopyImageToSurface(image, src);
324
325 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
326 return;
327 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
328 return;
329 }
330 }
331
332 // FIXME: implement mask creation from image's mask (or alpha channel?)
333 wxASSERT_MSG( !image.HasMask(), _T("image masks are ignored for now") );
334 }
335
336 wxImage wxBitmap::ConvertToImage() const
337 {
338 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
339
340 wxImage img(GetWidth(), GetHeight());
341 wxIDirectFBSurfacePtr src = M_BITMAP->m_surface;
342
343 switch ( src->GetPixelFormat() )
344 {
345 case DSPF_RGB24:
346 case DSPF_RGB32:
347 case DSPF_ARGB:
348 CopySurfaceToImage(src, img);
349 break;
350 default:
351 {
352 // wxBitmap uses different pixel format, so we have to use a
353 // temporary surface and blit to the bitmap via it:
354 wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img));
355
356 if ( !dst->SetBlittingFlags(DSBLIT_NOFX) )
357 return wxNullImage;
358 if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) )
359 return wxNullImage;
360
361 CopySurfaceToImage(dst, img);
362 }
363 }
364
365 // FIXME: implement mask setting in the image
366 wxASSERT_MSG( GetMask() == NULL, _T("bitmap masks are ignored for now") );
367
368 return img;
369 }
370 #endif // wxUSE_IMAGE
371
372 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
373 {
374 wxCHECK_MSG( Ok(), NULL, "invalid bitmap" );
375
376 AllocExclusive();
377
378 DFBSurfacePixelFormat format;
379 if ( bpp == 32 )
380 format = DSPF_ARGB;
381 else
382 format = DSPF_RGB24;
383
384 // requested format is not what this bitmap uses
385 if ( format != M_BITMAP->m_surface->GetPixelFormat() )
386 return NULL;
387
388 void *bits = NULL;
389 if ( !M_BITMAP->m_surface->Lock
390 (
391 (DFBSurfaceLockFlags)(DSLF_READ | DSLF_WRITE),
392 &bits,
393 &data.m_stride
394 ) )
395 return NULL;
396
397 M_BITMAP->m_surface->GetSize(&data.m_width, &data.m_height);
398
399 return bits;
400 }
401
402 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
403 {
404 M_BITMAP->m_surface->Unlock();
405 }
406
407 bool wxBitmap::HasAlpha() const
408 {
409 wxCHECK_MSG( Ok(), false, "invalid bitmap" );
410
411 return M_BITMAP->m_surface->GetPixelFormat() == DSPF_ARGB;
412 }
413
414 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
415 {
416 LoadFile(filename, type);
417 }
418
419 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
420 {
421 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
422
423 wxFAIL_MSG( _T("not implemented") );
424 }
425
426 bool wxBitmap::IsOk() const
427 {
428 return (m_refData != NULL && M_BITMAP->m_surface);
429 }
430
431 int wxBitmap::GetHeight() const
432 {
433 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
434
435 int h = -1;
436 M_BITMAP->m_surface->GetSize(NULL, &h);
437 return h;
438 }
439
440 int wxBitmap::GetWidth() const
441 {
442 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
443
444 int w = -1;
445 M_BITMAP->m_surface->GetSize(&w, NULL);
446 return w;
447 }
448
449 int wxBitmap::GetDepth() const
450 {
451 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
452
453 return M_BITMAP->m_surface->GetDepth();
454 }
455
456 wxMask *wxBitmap::GetMask() const
457 {
458 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
459
460 return M_BITMAP->m_mask;
461 }
462
463 void wxBitmap::SetMask(wxMask *mask)
464 {
465 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
466
467 AllocExclusive();
468 delete M_BITMAP->m_mask;
469 M_BITMAP->m_mask = mask;
470 }
471
472 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
473 {
474 *this = *((wxBitmap*)(&icon));
475 return true;
476 }
477
478 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
479 {
480 wxCHECK_MSG( Ok() &&
481 rect.x >= 0 && rect.y >= 0 &&
482 rect.x+rect.width <= GetWidth() &&
483 rect.y+rect.height <= GetHeight(),
484 wxNullBitmap,
485 wxT("invalid bitmap or bitmap region") );
486
487 // NB: DirectFB subsurfaces share the same pixels buffer, so we must
488 // clone the obtained subsurface
489 DFBRectangle r = { rect.x, rect.y, rect.width, rect.height };
490 return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone());
491 }
492
493 #warning "to common code"
494 bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
495 {
496 UnRef();
497
498 wxBitmapHandler *handler = FindHandler(type);
499
500 if ( handler == NULL )
501 {
502 wxImage image;
503 if ( !image.LoadFile(name, type) || !image.Ok() )
504 {
505 wxLogError(_("No bitmap handler for type %d defined."), type);
506 return false;
507 }
508 else
509 {
510 *this = wxBitmap(image);
511 return true;
512 }
513 }
514
515 m_refData = new wxBitmapRefData();
516
517 return handler->LoadFile(this, name, type, -1, -1);
518 }
519
520 #warning "to common code"
521 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
522 {
523 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
524
525 wxBitmapHandler *handler = FindHandler(type);
526
527 if ( handler == NULL )
528 {
529 wxImage image = ConvertToImage();
530 #if wxUSE_PALETTE
531 if ( palette )
532 image.SetPalette(*palette);
533 #endif // wxUSE_PALETTE
534
535 if ( image.Ok() )
536 return image.SaveFile(filename, type);
537 else
538 {
539 wxLogError(_("No bitmap handler for type %d defined."), type);
540 return false;
541 }
542 }
543
544 return handler->SaveFile(this, filename, type, palette);
545 }
546
547 #if wxUSE_PALETTE
548 wxPalette *wxBitmap::GetPalette() const
549 {
550 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
551
552 return M_BITMAP->m_palette;
553 }
554
555 void wxBitmap::SetPalette(const wxPalette& palette)
556 {
557 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
558 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
559
560 AllocExclusive();
561 delete M_BITMAP->m_palette;
562 M_BITMAP->m_palette = NULL;
563
564 if ( !palette.Ok() ) return;
565
566 M_BITMAP->m_palette = new wxPalette(palette);
567 }
568 #endif // wxUSE_PALETTE
569
570 void wxBitmap::SetHeight(int height)
571 {
572 AllocExclusive();
573
574 wxFAIL_MSG( _T("SetHeight not implemented") );
575 }
576
577 void wxBitmap::SetWidth(int width)
578 {
579 AllocExclusive();
580
581 wxFAIL_MSG( _T("SetWidth not implemented") );
582 }
583
584 void wxBitmap::SetDepth(int depth)
585 {
586 AllocExclusive();
587
588 wxFAIL_MSG( _T("SetDepth not implemented") );
589 }
590
591 wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
592 {
593 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
594
595 return M_BITMAP->m_surface;
596 }
597
598 wxObjectRefData *wxBitmap::CreateRefData() const
599 {
600 return new wxBitmapRefData;
601 }
602
603 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
604 {
605 return new wxBitmapRefData(*(wxBitmapRefData *)data);
606 }
607
608
609 /*static*/
610 void wxBitmap::InitStandardHandlers()
611 {
612 // not wxBitmap handlers, we rely on wxImage
613 }