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