1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
19 #include "wx/palette.h"
20 #include "wx/bitmap.h"
24 #include "wx/xpmdecod.h"
26 #if !USE_SHARED_LIBRARIES
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
29 IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase
, wxGDIObject
)
30 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandlerBase
, wxObject
)
34 #include <ApplicationServices/ApplicationServices.h>
36 #include <PictUtils.h>
39 #include "wx/mac/uma.h"
41 CTabHandle
wxMacCreateColorTable( int numColors
)
43 CTabHandle newColors
; /* Handle to the new color table */
45 /* Allocate memory for the color table */
46 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
47 sizeof (ColorSpec
) * (numColors
- 1) );
50 /* Initialize the fields */
51 (**newColors
).ctSeed
= GetCTSeed();
52 (**newColors
).ctFlags
= 0;
53 (**newColors
).ctSize
= numColors
- 1;
54 /* Initialize the table of colors */
59 void wxMacDestroyColorTable( CTabHandle colors
)
61 DisposeHandle( (Handle
) colors
) ;
64 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
66 (**newColors
).ctTable
[index
].value
= index
;
67 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
68 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
69 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
72 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
76 Rect rect
= { 0 , 0 , height
, width
} ;
80 depth
= wxDisplayDepth() ;
83 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
91 void wxMacDestroyGWorld( GWorldPtr gw
)
97 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
102 PicHandle pict
; // this is the Picture we give back
104 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
105 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
106 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
108 unsigned char *maskimage
= NULL
;
110 GetPortBounds( wp
, &portRect
) ;
111 int width
= portRect
.right
- portRect
.left
;
112 int height
= portRect
.bottom
- portRect
.top
;
114 LockPixels( GetGWorldPixMap( wp
) ) ;
115 GetGWorld( &origPort
, &origDev
) ;
119 maskimage
= (unsigned char*) malloc( width
* height
) ;
120 SetGWorld( mask
, NULL
) ;
121 LockPixels( GetGWorldPixMap( mask
) ) ;
122 for ( int y
= 0 ; y
< height
; y
++ )
124 for( int x
= 0 ; x
< width
; x
++ )
128 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
129 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
132 UnlockPixels( GetGWorldPixMap( mask
) ) ;
135 SetGWorld( wp
, NULL
) ;
137 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
143 RGBForeColor( &black
) ;
144 RGBBackColor( &white
) ;
145 PenMode(transparent
);
147 for ( int y
= 0 ; y
< height
; ++y
)
149 for( int x
= 0 ; x
< width
; ++x
)
151 if ( maskimage
[y
*width
+ x
] )
155 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
156 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
159 // With transparency set this sets a blank pixel not a white one
160 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &white
);
169 RGBBackColor( &gray
) ;
170 EraseRect(&portRect
);
171 RGBForeColor( &black
) ;
172 RGBBackColor( &white
) ;
174 CopyBits(GetPortBitMapForCopyBits(wp
), /* src PixMap - we copy image over
176 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs
177 &portRect
, // srcRect - it will be recorded and compressed -
178 &portRect
, // dstRect - into the picture that is open -
179 srcCopy
,NULL
); // copyMode and no clip region
181 ClosePicture(); // We are done recording the picture
182 UnlockPixels( GetGWorldPixMap( wp
) ) ;
183 SetGWorld( origPort
, origDev
) ;
185 return pict
; // return our groovy pict handle
188 wxBitmapRefData::wxBitmapRefData()
200 m_bitmapType
= kMacBitmapTypeUnknownType
;
203 wxBitmapRefData::~wxBitmapRefData()
205 switch (m_bitmapType
)
207 case kMacBitmapTypePict
:
211 KillPicture( m_hPict
) ;
216 case kMacBitmapTypeGrafWorld
:
220 wxMacDestroyGWorld( m_hBitmap
) ;
225 case kMacBitmapTypeIcon
:
228 DisposeCIcon( m_hIcon
) ;
244 wxList
wxBitmap::sm_handlers
;
247 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
256 if ( wxTheBitmapList
)
257 wxTheBitmapList
->AddBitmap(this);
260 wxBitmap::~wxBitmap()
263 wxTheBitmapList
->DeleteObject(this);
266 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
268 m_refData
= new wxBitmapRefData
;
270 M_BITMAPDATA
->m_width
= the_width
;
271 M_BITMAPDATA
->m_height
= the_height
;
272 M_BITMAPDATA
->m_depth
= no_bits
;
273 M_BITMAPDATA
->m_numColors
= 0;
276 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
277 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
278 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
281 GDHandle origDevice
;
283 GetGWorld( &origPort
, &origDevice
) ;
284 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
285 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
287 // bits is a char array
289 unsigned char* linestart
= (unsigned char*) bits
;
290 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
291 if ( the_width
% (sizeof(unsigned char) * 8) ) {
292 linesize
+= sizeof(unsigned char);
295 RGBColor colors
[2] = {
296 { 0xFFFF , 0xFFFF , 0xFFFF } ,
300 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
302 for ( int x
= 0 ; x
< the_width
; ++x
)
306 int mask
= 1 << bit
;
307 if ( linestart
[index
] & mask
)
309 SetCPixel( x
, y
, &colors
[1] ) ;
313 SetCPixel( x
, y
, &colors
[0] ) ;
317 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
319 SetGWorld( origPort
, origDevice
) ;
323 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
326 if ( wxTheBitmapList
) {
327 wxTheBitmapList
->AddBitmap(this);
331 wxBitmap::wxBitmap(int w
, int h
, int d
)
333 (void)Create(w
, h
, d
);
335 if ( wxTheBitmapList
)
336 wxTheBitmapList
->AddBitmap(this);
339 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
341 (void) Create(data
, type
, width
, height
, depth
);
343 if ( wxTheBitmapList
)
344 wxTheBitmapList
->AddBitmap(this);
347 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
349 LoadFile(filename
, type
);
351 if ( wxTheBitmapList
)
352 wxTheBitmapList
->AddBitmap(this);
355 bool wxBitmap::CreateFromXpm(const char **bits
)
357 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
358 wxXPMDecoder decoder
;
359 wxImage img
= decoder
.ReadData(bits
);
360 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
361 *this = wxBitmap(img
);
362 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
366 wxBitmap::wxBitmap(const char **bits
)
368 (void) CreateFromXpm(bits
);
371 wxBitmap::wxBitmap(char **bits
)
373 (void) CreateFromXpm((const char **)bits
);
376 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
379 (rect
.x
>= 0) && (rect
.y
>= 0) &&
380 (rect
.x
+rect
.width
<= GetWidth()) &&
381 (rect
.y
+rect
.height
<= GetHeight()),
382 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
385 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
386 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
391 GetGWorld( &origPort
, &origDevice
);
393 // Update the subbitmaps reference data
394 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
396 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
397 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
398 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
400 // Copy sub region of this bitmap
401 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
403 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
405 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
410 WXHBITMAP submask
, mask
;
413 mask
= GetMask()->GetMaskBitmap();
414 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
415 LockPixels(GetGWorldPixMap(mask
));
416 LockPixels(GetGWorldPixMap(submask
));
418 for(int yy
= 0; yy
< rect
.height
; yy
++)
420 for(int xx
= 0; xx
< rect
.width
; xx
++)
422 SetGWorld(mask
, NULL
);
423 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
424 SetGWorld(submask
, NULL
);
425 SetCPixel(xx
,yy
, &color
);
428 UnlockPixels(GetGWorldPixMap(mask
));
429 UnlockPixels(GetGWorldPixMap(submask
));
430 ref
->m_bitmapMask
= new wxMask
;
431 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
437 WXHBITMAP subbitmap
, bitmap
;
440 bitmap
= GetHBITMAP();
441 subbitmap
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetDepth());
442 LockPixels(GetGWorldPixMap(bitmap
));
443 LockPixels(GetGWorldPixMap(subbitmap
));
445 for(int yy
= 0; yy
< rect
.height
; yy
++)
447 for(int xx
= 0; xx
< rect
.width
; xx
++)
449 SetGWorld(bitmap
, NULL
);
450 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
451 SetGWorld(subbitmap
, NULL
);
452 SetCPixel(xx
, yy
, &color
);
455 UnlockPixels(GetGWorldPixMap(bitmap
));
456 UnlockPixels(GetGWorldPixMap(subbitmap
));
457 ret
.SetHBITMAP(subbitmap
);
460 SetGWorld( origPort
, origDevice
);
465 bool wxBitmap::Create(int w
, int h
, int d
)
469 m_refData
= new wxBitmapRefData
;
471 M_BITMAPDATA
->m_width
= w
;
472 M_BITMAPDATA
->m_height
= h
;
473 M_BITMAPDATA
->m_depth
= d
;
475 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
476 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
477 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
478 return M_BITMAPDATA
->m_ok
;
481 int wxBitmap::GetBitmapType() const
483 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
485 return M_BITMAPDATA
->m_bitmapType
;
488 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
490 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
491 M_BITMAPDATA
->m_hBitmap
= bmp
;
492 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
495 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
499 wxBitmapHandler
*handler
= FindHandler(type
);
503 m_refData
= new wxBitmapRefData
;
505 return handler
->LoadFile(this, filename
, type
, -1, -1);
509 wxImage
loadimage(filename
, type
);
510 if (loadimage
.Ok()) {
515 wxLogWarning("no bitmap handler for type %d defined.", type
);
519 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
523 m_refData
= new wxBitmapRefData
;
525 wxBitmapHandler
*handler
= FindHandler(type
);
527 if ( handler
== NULL
) {
528 wxLogWarning("no bitmap handler for type %d defined.", type
);
533 return handler
->Create(this, data
, type
, width
, height
, depth
);
536 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
538 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
539 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
541 m_refData
= new wxBitmapRefData();
543 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
545 // width and height of the device-dependent bitmap
546 int width
= image
.GetWidth();
547 int height
= image
.GetHeight();
551 Create( width
, height
, wxDisplayDepth() ) ;
552 wxBitmap
maskBitmap( width
, height
, 1);
555 GDHandle origDevice
;
557 LockPixels( GetGWorldPixMap(GetHBITMAP()) );
558 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
560 GetGWorld( &origPort
, &origDevice
) ;
561 SetGWorld( GetHBITMAP() , NULL
) ;
564 wxColour rgb
, maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
566 RGBColor white
= { 0xffff, 0xffff, 0xffff };
567 RGBColor black
= { 0 , 0 , 0 };
569 register unsigned char* data
= image
.GetData();
572 for (int y
= 0; y
< height
; y
++)
574 for (int x
= 0; x
< width
; x
++)
576 rgb
.Set(data
[index
++], data
[index
++], data
[index
++]);
577 color
= rgb
.GetPixel();
578 SetCPixel( x
, y
, &color
) ;
581 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
582 if (rgb
== maskcolor
) {
583 SetCPixel(x
,y
, &white
);
586 SetCPixel(x
,y
, &black
);
588 SetGWorld(GetHBITMAP(), NULL
);
594 if ( image
.HasMask() ) {
595 SetMask(new wxMask( maskBitmap
));
598 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
599 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
600 SetGWorld( origPort
, origDevice
);
603 wxImage
wxBitmap::ConvertToImage() const
607 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
609 // create an wxImage object
610 int width
= GetWidth();
611 int height
= GetHeight();
612 image
.Create( width
, height
);
614 unsigned char *data
= image
.GetData();
616 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
622 // background color set to RGB(16,16,16) in consistent with wxGTK
623 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
625 wxMask
*mask
= GetMask();
627 GetGWorld( &origPort
, &origDevice
);
628 LockPixels(GetGWorldPixMap(GetHBITMAP()));
629 SetGWorld( GetHBITMAP(), NULL
);
631 // Copy data into image
633 for (int yy
= 0; yy
< height
; yy
++)
635 for (int xx
= 0; xx
< width
; xx
++)
637 GetCPixel(xx
,yy
, &color
);
638 r
= ((color
.red
) >> 8);
639 g
= ((color
.green
) >> 8);
640 b
= ((color
.blue
) >> 8);
646 if (mask
->PointMasked(xx
,yy
))
648 data
[index
] = mask_r
;
649 data
[index
+ 1] = mask_g
;
650 data
[index
+ 2] = mask_b
;
658 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
659 image
.SetMask( true );
663 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
664 SetGWorld(origPort
, origDevice
);
670 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
671 const wxPalette
*palette
) const
673 wxBitmapHandler
*handler
= FindHandler(type
);
677 return handler
->SaveFile(this, filename
, type
, palette
);
681 wxImage image
= ConvertToImage();
683 return image
.SaveFile(filename
, type
);
686 wxLogWarning("no bitmap handler for type %d defined.", type
);
690 bool wxBitmap::Ok() const
692 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
695 int wxBitmap::GetHeight() const
697 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
699 return M_BITMAPDATA
->m_height
;
702 int wxBitmap::GetWidth() const
704 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
706 return M_BITMAPDATA
->m_width
;
709 int wxBitmap::GetDepth() const
711 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
713 return M_BITMAPDATA
->m_depth
;
716 int wxBitmap::GetQuality() const
718 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
720 return M_BITMAPDATA
->m_quality
;
723 wxMask
*wxBitmap::GetMask() const
725 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
727 return M_BITMAPDATA
->m_bitmapMask
;
730 void wxBitmap::SetWidth(int w
)
733 m_refData
= new wxBitmapRefData
;
735 M_BITMAPDATA
->m_width
= w
;
738 void wxBitmap::SetHeight(int h
)
741 m_refData
= new wxBitmapRefData
;
743 M_BITMAPDATA
->m_height
= h
;
746 void wxBitmap::SetDepth(int d
)
749 m_refData
= new wxBitmapRefData
;
751 M_BITMAPDATA
->m_depth
= d
;
754 void wxBitmap::SetQuality(int q
)
757 m_refData
= new wxBitmapRefData
;
759 M_BITMAPDATA
->m_quality
= q
;
762 void wxBitmap::SetOk(bool isOk
)
765 m_refData
= new wxBitmapRefData
;
767 M_BITMAPDATA
->m_ok
= isOk
;
770 wxPalette
*wxBitmap::GetPalette() const
772 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
774 return &M_BITMAPDATA
->m_bitmapPalette
;
777 void wxBitmap::SetPalette(const wxPalette
& palette
)
780 m_refData
= new wxBitmapRefData
;
782 M_BITMAPDATA
->m_bitmapPalette
= palette
;
785 void wxBitmap::SetMask(wxMask
*mask
)
788 m_refData
= new wxBitmapRefData
;
790 // Remove existing mask if there is one.
791 if (M_BITMAPDATA
->m_bitmapMask
)
792 delete M_BITMAPDATA
->m_bitmapMask
;
794 M_BITMAPDATA
->m_bitmapMask
= mask
;
797 WXHBITMAP
wxBitmap::GetHBITMAP() const
799 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
801 return M_BITMAPDATA
->m_hBitmap
;
804 PicHandle
wxBitmap::GetPict() const
806 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
808 PicHandle picture
; // This is the returned picture
810 // If bitmap already in Pict format return pointer
811 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
812 return M_BITMAPDATA
->m_hPict
;
814 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
819 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
820 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
821 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
827 GetPortBounds( GetHBITMAP() , &portRect
) ;
828 int width
= portRect
.right
- portRect
.left
;
829 int height
= portRect
.bottom
- portRect
.top
;
831 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
832 GetGWorld( &origPort
, &origDev
) ;
836 SetGWorld( GetHBITMAP() , NULL
) ;
838 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
846 RGBColor trans
= white
;
848 RGBBackColor( &gray
);
849 EraseRect( &portRect
);
850 RGBColor trans
= gray
;
852 RGBForeColor( &black
) ;
853 RGBBackColor( &white
) ;
854 PenMode(transparent
);
856 for ( int y
= 0 ; y
< height
; ++y
)
858 for( int x
= 0 ; x
< width
; ++x
)
860 if ( !mask
->PointMasked(x
,y
) )
864 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
865 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
868 // With transparency this sets a blank pixel
869 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
876 RGBBackColor( &gray
) ;
877 EraseRect(&portRect
);
878 RGBForeColor( &black
) ;
879 RGBBackColor( &white
) ;
881 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
882 // src PixMap - we copy image over itself -
883 GetPortBitMapForCopyBits(GetHBITMAP()),
884 // dst PixMap - no drawing occurs
885 &portRect
, // srcRect - it will be recorded and compressed -
886 &portRect
, // dstRect - into the picture that is open -
887 srcCopy
,NULL
); // copyMode and no clip region
889 ClosePicture(); // We are done recording the picture
890 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
891 SetGWorld( origPort
, origDev
) ;
893 return picture
; // return our groovy pict handle
896 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
898 sm_handlers
.Append(handler
);
901 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
903 sm_handlers
.Insert(handler
);
906 bool wxBitmap::RemoveHandler(const wxString
& name
)
908 wxBitmapHandler
*handler
= FindHandler(name
);
911 sm_handlers
.DeleteObject(handler
);
918 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
920 wxNode
*node
= sm_handlers
.First();
923 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
924 if ( handler
->GetName() == name
)
931 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, wxBitmapType type
)
933 wxNode
*node
= sm_handlers
.First();
936 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
937 if ( handler
->GetExtension() == extension
&&
938 (type
== -1 || handler
->GetType() == type
) )
945 wxBitmapHandler
*wxBitmap::FindHandler(wxBitmapType type
)
947 wxNode
*node
= sm_handlers
.First();
950 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
951 if (handler
->GetType() == type
)
967 // Construct a mask from a bitmap and a colour indicating
968 // the transparent area
969 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
972 Create(bitmap
, colour
);
975 // Construct a mask from a bitmap and a palette index indicating
976 // the transparent area
977 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
980 Create(bitmap
, paletteIndex
);
983 // Construct a mask from a mono bitmap (copies the bitmap).
984 wxMask::wxMask(const wxBitmap
& bitmap
)
994 wxMacDestroyGWorld( m_maskBitmap
) ;
995 m_maskBitmap
= NULL
;
999 // Create a mask from a mono bitmap (copies the bitmap).
1000 bool wxMask::Create(const wxBitmap
& bitmap
)
1004 wxMacDestroyGWorld( m_maskBitmap
) ;
1005 m_maskBitmap
= NULL
;
1007 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1008 wxT("Cannot create mask from this bitmap type (TODO)"));
1009 // other types would require a temporary bitmap. not yet implemented
1011 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1013 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1014 wxT("Cannot create mask from colour bitmap"));
1016 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1017 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1019 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1020 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1021 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1022 GetPortBitMapForCopyBits(m_maskBitmap
),
1023 &rect
, &rect
, srcCopy
, 0);
1024 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1025 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1030 // Create a mask from a bitmap and a palette index indicating
1031 // the transparent area
1032 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1035 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1039 // Create a mask from a bitmap and a colour indicating
1040 // the transparent area
1041 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1045 wxMacDestroyGWorld( m_maskBitmap
) ;
1046 m_maskBitmap
= NULL
;
1048 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1049 wxT("Cannot create mask from this bitmap type (TODO)"));
1050 // other types would require a temporary bitmap. not yet implemented
1052 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1054 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1055 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1056 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1057 RGBColor maskColor
= colour
.GetPixel();
1059 // this is not very efficient, but I can't think
1060 // of a better way of doing it
1062 GDHandle origDevice
;
1064 RGBColor colors
[2] = {
1065 { 0xFFFF, 0xFFFF, 0xFFFF },
1068 GetGWorld( &origPort
, &origDevice
) ;
1069 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1071 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1073 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1074 GetCPixel( w
, h
, &col
) ;
1075 SetGWorld( m_maskBitmap
, NULL
) ;
1076 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1078 SetCPixel( w
, h
, &colors
[0] ) ;
1082 SetCPixel( w
, h
, &colors
[1] ) ;
1086 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1087 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1088 SetGWorld( origPort
, origDevice
) ;
1093 bool wxMask::PointMasked(int x
, int y
)
1096 GDHandle origDevice
;
1100 GetGWorld( &origPort
, &origDevice
);
1102 //Set port to mask and see if it masked (1) or not ( 0 )
1103 SetGWorld(m_maskBitmap
, NULL
);
1104 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1105 GetCPixel(x
,y
, &color
);
1106 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1107 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1109 SetGWorld( origPort
, origDevice
);
1118 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1120 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1125 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1126 int desiredWidth
, int desiredHeight
)
1131 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1140 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1142 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1144 inline wxPICTResourceHandler()
1146 m_name
= "Macintosh Pict resource";
1148 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1151 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1152 int desiredWidth
, int desiredHeight
);
1154 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1156 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1157 int desiredWidth
, int desiredHeight
)
1162 c2pstrcpy( (StringPtr
) theName
, name
) ;
1164 strcpy( (char *) theName
, name
) ;
1165 c2pstr( (char *)theName
) ;
1168 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1173 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1174 DetachResource( (Handle
) thePict
) ;
1175 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1176 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1177 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1178 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1180 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1181 M_BITMAPHANDLERDATA
->m_ok
= true ;
1182 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1183 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1184 // M_BITMAPHANDLERDATA->m_quality;
1190 #if 0 // The following is an example for creating a bitmap handler
1192 // TODO: bitmap handlers, a bit like this:
1193 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1195 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1197 inline wxBMPResourceHandler()
1199 m_name
= "Windows bitmap resource";
1201 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1204 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1205 int desiredWidth
, int desiredHeight
);
1207 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1211 void wxBitmap::CleanUpHandlers()
1213 wxNode
*node
= sm_handlers
.First();
1216 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1217 wxNode
*next
= node
->Next();
1224 void wxBitmap::InitStandardHandlers()
1226 AddHandler(new wxPICTResourceHandler
) ;
1227 AddHandler(new wxICONResourceHandler
) ;