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
)
32 #include <ApplicationServices/ApplicationServices.h>
34 #include <PictUtils.h>
37 #include "wx/mac/uma.h"
39 CTabHandle
wxMacCreateColorTable( int numColors
)
41 CTabHandle newColors
; /* Handle to the new color table */
43 /* Allocate memory for the color table */
44 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
45 sizeof (ColorSpec
) * (numColors
- 1) );
48 /* Initialize the fields */
49 (**newColors
).ctSeed
= GetCTSeed();
50 (**newColors
).ctFlags
= 0;
51 (**newColors
).ctSize
= numColors
- 1;
52 /* Initialize the table of colors */
57 void wxMacDestroyColorTable( CTabHandle colors
)
59 DisposeHandle( (Handle
) colors
) ;
62 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
64 (**newColors
).ctTable
[index
].value
= index
;
65 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
66 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
67 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
70 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
74 Rect rect
= { 0 , 0 , height
, width
} ;
78 depth
= wxDisplayDepth() ;
81 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
89 void wxMacDestroyGWorld( GWorldPtr gw
)
95 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
100 PicHandle pict
; // this is the Picture we give back
102 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
103 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
104 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
106 unsigned char *maskimage
= NULL
;
108 GetPortBounds( wp
, &portRect
) ;
109 int width
= portRect
.right
- portRect
.left
;
110 int height
= portRect
.bottom
- portRect
.top
;
112 LockPixels( GetGWorldPixMap( wp
) ) ;
113 GetGWorld( &origPort
, &origDev
) ;
117 maskimage
= (unsigned char*) malloc( width
* height
) ;
118 SetGWorld( mask
, NULL
) ;
119 LockPixels( GetGWorldPixMap( mask
) ) ;
120 for ( int y
= 0 ; y
< height
; y
++ )
122 for( int x
= 0 ; x
< width
; x
++ )
126 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
127 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
130 UnlockPixels( GetGWorldPixMap( mask
) ) ;
133 SetGWorld( wp
, NULL
) ;
135 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
141 RGBForeColor( &black
) ;
142 RGBBackColor( &white
) ;
143 PenMode(transparent
);
145 for ( int y
= 0 ; y
< height
; ++y
)
147 for( int x
= 0 ; x
< width
; ++x
)
149 if ( maskimage
[y
*width
+ x
] )
153 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
154 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
157 // With transparency set this sets a blank pixel not a white one
158 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &white
);
167 RGBBackColor( &gray
) ;
168 EraseRect(&portRect
);
169 RGBForeColor( &black
) ;
170 RGBBackColor( &white
) ;
172 CopyBits(GetPortBitMapForCopyBits(wp
), /* src PixMap - we copy image over
174 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs
175 &portRect
, // srcRect - it will be recorded and compressed -
176 &portRect
, // dstRect - into the picture that is open -
177 srcCopy
,NULL
); // copyMode and no clip region
179 ClosePicture(); // We are done recording the picture
180 UnlockPixels( GetGWorldPixMap( wp
) ) ;
181 SetGWorld( origPort
, origDev
) ;
183 return pict
; // return our groovy pict handle
186 wxBitmapRefData::wxBitmapRefData()
198 m_bitmapType
= kMacBitmapTypeUnknownType
;
201 wxBitmapRefData::~wxBitmapRefData()
203 switch (m_bitmapType
)
205 case kMacBitmapTypePict
:
209 KillPicture( m_hPict
) ;
214 case kMacBitmapTypeGrafWorld
:
218 wxMacDestroyGWorld( m_hBitmap
) ;
223 case kMacBitmapTypeIcon
:
226 DisposeCIcon( m_hIcon
) ;
242 wxList
wxBitmap::sm_handlers
;
248 if ( wxTheBitmapList
)
249 wxTheBitmapList
->AddBitmap(this);
252 wxBitmap::~wxBitmap()
255 wxTheBitmapList
->DeleteObject(this);
258 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
260 m_refData
= new wxBitmapRefData
;
262 M_BITMAPDATA
->m_width
= the_width
;
263 M_BITMAPDATA
->m_height
= the_height
;
264 M_BITMAPDATA
->m_depth
= no_bits
;
265 M_BITMAPDATA
->m_numColors
= 0;
268 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
269 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
270 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
273 GDHandle origDevice
;
275 GetGWorld( &origPort
, &origDevice
) ;
276 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
277 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
279 // bits is a char array
281 unsigned char* linestart
= (unsigned char*) bits
;
282 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
283 if ( the_width
% (sizeof(unsigned char) * 8) ) {
284 linesize
+= sizeof(unsigned char);
287 RGBColor colors
[2] = {
288 { 0xFFFF , 0xFFFF , 0xFFFF } ,
292 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
294 for ( int x
= 0 ; x
< the_width
; ++x
)
298 int mask
= 1 << bit
;
299 if ( linestart
[index
] & mask
)
301 SetCPixel( x
, y
, &colors
[1] ) ;
305 SetCPixel( x
, y
, &colors
[0] ) ;
309 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
311 SetGWorld( origPort
, origDevice
) ;
315 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
318 if ( wxTheBitmapList
) {
319 wxTheBitmapList
->AddBitmap(this);
323 wxBitmap::wxBitmap(int w
, int h
, int d
)
325 (void)Create(w
, h
, d
);
327 if ( wxTheBitmapList
)
328 wxTheBitmapList
->AddBitmap(this);
331 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
333 (void) Create(data
, type
, width
, height
, depth
);
335 if ( wxTheBitmapList
)
336 wxTheBitmapList
->AddBitmap(this);
339 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
341 LoadFile(filename
, type
);
343 if ( wxTheBitmapList
)
344 wxTheBitmapList
->AddBitmap(this);
347 bool wxBitmap::CreateFromXpm(const char **bits
)
349 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
350 wxXPMDecoder decoder
;
351 wxImage img
= decoder
.ReadData(bits
);
352 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
353 *this = wxBitmap(img
);
354 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
358 wxBitmap::wxBitmap(const char **bits
)
360 (void) CreateFromXpm(bits
);
363 wxBitmap::wxBitmap(char **bits
)
365 (void) CreateFromXpm((const char **)bits
);
368 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
371 (rect
.x
>= 0) && (rect
.y
>= 0) &&
372 (rect
.x
+rect
.width
<= GetWidth()) &&
373 (rect
.y
+rect
.height
<= GetHeight()),
374 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
377 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
378 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
383 GetGWorld( &origPort
, &origDevice
);
385 // Update the subbitmaps reference data
386 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
388 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
389 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
390 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
392 // Copy sub region of this bitmap
393 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
395 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
397 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
402 WXHBITMAP submask
, mask
;
405 mask
= GetMask()->GetMaskBitmap();
406 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
407 LockPixels(GetGWorldPixMap(mask
));
408 LockPixels(GetGWorldPixMap(submask
));
410 for(int yy
= 0; yy
< rect
.height
; yy
++)
412 for(int xx
= 0; xx
< rect
.width
; xx
++)
414 SetGWorld(mask
, NULL
);
415 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
416 SetGWorld(submask
, NULL
);
417 SetCPixel(xx
,yy
, &color
);
420 UnlockPixels(GetGWorldPixMap(mask
));
421 UnlockPixels(GetGWorldPixMap(submask
));
422 ref
->m_bitmapMask
= new wxMask
;
423 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
429 WXHBITMAP subbitmap
, bitmap
;
432 bitmap
= GetHBITMAP();
433 subbitmap
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetDepth());
434 LockPixels(GetGWorldPixMap(bitmap
));
435 LockPixels(GetGWorldPixMap(subbitmap
));
437 for(int yy
= 0; yy
< rect
.height
; yy
++)
439 for(int xx
= 0; xx
< rect
.width
; xx
++)
441 SetGWorld(bitmap
, NULL
);
442 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
443 SetGWorld(subbitmap
, NULL
);
444 SetCPixel(xx
, yy
, &color
);
447 UnlockPixels(GetGWorldPixMap(bitmap
));
448 UnlockPixels(GetGWorldPixMap(subbitmap
));
449 ret
.SetHBITMAP(subbitmap
);
452 SetGWorld( origPort
, origDevice
);
457 bool wxBitmap::Create(int w
, int h
, int d
)
461 m_refData
= new wxBitmapRefData
;
463 M_BITMAPDATA
->m_width
= w
;
464 M_BITMAPDATA
->m_height
= h
;
465 M_BITMAPDATA
->m_depth
= d
;
467 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
468 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
469 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
470 return M_BITMAPDATA
->m_ok
;
473 int wxBitmap::GetBitmapType() const
475 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
477 return M_BITMAPDATA
->m_bitmapType
;
480 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
482 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
483 M_BITMAPDATA
->m_hBitmap
= bmp
;
484 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
487 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
491 wxBitmapHandler
*handler
= FindHandler(type
);
495 m_refData
= new wxBitmapRefData
;
497 return handler
->LoadFile(this, filename
, type
, -1, -1);
501 wxImage
loadimage(filename
, type
);
502 if (loadimage
.Ok()) {
507 wxLogWarning("no bitmap handler for type %d defined.", type
);
511 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
515 m_refData
= new wxBitmapRefData
;
517 wxBitmapHandler
*handler
= FindHandler(type
);
519 if ( handler
== NULL
) {
520 wxLogWarning("no bitmap handler for type %d defined.", type
);
525 return handler
->Create(this, data
, type
, width
, height
, depth
);
528 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
530 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
531 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
533 m_refData
= new wxBitmapRefData();
535 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
537 // width and height of the device-dependent bitmap
538 int width
= image
.GetWidth();
539 int height
= image
.GetHeight();
543 Create( width
, height
, wxDisplayDepth() ) ;
544 wxBitmap
maskBitmap( width
, height
, 1);
547 GDHandle origDevice
;
549 LockPixels( GetGWorldPixMap(GetHBITMAP()) );
550 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
552 GetGWorld( &origPort
, &origDevice
) ;
553 SetGWorld( GetHBITMAP() , NULL
) ;
556 wxColour rgb
, maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
558 RGBColor white
= { 0xffff, 0xffff, 0xffff };
559 RGBColor black
= { 0 , 0 , 0 };
561 register unsigned char* data
= image
.GetData();
564 for (int y
= 0; y
< height
; y
++)
566 for (int x
= 0; x
< width
; x
++)
568 rgb
.Set(data
[index
++], data
[index
++], data
[index
++]);
569 color
= rgb
.GetPixel();
570 SetCPixel( x
, y
, &color
) ;
573 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
574 if (rgb
== maskcolor
) {
575 SetCPixel(x
,y
, &white
);
578 SetCPixel(x
,y
, &black
);
580 SetGWorld(GetHBITMAP(), NULL
);
586 if ( image
.HasMask() ) {
587 SetMask(new wxMask( maskBitmap
));
590 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
591 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
592 SetGWorld( origPort
, origDevice
);
595 wxImage
wxBitmap::ConvertToImage() const
599 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
601 // create an wxImage object
602 int width
= GetWidth();
603 int height
= GetHeight();
604 image
.Create( width
, height
);
606 unsigned char *data
= image
.GetData();
608 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
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;
617 wxMask
*mask
= GetMask();
619 GetGWorld( &origPort
, &origDevice
);
620 LockPixels(GetGWorldPixMap(GetHBITMAP()));
621 SetGWorld( GetHBITMAP(), NULL
);
623 // Copy data into image
625 for (int yy
= 0; yy
< height
; yy
++)
627 for (int xx
= 0; xx
< width
; xx
++)
629 GetCPixel(xx
,yy
, &color
);
630 r
= ((color
.red
) >> 8);
631 g
= ((color
.green
) >> 8);
632 b
= ((color
.blue
) >> 8);
638 if (mask
->PointMasked(xx
,yy
))
640 data
[index
] = mask_r
;
641 data
[index
+ 1] = mask_g
;
642 data
[index
+ 2] = mask_b
;
650 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
651 image
.SetMask( true );
655 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
656 SetGWorld(origPort
, origDevice
);
662 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
663 const wxPalette
*palette
) const
665 wxBitmapHandler
*handler
= FindHandler(type
);
669 return handler
->SaveFile(this, filename
, type
, palette
);
673 wxImage image
= ConvertToImage();
675 return image
.SaveFile(filename
, type
);
678 wxLogWarning("no bitmap handler for type %d defined.", type
);
682 bool wxBitmap::Ok() const
684 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
687 int wxBitmap::GetHeight() const
689 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
691 return M_BITMAPDATA
->m_height
;
694 int wxBitmap::GetWidth() const
696 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
698 return M_BITMAPDATA
->m_width
;
701 int wxBitmap::GetDepth() const
703 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
705 return M_BITMAPDATA
->m_depth
;
708 int wxBitmap::GetQuality() const
710 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
712 return M_BITMAPDATA
->m_quality
;
715 wxMask
*wxBitmap::GetMask() const
717 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
719 return M_BITMAPDATA
->m_bitmapMask
;
722 void wxBitmap::SetWidth(int w
)
725 m_refData
= new wxBitmapRefData
;
727 M_BITMAPDATA
->m_width
= w
;
730 void wxBitmap::SetHeight(int h
)
733 m_refData
= new wxBitmapRefData
;
735 M_BITMAPDATA
->m_height
= h
;
738 void wxBitmap::SetDepth(int d
)
741 m_refData
= new wxBitmapRefData
;
743 M_BITMAPDATA
->m_depth
= d
;
746 void wxBitmap::SetQuality(int q
)
749 m_refData
= new wxBitmapRefData
;
751 M_BITMAPDATA
->m_quality
= q
;
754 void wxBitmap::SetOk(bool isOk
)
757 m_refData
= new wxBitmapRefData
;
759 M_BITMAPDATA
->m_ok
= isOk
;
762 wxPalette
*wxBitmap::GetPalette() const
764 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
766 return &M_BITMAPDATA
->m_bitmapPalette
;
769 void wxBitmap::SetPalette(const wxPalette
& palette
)
772 m_refData
= new wxBitmapRefData
;
774 M_BITMAPDATA
->m_bitmapPalette
= palette
;
777 void wxBitmap::SetMask(wxMask
*mask
)
780 m_refData
= new wxBitmapRefData
;
782 // Remove existing mask if there is one.
783 if (M_BITMAPDATA
->m_bitmapMask
)
784 delete M_BITMAPDATA
->m_bitmapMask
;
786 M_BITMAPDATA
->m_bitmapMask
= mask
;
789 WXHBITMAP
wxBitmap::GetHBITMAP() const
791 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
793 return M_BITMAPDATA
->m_hBitmap
;
796 PicHandle
wxBitmap::GetPict() const
798 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
800 PicHandle picture
; // This is the returned picture
802 // If bitmap already in Pict format return pointer
803 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
804 return M_BITMAPDATA
->m_hPict
;
806 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
811 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
812 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
813 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
819 GetPortBounds( GetHBITMAP() , &portRect
) ;
820 int width
= portRect
.right
- portRect
.left
;
821 int height
= portRect
.bottom
- portRect
.top
;
823 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
824 GetGWorld( &origPort
, &origDev
) ;
828 SetGWorld( GetHBITMAP() , NULL
) ;
830 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
838 RGBColor trans
= white
;
840 RGBBackColor( &gray
);
841 EraseRect( &portRect
);
842 RGBColor trans
= gray
;
844 RGBForeColor( &black
) ;
845 RGBBackColor( &white
) ;
846 PenMode(transparent
);
848 for ( int y
= 0 ; y
< height
; ++y
)
850 for( int x
= 0 ; x
< width
; ++x
)
852 if ( !mask
->PointMasked(x
,y
) )
856 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
857 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
860 // With transparency this sets a blank pixel
861 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
868 RGBBackColor( &gray
) ;
869 EraseRect(&portRect
);
870 RGBForeColor( &black
) ;
871 RGBBackColor( &white
) ;
873 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
874 // src PixMap - we copy image over itself -
875 GetPortBitMapForCopyBits(GetHBITMAP()),
876 // dst PixMap - no drawing occurs
877 &portRect
, // srcRect - it will be recorded and compressed -
878 &portRect
, // dstRect - into the picture that is open -
879 srcCopy
,NULL
); // copyMode and no clip region
881 ClosePicture(); // We are done recording the picture
882 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
883 SetGWorld( origPort
, origDev
) ;
885 return picture
; // return our groovy pict handle
888 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
890 sm_handlers
.Append(handler
);
893 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
895 sm_handlers
.Insert(handler
);
898 bool wxBitmap::RemoveHandler(const wxString
& name
)
900 wxBitmapHandler
*handler
= FindHandler(name
);
903 sm_handlers
.DeleteObject(handler
);
910 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
912 wxNode
*node
= sm_handlers
.First();
915 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
916 if ( handler
->GetName() == name
)
923 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, wxBitmapType type
)
925 wxNode
*node
= sm_handlers
.First();
928 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
929 if ( handler
->GetExtension() == extension
&&
930 (type
== -1 || handler
->GetType() == type
) )
937 wxBitmapHandler
*wxBitmap::FindHandler(wxBitmapType type
)
939 wxNode
*node
= sm_handlers
.First();
942 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
943 if (handler
->GetType() == type
)
959 // Construct a mask from a bitmap and a colour indicating
960 // the transparent area
961 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
964 Create(bitmap
, colour
);
967 // Construct a mask from a bitmap and a palette index indicating
968 // the transparent area
969 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
972 Create(bitmap
, paletteIndex
);
975 // Construct a mask from a mono bitmap (copies the bitmap).
976 wxMask::wxMask(const wxBitmap
& bitmap
)
986 wxMacDestroyGWorld( m_maskBitmap
) ;
987 m_maskBitmap
= NULL
;
991 // Create a mask from a mono bitmap (copies the bitmap).
992 bool wxMask::Create(const wxBitmap
& bitmap
)
996 wxMacDestroyGWorld( m_maskBitmap
) ;
997 m_maskBitmap
= NULL
;
999 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1000 wxT("Cannot create mask from this bitmap type (TODO)"));
1001 // other types would require a temporary bitmap. not yet implemented
1003 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1005 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1006 wxT("Cannot create mask from colour bitmap"));
1008 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1009 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1011 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1012 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1013 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1014 GetPortBitMapForCopyBits(m_maskBitmap
),
1015 &rect
, &rect
, srcCopy
, 0);
1016 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1017 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1022 // Create a mask from a bitmap and a palette index indicating
1023 // the transparent area
1024 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1027 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1031 // Create a mask from a bitmap and a colour indicating
1032 // the transparent area
1033 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1037 wxMacDestroyGWorld( m_maskBitmap
) ;
1038 m_maskBitmap
= NULL
;
1040 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1041 wxT("Cannot create mask from this bitmap type (TODO)"));
1042 // other types would require a temporary bitmap. not yet implemented
1044 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1046 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1047 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1048 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1049 RGBColor maskColor
= colour
.GetPixel();
1051 // this is not very efficient, but I can't think
1052 // of a better way of doing it
1054 GDHandle origDevice
;
1056 RGBColor colors
[2] = {
1057 { 0xFFFF, 0xFFFF, 0xFFFF },
1060 GetGWorld( &origPort
, &origDevice
) ;
1061 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1063 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1065 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1066 GetCPixel( w
, h
, &col
) ;
1067 SetGWorld( m_maskBitmap
, NULL
) ;
1068 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1070 SetCPixel( w
, h
, &colors
[0] ) ;
1074 SetCPixel( w
, h
, &colors
[1] ) ;
1078 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1079 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1080 SetGWorld( origPort
, origDevice
) ;
1085 bool wxMask::PointMasked(int x
, int y
)
1088 GDHandle origDevice
;
1092 GetGWorld( &origPort
, &origDevice
);
1094 //Set port to mask and see if it masked (1) or not ( 0 )
1095 SetGWorld(m_maskBitmap
, NULL
);
1096 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1097 GetCPixel(x
,y
, &color
);
1098 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1099 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1101 SetGWorld( origPort
, origDevice
);
1110 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1112 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1117 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1118 int desiredWidth
, int desiredHeight
)
1123 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1132 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1134 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1136 inline wxPICTResourceHandler()
1138 m_name
= "Macintosh Pict resource";
1140 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1143 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1144 int desiredWidth
, int desiredHeight
);
1146 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1148 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1149 int desiredWidth
, int desiredHeight
)
1154 c2pstrcpy( (StringPtr
) theName
, name
) ;
1156 strcpy( (char *) theName
, name
) ;
1157 c2pstr( (char *)theName
) ;
1160 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1165 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1166 DetachResource( (Handle
) thePict
) ;
1167 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1168 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1169 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1170 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1172 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1173 M_BITMAPHANDLERDATA
->m_ok
= true ;
1174 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1175 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1176 // M_BITMAPHANDLERDATA->m_quality;
1182 #if 0 // The following is an example for creating a bitmap handler
1184 // TODO: bitmap handlers, a bit like this:
1185 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1187 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1189 inline wxBMPResourceHandler()
1191 m_name
= "Windows bitmap resource";
1193 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1196 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1197 int desiredWidth
, int desiredHeight
);
1199 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1203 void wxBitmap::CleanUpHandlers()
1205 wxNode
*node
= sm_handlers
.First();
1208 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1209 wxNode
*next
= node
->Next();
1216 void wxBitmap::InitStandardHandlers()
1218 AddHandler(new wxPICTResourceHandler
) ;
1219 AddHandler(new wxICONResourceHandler
) ;