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"
27 #define OBSOLETE_XPM_DATA_HANDLER
32 #ifdef OBSOLETE_XPM_DATA_HANDLER
37 #if !USE_SHARED_LIBRARIES
38 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
39 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
43 #include <ApplicationServices/ApplicationServices.h>
45 #include <PictUtils.h>
48 #include "wx/mac/uma.h"
50 CTabHandle
wxMacCreateColorTable( int numColors
)
52 CTabHandle newColors
; /* Handle to the new color table */
54 /* Allocate memory for the color table */
55 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
56 sizeof (ColorSpec
) * (numColors
- 1) );
59 /* Initialize the fields */
60 (**newColors
).ctSeed
= GetCTSeed();
61 (**newColors
).ctFlags
= 0;
62 (**newColors
).ctSize
= numColors
- 1;
63 /* Initialize the table of colors */
68 void wxMacDestroyColorTable( CTabHandle colors
)
70 DisposeHandle( (Handle
) colors
) ;
73 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
75 (**newColors
).ctTable
[index
].value
= index
;
76 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
77 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
78 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
81 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
85 Rect rect
= { 0 , 0 , height
, width
} ;
89 depth
= wxDisplayDepth() ;
92 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
100 void wxMacDestroyGWorld( GWorldPtr gw
)
103 DisposeGWorld( gw
) ;
106 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
111 PicHandle pict
; // this is the Picture we give back
113 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
114 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
115 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
117 unsigned char *maskimage
= NULL
;
119 GetPortBounds( wp
, &portRect
) ;
120 int width
= portRect
.right
- portRect
.left
;
121 int height
= portRect
.bottom
- portRect
.top
;
123 LockPixels( GetGWorldPixMap( wp
) ) ;
124 GetGWorld( &origPort
, &origDev
) ;
128 maskimage
= (unsigned char*) malloc( width
* height
) ;
129 SetGWorld( mask
, NULL
) ;
130 LockPixels( GetGWorldPixMap( mask
) ) ;
131 for ( int y
= 0 ; y
< height
; y
++ )
133 for( int x
= 0 ; x
< width
; x
++ )
137 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
138 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
141 UnlockPixels( GetGWorldPixMap( mask
) ) ;
144 SetGWorld( wp
, NULL
) ;
146 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
152 RGBForeColor( &black
) ;
153 RGBBackColor( &white
) ;
154 PenMode(transparent
);
156 for ( int y
= 0 ; y
< height
; ++y
)
158 for( int x
= 0 ; x
< width
; ++x
)
160 if ( maskimage
[y
*width
+ x
] )
164 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
165 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
168 // With transparency set this sets a blank pixel not a white one
169 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &white
);
178 RGBBackColor( &gray
) ;
179 EraseRect(&portRect
);
180 RGBForeColor( &black
) ;
181 RGBBackColor( &white
) ;
183 CopyBits(GetPortBitMapForCopyBits(wp
), /* src PixMap - we copy image over
185 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs
186 &portRect
, // srcRect - it will be recorded and compressed -
187 &portRect
, // dstRect - into the picture that is open -
188 srcCopy
,NULL
); // copyMode and no clip region
190 ClosePicture(); // We are done recording the picture
191 UnlockPixels( GetGWorldPixMap( wp
) ) ;
192 SetGWorld( origPort
, origDev
) ;
194 return pict
; // return our groovy pict handle
197 wxBitmapRefData::wxBitmapRefData()
208 m_bitmapType
= kMacBitmapTypeUnknownType
;
211 wxBitmapRefData::~wxBitmapRefData()
213 switch (m_bitmapType
)
215 case kMacBitmapTypePict
:
219 KillPicture( m_hPict
) ;
224 case kMacBitmapTypeGrafWorld
:
228 wxMacDestroyGWorld( m_hBitmap
) ;
245 wxList
wxBitmap::sm_handlers
;
251 if ( wxTheBitmapList
)
252 wxTheBitmapList
->AddBitmap(this);
255 wxBitmap::~wxBitmap()
258 wxTheBitmapList
->DeleteObject(this);
261 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
263 m_refData
= new wxBitmapRefData
;
265 M_BITMAPDATA
->m_width
= the_width
;
266 M_BITMAPDATA
->m_height
= the_height
;
267 M_BITMAPDATA
->m_depth
= no_bits
;
268 M_BITMAPDATA
->m_numColors
= 0;
271 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
272 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
273 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
276 GDHandle origDevice
;
278 GetGWorld( &origPort
, &origDevice
) ;
279 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
280 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
282 // bits is a char array
284 unsigned char* linestart
= (unsigned char*) bits
;
285 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
286 if ( the_width
% (sizeof(unsigned char) * 8) ) {
287 linesize
+= sizeof(unsigned char);
290 RGBColor colors
[2] = {
291 { 0xFFFF , 0xFFFF , 0xFFFF } ,
295 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
297 for ( int x
= 0 ; x
< the_width
; ++x
)
301 int mask
= 1 << bit
;
302 if ( linestart
[index
] & mask
)
304 SetCPixel( x
, y
, &colors
[1] ) ;
308 SetCPixel( x
, y
, &colors
[0] ) ;
312 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
314 SetGWorld( origPort
, origDevice
) ;
318 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
321 if ( wxTheBitmapList
) {
322 wxTheBitmapList
->AddBitmap(this);
326 wxBitmap::wxBitmap(int w
, int h
, int d
)
328 (void)Create(w
, h
, d
);
330 if ( wxTheBitmapList
)
331 wxTheBitmapList
->AddBitmap(this);
334 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
336 (void) Create(data
, type
, width
, height
, depth
);
338 if ( wxTheBitmapList
)
339 wxTheBitmapList
->AddBitmap(this);
342 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
344 LoadFile(filename
, (int)type
);
346 if ( wxTheBitmapList
)
347 wxTheBitmapList
->AddBitmap(this);
350 bool wxBitmap::CreateFromXpm(const char **bits
)
352 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
353 wxXPMDecoder decoder
;
354 wxImage img
= decoder
.ReadData(bits
);
355 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
356 *this = wxBitmap(img
);
357 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
361 wxBitmap::wxBitmap(const char **bits
)
363 #ifdef OBSOLETE_XPM_DATA_HANDLER
364 (void) Create((void *)bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
366 (void) CreateFromXpm(bits
);
370 wxBitmap::wxBitmap(char **bits
)
372 #ifdef OBSOLETE_XPM_DATA_HANDLER
373 (void) Create((void *)bits
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
375 (void) CreateFromXpm((const char **)bits
);
379 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
382 (rect
.x
>= 0) && (rect
.y
>= 0) &&
383 (rect
.x
+rect
.width
<= GetWidth()) &&
384 (rect
.y
+rect
.height
<= GetHeight()),
385 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
388 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
389 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
394 GetGWorld( &origPort
, &origDevice
);
396 // Update the subbitmaps reference data
397 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
399 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
400 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
401 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
403 // Copy sub region of this bitmap
404 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
406 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
408 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
413 WXHBITMAP submask
, mask
;
416 mask
= GetMask()->GetMaskBitmap();
417 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
418 LockPixels(GetGWorldPixMap(mask
));
419 LockPixels(GetGWorldPixMap(submask
));
421 for(int yy
= 0; yy
< rect
.height
; yy
++)
423 for(int xx
= 0; xx
< rect
.width
; xx
++)
425 SetGWorld(mask
, NULL
);
426 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
427 SetGWorld(submask
, NULL
);
428 SetCPixel(xx
,yy
, &color
);
431 UnlockPixels(GetGWorldPixMap(mask
));
432 UnlockPixels(GetGWorldPixMap(submask
));
433 ref
->m_bitmapMask
= new wxMask
;
434 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
440 WXHBITMAP subbitmap
, bitmap
;
443 bitmap
= GetHBITMAP();
444 subbitmap
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetDepth());
445 LockPixels(GetGWorldPixMap(bitmap
));
446 LockPixels(GetGWorldPixMap(subbitmap
));
448 for(int yy
= 0; yy
< rect
.height
; yy
++)
450 for(int xx
= 0; xx
< rect
.width
; xx
++)
452 SetGWorld(bitmap
, NULL
);
453 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
454 SetGWorld(subbitmap
, NULL
);
455 SetCPixel(xx
, yy
, &color
);
458 UnlockPixels(GetGWorldPixMap(bitmap
));
459 UnlockPixels(GetGWorldPixMap(subbitmap
));
460 ret
.SetHBITMAP(subbitmap
);
463 SetGWorld( origPort
, origDevice
);
468 bool wxBitmap::Create(int w
, int h
, int d
)
472 m_refData
= new wxBitmapRefData
;
474 M_BITMAPDATA
->m_width
= w
;
475 M_BITMAPDATA
->m_height
= h
;
476 M_BITMAPDATA
->m_depth
= d
;
478 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
479 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
480 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
481 return M_BITMAPDATA
->m_ok
;
484 int wxBitmap::GetBitmapType() const
486 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
488 return M_BITMAPDATA
->m_bitmapType
;
491 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
493 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
494 M_BITMAPDATA
->m_hBitmap
= bmp
;
495 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
498 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
502 m_refData
= new wxBitmapRefData
;
504 wxBitmapHandler
*handler
= FindHandler(type
);
506 if ( handler
== NULL
) {
507 wxLogWarning("no bitmap handler for type %d defined.", type
);
512 return handler
->LoadFile(this, filename
, type
, -1, -1);
515 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
519 m_refData
= new wxBitmapRefData
;
521 wxBitmapHandler
*handler
= FindHandler(type
);
523 if ( handler
== NULL
) {
524 wxLogWarning("no bitmap handler for type %d defined.", type
);
529 return handler
->Create(this, data
, type
, width
, height
, depth
);
532 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
534 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
535 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
537 m_refData
= new wxBitmapRefData();
539 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
541 // width and height of the device-dependent bitmap
542 int width
= image
.GetWidth();
543 int height
= image
.GetHeight();
547 Create( width
, height
, wxDisplayDepth() ) ;
548 wxBitmap
maskBitmap( width
, height
, 1);
551 GDHandle origDevice
;
553 LockPixels( GetGWorldPixMap(GetHBITMAP()) );
554 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
556 GetGWorld( &origPort
, &origDevice
) ;
557 SetGWorld( GetHBITMAP() , NULL
) ;
560 wxColour rgb
, maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
562 RGBColor white
= { 0xffff, 0xffff, 0xffff };
563 RGBColor black
= { 0 , 0 , 0 };
565 register unsigned char* data
= image
.GetData();
568 for (int y
= 0; y
< height
; y
++)
570 for (int x
= 0; x
< width
; x
++)
572 rgb
.Set(data
[index
++], data
[index
++], data
[index
++]);
573 color
= rgb
.GetPixel();
574 SetCPixel( x
, y
, &color
) ;
577 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
578 if (rgb
== maskcolor
) {
579 SetCPixel(x
,y
, &white
);
582 SetCPixel(x
,y
, &black
);
584 SetGWorld(GetHBITMAP(), NULL
);
590 if ( image
.HasMask() ) {
591 SetMask(new wxMask( maskBitmap
));
594 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
595 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
596 SetGWorld( origPort
, origDevice
);
599 wxImage
wxBitmap::ConvertToImage() const
603 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
605 // create an wxImage object
606 int width
= GetWidth();
607 int height
= GetHeight();
608 image
.Create( width
, height
);
610 unsigned char *data
= image
.GetData();
612 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
618 // background color set to RGB(16,16,16) in consistent with wxGTK
619 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
621 wxMask
*mask
= GetMask();
623 GetGWorld( &origPort
, &origDevice
);
624 LockPixels(GetGWorldPixMap(GetHBITMAP()));
625 SetGWorld( GetHBITMAP(), NULL
);
627 // Copy data into image
629 for (int yy
= 0; yy
< height
; yy
++)
631 for (int xx
= 0; xx
< width
; xx
++)
633 GetCPixel(xx
,yy
, &color
);
634 r
= ((color
.red
) >> 8);
635 g
= ((color
.green
) >> 8);
636 b
= ((color
.blue
) >> 8);
642 if (mask
->PointMasked(xx
,yy
))
644 data
[index
] = mask_r
;
645 data
[index
+ 1] = mask_g
;
646 data
[index
+ 2] = mask_b
;
654 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
655 image
.SetMask( true );
659 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
660 SetGWorld(origPort
, origDevice
);
666 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
668 wxBitmapHandler
*handler
= FindHandler(type
);
670 if ( handler
== NULL
) {
671 wxLogWarning("no bitmap handler for type %d defined.", type
);
676 return handler
->SaveFile(this, filename
, type
, palette
);
679 bool wxBitmap::Ok() const
681 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
684 int wxBitmap::GetHeight() const
686 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
688 return M_BITMAPDATA
->m_height
;
691 int wxBitmap::GetWidth() const
693 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
695 return M_BITMAPDATA
->m_width
;
698 int wxBitmap::GetDepth() const
700 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
702 return M_BITMAPDATA
->m_depth
;
705 int wxBitmap::GetQuality() const
707 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
709 return M_BITMAPDATA
->m_quality
;
712 wxMask
*wxBitmap::GetMask() const
714 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
716 return M_BITMAPDATA
->m_bitmapMask
;
719 void wxBitmap::SetWidth(int w
)
722 m_refData
= new wxBitmapRefData
;
724 M_BITMAPDATA
->m_width
= w
;
727 void wxBitmap::SetHeight(int h
)
730 m_refData
= new wxBitmapRefData
;
732 M_BITMAPDATA
->m_height
= h
;
735 void wxBitmap::SetDepth(int d
)
738 m_refData
= new wxBitmapRefData
;
740 M_BITMAPDATA
->m_depth
= d
;
743 void wxBitmap::SetQuality(int q
)
746 m_refData
= new wxBitmapRefData
;
748 M_BITMAPDATA
->m_quality
= q
;
751 void wxBitmap::SetOk(bool isOk
)
754 m_refData
= new wxBitmapRefData
;
756 M_BITMAPDATA
->m_ok
= isOk
;
759 wxPalette
*wxBitmap::GetPalette() const
761 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
763 return &M_BITMAPDATA
->m_bitmapPalette
;
766 void wxBitmap::SetPalette(const wxPalette
& palette
)
769 m_refData
= new wxBitmapRefData
;
771 M_BITMAPDATA
->m_bitmapPalette
= palette
;
774 void wxBitmap::SetMask(wxMask
*mask
)
777 m_refData
= new wxBitmapRefData
;
779 M_BITMAPDATA
->m_bitmapMask
= mask
;
782 WXHBITMAP
wxBitmap::GetHBITMAP() const
784 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
786 return M_BITMAPDATA
->m_hBitmap
;
789 PicHandle
wxBitmap::GetPict() const
791 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
793 PicHandle picture
; // This is the returned picture
795 // If bitmap already in Pict format return pointer
796 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
797 return M_BITMAPDATA
->m_hPict
;
799 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
804 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
805 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
806 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
812 GetPortBounds( GetHBITMAP() , &portRect
) ;
813 int width
= portRect
.right
- portRect
.left
;
814 int height
= portRect
.bottom
- portRect
.top
;
816 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
817 GetGWorld( &origPort
, &origDev
) ;
821 SetGWorld( GetHBITMAP() , NULL
) ;
823 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
831 RGBColor trans
= white
;
833 RGBBackColor( &gray
);
834 EraseRect( &portRect
);
835 RGBColor trans
= gray
;
837 RGBForeColor( &black
) ;
838 RGBBackColor( &white
) ;
839 PenMode(transparent
);
841 for ( int y
= 0 ; y
< height
; ++y
)
843 for( int x
= 0 ; x
< width
; ++x
)
845 if ( !mask
->PointMasked(x
,y
) )
849 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
850 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
853 // With transparency this sets a blank pixel
854 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
861 RGBBackColor( &gray
) ;
862 EraseRect(&portRect
);
863 RGBForeColor( &black
) ;
864 RGBBackColor( &white
) ;
866 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
867 // src PixMap - we copy image over itself -
868 GetPortBitMapForCopyBits(GetHBITMAP()),
869 // dst PixMap - no drawing occurs
870 &portRect
, // srcRect - it will be recorded and compressed -
871 &portRect
, // dstRect - into the picture that is open -
872 srcCopy
,NULL
); // copyMode and no clip region
874 ClosePicture(); // We are done recording the picture
875 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
876 SetGWorld( origPort
, origDev
) ;
878 return picture
; // return our groovy pict handle
881 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
883 sm_handlers
.Append(handler
);
886 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
888 sm_handlers
.Insert(handler
);
891 bool wxBitmap::RemoveHandler(const wxString
& name
)
893 wxBitmapHandler
*handler
= FindHandler(name
);
896 sm_handlers
.DeleteObject(handler
);
903 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
905 wxNode
*node
= sm_handlers
.First();
908 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
909 if ( handler
->GetName() == name
)
916 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
918 wxNode
*node
= sm_handlers
.First();
921 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
922 if ( handler
->GetExtension() == extension
&&
923 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
930 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
932 wxNode
*node
= sm_handlers
.First();
935 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
936 if (handler
->GetType() == bitmapType
)
952 // Construct a mask from a bitmap and a colour indicating
953 // the transparent area
954 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
957 Create(bitmap
, colour
);
960 // Construct a mask from a bitmap and a palette index indicating
961 // the transparent area
962 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
965 Create(bitmap
, paletteIndex
);
968 // Construct a mask from a mono bitmap (copies the bitmap).
969 wxMask::wxMask(const wxBitmap
& bitmap
)
979 wxMacDestroyGWorld( m_maskBitmap
) ;
980 m_maskBitmap
= NULL
;
984 // Create a mask from a mono bitmap (copies the bitmap).
985 bool wxMask::Create(const wxBitmap
& bitmap
)
989 wxMacDestroyGWorld( m_maskBitmap
) ;
990 m_maskBitmap
= NULL
;
992 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
993 wxT("Cannot create mask from this bitmap type (TODO)"));
994 // other types would require a temporary bitmap. not yet implemented
996 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
998 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
999 wxT("Cannot create mask from colour bitmap"));
1001 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1002 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1004 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1005 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1006 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1007 GetPortBitMapForCopyBits(m_maskBitmap
),
1008 &rect
, &rect
, srcCopy
, 0);
1009 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1010 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1015 // Create a mask from a bitmap and a palette index indicating
1016 // the transparent area
1017 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1020 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1024 // Create a mask from a bitmap and a colour indicating
1025 // the transparent area
1026 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1030 wxMacDestroyGWorld( m_maskBitmap
) ;
1031 m_maskBitmap
= NULL
;
1033 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1034 wxT("Cannot create mask from this bitmap type (TODO)"));
1035 // other types would require a temporary bitmap. not yet implemented
1037 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1039 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1040 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1041 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1042 RGBColor maskColor
= colour
.GetPixel();
1044 // this is not very efficient, but I can't think
1045 // of a better way of doing it
1047 GDHandle origDevice
;
1049 RGBColor colors
[2] = {
1050 { 0xFFFF, 0xFFFF, 0xFFFF },
1053 GetGWorld( &origPort
, &origDevice
) ;
1054 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1056 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1058 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1059 GetCPixel( w
, h
, &col
) ;
1060 SetGWorld( m_maskBitmap
, NULL
) ;
1061 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1063 SetCPixel( w
, h
, &colors
[0] ) ;
1067 SetCPixel( w
, h
, &colors
[1] ) ;
1071 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1072 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1073 SetGWorld( origPort
, origDevice
) ;
1078 bool wxMask::PointMasked(int x
, int y
)
1081 GDHandle origDevice
;
1085 GetGWorld( &origPort
, &origDevice
);
1087 //Set port to mask and see if it masked (1) or not ( 0 )
1088 SetGWorld(m_maskBitmap
, NULL
);
1089 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1090 GetCPixel(x
,y
, &color
);
1091 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1092 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1094 SetGWorld( origPort
, origDevice
);
1103 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1105 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1110 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
1111 int desiredWidth
, int desiredHeight
)
1116 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1125 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1127 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1129 inline wxPICTResourceHandler()
1131 m_name
= "Macintosh Pict resource";
1133 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1136 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1137 int desiredWidth
, int desiredHeight
);
1139 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1141 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1142 int desiredWidth
, int desiredHeight
)
1147 c2pstrcpy( (StringPtr
) theName
, name
) ;
1149 strcpy( (char *) theName
, name
) ;
1150 c2pstr( (char *)theName
) ;
1153 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1158 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1159 DetachResource( (Handle
) thePict
) ;
1160 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1161 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1162 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1163 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1165 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1166 M_BITMAPHANDLERDATA
->m_ok
= true ;
1167 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1168 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1169 // M_BITMAPHANDLERDATA->m_quality;
1175 /* TODO: bitmap handlers, a bit like this:
1176 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
1178 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
1180 inline wxBMPResourceHandler()
1182 m_name = "Windows bitmap resource";
1184 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
1187 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1188 int desiredWidth, int desiredHeight);
1190 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
1193 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
1195 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1197 inline wxXPMFileHandler(void)
1199 m_name
= "XPM bitmap file";
1200 m_extension
= "xpm";
1201 m_type
= wxBITMAP_TYPE_XPM
;
1204 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1205 int desiredWidth
= -1, int desiredHeight
= -1);
1206 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
1208 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1210 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1211 int desiredWidth
, int desiredHeight
)
1215 XpmAttributes xpmAttr
;
1218 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1219 dc
= CreateCompatibleDC(NULL
);
1222 xpmAttr
.valuemask
= XpmReturnPixels
;
1223 int errorStatus
= XpmReadFileToImage(&dc
, WXSTRINGCAST name
, &ximage
, (XImage
**) NULL
, &xpmAttr
);
1225 if (errorStatus
== XpmSuccess
)
1227 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
1230 GetObject((HBITMAP
)M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
1232 M_BITMAPHANDLERDATA
->m_width
= (bm
.bmWidth
);
1233 M_BITMAPHANDLERDATA
->m_height
= (bm
.bmHeight
);
1234 M_BITMAPHANDLERDATA
->m_depth
= (bm
.bmPlanes
* bm
.bmBitsPixel
);
1235 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
1236 XpmFreeAttributes(&xpmAttr
);
1239 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
1244 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1253 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1258 Visual
*visual
= NULL
;
1261 dc
= CreateCompatibleDC(NULL
);
1264 if (SelectObject(dc
, (HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
))
1267 ximage
.width
= M_BITMAPHANDLERDATA
->m_width
;
1268 ximage
.height
= M_BITMAPHANDLERDATA
->m_height
;
1269 ximage
.depth
= M_BITMAPHANDLERDATA
->m_depth
;
1270 ximage
.bitmap
= (void *)M_BITMAPHANDLERDATA
->m_hBitmap
;
1271 int errorStatus
= XpmWriteFileFromImage(&dc
, WXSTRINGCAST name
,
1272 &ximage
, (XImage
*) NULL
, (XpmAttributes
*) NULL
);
1277 if (errorStatus
== XpmSuccess
)
1281 } else return FALSE
;
1282 } else return FALSE
;
1288 #ifdef OBSOLETE_XPM_DATA_HANDLER
1289 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
1291 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1293 inline wxXPMDataHandler(void)
1295 m_name
= "XPM bitmap data";
1296 m_extension
= "xpm";
1297 m_type
= wxBITMAP_TYPE_XPM_DATA
;
1300 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
1302 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1304 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
1306 XImage
* ximage
= NULL
;
1307 XImage
* xshapeimage
= NULL
;
1309 XpmAttributes xpmAttr
;
1311 xpmAttr
.valuemask
= XpmReturnInfos
; // get infos back
1312 ErrorStatus
= XpmCreateImageFromData( GetMainDevice() , (char **)data
,
1313 &ximage
, &xshapeimage
, &xpmAttr
);
1315 if (ErrorStatus
== XpmSuccess
)
1317 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1318 M_BITMAPHANDLERDATA
->m_numColors
= 0;
1319 M_BITMAPHANDLERDATA
->m_hBitmap
= ximage
->gworldptr
;
1321 M_BITMAPHANDLERDATA
->m_width
= ximage
->width
;
1322 M_BITMAPHANDLERDATA
->m_height
= ximage
->height
;
1323 M_BITMAPHANDLERDATA
->m_depth
= ximage
->depth
;
1324 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
1325 XpmFreeAttributes(&xpmAttr
);
1326 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
1327 ximage
->gworldptr
= NULL
;
1328 XImageFree(ximage
); // releases the malloc, but does not detroy
1330 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
1331 if ( xshapeimage
!= NULL
)
1333 wxMask
* m
= new wxMask() ;
1334 m
->SetMaskBitmap( xshapeimage
->gworldptr
) ;
1335 M_BITMAPHANDLERDATA
->m_bitmapMask
= m
;
1341 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1348 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1350 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1352 inline wxBMPResourceHandler()
1354 m_name
= "Windows bitmap resource";
1356 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1359 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1360 int desiredWidth
, int desiredHeight
);
1363 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1365 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1366 int desiredWidth
, int desiredHeight
)
1368 // TODO: load colourmap.
1369 // it's probably not found
1370 wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name
.c_str());
1375 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
1377 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
1379 inline wxBMPFileHandler(void)
1381 m_name
= "Windows bitmap file";
1382 m_extension
= "bmp";
1383 m_type
= wxBITMAP_TYPE_BMP
;
1386 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1387 int desiredWidth
, int desiredHeight
);
1388 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
1391 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
1393 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1394 int desiredWidth
, int desiredHeight
)
1396 #if USE_IMAGE_LOADING_IN_MSW
1397 wxPalette
*palette
= NULL
;
1398 bool success
= FALSE
;
1399 success
= (wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0);
1400 if (!success
&& palette
)
1406 M_BITMAPHANDLERDATA
->m_bitmapPalette
= *palette
;
1413 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)
1415 #if USE_IMAGE_LOADING_IN_MSW
1416 wxPalette
*actualPalette
= (wxPalette
*)pal
;
1417 if (!actualPalette
&& (!M_BITMAPHANDLERDATA
->m_bitmapPalette
.IsNull()))
1418 actualPalette
= & (M_BITMAPHANDLERDATA
->m_bitmapPalette
);
1419 return (wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0);
1426 void wxBitmap::CleanUpHandlers()
1428 wxNode
*node
= sm_handlers
.First();
1431 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1432 wxNode
*next
= node
->Next();
1439 void wxBitmap::InitStandardHandlers()
1441 AddHandler(new wxPICTResourceHandler
) ;
1442 AddHandler(new wxICONResourceHandler
) ;
1443 AddHandler(new wxXPMFileHandler
);
1444 #ifdef OBSOLETE_XPM_DATA_HANDLER
1445 AddHandler(new wxXPMDataHandler
);
1447 AddHandler(new wxBMPResourceHandler
);
1448 AddHandler(new wxBMPFileHandler
);