1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmapbase.h"
14 #pragma implementation "bitmap.h"
19 #include "wx/bitmap.h"
23 #include "wx/xpmdecod.h"
25 #if !USE_SHARED_LIBRARIES
26 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
27 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
28 IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase
, wxGDIObject
)
29 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandlerBase
, wxObject
)
33 #include <ApplicationServices/ApplicationServices.h>
35 #include <PictUtils.h>
38 #include "wx/mac/uma.h"
40 CTabHandle
wxMacCreateColorTable( int numColors
)
42 CTabHandle newColors
; /* Handle to the new color table */
44 /* Allocate memory for the color table */
45 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
46 sizeof (ColorSpec
) * (numColors
- 1) );
49 /* Initialize the fields */
50 (**newColors
).ctSeed
= GetCTSeed();
51 (**newColors
).ctFlags
= 0;
52 (**newColors
).ctSize
= numColors
- 1;
53 /* Initialize the table of colors */
58 void wxMacDestroyColorTable( CTabHandle colors
)
60 DisposeHandle( (Handle
) colors
) ;
63 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
65 (**newColors
).ctTable
[index
].value
= index
;
66 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
67 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
68 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
71 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
75 Rect rect
= { 0 , 0 , height
, width
} ;
79 depth
= wxDisplayDepth() ;
82 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
90 void wxMacDestroyGWorld( GWorldPtr gw
)
96 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
101 PicHandle pict
; // this is the Picture we give back
103 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
104 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
105 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
107 unsigned char *maskimage
= NULL
;
109 GetPortBounds( wp
, &portRect
) ;
110 int width
= portRect
.right
- portRect
.left
;
111 int height
= portRect
.bottom
- portRect
.top
;
113 LockPixels( GetGWorldPixMap( wp
) ) ;
114 GetGWorld( &origPort
, &origDev
) ;
118 maskimage
= (unsigned char*) malloc( width
* height
) ;
119 SetGWorld( mask
, NULL
) ;
120 LockPixels( GetGWorldPixMap( mask
) ) ;
121 for ( int y
= 0 ; y
< height
; y
++ )
123 for( int x
= 0 ; x
< width
; x
++ )
127 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
128 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
131 UnlockPixels( GetGWorldPixMap( mask
) ) ;
134 SetGWorld( wp
, NULL
) ;
136 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
142 RGBForeColor( &black
) ;
143 RGBBackColor( &white
) ;
144 PenMode(transparent
);
146 for ( int y
= 0 ; y
< height
; ++y
)
148 for( int x
= 0 ; x
< width
; ++x
)
150 if ( maskimage
[y
*width
+ x
] )
154 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
155 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
158 // With transparency set this sets a blank pixel not a white one
159 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &white
);
168 RGBBackColor( &gray
) ;
169 EraseRect(&portRect
);
170 RGBForeColor( &black
) ;
171 RGBBackColor( &white
) ;
173 CopyBits(GetPortBitMapForCopyBits(wp
), /* src PixMap - we copy image over
175 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs
176 &portRect
, // srcRect - it will be recorded and compressed -
177 &portRect
, // dstRect - into the picture that is open -
178 srcCopy
,NULL
); // copyMode and no clip region
180 ClosePicture(); // We are done recording the picture
181 UnlockPixels( GetGWorldPixMap( wp
) ) ;
182 SetGWorld( origPort
, origDev
) ;
184 return pict
; // return our groovy pict handle
187 wxBitmapRefData::wxBitmapRefData()
199 m_bitmapType
= kMacBitmapTypeUnknownType
;
202 wxBitmapRefData::~wxBitmapRefData()
204 switch (m_bitmapType
)
206 case kMacBitmapTypePict
:
210 KillPicture( m_hPict
) ;
215 case kMacBitmapTypeGrafWorld
:
219 wxMacDestroyGWorld( m_hBitmap
) ;
224 case kMacBitmapTypeIcon
:
227 DisposeCIcon( m_hIcon
) ;
243 wxList
wxBitmapBase::sm_handlers
;
246 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
255 if ( wxTheBitmapList
)
256 wxTheBitmapList
->AddBitmap(this);
259 wxBitmap::~wxBitmap()
262 wxTheBitmapList
->DeleteObject(this);
265 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
267 m_refData
= new wxBitmapRefData
;
269 M_BITMAPDATA
->m_width
= the_width
;
270 M_BITMAPDATA
->m_height
= the_height
;
271 M_BITMAPDATA
->m_depth
= no_bits
;
272 M_BITMAPDATA
->m_numColors
= 0;
275 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
276 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
277 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
280 GDHandle origDevice
;
282 GetGWorld( &origPort
, &origDevice
) ;
283 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
284 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
286 // bits is a char array
288 unsigned char* linestart
= (unsigned char*) bits
;
289 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
290 if ( the_width
% (sizeof(unsigned char) * 8) ) {
291 linesize
+= sizeof(unsigned char);
294 RGBColor colors
[2] = {
295 { 0xFFFF , 0xFFFF , 0xFFFF } ,
299 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
301 for ( int x
= 0 ; x
< the_width
; ++x
)
305 int mask
= 1 << bit
;
306 if ( linestart
[index
] & mask
)
308 SetCPixel( x
, y
, &colors
[1] ) ;
312 SetCPixel( x
, y
, &colors
[0] ) ;
316 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
318 SetGWorld( origPort
, origDevice
) ;
322 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
325 if ( wxTheBitmapList
) {
326 wxTheBitmapList
->AddBitmap(this);
330 wxBitmap::wxBitmap(int w
, int h
, int d
)
332 (void)Create(w
, h
, d
);
334 if ( wxTheBitmapList
)
335 wxTheBitmapList
->AddBitmap(this);
338 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
340 (void) Create(data
, type
, width
, height
, depth
);
342 if ( wxTheBitmapList
)
343 wxTheBitmapList
->AddBitmap(this);
346 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
348 LoadFile(filename
, type
);
350 if ( wxTheBitmapList
)
351 wxTheBitmapList
->AddBitmap(this);
354 bool wxBitmap::CreateFromXpm(const char **bits
)
356 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
357 wxXPMDecoder decoder
;
358 wxImage img
= decoder
.ReadData(bits
);
359 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
360 *this = wxBitmap(img
);
361 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
365 wxBitmap::wxBitmap(const char **bits
)
367 (void) CreateFromXpm(bits
);
370 wxBitmap::wxBitmap(char **bits
)
372 (void) CreateFromXpm((const char **)bits
);
375 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
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") );
384 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
385 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
390 GetGWorld( &origPort
, &origDevice
);
392 // Update the subbitmaps reference data
393 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
395 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
396 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
397 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
399 // Copy sub region of this bitmap
400 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
402 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
404 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
409 WXHBITMAP submask
, mask
;
412 mask
= GetMask()->GetMaskBitmap();
413 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
414 LockPixels(GetGWorldPixMap(mask
));
415 LockPixels(GetGWorldPixMap(submask
));
417 for(int yy
= 0; yy
< rect
.height
; yy
++)
419 for(int xx
= 0; xx
< rect
.width
; xx
++)
421 SetGWorld(mask
, NULL
);
422 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
423 SetGWorld(submask
, NULL
);
424 SetCPixel(xx
,yy
, &color
);
427 UnlockPixels(GetGWorldPixMap(mask
));
428 UnlockPixels(GetGWorldPixMap(submask
));
429 ref
->m_bitmapMask
= new wxMask
;
430 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
436 WXHBITMAP subbitmap
, bitmap
;
439 bitmap
= GetHBITMAP();
440 subbitmap
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetDepth());
441 LockPixels(GetGWorldPixMap(bitmap
));
442 LockPixels(GetGWorldPixMap(subbitmap
));
444 for(int yy
= 0; yy
< rect
.height
; yy
++)
446 for(int xx
= 0; xx
< rect
.width
; xx
++)
448 SetGWorld(bitmap
, NULL
);
449 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
450 SetGWorld(subbitmap
, NULL
);
451 SetCPixel(xx
, yy
, &color
);
454 UnlockPixels(GetGWorldPixMap(bitmap
));
455 UnlockPixels(GetGWorldPixMap(subbitmap
));
456 ret
.SetHBITMAP(subbitmap
);
459 SetGWorld( origPort
, origDevice
);
464 bool wxBitmap::Create(int w
, int h
, int d
)
468 m_refData
= new wxBitmapRefData
;
470 M_BITMAPDATA
->m_width
= w
;
471 M_BITMAPDATA
->m_height
= h
;
472 M_BITMAPDATA
->m_depth
= d
;
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
;
480 int wxBitmap::GetBitmapType() const
482 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
484 return M_BITMAPDATA
->m_bitmapType
;
487 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
489 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
490 M_BITMAPDATA
->m_hBitmap
= bmp
;
491 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
494 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
498 wxBitmapHandler
*handler
= FindHandler(type
);
502 m_refData
= new wxBitmapRefData
;
504 return handler
->LoadFile(this, filename
, type
, -1, -1);
508 wxImage
loadimage(filename
, type
);
509 if (loadimage
.Ok()) {
514 wxLogWarning("no bitmap handler for type %d defined.", type
);
518 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
522 m_refData
= new wxBitmapRefData
;
524 wxBitmapHandler
*handler
= FindHandler(type
);
526 if ( handler
== NULL
) {
527 wxLogWarning("no bitmap handler for type %d defined.", type
);
532 return handler
->Create(this, data
, type
, width
, height
, depth
);
535 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
537 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
538 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
540 m_refData
= new wxBitmapRefData();
542 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
544 // width and height of the device-dependent bitmap
545 int width
= image
.GetWidth();
546 int height
= image
.GetHeight();
550 Create( width
, height
, 32 ) ;
553 GDHandle origDevice
;
555 PixMapHandle pixMap
= GetGWorldPixMap(GetHBITMAP()) ;
556 LockPixels( pixMap
);
558 GetGWorld( &origPort
, &origDevice
) ;
559 SetGWorld( GetHBITMAP() , NULL
) ;
564 register unsigned char* data
= image
.GetData();
565 char* destinationBase
= GetPixBaseAddr( pixMap
);
566 register unsigned char* destination
= (unsigned char*) destinationBase
;
567 for (int y
= 0; y
< height
; y
++)
569 for (int x
= 0; x
< width
; x
++)
572 *destination
++ = *data
++ ;
573 *destination
++ = *data
++ ;
574 *destination
++ = *data
++ ;
576 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
577 destination
= (unsigned char*) destinationBase
;
579 if ( image
.HasMask() )
581 data
= image
.GetData();
583 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
584 RGBColor white
= { 0xffff, 0xffff, 0xffff };
585 RGBColor black
= { 0 , 0 , 0 };
586 wxBitmap maskBitmap
;
588 maskBitmap
.Create( width
, height
, 1);
589 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
590 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
592 for (int y
= 0; y
< height
; y
++)
594 for (int x
= 0; x
< width
; x
++)
596 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
598 SetCPixel(x
,y
, &white
);
601 SetCPixel(x
,y
, &black
);
606 SetGWorld(GetHBITMAP(), NULL
);
607 SetMask(new wxMask( maskBitmap
));
608 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
611 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
612 SetGWorld( origPort
, origDevice
);
615 wxImage
wxBitmap::ConvertToImage() const
619 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
621 // create an wxImage object
622 int width
= GetWidth();
623 int height
= GetHeight();
624 image
.Create( width
, height
);
626 unsigned char *data
= image
.GetData();
628 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
634 // background color set to RGB(16,16,16) in consistent with wxGTK
635 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
637 wxMask
*mask
= GetMask();
639 GetGWorld( &origPort
, &origDevice
);
640 LockPixels(GetGWorldPixMap(GetHBITMAP()));
641 SetGWorld( GetHBITMAP(), NULL
);
643 // Copy data into image
645 for (int yy
= 0; yy
< height
; yy
++)
647 for (int xx
= 0; xx
< width
; xx
++)
649 GetCPixel(xx
,yy
, &color
);
650 r
= ((color
.red
) >> 8);
651 g
= ((color
.green
) >> 8);
652 b
= ((color
.blue
) >> 8);
658 if (mask
->PointMasked(xx
,yy
))
660 data
[index
] = mask_r
;
661 data
[index
+ 1] = mask_g
;
662 data
[index
+ 2] = mask_b
;
670 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
671 image
.SetMask( true );
675 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
676 SetGWorld(origPort
, origDevice
);
682 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
683 const wxPalette
*palette
) const
685 wxBitmapHandler
*handler
= FindHandler(type
);
689 return handler
->SaveFile(this, filename
, type
, palette
);
693 wxImage image
= ConvertToImage();
695 return image
.SaveFile(filename
, type
);
698 wxLogWarning("no bitmap handler for type %d defined.", type
);
702 bool wxBitmap::Ok() const
704 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
707 int wxBitmap::GetHeight() const
709 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
711 return M_BITMAPDATA
->m_height
;
714 int wxBitmap::GetWidth() const
716 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
718 return M_BITMAPDATA
->m_width
;
721 int wxBitmap::GetDepth() const
723 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
725 return M_BITMAPDATA
->m_depth
;
728 int wxBitmap::GetQuality() const
730 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
732 return M_BITMAPDATA
->m_quality
;
735 wxMask
*wxBitmap::GetMask() const
737 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
739 return M_BITMAPDATA
->m_bitmapMask
;
742 void wxBitmap::SetWidth(int w
)
745 m_refData
= new wxBitmapRefData
;
747 M_BITMAPDATA
->m_width
= w
;
750 void wxBitmap::SetHeight(int h
)
753 m_refData
= new wxBitmapRefData
;
755 M_BITMAPDATA
->m_height
= h
;
758 void wxBitmap::SetDepth(int d
)
761 m_refData
= new wxBitmapRefData
;
763 M_BITMAPDATA
->m_depth
= d
;
766 void wxBitmap::SetQuality(int q
)
769 m_refData
= new wxBitmapRefData
;
771 M_BITMAPDATA
->m_quality
= q
;
774 void wxBitmap::SetOk(bool isOk
)
777 m_refData
= new wxBitmapRefData
;
779 M_BITMAPDATA
->m_ok
= isOk
;
782 wxPalette
*wxBitmap::GetPalette() const
784 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
786 return &M_BITMAPDATA
->m_bitmapPalette
;
789 void wxBitmap::SetPalette(const wxPalette
& palette
)
792 m_refData
= new wxBitmapRefData
;
794 M_BITMAPDATA
->m_bitmapPalette
= palette
;
797 void wxBitmap::SetMask(wxMask
*mask
)
800 m_refData
= new wxBitmapRefData
;
802 // Remove existing mask if there is one.
803 if (M_BITMAPDATA
->m_bitmapMask
)
804 delete M_BITMAPDATA
->m_bitmapMask
;
806 M_BITMAPDATA
->m_bitmapMask
= mask
;
809 WXHBITMAP
wxBitmap::GetHBITMAP() const
811 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
813 return M_BITMAPDATA
->m_hBitmap
;
816 PicHandle
wxBitmap::GetPict() const
818 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
820 PicHandle picture
; // This is the returned picture
822 // If bitmap already in Pict format return pointer
823 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
824 return M_BITMAPDATA
->m_hPict
;
826 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
831 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
832 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
833 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
839 GetPortBounds( GetHBITMAP() , &portRect
) ;
840 int width
= portRect
.right
- portRect
.left
;
841 int height
= portRect
.bottom
- portRect
.top
;
843 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
844 GetGWorld( &origPort
, &origDev
) ;
848 SetGWorld( GetHBITMAP() , NULL
) ;
850 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
858 RGBColor trans
= white
;
860 RGBBackColor( &gray
);
861 EraseRect( &portRect
);
862 RGBColor trans
= gray
;
864 RGBForeColor( &black
) ;
865 RGBBackColor( &white
) ;
866 PenMode(transparent
);
868 for ( int y
= 0 ; y
< height
; ++y
)
870 for( int x
= 0 ; x
< width
; ++x
)
872 if ( !mask
->PointMasked(x
,y
) )
876 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
877 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
880 // With transparency this sets a blank pixel
881 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
888 RGBBackColor( &gray
) ;
889 EraseRect(&portRect
);
890 RGBForeColor( &black
) ;
891 RGBBackColor( &white
) ;
893 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
894 // src PixMap - we copy image over itself -
895 GetPortBitMapForCopyBits(GetHBITMAP()),
896 // dst PixMap - no drawing occurs
897 &portRect
, // srcRect - it will be recorded and compressed -
898 &portRect
, // dstRect - into the picture that is open -
899 srcCopy
,NULL
); // copyMode and no clip region
901 ClosePicture(); // We are done recording the picture
902 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
903 SetGWorld( origPort
, origDev
) ;
905 return picture
; // return our groovy pict handle
908 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
910 sm_handlers
.Append(handler
);
913 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
915 sm_handlers
.Insert(handler
);
918 bool wxBitmap::RemoveHandler(const wxString
& name
)
920 wxBitmapHandler
*handler
= FindHandler(name
);
923 sm_handlers
.DeleteObject(handler
);
930 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
932 wxNode
*node
= sm_handlers
.First();
935 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
936 if ( handler
->GetName() == name
)
943 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, wxBitmapType type
)
945 wxNode
*node
= sm_handlers
.First();
948 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
949 if ( handler
->GetExtension() == extension
&&
950 (type
== -1 || handler
->GetType() == type
) )
957 wxBitmapHandler
*wxBitmap::FindHandler(wxBitmapType type
)
959 wxNode
*node
= sm_handlers
.First();
962 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
963 if (handler
->GetType() == type
)
979 // Construct a mask from a bitmap and a colour indicating
980 // the transparent area
981 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
984 Create(bitmap
, colour
);
987 // Construct a mask from a bitmap and a palette index indicating
988 // the transparent area
989 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
992 Create(bitmap
, paletteIndex
);
995 // Construct a mask from a mono bitmap (copies the bitmap).
996 wxMask::wxMask(const wxBitmap
& bitmap
)
1006 wxMacDestroyGWorld( m_maskBitmap
) ;
1007 m_maskBitmap
= NULL
;
1011 // Create a mask from a mono bitmap (copies the bitmap).
1012 bool wxMask::Create(const wxBitmap
& bitmap
)
1016 wxMacDestroyGWorld( m_maskBitmap
) ;
1017 m_maskBitmap
= NULL
;
1019 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1020 wxT("Cannot create mask from this bitmap type (TODO)"));
1021 // other types would require a temporary bitmap. not yet implemented
1023 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1025 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1026 wxT("Cannot create mask from colour bitmap"));
1028 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1029 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1031 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1032 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1033 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1034 GetPortBitMapForCopyBits(m_maskBitmap
),
1035 &rect
, &rect
, srcCopy
, 0);
1036 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1037 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1042 // Create a mask from a bitmap and a palette index indicating
1043 // the transparent area
1044 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1047 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1051 // Create a mask from a bitmap and a colour indicating
1052 // the transparent area
1053 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1057 wxMacDestroyGWorld( m_maskBitmap
) ;
1058 m_maskBitmap
= NULL
;
1060 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1061 wxT("Cannot create mask from this bitmap type (TODO)"));
1062 // other types would require a temporary bitmap. not yet implemented
1064 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1066 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1067 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1068 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1069 RGBColor maskColor
= colour
.GetPixel();
1071 // this is not very efficient, but I can't think
1072 // of a better way of doing it
1074 GDHandle origDevice
;
1076 RGBColor colors
[2] = {
1077 { 0xFFFF, 0xFFFF, 0xFFFF },
1080 GetGWorld( &origPort
, &origDevice
) ;
1081 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1083 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1085 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1086 GetCPixel( w
, h
, &col
) ;
1087 SetGWorld( m_maskBitmap
, NULL
) ;
1088 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1090 SetCPixel( w
, h
, &colors
[0] ) ;
1094 SetCPixel( w
, h
, &colors
[1] ) ;
1098 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1099 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1100 SetGWorld( origPort
, origDevice
) ;
1105 bool wxMask::PointMasked(int x
, int y
)
1108 GDHandle origDevice
;
1112 GetGWorld( &origPort
, &origDevice
);
1114 //Set port to mask and see if it masked (1) or not ( 0 )
1115 SetGWorld(m_maskBitmap
, NULL
);
1116 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1117 GetCPixel(x
,y
, &color
);
1118 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1119 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1121 SetGWorld( origPort
, origDevice
);
1130 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1132 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1137 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1138 int desiredWidth
, int desiredHeight
)
1143 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1152 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1154 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1156 inline wxPICTResourceHandler()
1158 m_name
= "Macintosh Pict resource";
1160 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1163 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1164 int desiredWidth
, int desiredHeight
);
1166 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1168 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1169 int desiredWidth
, int desiredHeight
)
1174 c2pstrcpy( (StringPtr
) theName
, name
) ;
1176 strcpy( (char *) theName
, name
) ;
1177 c2pstr( (char *)theName
) ;
1180 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1185 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1186 DetachResource( (Handle
) thePict
) ;
1187 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1188 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1189 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1190 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1192 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1193 M_BITMAPHANDLERDATA
->m_ok
= true ;
1194 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1195 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1196 // M_BITMAPHANDLERDATA->m_quality;
1202 #if 0 // The following is an example for creating a bitmap handler
1204 // TODO: bitmap handlers, a bit like this:
1205 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1207 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1209 inline wxBMPResourceHandler()
1211 m_name
= "Windows bitmap resource";
1213 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1216 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1217 int desiredWidth
, int desiredHeight
);
1219 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1223 void wxBitmap::CleanUpHandlers()
1225 wxNode
*node
= sm_handlers
.First();
1228 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1229 wxNode
*next
= node
->Next();
1236 void wxBitmap::InitStandardHandlers()
1238 AddHandler(new wxPICTResourceHandler
) ;
1239 AddHandler(new wxICONResourceHandler
) ;