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 CTabHandle
wxMacCreateColorTable( int numColors
)
47 CTabHandle newColors
; /* Handle to the new color table */
49 /* Allocate memory for the color table */
50 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
51 sizeof (ColorSpec
) * (numColors
- 1) );
54 /* Initialize the fields */
55 (**newColors
).ctSeed
= GetCTSeed();
56 (**newColors
).ctFlags
= 0;
57 (**newColors
).ctSize
= numColors
- 1;
58 /* Initialize the table of colors */
63 void wxMacDestroyColorTable( CTabHandle colors
)
65 DisposeHandle( (Handle
) colors
) ;
68 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
70 (**newColors
).ctTable
[index
].value
= index
;
71 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
72 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
73 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
76 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
80 Rect rect
= { 0 , 0 , height
, width
} ;
84 depth
= wxDisplayDepth() ;
87 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
95 void wxMacDestroyGWorld( GWorldPtr gw
)
101 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
106 PicHandle pict
; // this is the Picture we give back
108 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
109 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
110 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
112 unsigned char *maskimage
= NULL
;
114 GetPortBounds( wp
, &portRect
) ;
115 int width
= portRect
.right
- portRect
.left
;
116 int height
= portRect
.bottom
- portRect
.top
;
118 LockPixels( GetGWorldPixMap( wp
) ) ;
119 GetGWorld( &origPort
, &origDev
) ;
123 maskimage
= (unsigned char*) malloc( width
* height
) ;
124 SetGWorld( mask
, NULL
) ;
125 LockPixels( GetGWorldPixMap( mask
) ) ;
126 for ( int y
= 0 ; y
< height
; y
++ )
128 for( int x
= 0 ; x
< width
; x
++ )
132 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
133 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
136 UnlockPixels( GetGWorldPixMap( mask
) ) ;
139 SetGWorld( wp
, NULL
) ;
141 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
147 RGBForeColor( &black
) ;
148 RGBBackColor( &white
) ;
149 PenMode(transparent
);
151 for ( int y
= 0 ; y
< height
; ++y
)
153 for( int x
= 0 ; x
< width
; ++x
)
155 if ( maskimage
[y
*width
+ x
] )
159 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
160 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
163 // With transparency set this sets a blank pixel not a white one
164 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &white
);
173 RGBBackColor( &gray
) ;
174 EraseRect(&portRect
);
175 RGBForeColor( &black
) ;
176 RGBBackColor( &white
) ;
178 CopyBits(GetPortBitMapForCopyBits(wp
), /* src PixMap - we copy image over
180 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs
181 &portRect
, // srcRect - it will be recorded and compressed -
182 &portRect
, // dstRect - into the picture that is open -
183 srcCopy
,NULL
); // copyMode and no clip region
185 ClosePicture(); // We are done recording the picture
186 UnlockPixels( GetGWorldPixMap( wp
) ) ;
187 SetGWorld( origPort
, origDev
) ;
189 return pict
; // return our groovy pict handle
192 wxBitmapRefData::wxBitmapRefData()
203 m_bitmapType
= kMacBitmapTypeUnknownType
;
206 wxBitmapRefData::~wxBitmapRefData()
208 switch (m_bitmapType
)
210 case kMacBitmapTypePict
:
214 KillPicture( m_hPict
) ;
219 case kMacBitmapTypeGrafWorld
:
223 wxMacDestroyGWorld( m_hBitmap
) ;
240 wxList
wxBitmap::sm_handlers
;
246 if ( wxTheBitmapList
)
247 wxTheBitmapList
->AddBitmap(this);
250 wxBitmap::~wxBitmap()
253 wxTheBitmapList
->DeleteObject(this);
256 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
258 m_refData
= new wxBitmapRefData
;
260 M_BITMAPDATA
->m_width
= the_width
;
261 M_BITMAPDATA
->m_height
= the_height
;
262 M_BITMAPDATA
->m_depth
= no_bits
;
263 M_BITMAPDATA
->m_numColors
= 0;
266 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
267 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
268 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
271 GDHandle origDevice
;
273 GetGWorld( &origPort
, &origDevice
) ;
274 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
275 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
278 // bits is a word aligned array?? Don't think so
279 // bits is a char array on MAC OS X however using the benefit of the
280 // doubt I replaced references to 16 with sizeof(unsigned char)*8
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 // bits is a word aligned array
289 unsigned char* linestart
= (unsigned char*) bits
;
290 int linesize
= ( the_width
/ 16 ) * 2 ;
291 if ( the_width
% 16 )
297 RGBColor colors
[2] = {
298 { 0xFFFF , 0xFFFF , 0xFFFF } ,
302 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
304 for ( int x
= 0 ; x
< the_width
; ++x
)
308 int mask
= 1 << bit
;
309 if ( linestart
[index
] & mask
)
311 SetCPixel( x
, y
, &colors
[1] ) ;
315 SetCPixel( x
, y
, &colors
[0] ) ;
320 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
322 SetGWorld( origPort
, origDevice
) ;
326 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
329 if ( wxTheBitmapList
)
330 wxTheBitmapList
->AddBitmap(this);
333 wxBitmap::wxBitmap(int w
, int h
, int d
)
335 (void)Create(w
, h
, d
);
337 if ( wxTheBitmapList
)
338 wxTheBitmapList
->AddBitmap(this);
341 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
343 (void) Create(data
, type
, width
, height
, depth
);
345 if ( wxTheBitmapList
)
346 wxTheBitmapList
->AddBitmap(this);
349 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
351 LoadFile(filename
, (int)type
);
353 if ( wxTheBitmapList
)
354 wxTheBitmapList
->AddBitmap(this);
357 wxBitmap::wxBitmap(const char **data
)
359 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
362 wxBitmap::wxBitmap(char **data
)
364 (void) Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
367 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
370 (rect
.x
>= 0) && (rect
.y
>= 0) &&
371 (rect
.x
+rect
.width
<= GetWidth()) &&
372 (rect
.y
+rect
.height
<= GetHeight()),
373 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
376 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
377 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
382 GetGWorld( &origPort
, &origDevice
);
384 // Update the subbitmaps reference data
385 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
387 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
388 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
389 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
391 // Copy sub region of this bitmap
392 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
394 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
396 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
401 WXHBITMAP submask
, mask
;
404 mask
= GetMask()->GetMaskBitmap();
405 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
406 LockPixels(GetGWorldPixMap(mask
));
407 LockPixels(GetGWorldPixMap(submask
));
409 for(int yy
= 0; yy
< rect
.height
; yy
++)
411 for(int xx
= 0; xx
< rect
.width
; xx
++)
413 SetGWorld(mask
, NULL
);
414 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
415 SetGWorld(submask
, NULL
);
416 SetCPixel(xx
,yy
, &color
);
419 UnlockPixels(GetGWorldPixMap(mask
));
420 UnlockPixels(GetGWorldPixMap(submask
));
421 ref
->m_bitmapMask
= new wxMask
;
422 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
428 WXHBITMAP subbitmap
, bitmap
;
431 bitmap
= GetHBITMAP();
432 subbitmap
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetDepth());
433 LockPixels(GetGWorldPixMap(bitmap
));
434 LockPixels(GetGWorldPixMap(subbitmap
));
436 for(int yy
= 0; yy
< rect
.height
; yy
++)
438 for(int xx
= 0; xx
< rect
.width
; xx
++)
440 SetGWorld(bitmap
, NULL
);
441 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
442 SetGWorld(subbitmap
, NULL
);
443 SetCPixel(xx
, yy
, &color
);
446 UnlockPixels(GetGWorldPixMap(bitmap
));
447 UnlockPixels(GetGWorldPixMap(subbitmap
));
448 ret
.SetHBITMAP(subbitmap
);
451 SetGWorld( origPort
, origDevice
);
456 bool wxBitmap::Create(int w
, int h
, int d
)
460 m_refData
= new wxBitmapRefData
;
462 M_BITMAPDATA
->m_width
= w
;
463 M_BITMAPDATA
->m_height
= h
;
464 M_BITMAPDATA
->m_depth
= d
;
466 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
467 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
468 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
469 return M_BITMAPDATA
->m_ok
;
472 int wxBitmap::GetBitmapType() const
474 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
476 return M_BITMAPDATA
->m_bitmapType
;
479 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
481 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
482 M_BITMAPDATA
->m_hBitmap
= bmp
;
483 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
486 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
490 m_refData
= new wxBitmapRefData
;
492 wxBitmapHandler
*handler
= FindHandler(type
);
494 if ( handler
== NULL
) {
495 wxLogWarning("no bitmap handler for type %d defined.", type
);
500 return handler
->LoadFile(this, filename
, type
, -1, -1);
503 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
507 m_refData
= new wxBitmapRefData
;
509 wxBitmapHandler
*handler
= FindHandler(type
);
511 if ( handler
== NULL
) {
512 wxLogWarning("no bitmap handler for type %d defined.", type
);
517 return handler
->Create(this, data
, type
, width
, height
, depth
);
520 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
522 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
523 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
525 m_refData
= new wxBitmapRefData();
527 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
529 // width and height of the device-dependent bitmap
530 int width
= image
.GetWidth();
531 int height
= image
.GetHeight();
535 Create( width
, height
, wxDisplayDepth() ) ;
536 wxBitmap
maskBitmap( width
, height
, 1);
539 GDHandle origDevice
;
541 LockPixels( GetGWorldPixMap(GetHBITMAP()) );
542 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
544 GetGWorld( &origPort
, &origDevice
) ;
545 SetGWorld( GetHBITMAP() , NULL
) ;
548 wxColour rgb
, maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
550 RGBColor white
= { 0xffff, 0xffff, 0xffff };
551 RGBColor black
= { 0 , 0 , 0 };
553 register unsigned char* data
= image
.GetData();
556 for (int y
= 0; y
< height
; y
++)
558 for (int x
= 0; x
< width
; x
++)
560 rgb
.Set(data
[index
++], data
[index
++], data
[index
++]);
561 color
= rgb
.GetPixel();
562 SetCPixel( x
, y
, &color
) ;
565 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
566 if (rgb
== maskcolor
) {
567 SetCPixel(x
,y
, &white
);
570 SetCPixel(x
,y
, &black
);
572 SetGWorld(GetHBITMAP(), NULL
);
578 if ( image
.HasMask() ) {
579 wxMask
*mask
= new wxMask( maskBitmap
);
582 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
583 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
584 SetGWorld( origPort
, origDevice
);
587 wxImage
wxBitmap::ConvertToImage() const
591 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
593 // create an wxImage object
594 int width
= GetWidth();
595 int height
= GetHeight();
596 image
.Create( width
, height
);
598 unsigned char *data
= image
.GetData();
600 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
606 // background color set to RGB(16,16,16) in consistent with wxGTK
607 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
609 wxMask
*mask
= GetMask();
611 GetGWorld( &origPort
, &origDevice
);
612 LockPixels(GetGWorldPixMap(GetHBITMAP()));
613 SetGWorld( GetHBITMAP(), NULL
);
615 // Copy data into image
617 for (int yy
= 0; yy
< height
; yy
++)
619 for (int xx
= 0; xx
< width
; xx
++)
621 GetCPixel(xx
,yy
, &color
);
622 r
= ((color
.red
) >> 8);
623 g
= ((color
.green
) >> 8);
624 b
= ((color
.blue
) >> 8);
630 if (mask
->PointMasked(xx
,yy
))
632 data
[index
] = mask_r
;
633 data
[index
+ 1] = mask_g
;
634 data
[index
+ 2] = mask_b
;
642 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
643 image
.SetMask( true );
647 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
648 SetGWorld(origPort
, origDevice
);
654 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
656 wxBitmapHandler
*handler
= FindHandler(type
);
658 if ( handler
== NULL
) {
659 wxLogWarning("no bitmap handler for type %d defined.", type
);
664 return handler
->SaveFile(this, filename
, type
, palette
);
667 bool wxBitmap::Ok() const
669 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
672 int wxBitmap::GetHeight() const
674 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
676 return M_BITMAPDATA
->m_height
;
679 int wxBitmap::GetWidth() const
681 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
683 return M_BITMAPDATA
->m_width
;
686 int wxBitmap::GetDepth() const
688 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
690 return M_BITMAPDATA
->m_depth
;
693 int wxBitmap::GetQuality() const
695 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
697 return M_BITMAPDATA
->m_quality
;
700 wxMask
*wxBitmap::GetMask() const
702 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
704 return M_BITMAPDATA
->m_bitmapMask
;
707 void wxBitmap::SetWidth(int w
)
710 m_refData
= new wxBitmapRefData
;
712 M_BITMAPDATA
->m_width
= w
;
715 void wxBitmap::SetHeight(int h
)
718 m_refData
= new wxBitmapRefData
;
720 M_BITMAPDATA
->m_height
= h
;
723 void wxBitmap::SetDepth(int d
)
726 m_refData
= new wxBitmapRefData
;
728 M_BITMAPDATA
->m_depth
= d
;
731 void wxBitmap::SetQuality(int q
)
734 m_refData
= new wxBitmapRefData
;
736 M_BITMAPDATA
->m_quality
= q
;
739 void wxBitmap::SetOk(bool isOk
)
742 m_refData
= new wxBitmapRefData
;
744 M_BITMAPDATA
->m_ok
= isOk
;
747 wxPalette
*wxBitmap::GetPalette() const
749 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
751 return &M_BITMAPDATA
->m_bitmapPalette
;
754 void wxBitmap::SetPalette(const wxPalette
& palette
)
757 m_refData
= new wxBitmapRefData
;
759 M_BITMAPDATA
->m_bitmapPalette
= palette
;
762 void wxBitmap::SetMask(wxMask
*mask
)
765 m_refData
= new wxBitmapRefData
;
767 M_BITMAPDATA
->m_bitmapMask
= mask
;
770 WXHBITMAP
wxBitmap::GetHBITMAP() const
772 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
774 return M_BITMAPDATA
->m_hBitmap
;
777 PicHandle
wxBitmap::GetPict() const
779 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
781 PicHandle picture
; // This is the returned picture
783 // If bitmap already in Pict format return pointer
784 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
785 return M_BITMAPDATA
->m_hPict
;
787 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
792 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
793 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
794 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
800 GetPortBounds( GetHBITMAP() , &portRect
) ;
801 int width
= portRect
.right
- portRect
.left
;
802 int height
= portRect
.bottom
- portRect
.top
;
804 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
805 GetGWorld( &origPort
, &origDev
) ;
809 SetGWorld( GetHBITMAP() , NULL
) ;
811 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
819 RGBColor trans
= white
;
821 RGBBackColor( &gray
);
822 EraseRect( &portRect
);
823 RGBColor trans
= gray
;
825 RGBForeColor( &black
) ;
826 RGBBackColor( &white
) ;
827 PenMode(transparent
);
829 for ( int y
= 0 ; y
< height
; ++y
)
831 for( int x
= 0 ; x
< width
; ++x
)
833 if ( !mask
->PointMasked(x
,y
) )
837 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
838 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
841 // With transparency this sets a blank pixel
842 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
849 RGBBackColor( &gray
) ;
850 EraseRect(&portRect
);
851 RGBForeColor( &black
) ;
852 RGBBackColor( &white
) ;
854 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
855 // src PixMap - we copy image over itself -
856 GetPortBitMapForCopyBits(GetHBITMAP()),
857 // dst PixMap - no drawing occurs
858 &portRect
, // srcRect - it will be recorded and compressed -
859 &portRect
, // dstRect - into the picture that is open -
860 srcCopy
,NULL
); // copyMode and no clip region
862 ClosePicture(); // We are done recording the picture
863 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
864 SetGWorld( origPort
, origDev
) ;
866 return picture
; // return our groovy pict handle
869 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
871 sm_handlers
.Append(handler
);
874 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
876 sm_handlers
.Insert(handler
);
879 bool wxBitmap::RemoveHandler(const wxString
& name
)
881 wxBitmapHandler
*handler
= FindHandler(name
);
884 sm_handlers
.DeleteObject(handler
);
891 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
893 wxNode
*node
= sm_handlers
.First();
896 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
897 if ( handler
->GetName() == name
)
904 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, long bitmapType
)
906 wxNode
*node
= sm_handlers
.First();
909 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
910 if ( handler
->GetExtension() == extension
&&
911 (bitmapType
== -1 || handler
->GetType() == bitmapType
) )
918 wxBitmapHandler
*wxBitmap::FindHandler(long bitmapType
)
920 wxNode
*node
= sm_handlers
.First();
923 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
924 if (handler
->GetType() == bitmapType
)
940 // Construct a mask from a bitmap and a colour indicating
941 // the transparent area
942 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
945 Create(bitmap
, colour
);
948 // Construct a mask from a bitmap and a palette index indicating
949 // the transparent area
950 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
953 Create(bitmap
, paletteIndex
);
956 // Construct a mask from a mono bitmap (copies the bitmap).
957 wxMask::wxMask(const wxBitmap
& bitmap
)
967 wxMacDestroyGWorld( m_maskBitmap
) ;
968 m_maskBitmap
= NULL
;
972 // Create a mask from a mono bitmap (copies the bitmap).
973 bool wxMask::Create(const wxBitmap
& bitmap
)
977 wxMacDestroyGWorld( m_maskBitmap
) ;
978 m_maskBitmap
= NULL
;
980 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
981 wxT("Cannot create mask from this bitmap type (TODO)"));
982 // other types would require a temporary bitmap. not yet implemented
984 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
986 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
987 wxT("Cannot create mask from colour bitmap"));
989 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
990 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
992 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
993 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
994 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
995 GetPortBitMapForCopyBits(m_maskBitmap
),
996 &rect
, &rect
, srcCopy
, 0);
997 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
998 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1003 // Create a mask from a bitmap and a palette index indicating
1004 // the transparent area
1005 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1008 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1012 // Create a mask from a bitmap and a colour indicating
1013 // the transparent area
1014 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1018 wxMacDestroyGWorld( m_maskBitmap
) ;
1019 m_maskBitmap
= NULL
;
1021 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1022 wxT("Cannot create mask from this bitmap type (TODO)"));
1023 // other types would require a temporary bitmap. not yet implemented
1025 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1027 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1028 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1029 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1030 RGBColor maskColor
= colour
.GetPixel();
1032 // this is not very efficient, but I can't think
1033 // of a better way of doing it
1035 GDHandle origDevice
;
1037 RGBColor colors
[2] = {
1038 { 0xFFFF, 0xFFFF, 0xFFFF },
1041 GetGWorld( &origPort
, &origDevice
) ;
1042 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1044 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1046 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1047 GetCPixel( w
, h
, &col
) ;
1048 SetGWorld( m_maskBitmap
, NULL
) ;
1049 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1051 SetCPixel( w
, h
, &colors
[0] ) ;
1055 SetCPixel( w
, h
, &colors
[1] ) ;
1059 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1060 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1061 SetGWorld( origPort
, origDevice
) ;
1066 bool wxMask::PointMasked(int x
, int y
)
1069 GDHandle origDevice
;
1073 GetGWorld( &origPort
, &origDevice
);
1075 //Set port to mask and see if it masked (1) or not ( 0 )
1076 SetGWorld(m_maskBitmap
, NULL
);
1077 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1078 GetCPixel(x
,y
, &color
);
1079 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1080 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1082 SetGWorld( origPort
, origDevice
);
1091 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1093 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1098 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long type
,
1099 int desiredWidth
, int desiredHeight
)
1104 bool wxBitmapHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1113 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1115 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1117 inline wxPICTResourceHandler()
1119 m_name
= "Macintosh Pict resource";
1121 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1124 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1125 int desiredWidth
, int desiredHeight
);
1127 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1129 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1130 int desiredWidth
, int desiredHeight
)
1135 c2pstrcpy( (StringPtr
) theName
, name
) ;
1137 strcpy( (char *) theName
, name
) ;
1138 c2pstr( (char *)theName
) ;
1141 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1146 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1147 DetachResource( (Handle
) thePict
) ;
1148 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1149 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1150 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1151 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1153 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1154 M_BITMAPHANDLERDATA
->m_ok
= true ;
1155 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1156 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1157 // M_BITMAPHANDLERDATA->m_quality;
1163 /* TODO: bitmap handlers, a bit like this:
1164 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
1166 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
1168 inline wxBMPResourceHandler()
1170 m_name = "Windows bitmap resource";
1172 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
1175 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1176 int desiredWidth, int desiredHeight);
1178 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
1181 class WXDLLEXPORT wxXPMFileHandler
: public wxBitmapHandler
1183 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler
)
1185 inline wxXPMFileHandler(void)
1187 m_name
= "XPM bitmap file";
1188 m_extension
= "xpm";
1189 m_type
= wxBITMAP_TYPE_XPM
;
1192 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1193 int desiredWidth
= -1, int desiredHeight
= -1);
1194 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
1196 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler
, wxBitmapHandler
)
1198 bool wxXPMFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1199 int desiredWidth
, int desiredHeight
)
1203 XpmAttributes xpmAttr
;
1206 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1207 dc
= CreateCompatibleDC(NULL
);
1210 xpmAttr
.valuemask
= XpmReturnPixels
;
1211 int errorStatus
= XpmReadFileToImage(&dc
, WXSTRINGCAST name
, &ximage
, (XImage
**) NULL
, &xpmAttr
);
1213 if (errorStatus
== XpmSuccess
)
1215 M_BITMAPHANDLERDATA
->m_hBitmap
= (WXHBITMAP
) ximage
->bitmap
;
1218 GetObject((HBITMAP
)M_BITMAPHANDLERDATA
->m_hBitmap
, sizeof(bm
), (LPSTR
) & bm
);
1220 M_BITMAPHANDLERDATA
->m_width
= (bm
.bmWidth
);
1221 M_BITMAPHANDLERDATA
->m_height
= (bm
.bmHeight
);
1222 M_BITMAPHANDLERDATA
->m_depth
= (bm
.bmPlanes
* bm
.bmBitsPixel
);
1223 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
1224 XpmFreeAttributes(&xpmAttr
);
1227 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
1232 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1241 bool wxXPMFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1246 Visual
*visual
= NULL
;
1249 dc
= CreateCompatibleDC(NULL
);
1252 if (SelectObject(dc
, (HBITMAP
) M_BITMAPHANDLERDATA
->m_hBitmap
))
1255 ximage
.width
= M_BITMAPHANDLERDATA
->m_width
;
1256 ximage
.height
= M_BITMAPHANDLERDATA
->m_height
;
1257 ximage
.depth
= M_BITMAPHANDLERDATA
->m_depth
;
1258 ximage
.bitmap
= (void *)M_BITMAPHANDLERDATA
->m_hBitmap
;
1259 int errorStatus
= XpmWriteFileFromImage(&dc
, WXSTRINGCAST name
,
1260 &ximage
, (XImage
*) NULL
, (XpmAttributes
*) NULL
);
1265 if (errorStatus
== XpmSuccess
)
1269 } else return FALSE
;
1270 } else return FALSE
;
1277 class WXDLLEXPORT wxXPMDataHandler
: public wxBitmapHandler
1279 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler
)
1281 inline wxXPMDataHandler(void)
1283 m_name
= "XPM bitmap data";
1284 m_extension
= "xpm";
1285 m_type
= wxBITMAP_TYPE_XPM_DATA
;
1288 virtual bool Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
= 1);
1290 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler
, wxBitmapHandler
)
1292 bool wxXPMDataHandler::Create(wxBitmap
*bitmap
, void *data
, long flags
, int width
, int height
, int depth
)
1294 XImage
* ximage
= NULL
;
1295 XImage
* xshapeimage
= NULL
;
1297 XpmAttributes xpmAttr
;
1299 xpmAttr
.valuemask
= XpmReturnInfos
; // get infos back
1300 ErrorStatus
= XpmCreateImageFromData( GetMainDevice() , (char **)data
,
1301 &ximage
, &xshapeimage
, &xpmAttr
);
1303 if (ErrorStatus
== XpmSuccess
)
1305 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1306 M_BITMAPHANDLERDATA
->m_numColors
= 0;
1307 M_BITMAPHANDLERDATA
->m_hBitmap
= ximage
->gworldptr
;
1309 M_BITMAPHANDLERDATA
->m_width
= ximage
->width
;
1310 M_BITMAPHANDLERDATA
->m_height
= ximage
->height
;
1311 M_BITMAPHANDLERDATA
->m_depth
= ximage
->depth
;
1312 M_BITMAPHANDLERDATA
->m_numColors
= xpmAttr
.npixels
;
1313 XpmFreeAttributes(&xpmAttr
);
1314 M_BITMAPHANDLERDATA
->m_ok
= TRUE
;
1315 ximage
->gworldptr
= NULL
;
1316 XImageFree(ximage
); // releases the malloc, but does not detroy
1318 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
1319 if ( xshapeimage
!= NULL
)
1321 wxMask
* m
= new wxMask() ;
1322 m
->SetMaskBitmap( xshapeimage
->gworldptr
) ;
1323 M_BITMAPHANDLERDATA
->m_bitmapMask
= m
;
1329 M_BITMAPHANDLERDATA
->m_ok
= FALSE
;
1335 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1337 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1339 inline wxBMPResourceHandler()
1341 m_name
= "Windows bitmap resource";
1343 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1346 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1347 int desiredWidth
, int desiredHeight
);
1350 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1352 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1353 int desiredWidth
, int desiredHeight
)
1355 // TODO: load colourmap.
1356 // it's probably not found
1357 wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name
.c_str());
1362 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
1364 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
1366 inline wxBMPFileHandler(void)
1368 m_name
= "Windows bitmap file";
1369 m_extension
= "bmp";
1370 m_type
= wxBITMAP_TYPE_BMP
;
1373 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1374 int desiredWidth
, int desiredHeight
);
1375 virtual bool SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
= NULL
);
1378 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
1380 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1381 int desiredWidth
, int desiredHeight
)
1383 #if USE_IMAGE_LOADING_IN_MSW
1384 wxPalette
*palette
= NULL
;
1385 bool success
= FALSE
;
1386 success
= (wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0);
1387 if (!success
&& palette
)
1393 M_BITMAPHANDLERDATA
->m_bitmapPalette
= *palette
;
1400 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*pal
)
1402 #if USE_IMAGE_LOADING_IN_MSW
1403 wxPalette
*actualPalette
= (wxPalette
*)pal
;
1404 if (!actualPalette
&& (!M_BITMAPHANDLERDATA
->m_bitmapPalette
.IsNull()))
1405 actualPalette
= & (M_BITMAPHANDLERDATA
->m_bitmapPalette
);
1406 return (wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0);
1413 void wxBitmap::CleanUpHandlers()
1415 wxNode
*node
= sm_handlers
.First();
1418 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1419 wxNode
*next
= node
->Next();
1426 void wxBitmap::InitStandardHandlers()
1428 AddHandler( new wxPICTResourceHandler
) ;
1429 AddHandler( new wxICONResourceHandler
) ;
1430 AddHandler(new wxXPMFileHandler
);
1431 AddHandler(new wxXPMDataHandler
);
1432 AddHandler(new wxBMPResourceHandler
);
1433 AddHandler(new wxBMPFileHandler
);