]>
Commit | Line | Data |
---|---|---|
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 for translating between wx and DFB pixel formats | |
32 | //----------------------------------------------------------------------------- | |
33 | ||
34 | namespace | |
35 | { | |
36 | ||
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 | ||
43 | // pitch = stride = # of bytes between the start of N-th line and (N+1)-th line | |
44 | // {Src,Dst}PixSize = # of bytes used to represent one pixel | |
45 | template<int SrcPixSize, int DstPixSize> | |
46 | void CopyPixelsAndSwapRGB(unsigned w, unsigned h, | |
47 | const unsigned char *src, | |
48 | unsigned src_pitch, | |
49 | unsigned char *dst, | |
50 | unsigned dst_pitch) | |
51 | { | |
52 | unsigned src_advance = src_pitch - SrcPixSize * w; | |
53 | unsigned dst_advance = dst_pitch - DstPixSize * w; | |
54 | for ( unsigned y = 0; y < h; y++, src += src_advance, dst += dst_advance ) | |
55 | { | |
56 | for ( unsigned x = 0; x < w; x++, src += SrcPixSize, dst += DstPixSize ) | |
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 | ||
66 | void CopySurfaceToImage(const wxIDirectFBSurfacePtr& surface, wxImage& image) | |
67 | { | |
68 | wxIDirectFBSurface::Locked locked(surface, DSLF_READ); | |
69 | wxCHECK_RET( locked.ptr, "failed to lock surface" ); | |
70 | ||
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 | } | |
118 | } | |
119 | ||
120 | void CopyImageToSurface(const wxImage& image, | |
121 | const wxIDirectFBSurfacePtr& surface) | |
122 | { | |
123 | wxIDirectFBSurface::Locked locked(surface, DSLF_WRITE); | |
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" ); | |
161 | ||
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 | } | |
171 | } | |
172 | ||
173 | wxIDirectFBSurfacePtr | |
174 | CreateSurfaceWithFormat(int w, int h, DFBSurfacePixelFormat format) | |
175 | { | |
176 | DFBSurfaceDescription desc; | |
177 | desc.flags = (DFBSurfaceDescriptionFlags) | |
178 | (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT); | |
179 | desc.caps = DSCAPS_NONE; | |
180 | desc.width = w; | |
181 | desc.height = h; | |
182 | ||
183 | if ( format != DSPF_UNKNOWN ) | |
184 | { | |
185 | desc.flags = (DFBSurfaceDescriptionFlags)( | |
186 | desc.flags | DSDESC_PIXELFORMAT); | |
187 | desc.pixelformat = format; | |
188 | } | |
189 | ||
190 | return wxIDirectFB::Get()->CreateSurface(&desc); | |
191 | } | |
192 | ||
193 | // Creates a surface that will use wxImage's pixel data (RGB only) | |
194 | wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image) | |
195 | { | |
196 | wxCHECK_MSG( image.Ok(), NULL, "invalid image" ); | |
197 | // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB | |
198 | // into a temporary RGBA surface | |
199 | wxCHECK_MSG( !image.HasAlpha(), NULL, "alpha channel not supported" ); | |
200 | ||
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 | |
204 | return CreateSurfaceWithFormat(image.GetWidth(), image.GetHeight(), | |
205 | DSPF_RGB24); | |
206 | } | |
207 | ||
208 | bool ConvertSurfaceToFormat(wxIDirectFBSurfacePtr& surface, | |
209 | DFBSurfacePixelFormat format) | |
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 | ||
229 | DFBSurfacePixelFormat DepthToFormat(int depth) | |
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 | ||
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 | |
255 | template <typename T, int White, int Black> | |
256 | void | |
257 | CopyBits(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 | ||
299 | bool | |
300 | CopyBitsToSurface(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 | ||
347 | //----------------------------------------------------------------------------- | |
348 | // wxBitmapRefData | |
349 | //----------------------------------------------------------------------------- | |
350 | ||
351 | class wxBitmapRefData: public wxGDIRefData | |
352 | { | |
353 | public: | |
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 | { | |
364 | m_surface = data.m_surface ? data.m_surface->Clone() : NULL; | |
365 | ||
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 | ||
372 | virtual ~wxBitmapRefData() | |
373 | { | |
374 | delete m_mask; | |
375 | #if wxUSE_PALETTE | |
376 | delete m_palette; | |
377 | #endif | |
378 | } | |
379 | ||
380 | virtual bool IsOk() const { return m_surface; } | |
381 | ||
382 | wxIDirectFBSurfacePtr m_surface; | |
383 | wxMask *m_mask; | |
384 | #if wxUSE_PALETTE | |
385 | wxPalette *m_palette; | |
386 | #endif | |
387 | }; | |
388 | ||
389 | #define M_BITMAP ((wxBitmapRefData *)m_refData) | |
390 | ||
391 | //----------------------------------------------------------------------------- | |
392 | // wxBitmap | |
393 | //----------------------------------------------------------------------------- | |
394 | ||
395 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase) | |
396 | ||
397 | wxBitmap::wxBitmap(int width, int height, int depth) | |
398 | { | |
399 | Create(width, height, depth); | |
400 | } | |
401 | ||
402 | bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface) | |
403 | { | |
404 | UnRef(); | |
405 | ||
406 | wxCHECK_MSG( surface, false, "invalid surface" ); | |
407 | ||
408 | m_refData = new wxBitmapRefData(); | |
409 | M_BITMAP->m_surface = surface; | |
410 | return true; | |
411 | } | |
412 | ||
413 | bool wxBitmap::Create(int width, int height, int depth) | |
414 | { | |
415 | return CreateWithFormat(width, height, DepthToFormat(depth)); | |
416 | } | |
417 | ||
418 | bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat) | |
419 | { | |
420 | UnRef(); | |
421 | ||
422 | wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") ); | |
423 | ||
424 | return Create(CreateSurfaceWithFormat(width, height, | |
425 | DFBSurfacePixelFormat(dfbFormat))); | |
426 | } | |
427 | ||
428 | #if wxUSE_IMAGE | |
429 | wxBitmap::wxBitmap(const wxImage& imageOrig, int depth) | |
430 | { | |
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(); | |
439 | ||
440 | DFBSurfacePixelFormat format = DepthToFormat(depth); | |
441 | if ( format == DSPF_UNKNOWN && image.HasAlpha() ) | |
442 | format = DSPF_ARGB; | |
443 | ||
444 | // create surface in screen's format (unless we need alpha channel, | |
445 | // in which case use ARGB): | |
446 | if ( !CreateWithFormat(image.GetWidth(), image.GetHeight(), format) ) | |
447 | return; | |
448 | ||
449 | // then copy the image to it: | |
450 | wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface; | |
451 | ||
452 | switch ( dst->GetPixelFormat() ) | |
453 | { | |
454 | case DSPF_RGB24: | |
455 | case DSPF_RGB32: | |
456 | case DSPF_ARGB: | |
457 | CopyImageToSurface(image, dst); | |
458 | break; | |
459 | ||
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 | } | |
472 | } | |
473 | } | |
474 | ||
475 | wxImage wxBitmap::ConvertToImage() const | |
476 | { | |
477 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
478 | ||
479 | wxImage img(GetWidth(), GetHeight()); | |
480 | wxIDirectFBSurfacePtr src = M_BITMAP->m_surface; | |
481 | ||
482 | switch ( src->GetPixelFormat() ) | |
483 | { | |
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)); | |
494 | ||
495 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) | |
496 | return wxNullImage; | |
497 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) | |
498 | return wxNullImage; | |
499 | ||
500 | CopySurfaceToImage(dst, img); | |
501 | } | |
502 | } | |
503 | ||
504 | // FIXME: implement mask setting in the image | |
505 | wxASSERT_MSG( GetMask() == NULL, "bitmap masks are ignored for now" ); | |
506 | ||
507 | return img; | |
508 | } | |
509 | #endif // wxUSE_IMAGE | |
510 | ||
511 | void *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 | ||
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) ) | |
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 | ||
544 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) | |
545 | { | |
546 | M_BITMAP->m_surface->Unlock(); | |
547 | } | |
548 | ||
549 | bool wxBitmap::HasAlpha() const | |
550 | { | |
551 | wxCHECK_MSG( Ok(), false, "invalid bitmap" ); | |
552 | ||
553 | return M_BITMAP->m_surface->GetPixelFormat() == DSPF_ARGB; | |
554 | } | |
555 | ||
556 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) | |
557 | { | |
558 | LoadFile(filename, type); | |
559 | } | |
560 | ||
561 | wxBitmap::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") ); | |
564 | ||
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(); | |
572 | } | |
573 | ||
574 | int wxBitmap::GetHeight() const | |
575 | { | |
576 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
577 | ||
578 | int h = -1; | |
579 | M_BITMAP->m_surface->GetSize(NULL, &h); | |
580 | return h; | |
581 | } | |
582 | ||
583 | int wxBitmap::GetWidth() const | |
584 | { | |
585 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
586 | ||
587 | int w = -1; | |
588 | M_BITMAP->m_surface->GetSize(&w, NULL); | |
589 | return w; | |
590 | } | |
591 | ||
592 | int wxBitmap::GetDepth() const | |
593 | { | |
594 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
595 | ||
596 | return M_BITMAP->m_surface->GetDepth(); | |
597 | } | |
598 | ||
599 | wxMask *wxBitmap::GetMask() const | |
600 | { | |
601 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
602 | ||
603 | return M_BITMAP->m_mask; | |
604 | } | |
605 | ||
606 | void wxBitmap::SetMask(wxMask *mask) | |
607 | { | |
608 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
609 | ||
610 | AllocExclusive(); | |
611 | delete M_BITMAP->m_mask; | |
612 | M_BITMAP->m_mask = mask; | |
613 | } | |
614 | ||
615 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
616 | { | |
617 | *this = *((wxBitmap*)(&icon)); | |
618 | return true; | |
619 | } | |
620 | ||
621 | wxBitmap 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 | ||
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()); | |
634 | } | |
635 | ||
636 | #warning "to common code" | |
637 | bool 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 | { | |
648 | wxLogError(_("No bitmap handler for type %d defined."), type); | |
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" | |
664 | bool 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 | { | |
682 | wxLogError(_("No bitmap handler for type %d defined."), type); | |
683 | return false; | |
684 | } | |
685 | } | |
686 | ||
687 | return handler->SaveFile(this, filename, type, palette); | |
688 | } | |
689 | ||
690 | #if wxUSE_PALETTE | |
691 | wxPalette *wxBitmap::GetPalette() const | |
692 | { | |
693 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
694 | ||
695 | return M_BITMAP->m_palette; | |
696 | } | |
697 | ||
698 | void 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 | ||
703 | AllocExclusive(); | |
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 | ||
713 | void wxBitmap::SetHeight(int height) | |
714 | { | |
715 | AllocExclusive(); | |
716 | ||
717 | wxFAIL_MSG( "SetHeight not implemented" ); | |
718 | } | |
719 | ||
720 | void wxBitmap::SetWidth(int width) | |
721 | { | |
722 | AllocExclusive(); | |
723 | ||
724 | wxFAIL_MSG( "SetWidth not implemented" ); | |
725 | } | |
726 | ||
727 | void wxBitmap::SetDepth(int depth) | |
728 | { | |
729 | DFBSurfacePixelFormat format = DepthToFormat(depth); | |
730 | if ( M_BITMAP->m_surface->GetPixelFormat() == format ) | |
731 | return; | |
732 | ||
733 | AllocExclusive(); | |
734 | ||
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; | |
746 | } | |
747 | ||
748 | wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const | |
749 | { | |
750 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
751 | ||
752 | return M_BITMAP->m_surface; | |
753 | } | |
754 | ||
755 | wxGDIRefData *wxBitmap::CreateGDIRefData() const | |
756 | { | |
757 | return new wxBitmapRefData; | |
758 | } | |
759 | ||
760 | wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const | |
761 | { | |
762 | return new wxBitmapRefData(*(wxBitmapRefData *)data); | |
763 | } | |
764 | ||
765 | ||
766 | /*static*/ | |
767 | void wxBitmap::InitStandardHandlers() | |
768 | { | |
769 | // not wxBitmap handlers, we rely on wxImage | |
770 | } |