| 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 | bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface) |
| 398 | { |
| 399 | UnRef(); |
| 400 | |
| 401 | wxCHECK_MSG( surface, false, "invalid surface" ); |
| 402 | |
| 403 | m_refData = new wxBitmapRefData(); |
| 404 | M_BITMAP->m_surface = surface; |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | bool wxBitmap::Create(int width, int height, int depth) |
| 409 | { |
| 410 | return CreateWithFormat(width, height, DepthToFormat(depth)); |
| 411 | } |
| 412 | |
| 413 | bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat) |
| 414 | { |
| 415 | UnRef(); |
| 416 | |
| 417 | wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") ); |
| 418 | |
| 419 | return Create(CreateSurfaceWithFormat(width, height, |
| 420 | DFBSurfacePixelFormat(dfbFormat))); |
| 421 | } |
| 422 | |
| 423 | #if wxUSE_IMAGE |
| 424 | wxBitmap::wxBitmap(const wxImage& imageOrig, int depth) |
| 425 | { |
| 426 | wxCHECK_RET( imageOrig.Ok(), wxT("invalid image") ); |
| 427 | |
| 428 | wxImage image(imageOrig); |
| 429 | |
| 430 | // convert mask to alpha channel, because wxMask isn't implemented yet |
| 431 | // FIXME: don't do this, implement proper wxMask support |
| 432 | if ( image.HasMask() ) |
| 433 | image.InitAlpha(); |
| 434 | |
| 435 | DFBSurfacePixelFormat format = DepthToFormat(depth); |
| 436 | if ( format == DSPF_UNKNOWN && image.HasAlpha() ) |
| 437 | format = DSPF_ARGB; |
| 438 | |
| 439 | // create surface in screen's format (unless we need alpha channel, |
| 440 | // in which case use ARGB): |
| 441 | if ( !CreateWithFormat(image.GetWidth(), image.GetHeight(), format) ) |
| 442 | return; |
| 443 | |
| 444 | // then copy the image to it: |
| 445 | wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface; |
| 446 | |
| 447 | switch ( dst->GetPixelFormat() ) |
| 448 | { |
| 449 | case DSPF_RGB24: |
| 450 | case DSPF_RGB32: |
| 451 | case DSPF_ARGB: |
| 452 | CopyImageToSurface(image, dst); |
| 453 | break; |
| 454 | |
| 455 | default: |
| 456 | { |
| 457 | // wxBitmap uses different pixel format, so we have to use a |
| 458 | // temporary surface and blit to the bitmap via it: |
| 459 | wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image)); |
| 460 | CopyImageToSurface(image, src); |
| 461 | |
| 462 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) |
| 463 | return; |
| 464 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) |
| 465 | return; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | wxImage wxBitmap::ConvertToImage() const |
| 471 | { |
| 472 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
| 473 | |
| 474 | wxImage img(GetWidth(), GetHeight()); |
| 475 | wxIDirectFBSurfacePtr src = M_BITMAP->m_surface; |
| 476 | |
| 477 | switch ( src->GetPixelFormat() ) |
| 478 | { |
| 479 | case DSPF_RGB24: |
| 480 | case DSPF_RGB32: |
| 481 | case DSPF_ARGB: |
| 482 | CopySurfaceToImage(src, img); |
| 483 | break; |
| 484 | default: |
| 485 | { |
| 486 | // wxBitmap uses different pixel format, so we have to use a |
| 487 | // temporary surface and blit to the bitmap via it: |
| 488 | wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img)); |
| 489 | |
| 490 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) |
| 491 | return wxNullImage; |
| 492 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) |
| 493 | return wxNullImage; |
| 494 | |
| 495 | CopySurfaceToImage(dst, img); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // FIXME: implement mask setting in the image |
| 500 | wxASSERT_MSG( GetMask() == NULL, "bitmap masks are ignored for now" ); |
| 501 | |
| 502 | return img; |
| 503 | } |
| 504 | #endif // wxUSE_IMAGE |
| 505 | |
| 506 | void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp) |
| 507 | { |
| 508 | wxCHECK_MSG( Ok(), NULL, "invalid bitmap" ); |
| 509 | |
| 510 | AllocExclusive(); |
| 511 | |
| 512 | DFBSurfacePixelFormat format; |
| 513 | if ( bpp == 32 ) |
| 514 | format = DSPF_ARGB; |
| 515 | else |
| 516 | format = DSPF_RGB24; |
| 517 | |
| 518 | // convert the bitmap into format compatible with requested raw access; |
| 519 | // note that we don't bother converting the bitmap back in UngetRawData(), |
| 520 | // as unpacked formats (RGB24, RGB32) are the common case and converting |
| 521 | // between them while blitting is fast enough (FIXME?) |
| 522 | if ( !ConvertSurfaceToFormat(M_BITMAP->m_surface, format) ) |
| 523 | return NULL; |
| 524 | |
| 525 | void *bits = NULL; |
| 526 | if ( !M_BITMAP->m_surface->Lock |
| 527 | ( |
| 528 | (DFBSurfaceLockFlags)(DSLF_READ | DSLF_WRITE), |
| 529 | &bits, |
| 530 | &data.m_stride |
| 531 | ) ) |
| 532 | return NULL; |
| 533 | |
| 534 | M_BITMAP->m_surface->GetSize(&data.m_width, &data.m_height); |
| 535 | |
| 536 | return bits; |
| 537 | } |
| 538 | |
| 539 | void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data)) |
| 540 | { |
| 541 | M_BITMAP->m_surface->Unlock(); |
| 542 | } |
| 543 | |
| 544 | bool wxBitmap::HasAlpha() const |
| 545 | { |
| 546 | wxCHECK_MSG( Ok(), false, "invalid bitmap" ); |
| 547 | |
| 548 | return M_BITMAP->m_surface->GetPixelFormat() == DSPF_ARGB; |
| 549 | } |
| 550 | |
| 551 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) |
| 552 | { |
| 553 | LoadFile(filename, type); |
| 554 | } |
| 555 | |
| 556 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) |
| 557 | { |
| 558 | wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") ); |
| 559 | |
| 560 | // create bitmap in the device-dependent format |
| 561 | if ( !CreateWithFormat(width, height, DSPF_UNKNOWN) ) |
| 562 | return; |
| 563 | |
| 564 | if ( !CopyBitsToSurface((const unsigned char *)bits, |
| 565 | width, height, M_BITMAP->m_surface) ) |
| 566 | UnRef(); |
| 567 | } |
| 568 | |
| 569 | int wxBitmap::GetHeight() const |
| 570 | { |
| 571 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 572 | |
| 573 | int h = -1; |
| 574 | M_BITMAP->m_surface->GetSize(NULL, &h); |
| 575 | return h; |
| 576 | } |
| 577 | |
| 578 | int wxBitmap::GetWidth() const |
| 579 | { |
| 580 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 581 | |
| 582 | int w = -1; |
| 583 | M_BITMAP->m_surface->GetSize(&w, NULL); |
| 584 | return w; |
| 585 | } |
| 586 | |
| 587 | int wxBitmap::GetDepth() const |
| 588 | { |
| 589 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 590 | |
| 591 | return M_BITMAP->m_surface->GetDepth(); |
| 592 | } |
| 593 | |
| 594 | wxMask *wxBitmap::GetMask() const |
| 595 | { |
| 596 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); |
| 597 | |
| 598 | return M_BITMAP->m_mask; |
| 599 | } |
| 600 | |
| 601 | void wxBitmap::SetMask(wxMask *mask) |
| 602 | { |
| 603 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
| 604 | |
| 605 | AllocExclusive(); |
| 606 | delete M_BITMAP->m_mask; |
| 607 | M_BITMAP->m_mask = mask; |
| 608 | } |
| 609 | |
| 610 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
| 611 | { |
| 612 | *this = *((wxBitmap*)(&icon)); |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const |
| 617 | { |
| 618 | wxCHECK_MSG( Ok() && |
| 619 | rect.x >= 0 && rect.y >= 0 && |
| 620 | rect.x+rect.width <= GetWidth() && |
| 621 | rect.y+rect.height <= GetHeight(), |
| 622 | wxNullBitmap, |
| 623 | wxT("invalid bitmap or bitmap region") ); |
| 624 | |
| 625 | // NB: DirectFB subsurfaces share the same pixels buffer, so we must |
| 626 | // clone the obtained subsurface |
| 627 | DFBRectangle r = { rect.x, rect.y, rect.width, rect.height }; |
| 628 | return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone()); |
| 629 | } |
| 630 | |
| 631 | #warning "to common code" |
| 632 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) |
| 633 | { |
| 634 | UnRef(); |
| 635 | |
| 636 | wxBitmapHandler *handler = FindHandler(type); |
| 637 | |
| 638 | if ( handler == NULL ) |
| 639 | { |
| 640 | wxImage image; |
| 641 | if ( !image.LoadFile(name, type) || !image.Ok() ) |
| 642 | { |
| 643 | wxLogError(_("No bitmap handler for type %d defined."), type); |
| 644 | return false; |
| 645 | } |
| 646 | else |
| 647 | { |
| 648 | *this = wxBitmap(image); |
| 649 | return true; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | m_refData = new wxBitmapRefData(); |
| 654 | |
| 655 | return handler->LoadFile(this, name, type, -1, -1); |
| 656 | } |
| 657 | |
| 658 | #warning "to common code" |
| 659 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const |
| 660 | { |
| 661 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); |
| 662 | |
| 663 | wxBitmapHandler *handler = FindHandler(type); |
| 664 | |
| 665 | if ( handler == NULL ) |
| 666 | { |
| 667 | wxImage image = ConvertToImage(); |
| 668 | #if wxUSE_PALETTE |
| 669 | if ( palette ) |
| 670 | image.SetPalette(*palette); |
| 671 | #endif // wxUSE_PALETTE |
| 672 | |
| 673 | if ( image.Ok() ) |
| 674 | return image.SaveFile(filename, type); |
| 675 | else |
| 676 | { |
| 677 | wxLogError(_("No bitmap handler for type %d defined."), type); |
| 678 | return false; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | return handler->SaveFile(this, filename, type, palette); |
| 683 | } |
| 684 | |
| 685 | #if wxUSE_PALETTE |
| 686 | wxPalette *wxBitmap::GetPalette() const |
| 687 | { |
| 688 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); |
| 689 | |
| 690 | return M_BITMAP->m_palette; |
| 691 | } |
| 692 | |
| 693 | void wxBitmap::SetPalette(const wxPalette& palette) |
| 694 | { |
| 695 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); |
| 696 | wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") ); |
| 697 | |
| 698 | AllocExclusive(); |
| 699 | delete M_BITMAP->m_palette; |
| 700 | M_BITMAP->m_palette = NULL; |
| 701 | |
| 702 | if ( !palette.Ok() ) return; |
| 703 | |
| 704 | M_BITMAP->m_palette = new wxPalette(palette); |
| 705 | } |
| 706 | #endif // wxUSE_PALETTE |
| 707 | |
| 708 | void wxBitmap::SetHeight(int height) |
| 709 | { |
| 710 | AllocExclusive(); |
| 711 | |
| 712 | wxFAIL_MSG( "SetHeight not implemented" ); |
| 713 | } |
| 714 | |
| 715 | void wxBitmap::SetWidth(int width) |
| 716 | { |
| 717 | AllocExclusive(); |
| 718 | |
| 719 | wxFAIL_MSG( "SetWidth not implemented" ); |
| 720 | } |
| 721 | |
| 722 | void wxBitmap::SetDepth(int depth) |
| 723 | { |
| 724 | DFBSurfacePixelFormat format = DepthToFormat(depth); |
| 725 | if ( M_BITMAP->m_surface->GetPixelFormat() == format ) |
| 726 | return; |
| 727 | |
| 728 | AllocExclusive(); |
| 729 | |
| 730 | int w, h; |
| 731 | M_BITMAP->m_surface->GetSize(&w, &h); |
| 732 | wxIDirectFBSurfacePtr s = CreateSurfaceWithFormat(w, h, format); |
| 733 | if ( !s ) |
| 734 | return; |
| 735 | if ( !s->SetBlittingFlags(DSBLIT_NOFX) ) |
| 736 | return; |
| 737 | if ( !s->Blit(M_BITMAP->m_surface->GetRaw(), NULL, 0, 0) ) |
| 738 | return; |
| 739 | |
| 740 | M_BITMAP->m_surface = s; |
| 741 | } |
| 742 | |
| 743 | wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const |
| 744 | { |
| 745 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); |
| 746 | |
| 747 | return M_BITMAP->m_surface; |
| 748 | } |
| 749 | |
| 750 | wxGDIRefData *wxBitmap::CreateGDIRefData() const |
| 751 | { |
| 752 | return new wxBitmapRefData; |
| 753 | } |
| 754 | |
| 755 | wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const |
| 756 | { |
| 757 | return new wxBitmapRefData(*(wxBitmapRefData *)data); |
| 758 | } |
| 759 | |
| 760 | |
| 761 | /*static*/ |
| 762 | void wxBitmap::InitStandardHandlers() |
| 763 | { |
| 764 | // not wxBitmap handlers, we rely on wxImage |
| 765 | } |