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"
34 #if !USE_SHARED_LIBRARIES
35 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
36 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
40 #include <ApplicationServices/ApplicationServices.h>
42 #include <PictUtils.h>
45 #include "wx/mac/uma.h"
47 CTabHandle
wxMacCreateColorTable( int numColors
)
49 CTabHandle newColors
; /* Handle to the new color table */
51 /* Allocate memory for the color table */
52 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
53 sizeof (ColorSpec
) * (numColors
- 1) );
56 /* Initialize the fields */
57 (**newColors
).ctSeed
= GetCTSeed();
58 (**newColors
).ctFlags
= 0;
59 (**newColors
).ctSize
= numColors
- 1;
60 /* Initialize the table of colors */
65 void wxMacDestroyColorTable( CTabHandle colors
)
67 DisposeHandle( (Handle
) colors
) ;
70 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
72 (**newColors
).ctTable
[index
].value
= index
;
73 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
74 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
75 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
78 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
82 Rect rect
= { 0 , 0 , height
, width
} ;
86 depth
= wxDisplayDepth() ;
89 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
97 void wxMacDestroyGWorld( GWorldPtr gw
)
100 DisposeGWorld( gw
) ;
103 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
108 PicHandle pict
; // this is the Picture we give back
110 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
111 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
112 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
114 unsigned char *maskimage
= NULL
;
116 GetPortBounds( wp
, &portRect
) ;
117 int width
= portRect
.right
- portRect
.left
;
118 int height
= portRect
.bottom
- portRect
.top
;
120 LockPixels( GetGWorldPixMap( wp
) ) ;
121 GetGWorld( &origPort
, &origDev
) ;
125 maskimage
= (unsigned char*) malloc( width
* height
) ;
126 SetGWorld( mask
, NULL
) ;
127 LockPixels( GetGWorldPixMap( mask
) ) ;
128 for ( int y
= 0 ; y
< height
; y
++ )
130 for( int x
= 0 ; x
< width
; x
++ )
134 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
135 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
138 UnlockPixels( GetGWorldPixMap( mask
) ) ;
141 SetGWorld( wp
, NULL
) ;
143 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
149 RGBForeColor( &black
) ;
150 RGBBackColor( &white
) ;
151 PenMode(transparent
);
153 for ( int y
= 0 ; y
< height
; ++y
)
155 for( int x
= 0 ; x
< width
; ++x
)
157 if ( maskimage
[y
*width
+ x
] )
161 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
162 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
165 // With transparency set this sets a blank pixel not a white one
166 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &white
);
175 RGBBackColor( &gray
) ;
176 EraseRect(&portRect
);
177 RGBForeColor( &black
) ;
178 RGBBackColor( &white
) ;
180 CopyBits(GetPortBitMapForCopyBits(wp
), /* src PixMap - we copy image over
182 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs
183 &portRect
, // srcRect - it will be recorded and compressed -
184 &portRect
, // dstRect - into the picture that is open -
185 srcCopy
,NULL
); // copyMode and no clip region
187 ClosePicture(); // We are done recording the picture
188 UnlockPixels( GetGWorldPixMap( wp
) ) ;
189 SetGWorld( origPort
, origDev
) ;
191 return pict
; // return our groovy pict handle
194 wxBitmapRefData::wxBitmapRefData()
205 m_bitmapType
= kMacBitmapTypeUnknownType
;
208 wxBitmapRefData::~wxBitmapRefData()
210 switch (m_bitmapType
)
212 case kMacBitmapTypePict
:
216 KillPicture( m_hPict
) ;
221 case kMacBitmapTypeGrafWorld
:
225 wxMacDestroyGWorld( m_hBitmap
) ;
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
) ) ;
280 // bits is a word aligned array?? Don't think so
281 // bits is a char array on MAC OS X however using the benefit of the
282 // doubt I replaced references to 16 with sizeof(unsigned char)*8
283 unsigned char* linestart
= (unsigned char*) bits
;
284 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
285 if ( the_width
% (sizeof(unsigned char) * 8) ) {
286 linesize
+= sizeof(unsigned char);
289 // bits is a word aligned array
291 unsigned char* linestart
= (unsigned char*) bits
;
292 int linesize
= ( the_width
/ 16 ) * 2 ;
293 if ( the_width
% 16 )
299 RGBColor colors
[2] = {
300 { 0xFFFF , 0xFFFF , 0xFFFF } ,
304 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
306 for ( int x
= 0 ; x
< the_width
; ++x
)
310 int mask
= 1 << bit
;
311 if ( linestart
[index
] & mask
)
313 SetCPixel( x
, y
, &colors
[1] ) ;
317 SetCPixel( x
, y
, &colors
[0] ) ;
322 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
324 SetGWorld( origPort
, origDevice
) ;
328 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
331 if ( wxTheBitmapList
)
332 wxTheBitmapList
->AddBitmap(this);
335 wxBitmap::wxBitmap(int w
, int h
, int d
)
337 (void)Create(w
, h
, d
);
339 if ( wxTheBitmapList
)
340 wxTheBitmapList
->AddBitmap(this);
343 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
345 (void) Create(data
, type
, width
, height
, depth
);
347 if ( wxTheBitmapList
)
348 wxTheBitmapList
->AddBitmap(this);
351 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
353 LoadFile(filename
, (int)type
);
355 if ( wxTheBitmapList
)
356 wxTheBitmapList
->AddBitmap(this);
359 wxBitmap::wxBitmap(const char **data
)
361 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
364 wxBitmap::wxBitmap(char **data
)
366 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
369 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
372 (rect
.x
>= 0) && (rect
.y
>= 0) &&
373 (rect
.x
+rect
.width
<= GetWidth()) &&
374 (rect
.y
+rect
.height
<= GetHeight()),
375 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
378 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
379 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
384 GetGWorld( &origPort
, &origDevice
);
386 // Update the subbitmaps reference data
387 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
389 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
390 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
391 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
393 // Copy sub region of this bitmap
394 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
396 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
398 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
403 WXHBITMAP submask
, mask
;
406 mask
= GetMask()->GetMaskBitmap();
407 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
408 LockPixels(GetGWorldPixMap(mask
));
409 LockPixels(GetGWorldPixMap(submask
));
411 for(int yy
= 0; yy
< rect
.height
; yy
++)
413 for(int xx
= 0; xx
< rect
.width
; xx
++)
415 SetGWorld(mask
, NULL
);
416 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
417 SetGWorld(submask
, NULL
);
418 SetCPixel(xx
,yy
, &color
);
421 UnlockPixels(GetGWorldPixMap(mask
));
422 UnlockPixels(GetGWorldPixMap(submask
));
423 ref
->m_bitmapMask
= new wxMask
;
424 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
430 WXHBITMAP subbitmap
, bitmap
;
433 bitmap
= GetHBITMAP();
434 subbitmap
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetDepth());
435 LockPixels(GetGWorldPixMap(bitmap
));
436 LockPixels(GetGWorldPixMap(subbitmap
));
438 for(int yy
= 0; yy
< rect
.height
; yy
++)
440 for(int xx
= 0; xx
< rect
.width
; xx
++)
442 SetGWorld(bitmap
, NULL
);
443 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
444 SetGWorld(subbitmap
, NULL
);
445 SetCPixel(xx
, yy
, &color
);
448 UnlockPixels(GetGWorldPixMap(bitmap
));
449 UnlockPixels(GetGWorldPixMap(subbitmap
));
450 ret
.SetHBITMAP(subbitmap
);
453 SetGWorld( origPort
, origDevice
);
458 bool wxBitmap::Create(int w
, int h
, int d
)
462 m_refData
= new wxBitmapRefData
;
464 M_BITMAPDATA
->m_width
= w
;
465 M_BITMAPDATA
->m_height
= h
;
466 M_BITMAPDATA
->m_depth
= d
;
468 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
469 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
470 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
471 return M_BITMAPDATA
->m_ok
;
474 int wxBitmap::GetBitmapType() const
476 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
478 return M_BITMAPDATA
->m_bitmapType
;
481 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
483 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
484 M_BITMAPDATA
->m_hBitmap
= bmp
;
485 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
488 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
492 m_refData
= new wxBitmapRefData
;
494 wxBitmapHandler
*handler
= FindHandler(type
);
496 if ( handler
== NULL
) {
497 wxLogWarning("no bitmap handler for type %d defined.", type
);
502 return handler
->LoadFile(this, filename
, type
, -1, -1);
505 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
509 m_refData
= new wxBitmapRefData
;
511 wxBitmapHandler
*handler
= FindHandler(type
);
513 if ( handler
== NULL
) {
514 wxLogWarning("no bitmap handler for type %d defined.", type
);
519 return handler
->Create(this, data
, type
, width
, height
, depth
);
522 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
524 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
525 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
527 m_refData
= new wxBitmapRefData();
529 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
531 // width and height of the device-dependent bitmap
532 int width
= image
.GetWidth();
533 int height
= image
.GetHeight();
537 Create( width
, height
, wxDisplayDepth() ) ;
538 wxBitmap
maskBitmap( width
, height
, 1);
541 GDHandle origDevice
;
543 LockPixels( GetGWorldPixMap(GetHBITMAP()) );
544 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
546 GetGWorld( &origPort
, &origDevice
) ;
547 SetGWorld( GetHBITMAP() , NULL
) ;
550 wxColour rgb
, maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
552 RGBColor white
= { 0xffff, 0xffff, 0xffff };
553 RGBColor black
= { 0 , 0 , 0 };
555 register unsigned char* data
= image
.GetData();
558 for (int y
= 0; y
< height
; y
++)
560 for (int x
= 0; x
< width
; x
++)
562 rgb
.Set(data
[index
++], data
[index
++], data
[index
++]);
563 color
= rgb
.GetPixel();
564 SetCPixel( x
, y
, &color
) ;
567 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
568 if (rgb
== maskcolor
) {
569 SetCPixel(x
,y
, &white
);
572 SetCPixel(x
,y
, &black
);
574 SetGWorld(GetHBITMAP(), NULL
);
580 if ( image
.HasMask() ) {
581 wxMask
*mask
= new wxMask( maskBitmap
);
584 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
585 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
586 SetGWorld( origPort
, origDevice
);
589 wxImage
wxBitmap::ConvertToImage() const
593 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
595 // create an wxImage object
596 int width
= GetWidth();
597 int height
= GetHeight();
598 image
.Create( width
, height
);
600 unsigned char *data
= image
.GetData();
602 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
608 // background color set to RGB(16,16,16) in consistent with wxGTK
609 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
611 wxMask
*mask
= GetMask();
613 GetGWorld( &origPort
, &origDevice
);
614 LockPixels(GetGWorldPixMap(GetHBITMAP()));
615 SetGWorld( GetHBITMAP(), NULL
);
617 // Copy data into image
619 for (int yy
= 0; yy
< height
; yy
++)
621 for (int xx
= 0; xx
< width
; xx
++)
623 GetCPixel(xx
,yy
, &color
);
624 r
= ((color
.red
) >> 8);
625 g
= ((color
.green
) >> 8);
626 b
= ((color
.blue
) >> 8);
632 if (mask
->PointMasked(xx
,yy
))
634 data
[index
] = mask_r
;
635 data
[index
+ 1] = mask_g
;
636 data
[index
+ 2] = mask_b
;
644 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
645 image
.SetMask( true );
649 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
650 SetGWorld(origPort
, origDevice
);
656 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
658 wxBitmapHandler
*handler
= FindHandler(type
);
660 if ( handler
== NULL
) {
661 wxLogWarning("no bitmap handler for type %d defined.", type
);
666 return handler
->SaveFile(this, filename
, type
, palette
);
669 bool wxBitmap::Ok() const
671 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
674 int wxBitmap::GetHeight() const
676 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
678 return M_BITMAPDATA
->m_height
;
681 int wxBitmap::GetWidth() const
683 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
685 return M_BITMAPDATA
->m_width
;
688 int wxBitmap::GetDepth() const
690 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
692 return M_BITMAPDATA
->m_depth
;
695 int wxBitmap::GetQuality() const
697 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
699 return M_BITMAPDATA
->m_quality
;
702 wxMask
*wxBitmap::GetMask() const
704 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
706 return M_BITMAPDATA
->m_bitmapMask
;
709 void wxBitmap::SetWidth(int w
)
712 m_refData
= new wxBitmapRefData
;
714 M_BITMAPDATA
->m_width
= w
;
717 void wxBitmap::SetHeight(int h
)
720 m_refData
= new wxBitmapRefData
;
722 M_BITMAPDATA
->m_height
= h
;
725 void wxBitmap::SetDepth(int d
)
728 m_refData
= new wxBitmapRefData
;
730 M_BITMAPDATA
->m_depth
= d
;
733 void wxBitmap::SetQuality(int q
)
736 m_refData
= new wxBitmapRefData
;
738 M_BITMAPDATA
->m_quality
= q
;
741 void wxBitmap::SetOk(bool isOk
)
744 m_refData
= new wxBitmapRefData
;
746 M_BITMAPDATA
->m_ok
= isOk
;
749 wxPalette
*wxBitmap::GetPalette() const
751 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
753 return &M_BITMAPDATA
->m_bitmapPalette
;
756 void wxBitmap::SetPalette(const wxPalette
& palette
)
759 m_refData
= new wxBitmapRefData
;
761 M_BITMAPDATA
->m_bitmapPalette
= palette
;
764 void wxBitmap::SetMask(wxMask
*mask
)
767 m_refData
= new wxBitmapRefData
;
769 M_BITMAPDATA
->m_bitmapMask
= mask
;
772 WXHBITMAP
wxBitmap::GetHBITMAP() const
774 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
776 return M_BITMAPDATA
->m_hBitmap
;
779 PicHandle
wxBitmap::GetPict() const
781 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
783 PicHandle picture
; // This is the returned picture
785 // If bitmap already in Pict format return pointer
786 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
787 return M_BITMAPDATA
->m_hPict
;
789 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
794 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
795 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
796 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
802 GetPortBounds( GetHBITMAP() , &portRect
) ;
803 int width
= portRect
.right
- portRect
.left
;
804 int height
= portRect
.bottom
- portRect
.top
;
806 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
807 GetGWorld( &origPort
, &origDev
) ;
811 SetGWorld( GetHBITMAP() , NULL
) ;
813 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
821 RGBColor trans
= white
;
823 RGBBackColor( &gray
);
824 EraseRect( &portRect
);
825 RGBColor trans
= gray
;
827 RGBForeColor( &black
) ;
828 RGBBackColor( &white
) ;
829 PenMode(transparent
);
831 for ( int y
= 0 ; y
< height
; ++y
)
833 for( int x
= 0 ; x
< width
; ++x
)
835 if ( !mask
->PointMasked(x
,y
) )
839 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
840 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
843 // With transparency this sets a blank pixel
844 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
851 RGBBackColor( &gray
) ;
852 EraseRect(&portRect
);
853 RGBForeColor( &black
) ;
854 RGBBackColor( &white
) ;
856 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
857 // src PixMap - we copy image over itself -
858 GetPortBitMapForCopyBits(GetHBITMAP()),
859 // dst PixMap - no drawing occurs
860 &portRect
, // srcRect - it will be recorded and compressed -
861 &portRect
, // dstRect - into the picture that is open -
862 srcCopy
,NULL
); // copyMode and no clip region
864 ClosePicture(); // We are done recording the picture
865 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
866 SetGWorld( origPort
, origDev
) ;
868 return picture
; // return our groovy pict handle
871 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
873 sm_handlers
.Append(handler
);
876 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
878 sm_handlers
.Insert(handler
);
881 bool wxBitmap::RemoveHandler(const wxString
& name
)
883 wxBitmapHandler
*handler
= FindHandler(name
);
886 sm_handlers
.DeleteObject(handler
);
893 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
895 wxNode
*node
= sm_handlers
.First();
898 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
899 if ( handler
->GetName() == name
)
906 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
908 wxNode
*node
= sm_handlers
.First();
911 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
912 if ( handler
->GetExtension() == extension
&&
913 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
920 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
922 wxNode
*node
= sm_handlers
.First();
925 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
926 if (handler
->GetType() == bitmapType
)
942 // Construct a mask from a bitmap and a colour indicating
943 // the transparent area
944 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
947 Create(bitmap
, colour
);
950 // Construct a mask from a bitmap and a palette index indicating
951 // the transparent area
952 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
955 Create(bitmap
, paletteIndex
);
958 // Construct a mask from a mono bitmap (copies the bitmap).
959 wxMask::wxMask(const wxBitmap
& bitmap
)
969 wxMacDestroyGWorld( m_maskBitmap
) ;
970 m_maskBitmap
= NULL
;
974 // Create a mask from a mono bitmap (copies the bitmap).
975 bool wxMask::Create(const wxBitmap
& bitmap
)
979 wxMacDestroyGWorld( m_maskBitmap
) ;
980 m_maskBitmap
= NULL
;
982 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
983 wxT("Cannot create mask from this bitmap type (TODO)"));
984 // other types would require a temporary bitmap. not yet implemented
986 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
988 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
989 wxT("Cannot create mask from colour bitmap"));
991 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
992 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
994 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
995 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
996 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
997 GetPortBitMapForCopyBits(m_maskBitmap
),
998 &rect
, &rect
, srcCopy
, 0);
999 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1000 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1005 // Create a mask from a bitmap and a palette index indicating
1006 // the transparent area
1007 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1010 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1014 // Create a mask from a bitmap and a colour indicating
1015 // the transparent area
1016 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1020 wxMacDestroyGWorld( m_maskBitmap
) ;
1021 m_maskBitmap
= NULL
;
1023 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1024 wxT("Cannot create mask from this bitmap type (TODO)"));
1025 // other types would require a temporary bitmap. not yet implemented
1027 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1029 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1030 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1031 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1032 RGBColor maskColor
= colour
.GetPixel();
1034 // this is not very efficient, but I can't think
1035 // of a better way of doing it
1037 GDHandle origDevice
;
1039 RGBColor colors
[2] = {
1040 { 0xFFFF, 0xFFFF, 0xFFFF },
1043 GetGWorld( &origPort
, &origDevice
) ;
1044 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1046 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1048 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1049 GetCPixel( w
, h
, &col
) ;
1050 SetGWorld( m_maskBitmap
, NULL
) ;
1051 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1053 SetCPixel( w
, h
, &colors
[0] ) ;
1057 SetCPixel( w
, h
, &colors
[1] ) ;
1061 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1062 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1063 SetGWorld( origPort
, origDevice
) ;
1068 bool wxMask::PointMasked(int x
, int y
)
1071 GDHandle origDevice
;
1075 GetGWorld( &origPort
, &origDevice
);
1077 //Set port to mask and see if it masked (1) or not ( 0 )
1078 SetGWorld(m_maskBitmap
, NULL
);
1079 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1080 GetCPixel(x
,y
, &color
);
1081 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1082 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1084 SetGWorld( origPort
, origDevice
);
1093 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1095 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1100 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
1101 int desiredWidth
, int desiredHeight
)
1106 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1115 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1117 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1119 inline wxPICTResourceHandler()
1121 m_name
= "Macintosh Pict resource";
1123 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1126 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1127 int desiredWidth
, int desiredHeight
);
1129 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1131 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1132 int desiredWidth
, int desiredHeight
)
1137 c2pstrcpy( (StringPtr
) theName
, name
) ;
1139 strcpy( (char *) theName
, name
) ;
1140 c2pstr( (char *)theName
) ;
1143 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1148 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1149 DetachResource( (Handle
) thePict
) ;
1150 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1151 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1152 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1153 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1155 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1156 M_BITMAPHANDLERDATA
->m_ok
= true ;
1157 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1158 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1159 // M_BITMAPHANDLERDATA->m_quality;
1165 /* TODO: bitmap handlers, a bit like this:
1166 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
1168 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
1170 inline wxBMPResourceHandler()
1172 m_name = "Windows bitmap resource";
1174 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
1177 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1178 int desiredWidth, int desiredHeight);
1180 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
1183 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
1185 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1187 inline wxXPMFileHandler(void)
1189 m_name
= "XPM bitmap file";
1190 m_extension
= "xpm";
1191 m_type
= wxBITMAP_TYPE_XPM
;
1194 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1195 int desiredWidth
= -1, int desiredHeight
= -1);
1196 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
1198 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1200 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1201 int desiredWidth
, int desiredHeight
)
1205 XpmAttributes xpmAttr
;
1208 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1209 dc
= CreateCompatibleDC(NULL
);
1212 xpmAttr
.valuemask
= XpmReturnPixels
;
1213 int errorStatus
= XpmReadFileToImage(&dc
, WXSTRINGCAST name
, &ximage
, (XImage
**) NULL
, &xpmAttr
);
1215 if (errorStatus
== XpmSuccess
)
1217 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
1220 GetObject((HBITMAP
)M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
1222 M_BITMAPHANDLERDATA
->m_width
= (bm
.bmWidth
);
1223 M_BITMAPHANDLERDATA
->m_height
= (bm
.bmHeight
);
1224 M_BITMAPHANDLERDATA
->m_depth
= (bm
.bmPlanes
* bm
.bmBitsPixel
);
1225 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
1226 XpmFreeAttributes(&xpmAttr
);
1229 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
1234 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1243 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1248 Visual
*visual
= NULL
;
1251 dc
= CreateCompatibleDC(NULL
);
1254 if (SelectObject(dc
, (HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
))
1257 ximage
.width
= M_BITMAPHANDLERDATA
->m_width
;
1258 ximage
.height
= M_BITMAPHANDLERDATA
->m_height
;
1259 ximage
.depth
= M_BITMAPHANDLERDATA
->m_depth
;
1260 ximage
.bitmap
= (void *)M_BITMAPHANDLERDATA
->m_hBitmap
;
1261 int errorStatus
= XpmWriteFileFromImage(&dc
, WXSTRINGCAST name
,
1262 &ximage
, (XImage
*) NULL
, (XpmAttributes
*) NULL
);
1267 if (errorStatus
== XpmSuccess
)
1271 } else return FALSE
;
1272 } else return FALSE
;
1279 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
1281 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1283 inline wxXPMDataHandler(void)
1285 m_name
= "XPM bitmap data";
1286 m_extension
= "xpm";
1287 m_type
= wxBITMAP_TYPE_XPM_DATA
;
1290 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
1292 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1294 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
1296 XImage
* ximage
= NULL
;
1297 XImage
* xshapeimage
= NULL
;
1299 XpmAttributes xpmAttr
;
1301 xpmAttr
.valuemask
= XpmReturnInfos
; // get infos back
1302 ErrorStatus
= XpmCreateImageFromData( GetMainDevice() , (char **)data
,
1303 &ximage
, &xshapeimage
, &xpmAttr
);
1305 if (ErrorStatus
== XpmSuccess
)
1307 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1308 M_BITMAPHANDLERDATA
->m_numColors
= 0;
1309 M_BITMAPHANDLERDATA
->m_hBitmap
= ximage
->gworldptr
;
1311 M_BITMAPHANDLERDATA
->m_width
= ximage
->width
;
1312 M_BITMAPHANDLERDATA
->m_height
= ximage
->height
;
1313 M_BITMAPHANDLERDATA
->m_depth
= ximage
->depth
;
1314 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
1315 XpmFreeAttributes(&xpmAttr
);
1316 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
1317 ximage
->gworldptr
= NULL
;
1318 XImageFree(ximage
); // releases the malloc, but does not detroy
1320 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
1321 if ( xshapeimage
!= NULL
)
1323 wxMask
* m
= new wxMask() ;
1324 m
->SetMaskBitmap( xshapeimage
->gworldptr
) ;
1325 M_BITMAPHANDLERDATA
->m_bitmapMask
= m
;
1331 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1337 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1339 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1341 inline wxBMPResourceHandler()
1343 m_name
= "Windows bitmap resource";
1345 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1348 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1349 int desiredWidth
, int desiredHeight
);
1352 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1354 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1355 int desiredWidth
, int desiredHeight
)
1357 // TODO: load colourmap.
1358 // it's probably not found
1359 wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name
.c_str());
1364 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
1366 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
1368 inline wxBMPFileHandler(void)
1370 m_name
= "Windows bitmap file";
1371 m_extension
= "bmp";
1372 m_type
= wxBITMAP_TYPE_BMP
;
1375 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1376 int desiredWidth
, int desiredHeight
);
1377 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
1380 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
1382 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1383 int desiredWidth
, int desiredHeight
)
1385 #if USE_IMAGE_LOADING_IN_MSW
1386 wxPalette
*palette
= NULL
;
1387 bool success
= FALSE
;
1388 success
= (wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0);
1389 if (!success
&& palette
)
1395 M_BITMAPHANDLERDATA
->m_bitmapPalette
= *palette
;
1402 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)
1404 #if USE_IMAGE_LOADING_IN_MSW
1405 wxPalette
*actualPalette
= (wxPalette
*)pal
;
1406 if (!actualPalette
&& (!M_BITMAPHANDLERDATA
->m_bitmapPalette
.IsNull()))
1407 actualPalette
= & (M_BITMAPHANDLERDATA
->m_bitmapPalette
);
1408 return (wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0);
1415 void wxBitmap::CleanUpHandlers()
1417 wxNode
*node
= sm_handlers
.First();
1420 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1421 wxNode
*next
= node
->Next();
1428 void wxBitmap::InitStandardHandlers()
1430 AddHandler( new wxPICTResourceHandler
) ;
1431 AddHandler( new wxICONResourceHandler
) ;
1432 AddHandler(new wxXPMFileHandler
);
1433 AddHandler(new wxXPMDataHandler
);
1434 AddHandler(new wxBMPResourceHandler
);
1435 AddHandler(new wxBMPFileHandler
);