| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: bitmap.cpp |
| 3 | // Purpose: wxBitmap |
| 4 | // Author: AUTHOR |
| 5 | // Modified by: |
| 6 | // Created: ??/??/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) AUTHOR |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "bitmap.h" |
| 14 | #endif |
| 15 | |
| 16 | #include "wx/wx.h" |
| 17 | #include "wx/setup.h" |
| 18 | #include "wx/utils.h" |
| 19 | #include "wx/palette.h" |
| 20 | #include "wx/bitmap.h" |
| 21 | #include "wx/icon.h" |
| 22 | #include "wx/log.h" |
| 23 | #include "wx/image.h" |
| 24 | #include "wx/xpmdecod.h" |
| 25 | |
| 26 | extern "C" |
| 27 | { |
| 28 | #ifdef OBSOLETE_XPM_DATA_HANDLER |
| 29 | #include "xpm.h" |
| 30 | #endif |
| 31 | } ; |
| 32 | |
| 33 | #if !USE_SHARED_LIBRARIES |
| 34 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) |
| 35 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) |
| 36 | #endif |
| 37 | |
| 38 | #ifdef __UNIX__ |
| 39 | #include <ApplicationServices/ApplicationServices.h> |
| 40 | #else |
| 41 | #include <PictUtils.h> |
| 42 | #endif |
| 43 | |
| 44 | #include "wx/mac/uma.h" |
| 45 | |
| 46 | CTabHandle wxMacCreateColorTable( int numColors ) |
| 47 | { |
| 48 | CTabHandle newColors; /* Handle to the new color table */ |
| 49 | |
| 50 | /* Allocate memory for the color table */ |
| 51 | newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) + |
| 52 | sizeof (ColorSpec) * (numColors - 1) ); |
| 53 | if (newColors != nil) |
| 54 | { |
| 55 | /* Initialize the fields */ |
| 56 | (**newColors).ctSeed = GetCTSeed(); |
| 57 | (**newColors).ctFlags = 0; |
| 58 | (**newColors).ctSize = numColors - 1; |
| 59 | /* Initialize the table of colors */ |
| 60 | } |
| 61 | return newColors ; |
| 62 | } |
| 63 | |
| 64 | void wxMacDestroyColorTable( CTabHandle colors ) |
| 65 | { |
| 66 | DisposeHandle( (Handle) colors ) ; |
| 67 | } |
| 68 | |
| 69 | void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue ) |
| 70 | { |
| 71 | (**newColors).ctTable[index].value = index; |
| 72 | (**newColors).ctTable[index].rgb.red = 0 ;// someRedValue; |
| 73 | (**newColors).ctTable[index].rgb.green = 0 ; // someGreenValue; |
| 74 | (**newColors).ctTable[index].rgb.blue = 0 ; // someBlueValue; |
| 75 | } |
| 76 | |
| 77 | GWorldPtr wxMacCreateGWorld( int width , int height , int depth ) |
| 78 | { |
| 79 | OSErr err = noErr ; |
| 80 | GWorldPtr port ; |
| 81 | Rect rect = { 0 , 0 , height , width } ; |
| 82 | |
| 83 | if ( depth < 0 ) |
| 84 | { |
| 85 | depth = wxDisplayDepth() ; |
| 86 | } |
| 87 | |
| 88 | err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ; |
| 89 | if ( err == noErr ) |
| 90 | { |
| 91 | return port ; |
| 92 | } |
| 93 | return NULL ; |
| 94 | } |
| 95 | |
| 96 | void wxMacDestroyGWorld( GWorldPtr gw ) |
| 97 | { |
| 98 | if ( gw ) |
| 99 | DisposeGWorld( gw ) ; |
| 100 | } |
| 101 | |
| 102 | PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask) |
| 103 | { |
| 104 | CGrafPtr origPort ; |
| 105 | GDHandle origDev ; |
| 106 | |
| 107 | PicHandle pict; // this is the Picture we give back |
| 108 | |
| 109 | RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ; |
| 110 | RGBColor white = { 0xffff ,0xffff , 0xffff } ; |
| 111 | RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; |
| 112 | |
| 113 | unsigned char *maskimage = NULL ; |
| 114 | Rect portRect ; |
| 115 | GetPortBounds( wp , &portRect ) ; |
| 116 | int width = portRect.right - portRect.left ; |
| 117 | int height = portRect.bottom - portRect.top ; |
| 118 | |
| 119 | LockPixels( GetGWorldPixMap( wp ) ) ; |
| 120 | GetGWorld( &origPort , &origDev ) ; |
| 121 | |
| 122 | if ( mask ) |
| 123 | { |
| 124 | maskimage = (unsigned char*) malloc( width * height ) ; |
| 125 | SetGWorld( mask , NULL ) ; |
| 126 | LockPixels( GetGWorldPixMap( mask ) ) ; |
| 127 | for ( int y = 0 ; y < height ; y++ ) |
| 128 | { |
| 129 | for( int x = 0 ; x < width ; x++ ) |
| 130 | { |
| 131 | RGBColor col ; |
| 132 | |
| 133 | GetCPixel( x + portRect.left , y + portRect.top , &col ) ; |
| 134 | maskimage[y*width + x] = ( col.red == 0 ) ; // for monochrome masks |
| 135 | } |
| 136 | } |
| 137 | UnlockPixels( GetGWorldPixMap( mask ) ) ; |
| 138 | } |
| 139 | |
| 140 | SetGWorld( wp , NULL ) ; |
| 141 | |
| 142 | pict = OpenPicture(&portRect); // open a picture, this disables drawing |
| 143 | if(!pict) |
| 144 | return NULL; |
| 145 | |
| 146 | if ( maskimage ) |
| 147 | { |
| 148 | RGBForeColor( &black ) ; |
| 149 | RGBBackColor( &white ) ; |
| 150 | PenMode(transparent); |
| 151 | |
| 152 | for ( int y = 0 ; y < height ; ++y ) |
| 153 | { |
| 154 | for( int x = 0 ; x < width ; ++x ) |
| 155 | { |
| 156 | if ( maskimage[y*width + x] ) |
| 157 | { |
| 158 | RGBColor col ; |
| 159 | |
| 160 | GetCPixel( x + portRect.left , y + portRect.top , &col ) ; |
| 161 | SetCPixel( x + portRect.left , y + portRect.top , &col ) ; |
| 162 | } |
| 163 | else { |
| 164 | // With transparency set this sets a blank pixel not a white one |
| 165 | SetCPixel( x + portRect.left , y + portRect.top , &white); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | free( maskimage ) ; |
| 170 | maskimage = NULL ; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | RGBBackColor( &gray ) ; |
| 175 | EraseRect(&portRect); |
| 176 | RGBForeColor( &black ) ; |
| 177 | RGBBackColor( &white ) ; |
| 178 | |
| 179 | CopyBits(GetPortBitMapForCopyBits(wp), /* src PixMap - we copy image over |
| 180 | * itself - */ |
| 181 | GetPortBitMapForCopyBits(wp), // dst PixMap - no drawing occurs |
| 182 | &portRect, // srcRect - it will be recorded and compressed - |
| 183 | &portRect, // dstRect - into the picture that is open - |
| 184 | srcCopy,NULL); // copyMode and no clip region |
| 185 | } |
| 186 | ClosePicture(); // We are done recording the picture |
| 187 | UnlockPixels( GetGWorldPixMap( wp ) ) ; |
| 188 | SetGWorld( origPort , origDev ) ; |
| 189 | |
| 190 | return pict; // return our groovy pict handle |
| 191 | } |
| 192 | |
| 193 | wxBitmapRefData::wxBitmapRefData() |
| 194 | { |
| 195 | m_ok = FALSE; |
| 196 | m_width = 0; |
| 197 | m_height = 0; |
| 198 | m_depth = 0; |
| 199 | m_quality = 0; |
| 200 | m_numColors = 0; |
| 201 | m_bitmapMask = NULL; |
| 202 | m_hBitmap = NULL ; |
| 203 | m_hPict = NULL ; |
| 204 | m_bitmapType = kMacBitmapTypeUnknownType ; |
| 205 | } |
| 206 | |
| 207 | wxBitmapRefData::~wxBitmapRefData() |
| 208 | { |
| 209 | switch (m_bitmapType) |
| 210 | { |
| 211 | case kMacBitmapTypePict : |
| 212 | { |
| 213 | if ( m_hPict ) |
| 214 | { |
| 215 | KillPicture( m_hPict ) ; |
| 216 | m_hPict = NULL ; |
| 217 | } |
| 218 | } |
| 219 | break ; |
| 220 | case kMacBitmapTypeGrafWorld : |
| 221 | { |
| 222 | if ( m_hBitmap ) |
| 223 | { |
| 224 | wxMacDestroyGWorld( m_hBitmap ) ; |
| 225 | m_hBitmap = NULL ; |
| 226 | } |
| 227 | } |
| 228 | break ; |
| 229 | default : |
| 230 | // unkown type ? |
| 231 | break ; |
| 232 | } |
| 233 | |
| 234 | if (m_bitmapMask) |
| 235 | { |
| 236 | delete m_bitmapMask; |
| 237 | m_bitmapMask = NULL; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | wxList wxBitmap::sm_handlers; |
| 242 | |
| 243 | wxBitmap::wxBitmap() |
| 244 | { |
| 245 | m_refData = NULL; |
| 246 | |
| 247 | if ( wxTheBitmapList ) |
| 248 | wxTheBitmapList->AddBitmap(this); |
| 249 | } |
| 250 | |
| 251 | wxBitmap::~wxBitmap() |
| 252 | { |
| 253 | if (wxTheBitmapList) |
| 254 | wxTheBitmapList->DeleteObject(this); |
| 255 | } |
| 256 | |
| 257 | wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) |
| 258 | { |
| 259 | m_refData = new wxBitmapRefData; |
| 260 | |
| 261 | M_BITMAPDATA->m_width = the_width ; |
| 262 | M_BITMAPDATA->m_height = the_height ; |
| 263 | M_BITMAPDATA->m_depth = no_bits ; |
| 264 | M_BITMAPDATA->m_numColors = 0; |
| 265 | if ( no_bits == 1 ) |
| 266 | { |
| 267 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
| 268 | M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( the_width , the_height , no_bits ) ; |
| 269 | M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ; |
| 270 | |
| 271 | CGrafPtr origPort ; |
| 272 | GDHandle origDevice ; |
| 273 | |
| 274 | GetGWorld( &origPort , &origDevice ) ; |
| 275 | SetGWorld( M_BITMAPDATA->m_hBitmap , NULL ) ; |
| 276 | LockPixels( GetGWorldPixMap( M_BITMAPDATA->m_hBitmap ) ) ; |
| 277 | |
| 278 | // bits is a char array |
| 279 | |
| 280 | unsigned char* linestart = (unsigned char*) bits ; |
| 281 | int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; |
| 282 | if ( the_width % (sizeof(unsigned char) * 8) ) { |
| 283 | linesize += sizeof(unsigned char); |
| 284 | } |
| 285 | |
| 286 | RGBColor colors[2] = { |
| 287 | { 0xFFFF , 0xFFFF , 0xFFFF } , |
| 288 | { 0, 0 , 0 } |
| 289 | } ; |
| 290 | |
| 291 | for ( int y = 0 ; y < the_height ; ++y , linestart += linesize ) |
| 292 | { |
| 293 | for ( int x = 0 ; x < the_width ; ++x ) |
| 294 | { |
| 295 | int index = x / 8 ; |
| 296 | int bit = x % 8 ; |
| 297 | int mask = 1 << bit ; |
| 298 | if ( linestart[index] & mask ) |
| 299 | { |
| 300 | SetCPixel( x , y , &colors[1] ) ; |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | SetCPixel( x , y , &colors[0] ) ; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | UnlockPixels( GetGWorldPixMap( M_BITMAPDATA->m_hBitmap ) ) ; |
| 309 | |
| 310 | SetGWorld( origPort , origDevice ) ; |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); |
| 315 | } |
| 316 | |
| 317 | if ( wxTheBitmapList ) { |
| 318 | wxTheBitmapList->AddBitmap(this); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | wxBitmap::wxBitmap(int w, int h, int d) |
| 323 | { |
| 324 | (void)Create(w, h, d); |
| 325 | |
| 326 | if ( wxTheBitmapList ) |
| 327 | wxTheBitmapList->AddBitmap(this); |
| 328 | } |
| 329 | |
| 330 | wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) |
| 331 | { |
| 332 | (void) Create(data, type, width, height, depth); |
| 333 | |
| 334 | if ( wxTheBitmapList ) |
| 335 | wxTheBitmapList->AddBitmap(this); |
| 336 | } |
| 337 | |
| 338 | wxBitmap::wxBitmap(const wxString& filename, long type) |
| 339 | { |
| 340 | LoadFile(filename, (int)type); |
| 341 | |
| 342 | if ( wxTheBitmapList ) |
| 343 | wxTheBitmapList->AddBitmap(this); |
| 344 | } |
| 345 | |
| 346 | bool wxBitmap::CreateFromXpm(const char **bits) |
| 347 | { |
| 348 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) |
| 349 | wxXPMDecoder decoder; |
| 350 | wxImage img = decoder.ReadData(bits); |
| 351 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) |
| 352 | *this = wxBitmap(img); |
| 353 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); |
| 354 | return TRUE; |
| 355 | } |
| 356 | |
| 357 | wxBitmap::wxBitmap(const char **bits) |
| 358 | { |
| 359 | #ifdef OBSOLETE_XPM_DATA_HANDLER |
| 360 | (void) Create((void *)bits, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); |
| 361 | #else |
| 362 | (void) CreateFromXpm(bits); |
| 363 | #endif |
| 364 | } |
| 365 | |
| 366 | wxBitmap::wxBitmap(char **bits) |
| 367 | { |
| 368 | #ifdef OBSOLETE_XPM_DATA_HANDLER |
| 369 | (void) Create((void *)bits, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); |
| 370 | #else |
| 371 | (void) CreateFromXpm((const char **)bits); |
| 372 | #endif |
| 373 | } |
| 374 | |
| 375 | wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const |
| 376 | { |
| 377 | wxCHECK_MSG( Ok() && |
| 378 | (rect.x >= 0) && (rect.y >= 0) && |
| 379 | (rect.x+rect.width <= GetWidth()) && |
| 380 | (rect.y+rect.height <= GetHeight()), |
| 381 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); |
| 382 | |
| 383 | |
| 384 | wxBitmap ret( rect.width, rect.height, GetDepth() ); |
| 385 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); |
| 386 | |
| 387 | WXHBITMAP origPort; |
| 388 | GDHandle origDevice; |
| 389 | |
| 390 | GetGWorld( &origPort, &origDevice ); |
| 391 | |
| 392 | // Update the subbitmaps reference data |
| 393 | wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData(); |
| 394 | |
| 395 | ref->m_numColors = M_BITMAPDATA->m_numColors; |
| 396 | ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette; |
| 397 | ref->m_bitmapType = M_BITMAPDATA->m_bitmapType; |
| 398 | |
| 399 | // Copy sub region of this bitmap |
| 400 | if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) |
| 401 | { |
| 402 | printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n"); |
| 403 | } |
| 404 | else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld) |
| 405 | { |
| 406 | // Copy mask |
| 407 | if(GetMask()) |
| 408 | { |
| 409 | WXHBITMAP submask, mask; |
| 410 | RGBColor color; |
| 411 | |
| 412 | mask = GetMask()->GetMaskBitmap(); |
| 413 | submask = wxMacCreateGWorld(rect.width, rect.height, 1); |
| 414 | LockPixels(GetGWorldPixMap(mask)); |
| 415 | LockPixels(GetGWorldPixMap(submask)); |
| 416 | |
| 417 | for(int yy = 0; yy < rect.height; yy++) |
| 418 | { |
| 419 | for(int xx = 0; xx < rect.width; xx++) |
| 420 | { |
| 421 | SetGWorld(mask, NULL); |
| 422 | GetCPixel(rect.x + xx, rect.y + yy, &color); |
| 423 | SetGWorld(submask, NULL); |
| 424 | SetCPixel(xx,yy, &color); |
| 425 | } |
| 426 | } |
| 427 | UnlockPixels(GetGWorldPixMap(mask)); |
| 428 | UnlockPixels(GetGWorldPixMap(submask)); |
| 429 | ref->m_bitmapMask = new wxMask; |
| 430 | ref->m_bitmapMask->SetMaskBitmap(submask); |
| 431 | } |
| 432 | |
| 433 | // Copy bitmap |
| 434 | if(GetHBITMAP()) |
| 435 | { |
| 436 | WXHBITMAP subbitmap, bitmap; |
| 437 | RGBColor color; |
| 438 | |
| 439 | bitmap = GetHBITMAP(); |
| 440 | subbitmap = wxMacCreateGWorld(rect.width, rect.height, GetDepth()); |
| 441 | LockPixels(GetGWorldPixMap(bitmap)); |
| 442 | LockPixels(GetGWorldPixMap(subbitmap)); |
| 443 | |
| 444 | for(int yy = 0; yy < rect.height; yy++) |
| 445 | { |
| 446 | for(int xx = 0; xx < rect.width; xx++) |
| 447 | { |
| 448 | SetGWorld(bitmap, NULL); |
| 449 | GetCPixel(rect.x + xx, rect.y + yy, &color); |
| 450 | SetGWorld(subbitmap, NULL); |
| 451 | SetCPixel(xx, yy, &color); |
| 452 | } |
| 453 | } |
| 454 | UnlockPixels(GetGWorldPixMap(bitmap)); |
| 455 | UnlockPixels(GetGWorldPixMap(subbitmap)); |
| 456 | ret.SetHBITMAP(subbitmap); |
| 457 | } |
| 458 | } |
| 459 | SetGWorld( origPort, origDevice ); |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | bool wxBitmap::Create(int w, int h, int d) |
| 465 | { |
| 466 | UnRef(); |
| 467 | |
| 468 | m_refData = new wxBitmapRefData; |
| 469 | |
| 470 | M_BITMAPDATA->m_width = w; |
| 471 | M_BITMAPDATA->m_height = h; |
| 472 | M_BITMAPDATA->m_depth = d; |
| 473 | |
| 474 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
| 475 | M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ; |
| 476 | M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ; |
| 477 | return M_BITMAPDATA->m_ok; |
| 478 | } |
| 479 | |
| 480 | int wxBitmap::GetBitmapType() const |
| 481 | { |
| 482 | wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") ); |
| 483 | |
| 484 | return M_BITMAPDATA->m_bitmapType; |
| 485 | } |
| 486 | |
| 487 | void wxBitmap::SetHBITMAP(WXHBITMAP bmp) |
| 488 | { |
| 489 | M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
| 490 | M_BITMAPDATA->m_hBitmap = bmp ; |
| 491 | M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ; |
| 492 | } |
| 493 | |
| 494 | bool wxBitmap::LoadFile(const wxString& filename, long type) |
| 495 | { |
| 496 | UnRef(); |
| 497 | |
| 498 | m_refData = new wxBitmapRefData; |
| 499 | |
| 500 | wxBitmapHandler *handler = FindHandler(type); |
| 501 | |
| 502 | if ( handler == NULL ) { |
| 503 | wxLogWarning("no bitmap handler for type %d defined.", type); |
| 504 | |
| 505 | return FALSE; |
| 506 | } |
| 507 | |
| 508 | return handler->LoadFile(this, filename, type, -1, -1); |
| 509 | } |
| 510 | |
| 511 | bool wxBitmap::Create(void *data, long type, int width, int height, int depth) |
| 512 | { |
| 513 | UnRef(); |
| 514 | |
| 515 | m_refData = new wxBitmapRefData; |
| 516 | |
| 517 | wxBitmapHandler *handler = FindHandler(type); |
| 518 | |
| 519 | if ( handler == NULL ) { |
| 520 | wxLogWarning("no bitmap handler for type %d defined.", type); |
| 521 | |
| 522 | return FALSE; |
| 523 | } |
| 524 | |
| 525 | return handler->Create(this, data, type, width, height, depth); |
| 526 | } |
| 527 | |
| 528 | wxBitmap::wxBitmap(const wxImage& image, int depth) |
| 529 | { |
| 530 | wxCHECK_RET( image.Ok(), wxT("invalid image") ) |
| 531 | wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") ) |
| 532 | |
| 533 | m_refData = new wxBitmapRefData(); |
| 534 | |
| 535 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); |
| 536 | |
| 537 | // width and height of the device-dependent bitmap |
| 538 | int width = image.GetWidth(); |
| 539 | int height = image.GetHeight(); |
| 540 | |
| 541 | // Create picture |
| 542 | |
| 543 | Create( width , height , wxDisplayDepth() ) ; |
| 544 | wxBitmap maskBitmap( width, height, 1); |
| 545 | |
| 546 | CGrafPtr origPort ; |
| 547 | GDHandle origDevice ; |
| 548 | |
| 549 | LockPixels( GetGWorldPixMap(GetHBITMAP()) ); |
| 550 | LockPixels( GetGWorldPixMap(maskBitmap.GetHBITMAP()) ); |
| 551 | |
| 552 | GetGWorld( &origPort , &origDevice ) ; |
| 553 | SetGWorld( GetHBITMAP() , NULL ) ; |
| 554 | |
| 555 | // Render image |
| 556 | wxColour rgb, maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue()); |
| 557 | RGBColor color; |
| 558 | RGBColor white = { 0xffff, 0xffff, 0xffff }; |
| 559 | RGBColor black = { 0 , 0 , 0 }; |
| 560 | |
| 561 | register unsigned char* data = image.GetData(); |
| 562 | |
| 563 | int index = 0; |
| 564 | for (int y = 0; y < height; y++) |
| 565 | { |
| 566 | for (int x = 0; x < width; x++) |
| 567 | { |
| 568 | rgb.Set(data[index++], data[index++], data[index++]); |
| 569 | color = rgb.GetPixel(); |
| 570 | SetCPixel( x , y , &color ) ; |
| 571 | if (image.HasMask()) |
| 572 | { |
| 573 | SetGWorld(maskBitmap.GetHBITMAP(), NULL); |
| 574 | if (rgb == maskcolor) { |
| 575 | SetCPixel(x,y, &white); |
| 576 | } |
| 577 | else { |
| 578 | SetCPixel(x,y, &black); |
| 579 | } |
| 580 | SetGWorld(GetHBITMAP(), NULL); |
| 581 | } |
| 582 | } |
| 583 | } // for height |
| 584 | |
| 585 | // Create mask |
| 586 | if ( image.HasMask() ) { |
| 587 | // SetMask(new wxMask( maskBitmap )); |
| 588 | } |
| 589 | |
| 590 | UnlockPixels( GetGWorldPixMap(GetHBITMAP()) ); |
| 591 | UnlockPixels( GetGWorldPixMap(maskBitmap.GetHBITMAP()) ); |
| 592 | SetGWorld( origPort, origDevice ); |
| 593 | } |
| 594 | |
| 595 | wxImage wxBitmap::ConvertToImage() const |
| 596 | { |
| 597 | wxImage image; |
| 598 | |
| 599 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
| 600 | |
| 601 | // create an wxImage object |
| 602 | int width = GetWidth(); |
| 603 | int height = GetHeight(); |
| 604 | image.Create( width, height ); |
| 605 | |
| 606 | unsigned char *data = image.GetData(); |
| 607 | |
| 608 | wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") ); |
| 609 | |
| 610 | WXHBITMAP origPort; |
| 611 | GDHandle origDevice; |
| 612 | int index; |
| 613 | RGBColor color; |
| 614 | // background color set to RGB(16,16,16) in consistent with wxGTK |
| 615 | unsigned char mask_r=16, mask_g=16, mask_b=16; |
| 616 | SInt16 r,g,b; |
| 617 | wxMask *mask = GetMask(); |
| 618 | |
| 619 | GetGWorld( &origPort, &origDevice ); |
| 620 | LockPixels(GetGWorldPixMap(GetHBITMAP())); |
| 621 | SetGWorld( GetHBITMAP(), NULL); |
| 622 | |
| 623 | // Copy data into image |
| 624 | index = 0; |
| 625 | for (int yy = 0; yy < height; yy++) |
| 626 | { |
| 627 | for (int xx = 0; xx < width; xx++) |
| 628 | { |
| 629 | GetCPixel(xx,yy, &color); |
| 630 | r = ((color.red ) >> 8); |
| 631 | g = ((color.green ) >> 8); |
| 632 | b = ((color.blue ) >> 8); |
| 633 | data[index ] = r; |
| 634 | data[index + 1] = g; |
| 635 | data[index + 2] = b; |
| 636 | if (mask) |
| 637 | { |
| 638 | if (mask->PointMasked(xx,yy)) |
| 639 | { |
| 640 | data[index ] = mask_r; |
| 641 | data[index + 1] = mask_g; |
| 642 | data[index + 2] = mask_b; |
| 643 | } |
| 644 | } |
| 645 | index += 3; |
| 646 | } |
| 647 | } |
| 648 | if (mask) |
| 649 | { |
| 650 | image.SetMaskColour( mask_r, mask_g, mask_b ); |
| 651 | image.SetMask( true ); |
| 652 | } |
| 653 | |
| 654 | // Free resources |
| 655 | UnlockPixels(GetGWorldPixMap(GetHBITMAP())); |
| 656 | SetGWorld(origPort, origDevice); |
| 657 | |
| 658 | return image; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette) |
| 663 | { |
| 664 | wxBitmapHandler *handler = FindHandler(type); |
| 665 | |
| 666 | if ( handler == NULL ) { |
| 667 | wxLogWarning("no bitmap handler for type %d defined.", type); |
| 668 | |
| 669 | return FALSE; |
| 670 | } |
| 671 | |
| 672 | return handler->SaveFile(this, filename, type, palette); |
| 673 | } |
| 674 | |
| 675 | bool wxBitmap::Ok() const |
| 676 | { |
| 677 | return (M_BITMAPDATA && M_BITMAPDATA->m_ok); |
| 678 | } |
| 679 | |
| 680 | int wxBitmap::GetHeight() const |
| 681 | { |
| 682 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 683 | |
| 684 | return M_BITMAPDATA->m_height; |
| 685 | } |
| 686 | |
| 687 | int wxBitmap::GetWidth() const |
| 688 | { |
| 689 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 690 | |
| 691 | return M_BITMAPDATA->m_width; |
| 692 | } |
| 693 | |
| 694 | int wxBitmap::GetDepth() const |
| 695 | { |
| 696 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 697 | |
| 698 | return M_BITMAPDATA->m_depth; |
| 699 | } |
| 700 | |
| 701 | int wxBitmap::GetQuality() const |
| 702 | { |
| 703 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); |
| 704 | |
| 705 | return M_BITMAPDATA->m_quality; |
| 706 | } |
| 707 | |
| 708 | wxMask *wxBitmap::GetMask() const |
| 709 | { |
| 710 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); |
| 711 | |
| 712 | return M_BITMAPDATA->m_bitmapMask; |
| 713 | } |
| 714 | |
| 715 | void wxBitmap::SetWidth(int w) |
| 716 | { |
| 717 | if (!M_BITMAPDATA) |
| 718 | m_refData = new wxBitmapRefData; |
| 719 | |
| 720 | M_BITMAPDATA->m_width = w; |
| 721 | } |
| 722 | |
| 723 | void wxBitmap::SetHeight(int h) |
| 724 | { |
| 725 | if (!M_BITMAPDATA) |
| 726 | m_refData = new wxBitmapRefData; |
| 727 | |
| 728 | M_BITMAPDATA->m_height = h; |
| 729 | } |
| 730 | |
| 731 | void wxBitmap::SetDepth(int d) |
| 732 | { |
| 733 | if (!M_BITMAPDATA) |
| 734 | m_refData = new wxBitmapRefData; |
| 735 | |
| 736 | M_BITMAPDATA->m_depth = d; |
| 737 | } |
| 738 | |
| 739 | void wxBitmap::SetQuality(int q) |
| 740 | { |
| 741 | if (!M_BITMAPDATA) |
| 742 | m_refData = new wxBitmapRefData; |
| 743 | |
| 744 | M_BITMAPDATA->m_quality = q; |
| 745 | } |
| 746 | |
| 747 | void wxBitmap::SetOk(bool isOk) |
| 748 | { |
| 749 | if (!M_BITMAPDATA) |
| 750 | m_refData = new wxBitmapRefData; |
| 751 | |
| 752 | M_BITMAPDATA->m_ok = isOk; |
| 753 | } |
| 754 | |
| 755 | wxPalette *wxBitmap::GetPalette() const |
| 756 | { |
| 757 | wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") ); |
| 758 | |
| 759 | return &M_BITMAPDATA->m_bitmapPalette; |
| 760 | } |
| 761 | |
| 762 | void wxBitmap::SetPalette(const wxPalette& palette) |
| 763 | { |
| 764 | if (!M_BITMAPDATA) |
| 765 | m_refData = new wxBitmapRefData; |
| 766 | |
| 767 | M_BITMAPDATA->m_bitmapPalette = palette ; |
| 768 | } |
| 769 | |
| 770 | void wxBitmap::SetMask(wxMask *mask) |
| 771 | { |
| 772 | if (!M_BITMAPDATA) |
| 773 | m_refData = new wxBitmapRefData; |
| 774 | |
| 775 | M_BITMAPDATA->m_bitmapMask = mask ; |
| 776 | } |
| 777 | |
| 778 | WXHBITMAP wxBitmap::GetHBITMAP() const |
| 779 | { |
| 780 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); |
| 781 | |
| 782 | return M_BITMAPDATA->m_hBitmap; |
| 783 | } |
| 784 | |
| 785 | PicHandle wxBitmap::GetPict() const |
| 786 | { |
| 787 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); |
| 788 | |
| 789 | PicHandle picture; // This is the returned picture |
| 790 | |
| 791 | // If bitmap already in Pict format return pointer |
| 792 | if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { |
| 793 | return M_BITMAPDATA->m_hPict; |
| 794 | } |
| 795 | else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { |
| 796 | // Invalid bitmap |
| 797 | return NULL; |
| 798 | } |
| 799 | |
| 800 | RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ; |
| 801 | RGBColor white = { 0xffff ,0xffff , 0xffff } ; |
| 802 | RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; |
| 803 | CGrafPtr origPort; |
| 804 | GDHandle origDev ; |
| 805 | wxMask *mask; |
| 806 | Rect portRect ; |
| 807 | |
| 808 | GetPortBounds( GetHBITMAP() , &portRect ) ; |
| 809 | int width = portRect.right - portRect.left ; |
| 810 | int height = portRect.bottom - portRect.top ; |
| 811 | |
| 812 | LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ; |
| 813 | GetGWorld( &origPort , &origDev ) ; |
| 814 | |
| 815 | mask = GetMask(); |
| 816 | |
| 817 | SetGWorld( GetHBITMAP() , NULL ) ; |
| 818 | |
| 819 | picture = OpenPicture(&portRect); // open a picture, this disables drawing |
| 820 | if(!picture) { |
| 821 | return NULL; |
| 822 | } |
| 823 | |
| 824 | if( mask ) |
| 825 | { |
| 826 | #ifdef __UNIX__ |
| 827 | RGBColor trans = white; |
| 828 | #else |
| 829 | RGBBackColor( &gray ); |
| 830 | EraseRect( &portRect ); |
| 831 | RGBColor trans = gray; |
| 832 | #endif |
| 833 | RGBForeColor( &black ) ; |
| 834 | RGBBackColor( &white ) ; |
| 835 | PenMode(transparent); |
| 836 | |
| 837 | for ( int y = 0 ; y < height ; ++y ) |
| 838 | { |
| 839 | for( int x = 0 ; x < width ; ++x ) |
| 840 | { |
| 841 | if ( !mask->PointMasked(x,y) ) |
| 842 | { |
| 843 | RGBColor col ; |
| 844 | |
| 845 | GetCPixel( x + portRect.left , y + portRect.top , &col ) ; |
| 846 | SetCPixel( x + portRect.left , y + portRect.top , &col ) ; |
| 847 | } |
| 848 | else { |
| 849 | // With transparency this sets a blank pixel |
| 850 | SetCPixel( x + portRect.left , y + portRect.top , &trans); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | RGBBackColor( &gray ) ; |
| 858 | EraseRect(&portRect); |
| 859 | RGBForeColor( &black ) ; |
| 860 | RGBBackColor( &white ) ; |
| 861 | |
| 862 | CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()), |
| 863 | // src PixMap - we copy image over itself - |
| 864 | GetPortBitMapForCopyBits(GetHBITMAP()), |
| 865 | // dst PixMap - no drawing occurs |
| 866 | &portRect, // srcRect - it will be recorded and compressed - |
| 867 | &portRect, // dstRect - into the picture that is open - |
| 868 | srcCopy,NULL); // copyMode and no clip region |
| 869 | } |
| 870 | ClosePicture(); // We are done recording the picture |
| 871 | UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ; |
| 872 | SetGWorld( origPort , origDev ) ; |
| 873 | |
| 874 | return picture; // return our groovy pict handle |
| 875 | } |
| 876 | |
| 877 | void wxBitmap::AddHandler(wxBitmapHandler *handler) |
| 878 | { |
| 879 | sm_handlers.Append(handler); |
| 880 | } |
| 881 | |
| 882 | void wxBitmap::InsertHandler(wxBitmapHandler *handler) |
| 883 | { |
| 884 | sm_handlers.Insert(handler); |
| 885 | } |
| 886 | |
| 887 | bool wxBitmap::RemoveHandler(const wxString& name) |
| 888 | { |
| 889 | wxBitmapHandler *handler = FindHandler(name); |
| 890 | if ( handler ) |
| 891 | { |
| 892 | sm_handlers.DeleteObject(handler); |
| 893 | return TRUE; |
| 894 | } |
| 895 | else |
| 896 | return FALSE; |
| 897 | } |
| 898 | |
| 899 | wxBitmapHandler *wxBitmap::FindHandler(const wxString& name) |
| 900 | { |
| 901 | wxNode *node = sm_handlers.First(); |
| 902 | while ( node ) |
| 903 | { |
| 904 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); |
| 905 | if ( handler->GetName() == name ) |
| 906 | return handler; |
| 907 | node = node->Next(); |
| 908 | } |
| 909 | return NULL; |
| 910 | } |
| 911 | |
| 912 | wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType) |
| 913 | { |
| 914 | wxNode *node = sm_handlers.First(); |
| 915 | while ( node ) |
| 916 | { |
| 917 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); |
| 918 | if ( handler->GetExtension() == extension && |
| 919 | (bitmapType == -1 || handler->GetType() == bitmapType) ) |
| 920 | return handler; |
| 921 | node = node->Next(); |
| 922 | } |
| 923 | return NULL; |
| 924 | } |
| 925 | |
| 926 | wxBitmapHandler *wxBitmap::FindHandler(long bitmapType) |
| 927 | { |
| 928 | wxNode *node = sm_handlers.First(); |
| 929 | while ( node ) |
| 930 | { |
| 931 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); |
| 932 | if (handler->GetType() == bitmapType) |
| 933 | return handler; |
| 934 | node = node->Next(); |
| 935 | } |
| 936 | return NULL; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * wxMask |
| 941 | */ |
| 942 | |
| 943 | wxMask::wxMask() |
| 944 | { |
| 945 | m_maskBitmap = 0; |
| 946 | } |
| 947 | |
| 948 | // Construct a mask from a bitmap and a colour indicating |
| 949 | // the transparent area |
| 950 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) |
| 951 | { |
| 952 | m_maskBitmap = 0; |
| 953 | Create(bitmap, colour); |
| 954 | } |
| 955 | |
| 956 | // Construct a mask from a bitmap and a palette index indicating |
| 957 | // the transparent area |
| 958 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) |
| 959 | { |
| 960 | m_maskBitmap = 0; |
| 961 | Create(bitmap, paletteIndex); |
| 962 | } |
| 963 | |
| 964 | // Construct a mask from a mono bitmap (copies the bitmap). |
| 965 | wxMask::wxMask(const wxBitmap& bitmap) |
| 966 | { |
| 967 | m_maskBitmap = 0; |
| 968 | Create(bitmap); |
| 969 | } |
| 970 | |
| 971 | wxMask::~wxMask() |
| 972 | { |
| 973 | if ( m_maskBitmap ) |
| 974 | { |
| 975 | wxMacDestroyGWorld( m_maskBitmap ) ; |
| 976 | m_maskBitmap = NULL ; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | // Create a mask from a mono bitmap (copies the bitmap). |
| 981 | bool wxMask::Create(const wxBitmap& bitmap) |
| 982 | { |
| 983 | if ( m_maskBitmap ) |
| 984 | { |
| 985 | wxMacDestroyGWorld( m_maskBitmap ) ; |
| 986 | m_maskBitmap = NULL ; |
| 987 | } |
| 988 | wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false, |
| 989 | wxT("Cannot create mask from this bitmap type (TODO)")); |
| 990 | // other types would require a temporary bitmap. not yet implemented |
| 991 | |
| 992 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap")); |
| 993 | |
| 994 | wxCHECK_MSG(bitmap.GetDepth() == 1, false, |
| 995 | wxT("Cannot create mask from colour bitmap")); |
| 996 | |
| 997 | m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), 1); |
| 998 | Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() }; |
| 999 | |
| 1000 | LockPixels( GetGWorldPixMap(m_maskBitmap) ); |
| 1001 | LockPixels( GetGWorldPixMap(bitmap.GetHBITMAP()) ); |
| 1002 | CopyBits(GetPortBitMapForCopyBits(bitmap.GetHBITMAP()), |
| 1003 | GetPortBitMapForCopyBits(m_maskBitmap), |
| 1004 | &rect, &rect, srcCopy, 0); |
| 1005 | UnlockPixels( GetGWorldPixMap(m_maskBitmap) ); |
| 1006 | UnlockPixels( GetGWorldPixMap(bitmap.GetHBITMAP()) ); |
| 1007 | |
| 1008 | return FALSE; |
| 1009 | } |
| 1010 | |
| 1011 | // Create a mask from a bitmap and a palette index indicating |
| 1012 | // the transparent area |
| 1013 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) |
| 1014 | { |
| 1015 | // TODO |
| 1016 | wxCHECK_MSG( 0, false, wxT("Not implemented")); |
| 1017 | return FALSE; |
| 1018 | } |
| 1019 | |
| 1020 | // Create a mask from a bitmap and a colour indicating |
| 1021 | // the transparent area |
| 1022 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) |
| 1023 | { |
| 1024 | if ( m_maskBitmap ) |
| 1025 | { |
| 1026 | wxMacDestroyGWorld( m_maskBitmap ) ; |
| 1027 | m_maskBitmap = NULL ; |
| 1028 | } |
| 1029 | wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false, |
| 1030 | wxT("Cannot create mask from this bitmap type (TODO)")); |
| 1031 | // other types would require a temporary bitmap. not yet implemented |
| 1032 | |
| 1033 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap")); |
| 1034 | |
| 1035 | m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 ); |
| 1036 | LockPixels( GetGWorldPixMap( m_maskBitmap ) ); |
| 1037 | LockPixels( GetGWorldPixMap( bitmap.GetHBITMAP() ) ); |
| 1038 | RGBColor maskColor = colour.GetPixel(); |
| 1039 | |
| 1040 | // this is not very efficient, but I can't think |
| 1041 | // of a better way of doing it |
| 1042 | CGrafPtr origPort ; |
| 1043 | GDHandle origDevice ; |
| 1044 | RGBColor col; |
| 1045 | RGBColor colors[2] = { |
| 1046 | { 0xFFFF, 0xFFFF, 0xFFFF }, |
| 1047 | { 0, 0, 0 }}; |
| 1048 | |
| 1049 | GetGWorld( &origPort , &origDevice ) ; |
| 1050 | for (int w = 0; w < bitmap.GetWidth(); w++) |
| 1051 | { |
| 1052 | for (int h = 0; h < bitmap.GetHeight(); h++) |
| 1053 | { |
| 1054 | SetGWorld( bitmap.GetHBITMAP(), NULL ) ; |
| 1055 | GetCPixel( w , h , &col ) ; |
| 1056 | SetGWorld( m_maskBitmap , NULL ) ; |
| 1057 | if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue) |
| 1058 | { |
| 1059 | SetCPixel( w , h , &colors[0] ) ; |
| 1060 | } |
| 1061 | else |
| 1062 | { |
| 1063 | SetCPixel( w , h , &colors[1] ) ; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ; |
| 1068 | UnlockPixels( GetGWorldPixMap( bitmap.GetHBITMAP() ) ) ; |
| 1069 | SetGWorld( origPort , origDevice ) ; |
| 1070 | |
| 1071 | return TRUE; |
| 1072 | } |
| 1073 | |
| 1074 | bool wxMask::PointMasked(int x, int y) |
| 1075 | { |
| 1076 | WXHBITMAP origPort; |
| 1077 | GDHandle origDevice; |
| 1078 | RGBColor color; |
| 1079 | bool masked = true; |
| 1080 | |
| 1081 | GetGWorld( &origPort, &origDevice); |
| 1082 | |
| 1083 | //Set port to mask and see if it masked (1) or not ( 0 ) |
| 1084 | SetGWorld(m_maskBitmap, NULL); |
| 1085 | LockPixels(GetGWorldPixMap(m_maskBitmap)); |
| 1086 | GetCPixel(x,y, &color); |
| 1087 | masked = !(color.red == 0 && color.green == 0 && color.blue == 0); |
| 1088 | UnlockPixels(GetGWorldPixMap(m_maskBitmap)); |
| 1089 | |
| 1090 | SetGWorld( origPort, origDevice); |
| 1091 | |
| 1092 | return masked; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * wxBitmapHandler |
| 1097 | */ |
| 1098 | |
| 1099 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) |
| 1100 | |
| 1101 | bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth) |
| 1102 | { |
| 1103 | return FALSE; |
| 1104 | } |
| 1105 | |
| 1106 | bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type, |
| 1107 | int desiredWidth, int desiredHeight) |
| 1108 | { |
| 1109 | return FALSE; |
| 1110 | } |
| 1111 | |
| 1112 | bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) |
| 1113 | { |
| 1114 | return FALSE; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * Standard handlers |
| 1119 | */ |
| 1120 | |
| 1121 | class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler |
| 1122 | { |
| 1123 | DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) |
| 1124 | public: |
| 1125 | inline wxPICTResourceHandler() |
| 1126 | { |
| 1127 | m_name = "Macintosh Pict resource"; |
| 1128 | m_extension = ""; |
| 1129 | m_type = wxBITMAP_TYPE_PICT_RESOURCE; |
| 1130 | }; |
| 1131 | |
| 1132 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1133 | int desiredWidth, int desiredHeight); |
| 1134 | }; |
| 1135 | IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler) |
| 1136 | |
| 1137 | bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1138 | int desiredWidth, int desiredHeight) |
| 1139 | { |
| 1140 | Str255 theName ; |
| 1141 | |
| 1142 | #if TARGET_CARBON |
| 1143 | c2pstrcpy( (StringPtr) theName , name ) ; |
| 1144 | #else |
| 1145 | strcpy( (char *) theName , name ) ; |
| 1146 | c2pstr( (char *)theName ) ; |
| 1147 | #endif |
| 1148 | |
| 1149 | PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ; |
| 1150 | if ( thePict ) |
| 1151 | { |
| 1152 | PictInfo theInfo ; |
| 1153 | |
| 1154 | GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ; |
| 1155 | DetachResource( (Handle) thePict ) ; |
| 1156 | M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ; |
| 1157 | M_BITMAPHANDLERDATA->m_hPict = thePict ; |
| 1158 | M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ; |
| 1159 | M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ; |
| 1160 | |
| 1161 | M_BITMAPHANDLERDATA->m_depth = theInfo.depth ; |
| 1162 | M_BITMAPHANDLERDATA->m_ok = true ; |
| 1163 | M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ; |
| 1164 | // M_BITMAPHANDLERDATA->m_bitmapPalette; |
| 1165 | // M_BITMAPHANDLERDATA->m_quality; |
| 1166 | return TRUE ; |
| 1167 | } |
| 1168 | return FALSE ; |
| 1169 | } |
| 1170 | |
| 1171 | /* TODO: bitmap handlers, a bit like this: |
| 1172 | class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler |
| 1173 | { |
| 1174 | DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler) |
| 1175 | public: |
| 1176 | inline wxBMPResourceHandler() |
| 1177 | { |
| 1178 | m_name = "Windows bitmap resource"; |
| 1179 | m_extension = ""; |
| 1180 | m_type = wxBITMAP_TYPE_BMP_RESOURCE; |
| 1181 | }; |
| 1182 | |
| 1183 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1184 | int desiredWidth, int desiredHeight); |
| 1185 | }; |
| 1186 | IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler) |
| 1187 | */ |
| 1188 | |
| 1189 | class WXDLLEXPORT wxXPMFileHandler: public wxBitmapHandler |
| 1190 | { |
| 1191 | DECLARE_DYNAMIC_CLASS(wxXPMFileHandler) |
| 1192 | public: |
| 1193 | inline wxXPMFileHandler(void) |
| 1194 | { |
| 1195 | m_name = "XPM bitmap file"; |
| 1196 | m_extension = "xpm"; |
| 1197 | m_type = wxBITMAP_TYPE_XPM; |
| 1198 | }; |
| 1199 | |
| 1200 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1201 | int desiredWidth = -1, int desiredHeight = -1); |
| 1202 | virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL); |
| 1203 | }; |
| 1204 | IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) |
| 1205 | |
| 1206 | bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1207 | int desiredWidth, int desiredHeight) |
| 1208 | { |
| 1209 | #if USE_XPM_IN_MSW |
| 1210 | XImage *ximage; |
| 1211 | XpmAttributes xpmAttr; |
| 1212 | HDC dc; |
| 1213 | |
| 1214 | M_BITMAPHANDLERDATA->m_ok = FALSE; |
| 1215 | dc = CreateCompatibleDC(NULL); |
| 1216 | if (dc) |
| 1217 | { |
| 1218 | xpmAttr.valuemask = XpmReturnPixels; |
| 1219 | int errorStatus = XpmReadFileToImage(&dc, WXSTRINGCAST name, &ximage, (XImage **) NULL, &xpmAttr); |
| 1220 | DeleteDC(dc); |
| 1221 | if (errorStatus == XpmSuccess) |
| 1222 | { |
| 1223 | M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ximage->bitmap; |
| 1224 | |
| 1225 | BITMAP bm; |
| 1226 | GetObject((HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap, sizeof(bm), (LPSTR) & bm); |
| 1227 | |
| 1228 | M_BITMAPHANDLERDATA->m_width = (bm.bmWidth); |
| 1229 | M_BITMAPHANDLERDATA->m_height = (bm.bmHeight); |
| 1230 | M_BITMAPHANDLERDATA->m_depth = (bm.bmPlanes * bm.bmBitsPixel); |
| 1231 | M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels; |
| 1232 | XpmFreeAttributes(&xpmAttr); |
| 1233 | XImageFree(ximage); |
| 1234 | |
| 1235 | M_BITMAPHANDLERDATA->m_ok = TRUE; |
| 1236 | return TRUE; |
| 1237 | } |
| 1238 | else |
| 1239 | { |
| 1240 | M_BITMAPHANDLERDATA->m_ok = FALSE; |
| 1241 | return FALSE; |
| 1242 | } |
| 1243 | } |
| 1244 | #endif |
| 1245 | |
| 1246 | return FALSE; |
| 1247 | } |
| 1248 | |
| 1249 | bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) |
| 1250 | { |
| 1251 | #if USE_XPM_IN_MSW |
| 1252 | HDC dc = NULL; |
| 1253 | |
| 1254 | Visual *visual = NULL; |
| 1255 | XImage ximage; |
| 1256 | |
| 1257 | dc = CreateCompatibleDC(NULL); |
| 1258 | if (dc) |
| 1259 | { |
| 1260 | if (SelectObject(dc, (HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap)) |
| 1261 | { |
| 1262 | |
| 1263 | ximage.width = M_BITMAPHANDLERDATA->m_width; |
| 1264 | ximage.height = M_BITMAPHANDLERDATA->m_height; |
| 1265 | ximage.depth = M_BITMAPHANDLERDATA->m_depth; |
| 1266 | ximage.bitmap = (void *)M_BITMAPHANDLERDATA->m_hBitmap; |
| 1267 | int errorStatus = XpmWriteFileFromImage(&dc, WXSTRINGCAST name, |
| 1268 | &ximage, (XImage *) NULL, (XpmAttributes *) NULL); |
| 1269 | |
| 1270 | if (dc) |
| 1271 | DeleteDC(dc); |
| 1272 | |
| 1273 | if (errorStatus == XpmSuccess) |
| 1274 | return TRUE; |
| 1275 | else |
| 1276 | return FALSE; |
| 1277 | } else return FALSE; |
| 1278 | } else return FALSE; |
| 1279 | #else |
| 1280 | return FALSE; |
| 1281 | #endif |
| 1282 | } |
| 1283 | |
| 1284 | #ifdef OBSOLETE_XPM_DATA_HANDLER |
| 1285 | class WXDLLEXPORT wxXPMDataHandler: public wxBitmapHandler |
| 1286 | { |
| 1287 | DECLARE_DYNAMIC_CLASS(wxXPMDataHandler) |
| 1288 | public: |
| 1289 | inline wxXPMDataHandler(void) |
| 1290 | { |
| 1291 | m_name = "XPM bitmap data"; |
| 1292 | m_extension = "xpm"; |
| 1293 | m_type = wxBITMAP_TYPE_XPM_DATA; |
| 1294 | }; |
| 1295 | |
| 1296 | virtual bool Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth = 1); |
| 1297 | }; |
| 1298 | IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) |
| 1299 | |
| 1300 | bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth) |
| 1301 | { |
| 1302 | XImage * ximage = NULL ; |
| 1303 | XImage * xshapeimage = NULL ; |
| 1304 | int ErrorStatus; |
| 1305 | XpmAttributes xpmAttr; |
| 1306 | |
| 1307 | xpmAttr.valuemask = XpmReturnInfos; // get infos back |
| 1308 | ErrorStatus = XpmCreateImageFromData( GetMainDevice() , (char **)data, |
| 1309 | &ximage, &xshapeimage, &xpmAttr); |
| 1310 | |
| 1311 | if (ErrorStatus == XpmSuccess) |
| 1312 | { |
| 1313 | M_BITMAPHANDLERDATA->m_ok = FALSE; |
| 1314 | M_BITMAPHANDLERDATA->m_numColors = 0; |
| 1315 | M_BITMAPHANDLERDATA->m_hBitmap = ximage->gworldptr ; |
| 1316 | |
| 1317 | M_BITMAPHANDLERDATA->m_width = ximage->width; |
| 1318 | M_BITMAPHANDLERDATA->m_height = ximage->height; |
| 1319 | M_BITMAPHANDLERDATA->m_depth = ximage->depth; |
| 1320 | M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels; |
| 1321 | XpmFreeAttributes(&xpmAttr); |
| 1322 | M_BITMAPHANDLERDATA->m_ok = TRUE; |
| 1323 | ximage->gworldptr = NULL ; |
| 1324 | XImageFree(ximage); // releases the malloc, but does not detroy |
| 1325 | // the bitmap |
| 1326 | M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; |
| 1327 | if ( xshapeimage != NULL ) |
| 1328 | { |
| 1329 | wxMask* m = new wxMask() ; |
| 1330 | m->SetMaskBitmap( xshapeimage->gworldptr ) ; |
| 1331 | M_BITMAPHANDLERDATA->m_bitmapMask = m ; |
| 1332 | } |
| 1333 | return TRUE; |
| 1334 | } |
| 1335 | else |
| 1336 | { |
| 1337 | M_BITMAPHANDLERDATA->m_ok = FALSE; |
| 1338 | return FALSE; |
| 1339 | } |
| 1340 | return FALSE; |
| 1341 | } |
| 1342 | #endif |
| 1343 | |
| 1344 | class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler |
| 1345 | { |
| 1346 | DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler) |
| 1347 | public: |
| 1348 | inline wxBMPResourceHandler() |
| 1349 | { |
| 1350 | m_name = "Windows bitmap resource"; |
| 1351 | m_extension = ""; |
| 1352 | m_type = wxBITMAP_TYPE_BMP_RESOURCE; |
| 1353 | }; |
| 1354 | |
| 1355 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1356 | int desiredWidth, int desiredHeight); |
| 1357 | }; |
| 1358 | |
| 1359 | IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler) |
| 1360 | |
| 1361 | bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1362 | int desiredWidth, int desiredHeight) |
| 1363 | { |
| 1364 | // TODO: load colourmap. |
| 1365 | // it's probably not found |
| 1366 | wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name.c_str()); |
| 1367 | |
| 1368 | return FALSE; |
| 1369 | } |
| 1370 | |
| 1371 | class WXDLLEXPORT wxBMPFileHandler: public wxBitmapHandler |
| 1372 | { |
| 1373 | DECLARE_DYNAMIC_CLASS(wxBMPFileHandler) |
| 1374 | public: |
| 1375 | inline wxBMPFileHandler(void) |
| 1376 | { |
| 1377 | m_name = "Windows bitmap file"; |
| 1378 | m_extension = "bmp"; |
| 1379 | m_type = wxBITMAP_TYPE_BMP; |
| 1380 | }; |
| 1381 | |
| 1382 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1383 | int desiredWidth, int desiredHeight); |
| 1384 | virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL); |
| 1385 | }; |
| 1386 | |
| 1387 | IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler) |
| 1388 | |
| 1389 | bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, |
| 1390 | int desiredWidth, int desiredHeight) |
| 1391 | { |
| 1392 | #if USE_IMAGE_LOADING_IN_MSW |
| 1393 | wxPalette *palette = NULL; |
| 1394 | bool success = FALSE; |
| 1395 | success = (wxLoadIntoBitmap(WXSTRINGCAST name, bitmap, &palette) != 0); |
| 1396 | if (!success && palette) |
| 1397 | { |
| 1398 | delete palette; |
| 1399 | palette = NULL; |
| 1400 | } |
| 1401 | if (palette) |
| 1402 | M_BITMAPHANDLERDATA->m_bitmapPalette = *palette; |
| 1403 | return success; |
| 1404 | #else |
| 1405 | return FALSE; |
| 1406 | #endif |
| 1407 | } |
| 1408 | |
| 1409 | bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal) |
| 1410 | { |
| 1411 | #if USE_IMAGE_LOADING_IN_MSW |
| 1412 | wxPalette *actualPalette = (wxPalette *)pal; |
| 1413 | if (!actualPalette && (!M_BITMAPHANDLERDATA->m_bitmapPalette.IsNull())) |
| 1414 | actualPalette = & (M_BITMAPHANDLERDATA->m_bitmapPalette); |
| 1415 | return (wxSaveBitmap(WXSTRINGCAST name, bitmap, actualPalette) != 0); |
| 1416 | #else |
| 1417 | return FALSE; |
| 1418 | #endif |
| 1419 | } |
| 1420 | |
| 1421 | |
| 1422 | void wxBitmap::CleanUpHandlers() |
| 1423 | { |
| 1424 | wxNode *node = sm_handlers.First(); |
| 1425 | while ( node ) |
| 1426 | { |
| 1427 | wxBitmapHandler *handler = (wxBitmapHandler *)node->Data(); |
| 1428 | wxNode *next = node->Next(); |
| 1429 | delete handler; |
| 1430 | delete node; |
| 1431 | node = next; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | void wxBitmap::InitStandardHandlers() |
| 1436 | { |
| 1437 | AddHandler(new wxPICTResourceHandler) ; |
| 1438 | AddHandler(new wxICONResourceHandler) ; |
| 1439 | AddHandler(new wxXPMFileHandler); |
| 1440 | #ifdef OBSOLETE_XPM_DATA_HANDLER |
| 1441 | AddHandler(new wxXPMDataHandler); |
| 1442 | #endif |
| 1443 | AddHandler(new wxBMPResourceHandler); |
| 1444 | AddHandler(new wxBMPFileHandler); |
| 1445 | } |