1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
18 #include "wx/bitmap.h"
22 #include "wx/xpmdecod.h"
24 #include "wx/rawbmp.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
27 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
28 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
31 #include <ApplicationServices/ApplicationServices.h>
33 #include <PictUtils.h>
36 #include "wx/mac/uma.h"
38 CTabHandle
wxMacCreateColorTable( int numColors
)
40 CTabHandle newColors
; /* Handle to the new color table */
42 /* Allocate memory for the color table */
43 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
44 sizeof (ColorSpec
) * (numColors
- 1) );
47 /* Initialize the fields */
48 (**newColors
).ctSeed
= GetCTSeed();
49 (**newColors
).ctFlags
= 0;
50 (**newColors
).ctSize
= numColors
- 1;
51 /* Initialize the table of colors */
56 void wxMacDestroyColorTable( CTabHandle colors
)
58 DisposeHandle( (Handle
) colors
) ;
61 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
63 (**newColors
).ctTable
[index
].value
= index
;
64 (**newColors
).ctTable
[index
].rgb
.red
= red
; // someRedValue;
65 (**newColors
).ctTable
[index
].rgb
.green
= green
; // someGreenValue;
66 (**newColors
).ctTable
[index
].rgb
.blue
= blue
; // someBlueValue;
69 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
73 Rect rect
= { 0 , 0 , height
, width
} ;
77 depth
= wxDisplayDepth() ;
80 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
88 void wxMacDestroyGWorld( GWorldPtr gw
)
94 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
96 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
98 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
99 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
100 OSErr error
; /* Returns error code */
101 short bytesPerRow
; /* Number of bytes per row in the PixMap */
108 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
110 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
114 error
= HandToHand((Handle
*) &newColors
);
118 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
124 /* Allocate pixel image; long integer multiplication avoids overflow */
125 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
127 if ((**icon
).iconData
!= nil
)
129 /* Initialize fields common to indexed and direct PixMaps */
130 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
131 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
133 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
134 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
135 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
136 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
137 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
138 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
139 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
141 /* Initialize fields specific to indexed and direct PixMaps */
144 /* PixMap is indexed */
145 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
146 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
147 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
148 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
152 /* PixMap is direct */
153 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
154 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
156 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
158 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
159 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
160 (**newColors
).ctFlags
= 0;
161 (**newColors
).ctSize
= 0;
162 (**icon
).iconPMap
.pmTable
= newColors
;
171 /* If no errors occured, return a handle to the new off-screen PixMap */
174 if (newColors
!= nil
)
175 DisposeCTable(newColors
);
178 /* Return the error code */
182 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
187 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
188 SetGWorld(image
,nil
);
190 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
191 Rect imageBounds
= frame
;
192 GetPortBounds( image
, &imageBounds
) ;
194 int bwSize
= iconSize
/ 8 * iconSize
;
195 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
196 HLock((Handle
)icon
) ;
197 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
198 HLock( (**icon
).iconData
) ;
199 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
201 LockPixels(GetGWorldPixMap(image
));
203 CopyBits(GetPortBitMapForCopyBits(image
),
204 (BitMapPtr
)&((**icon
).iconPMap
),
207 srcCopy
| ditherCopy
, nil
);
210 UnlockPixels(GetGWorldPixMap(image
));
211 HUnlock( (**icon
).iconData
) ;
213 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
214 (**icon
).iconMask
.bounds
= frame
;
216 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
217 (**icon
).iconBMap
.bounds
= frame
;
218 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
219 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
224 GetPortBounds( image
, &r
) ;
225 LockPixels(GetGWorldPixMap(mask
) ) ;
226 CopyBits(GetPortBitMapForCopyBits(mask
) ,
227 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
228 CopyBits(GetPortBitMapForCopyBits(mask
) ,
229 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
230 UnlockPixels(GetGWorldPixMap( mask
) ) ;
235 GetPortBounds( image
, &r
) ;
236 LockPixels(GetGWorldPixMap(image
));
237 CopyBits(GetPortBitMapForCopyBits(image
) ,
238 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
239 CopyBits(GetPortBitMapForCopyBits(image
) ,
240 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
241 UnlockPixels(GetGWorldPixMap(image
));
244 (**icon
).iconMask
.baseAddr
= NULL
;
245 (**icon
).iconBMap
.baseAddr
= NULL
;
246 (**icon
).iconPMap
.baseAddr
= NULL
;
247 HUnlock((Handle
)icon
) ;
248 SetGWorld(saveWorld
,saveHandle
);
253 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
260 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
261 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
263 GetGWorld( &origPort
, &origDev
) ;
265 RgnHandle clipRgn
= NULL
;
270 LockPixels( GetGWorldPixMap( mask
) ) ;
271 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
272 UnlockPixels( GetGWorldPixMap( mask
) ) ;
275 SetGWorld( wp
, NULL
) ;
278 GetRegionBounds( clipRgn
, &portRect
) ;
280 GetPortBounds( wp
, &portRect
) ;
281 pict
= OpenPicture(&portRect
);
284 RGBForeColor( &black
) ;
285 RGBBackColor( &white
) ;
290 LockPixels( GetGWorldPixMap( wp
) ) ;
291 CopyBits(GetPortBitMapForCopyBits(wp
),
292 GetPortBitMapForCopyBits(wp
),
296 UnlockPixels( GetGWorldPixMap( wp
) ) ;
299 SetGWorld( origPort
, origDev
) ;
301 DisposeRgn( clipRgn
) ;
305 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
307 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
310 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
314 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
316 info
->contentType
= kControlContentPictHandle
;
317 info
->u
.picture
= MAC_WXHMETAFILE(bmap
->m_hPict
) ;
319 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
321 if ( (forceType
== kControlContentCIconHandle
|| ( bmap
->m_width
== bmap
->m_height
&& forceType
!= kControlContentPictHandle
) ) && ((bmap
->m_width
& 0x3) == 0) )
323 info
->contentType
= kControlContentCIconHandle
;
324 if ( bitmap
.GetMask() )
326 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap()) ,
327 8 , bmap
->m_width
) ;
331 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
,
332 8 , bmap
->m_width
) ;
337 info
->contentType
= kControlContentPictHandle
;
338 if ( bitmap
.GetMask() )
340 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap() ) ) ;
344 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
) ;
348 else if ( bmap
->m_bitmapType
== kMacBitmapTypeIcon
)
350 info
->contentType
= kControlContentCIconHandle
;
351 info
->u
.cIconHandle
= MAC_WXHICON(bmap
->m_hIcon
) ;
356 wxBitmapRefData::wxBitmapRefData()
368 m_bitmapType
= kMacBitmapTypeUnknownType
;
371 // TODO move this to a public function of Bitmap Ref
372 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
377 switch (data
->m_bitmapType
)
379 case kMacBitmapTypePict
:
383 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
384 data
->m_hPict
= NULL
;
388 case kMacBitmapTypeGrafWorld
:
390 if ( data
->m_hBitmap
)
392 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
393 data
->m_hBitmap
= NULL
;
397 case kMacBitmapTypeIcon
:
400 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
401 data
->m_hIcon
= NULL
;
409 if (data
->m_bitmapMask
)
411 delete data
->m_bitmapMask
;
412 data
->m_bitmapMask
= NULL
;
416 wxBitmapRefData::~wxBitmapRefData()
418 DisposeBitmapRefData( this ) ;
421 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
432 wxBitmap::~wxBitmap()
436 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
438 m_refData
= new wxBitmapRefData
;
440 M_BITMAPDATA
->m_width
= the_width
;
441 M_BITMAPDATA
->m_height
= the_height
;
442 M_BITMAPDATA
->m_depth
= no_bits
;
443 M_BITMAPDATA
->m_numColors
= 0;
446 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
447 MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) = wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
448 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
451 GDHandle origDevice
;
453 GetGWorld( &origPort
, &origDevice
) ;
454 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
455 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
457 // bits is a char array
459 unsigned char* linestart
= (unsigned char*) bits
;
460 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
461 if ( the_width
% (sizeof(unsigned char) * 8) ) {
462 linesize
+= sizeof(unsigned char);
465 RGBColor colors
[2] = {
466 { 0xFFFF , 0xFFFF , 0xFFFF } ,
470 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
472 for ( int x
= 0 ; x
< the_width
; ++x
)
476 int mask
= 1 << bit
;
477 if ( linestart
[index
] & mask
)
479 SetCPixel( x
, y
, &colors
[1] ) ;
483 SetCPixel( x
, y
, &colors
[0] ) ;
487 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
489 SetGWorld( origPort
, origDevice
) ;
493 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
497 wxBitmap::wxBitmap(int w
, int h
, int d
)
499 (void)Create(w
, h
, d
);
502 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
504 (void) Create(data
, type
, width
, height
, depth
);
507 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
509 LoadFile(filename
, type
);
512 bool wxBitmap::CreateFromXpm(const char **bits
)
514 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
515 wxXPMDecoder decoder
;
516 wxImage img
= decoder
.ReadData(bits
);
517 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
518 *this = wxBitmap(img
);
522 wxBitmap::wxBitmap(const char **bits
)
524 (void) CreateFromXpm(bits
);
527 wxBitmap::wxBitmap(char **bits
)
529 (void) CreateFromXpm((const char **)bits
);
532 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
535 (rect
.x
>= 0) && (rect
.y
>= 0) &&
536 (rect
.x
+rect
.width
<= GetWidth()) &&
537 (rect
.y
+rect
.height
<= GetHeight()),
538 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
541 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
542 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
547 GetGWorld( &origPort
, &origDevice
);
549 // Update the subbitmaps reference data
550 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
552 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
553 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
554 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
556 // Copy sub region of this bitmap
557 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
559 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
561 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
566 GWorldPtr submask
, mask
;
569 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
570 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetMask()->GetDepth() );
571 LockPixels(GetGWorldPixMap(mask
));
572 LockPixels(GetGWorldPixMap(submask
));
574 for(int yy
= 0; yy
< rect
.height
; yy
++)
576 for(int xx
= 0; xx
< rect
.width
; xx
++)
578 SetGWorld(mask
, NULL
);
579 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
580 SetGWorld(submask
, NULL
);
581 SetCPixel(xx
,yy
, &color
);
584 UnlockPixels(GetGWorldPixMap(mask
));
585 UnlockPixels(GetGWorldPixMap(submask
));
586 ref
->m_bitmapMask
= new wxMask
;
587 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
593 GWorldPtr subbitmap
, bitmap
;
596 bitmap
= (GWorldPtr
) GetHBITMAP();
597 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
598 LockPixels(GetGWorldPixMap(bitmap
));
599 LockPixels(GetGWorldPixMap(subbitmap
));
601 for(int yy
= 0; yy
< rect
.height
; yy
++)
603 for(int xx
= 0; xx
< rect
.width
; xx
++)
605 SetGWorld(bitmap
, NULL
);
606 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
607 SetGWorld(subbitmap
, NULL
);
608 SetCPixel(xx
, yy
, &color
);
611 UnlockPixels(GetGWorldPixMap(bitmap
));
612 UnlockPixels(GetGWorldPixMap(subbitmap
));
615 SetGWorld( origPort
, origDevice
);
620 bool wxBitmap::Create(int w
, int h
, int d
)
624 m_refData
= new wxBitmapRefData
;
626 M_BITMAPDATA
->m_width
= w
;
627 M_BITMAPDATA
->m_height
= h
;
628 M_BITMAPDATA
->m_depth
= d
;
630 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
631 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
632 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
633 return M_BITMAPDATA
->m_ok
;
636 int wxBitmap::GetBitmapType() const
638 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
640 return M_BITMAPDATA
->m_bitmapType
;
643 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
646 m_refData
= new wxBitmapRefData
;
648 DisposeBitmapRefData( M_BITMAPDATA
) ;
650 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
651 M_BITMAPDATA
->m_hBitmap
= bmp
;
652 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
655 void wxBitmap::SetHICON(WXHICON ico
)
658 m_refData
= new wxBitmapRefData
;
660 DisposeBitmapRefData( M_BITMAPDATA
) ;
662 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeIcon
;
663 M_BITMAPDATA
->m_hIcon
= ico
;
664 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hIcon
!= NULL
) ;
667 void wxBitmap::SetPict(WXHMETAFILE pict
)
670 m_refData
= new wxBitmapRefData
;
672 DisposeBitmapRefData( M_BITMAPDATA
) ;
674 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypePict
;
675 M_BITMAPDATA
->m_hPict
= pict
;
676 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hPict
!= NULL
) ;
679 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
683 wxBitmapHandler
*handler
= FindHandler(type
);
687 m_refData
= new wxBitmapRefData
;
689 return handler
->LoadFile(this, filename
, type
, -1, -1);
693 wxImage
loadimage(filename
, type
);
694 if (loadimage
.Ok()) {
699 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
703 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
707 m_refData
= new wxBitmapRefData
;
709 wxBitmapHandler
*handler
= FindHandler(type
);
711 if ( handler
== NULL
) {
712 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
717 return handler
->Create(this, data
, type
, width
, height
, depth
);
720 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
722 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
723 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
725 m_refData
= new wxBitmapRefData();
727 // width and height of the device-dependent bitmap
728 int width
= image
.GetWidth();
729 int height
= image
.GetHeight();
733 Create( width
, height
, 32 ) ;
736 GDHandle origDevice
;
738 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
739 LockPixels( pixMap
);
741 GetGWorld( &origPort
, &origDevice
) ;
742 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
745 register unsigned char* data
= image
.GetData();
746 char* destinationBase
= GetPixBaseAddr( pixMap
);
747 register unsigned char* destination
= (unsigned char*) destinationBase
;
748 for (int y
= 0; y
< height
; y
++)
750 for (int x
= 0; x
< width
; x
++)
753 *destination
++ = *data
++ ;
754 *destination
++ = *data
++ ;
755 *destination
++ = *data
++ ;
757 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
758 destination
= (unsigned char*) destinationBase
;
760 if ( image
.HasAlpha() )
762 unsigned char *alpha
= image
.GetAlpha();
764 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
766 wxBitmap maskBitmap
;
768 maskBitmap
.Create( width
, height
, 24);
769 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
770 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
772 for (int y
= 0; y
< height
; y
++)
774 for (int x
= 0; x
< width
; x
++)
776 memset( &color
, 255 - *alpha
, sizeof( color
) );
777 SetCPixel(x
,y
, &color
);
782 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
783 SetMask(new wxMask( maskBitmap
));
784 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
786 else if ( image
.HasMask() )
788 data
= image
.GetData();
790 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
791 RGBColor white
= { 0xffff, 0xffff, 0xffff };
792 RGBColor black
= { 0 , 0 , 0 };
793 wxBitmap maskBitmap
;
795 maskBitmap
.Create( width
, height
, 1);
796 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
797 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
799 for (int y
= 0; y
< height
; y
++)
801 for (int x
= 0; x
< width
; x
++)
803 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
805 SetCPixel(x
,y
, &white
);
808 SetCPixel(x
,y
, &black
);
813 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
814 SetMask(new wxMask( maskBitmap
));
815 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
818 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
819 SetGWorld( origPort
, origDevice
);
822 wxImage
wxBitmap::ConvertToImage() const
826 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
828 // create an wxImage object
829 int width
= GetWidth();
830 int height
= GetHeight();
831 image
.Create( width
, height
);
833 unsigned char *data
= image
.GetData();
835 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
839 RgnHandle maskRgn
= NULL
;
840 GWorldPtr tempPort
= NULL
;
843 // background color set to RGB(16,16,16) in consistent with wxGTK
844 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
846 wxMask
*mask
= GetMask();
848 GetGWorld( &origPort
, &origDevice
);
849 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
851 tempPort
= wxMacCreateGWorld( width
, height
, -1) ;
855 tempPort
= (GWorldPtr
) GetHBITMAP() ;
857 LockPixels(GetGWorldPixMap(tempPort
));
858 SetGWorld( tempPort
, NULL
);
859 if ( GetBitmapType() == kMacBitmapTypePict
|| GetBitmapType() == kMacBitmapTypeIcon
)
861 Rect bitmaprect
= { 0 , 0 , height
, width
};
862 if ( GetBitmapType() == kMacBitmapTypeIcon
)
864 ::PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, MAC_WXHICON(GetHICON()) ) ;
866 BitMapToRegion( maskRgn
, &(**(MAC_WXHICON(GetHICON()))).iconMask
) ;
869 ::DrawPicture( (PicHandle
) GetPict(), &bitmaprect
) ;
871 // Copy data into image
873 for (int yy
= 0; yy
< height
; yy
++)
875 for (int xx
= 0; xx
< width
; xx
++)
877 GetCPixel(xx
,yy
, &color
);
878 r
= ((color
.red
) >> 8);
879 g
= ((color
.green
) >> 8);
880 b
= ((color
.blue
) >> 8);
889 if ( !PtInRgn( pt
, maskRgn
) )
891 data
[index
] = mask_r
;
892 data
[index
+ 1] = mask_g
;
893 data
[index
+ 2] = mask_b
;
900 if (mask
->PointMasked(xx
,yy
))
902 data
[index
] = mask_r
;
903 data
[index
+ 1] = mask_g
;
904 data
[index
+ 2] = mask_b
;
911 if (mask
|| maskRgn
)
913 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
914 image
.SetMask( true );
918 UnlockPixels(GetGWorldPixMap( tempPort
));
919 SetGWorld(origPort
, origDevice
);
920 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
922 wxMacDestroyGWorld( tempPort
) ;
926 DisposeRgn( maskRgn
) ;
933 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
934 const wxPalette
*palette
) const
936 wxBitmapHandler
*handler
= FindHandler(type
);
940 return handler
->SaveFile(this, filename
, type
, palette
);
944 wxImage image
= ConvertToImage();
946 return image
.SaveFile(filename
, type
);
949 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
953 bool wxBitmap::Ok() const
955 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
958 int wxBitmap::GetHeight() const
960 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
962 return M_BITMAPDATA
->m_height
;
965 int wxBitmap::GetWidth() const
967 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
969 return M_BITMAPDATA
->m_width
;
972 int wxBitmap::GetDepth() const
974 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
976 return M_BITMAPDATA
->m_depth
;
979 int wxBitmap::GetQuality() const
981 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
983 return M_BITMAPDATA
->m_quality
;
986 wxMask
*wxBitmap::GetMask() const
988 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
990 return M_BITMAPDATA
->m_bitmapMask
;
993 void wxBitmap::SetWidth(int w
)
996 m_refData
= new wxBitmapRefData
;
998 M_BITMAPDATA
->m_width
= w
;
1001 void wxBitmap::SetHeight(int h
)
1004 m_refData
= new wxBitmapRefData
;
1006 M_BITMAPDATA
->m_height
= h
;
1009 void wxBitmap::SetDepth(int d
)
1012 m_refData
= new wxBitmapRefData
;
1014 M_BITMAPDATA
->m_depth
= d
;
1017 void wxBitmap::SetQuality(int q
)
1020 m_refData
= new wxBitmapRefData
;
1022 M_BITMAPDATA
->m_quality
= q
;
1025 void wxBitmap::SetOk(bool isOk
)
1028 m_refData
= new wxBitmapRefData
;
1030 M_BITMAPDATA
->m_ok
= isOk
;
1033 wxPalette
*wxBitmap::GetPalette() const
1035 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1037 return &M_BITMAPDATA
->m_bitmapPalette
;
1040 void wxBitmap::SetPalette(const wxPalette
& palette
)
1043 m_refData
= new wxBitmapRefData
;
1045 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1048 void wxBitmap::SetMask(wxMask
*mask
)
1051 m_refData
= new wxBitmapRefData
;
1053 // Remove existing mask if there is one.
1054 if (M_BITMAPDATA
->m_bitmapMask
)
1055 delete M_BITMAPDATA
->m_bitmapMask
;
1057 M_BITMAPDATA
->m_bitmapMask
= mask
;
1060 WXHBITMAP
wxBitmap::GetHBITMAP() const
1062 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1064 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1067 WXHMETAFILE
wxBitmap::GetPict( bool *created
) const
1069 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1071 PicHandle picture
= NULL
; // This is the returned picture
1073 (*created
) = false ;
1074 // If bitmap already in Pict format return pointer
1075 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
1076 return M_BITMAPDATA
->m_hPict
;
1078 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
1086 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
1090 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
1092 if ( created
&& picture
)
1103 : m_maskBitmap(NULL
)
1107 // Construct a mask from a bitmap and a colour indicating
1108 // the transparent area
1109 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1110 : m_maskBitmap(NULL
)
1112 Create(bitmap
, colour
);
1115 // Construct a mask from a bitmap and a palette index indicating
1116 // the transparent area
1117 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1118 : m_maskBitmap(NULL
)
1120 Create(bitmap
, paletteIndex
);
1123 // Construct a mask from a mono bitmap (copies the bitmap).
1124 wxMask::wxMask(const wxBitmap
& bitmap
)
1125 : m_maskBitmap(NULL
)
1134 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1135 m_maskBitmap
= NULL
;
1139 // Create a mask from a mono bitmap (copies the bitmap).
1140 bool wxMask::Create(const wxBitmap
& bitmap
)
1144 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1145 m_maskBitmap
= NULL
;
1147 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1148 wxT("Cannot create mask from this bitmap type (TODO)"));
1149 // other types would require a temporary bitmap. not yet implemented
1151 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1153 m_depth
= bitmap
.GetDepth() ;
1154 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), bitmap
.GetDepth() );
1155 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1157 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1158 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1159 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1160 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1161 &rect
, &rect
, srcCopy
, 0);
1162 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1163 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1168 // Create a mask from a bitmap and a palette index indicating
1169 // the transparent area
1170 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1173 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1177 // Create a mask from a bitmap and a colour indicating
1178 // the transparent area
1179 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1183 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1184 m_maskBitmap
= NULL
;
1186 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1187 wxT("Cannot create mask from this bitmap type (TODO)"));
1188 // other types would require a temporary bitmap. not yet implemented
1190 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1192 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1194 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1195 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1196 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1198 // this is not very efficient, but I can't think
1199 // of a better way of doing it
1201 GDHandle origDevice
;
1203 RGBColor colors
[2] = {
1204 { 0xFFFF, 0xFFFF, 0xFFFF },
1207 GetGWorld( &origPort
, &origDevice
) ;
1208 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1210 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1212 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1213 GetCPixel( w
, h
, &col
) ;
1214 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1215 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1217 SetCPixel( w
, h
, &colors
[0] ) ;
1221 SetCPixel( w
, h
, &colors
[1] ) ;
1225 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1226 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1227 SetGWorld( origPort
, origDevice
) ;
1232 bool wxMask::PointMasked(int x
, int y
)
1235 GDHandle origDevice
;
1239 GetGWorld( &origPort
, &origDevice
);
1241 //Set port to mask and see if it masked (1) or not ( 0 )
1242 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1243 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1244 GetCPixel(x
,y
, &color
);
1245 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1246 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1248 SetGWorld( origPort
, origDevice
);
1257 wxBitmapHandler::~wxBitmapHandler()
1261 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1266 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1267 int desiredWidth
, int desiredHeight
)
1272 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1281 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1283 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1285 inline wxPICTResourceHandler()
1287 m_name
= wxT("Macintosh Pict resource");
1288 m_extension
= wxEmptyString
;
1289 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1292 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1293 int desiredWidth
, int desiredHeight
);
1295 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1297 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1298 int desiredWidth
, int desiredHeight
)
1301 wxMacStringToPascal( name
, theName
) ;
1303 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1308 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1309 DetachResource( (Handle
) thePict
) ;
1310 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1311 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1312 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1313 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1315 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1316 M_BITMAPHANDLERDATA
->m_ok
= true ;
1317 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1318 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1319 // M_BITMAPHANDLERDATA->m_quality;
1325 void wxBitmap::InitStandardHandlers()
1327 AddHandler(new wxPICTResourceHandler
) ;
1328 AddHandler(new wxICONResourceHandler
) ;
1331 // ----------------------------------------------------------------------------
1332 // raw bitmap access support
1333 // ----------------------------------------------------------------------------
1335 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1339 // no bitmap, no data (raw or otherwise)
1343 if ( M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
)
1345 wxFAIL_MSG( _T("GetRawData() only supported for GWorlds") );
1350 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1351 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1352 wxCHECK_MSG( hPixMap
&& *hPixMap
, NULL
,
1353 _T("GetRawData(): failed to get PixMap from GWorld?") );
1355 wxCHECK_MSG( (*hPixMap
)->pixelSize
== bpp
, NULL
,
1356 _T("GetRawData(): pixel format mismatch") );
1358 if ( !LockPixels(hPixMap
) )
1360 wxFAIL_MSG( _T("failed to lock PixMap in GetRawData()") );
1365 data
.m_width
= GetWidth();
1366 data
.m_height
= GetHeight();
1367 data
.m_stride
= (*hPixMap
)->rowBytes
& 0x7fff;
1369 return GetPixBaseAddr(hPixMap
);
1372 void wxBitmap::UngetRawData(wxPixelDataBase
& data
)
1377 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1378 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1381 UnlockPixels(hPixMap
);
1385 void wxBitmap::UseAlpha()
1387 // nothing to do here so far