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