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