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 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
25 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
26 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
29 #include <ApplicationServices/ApplicationServices.h>
31 #include <PictUtils.h>
34 #include "wx/mac/uma.h"
36 CTabHandle
wxMacCreateColorTable( int numColors
)
38 CTabHandle newColors
; /* Handle to the new color table */
40 /* Allocate memory for the color table */
41 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
42 sizeof (ColorSpec
) * (numColors
- 1) );
45 /* Initialize the fields */
46 (**newColors
).ctSeed
= GetCTSeed();
47 (**newColors
).ctFlags
= 0;
48 (**newColors
).ctSize
= numColors
- 1;
49 /* Initialize the table of colors */
54 void wxMacDestroyColorTable( CTabHandle colors
)
56 DisposeHandle( (Handle
) colors
) ;
59 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
61 (**newColors
).ctTable
[index
].value
= index
;
62 (**newColors
).ctTable
[index
].rgb
.red
= red
; // someRedValue;
63 (**newColors
).ctTable
[index
].rgb
.green
= green
; // someGreenValue;
64 (**newColors
).ctTable
[index
].rgb
.blue
= blue
; // someBlueValue;
67 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
71 Rect rect
= { 0 , 0 , height
, width
} ;
75 depth
= wxDisplayDepth() ;
78 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
86 void wxMacDestroyGWorld( GWorldPtr gw
)
92 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
94 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
96 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
97 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
98 OSErr error
; /* Returns error code */
99 short bytesPerRow
; /* Number of bytes per row in the PixMap */
106 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
108 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
112 error
= HandToHand((Handle
*) &newColors
);
116 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
122 /* Allocate pixel image; long integer multiplication avoids overflow */
123 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
125 if ((**icon
).iconData
!= nil
)
127 /* Initialize fields common to indexed and direct PixMaps */
128 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
129 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
131 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
132 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
133 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
134 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
135 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
136 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
137 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
139 /* Initialize fields specific to indexed and direct PixMaps */
142 /* PixMap is indexed */
143 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
144 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
145 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
146 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
150 /* PixMap is direct */
151 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
152 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
154 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
156 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
157 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
158 (**newColors
).ctFlags
= 0;
159 (**newColors
).ctSize
= 0;
160 (**icon
).iconPMap
.pmTable
= newColors
;
169 /* If no errors occured, return a handle to the new off-screen PixMap */
172 if (newColors
!= nil
)
173 DisposeCTable(newColors
);
176 /* Return the error code */
180 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
185 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
186 SetGWorld(image
,nil
);
188 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
189 Rect imageBounds
= frame
;
190 GetPortBounds( image
, &imageBounds
) ;
192 int bwSize
= iconSize
/ 8 * iconSize
;
193 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
194 HLock((Handle
)icon
) ;
195 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
196 HLock( (**icon
).iconData
) ;
197 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
199 LockPixels(GetGWorldPixMap(image
));
201 CopyBits(GetPortBitMapForCopyBits(image
),
202 (BitMapPtr
)&((**icon
).iconPMap
),
205 srcCopy
| ditherCopy
, nil
);
208 UnlockPixels(GetGWorldPixMap(image
));
209 HUnlock( (**icon
).iconData
) ;
211 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
212 (**icon
).iconMask
.bounds
= frame
;
214 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
215 (**icon
).iconBMap
.bounds
= frame
;
216 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
217 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
222 GetPortBounds( image
, &r
) ;
223 LockPixels(GetGWorldPixMap(mask
) ) ;
224 CopyBits(GetPortBitMapForCopyBits(mask
) ,
225 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
226 CopyBits(GetPortBitMapForCopyBits(mask
) ,
227 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
228 UnlockPixels(GetGWorldPixMap( mask
) ) ;
233 GetPortBounds( image
, &r
) ;
234 LockPixels(GetGWorldPixMap(image
));
235 CopyBits(GetPortBitMapForCopyBits(image
) ,
236 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
237 CopyBits(GetPortBitMapForCopyBits(image
) ,
238 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
239 UnlockPixels(GetGWorldPixMap(image
));
242 (**icon
).iconMask
.baseAddr
= NULL
;
243 (**icon
).iconBMap
.baseAddr
= NULL
;
244 (**icon
).iconPMap
.baseAddr
= NULL
;
245 HUnlock((Handle
)icon
) ;
246 SetGWorld(saveWorld
,saveHandle
);
251 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
258 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
259 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
261 GetGWorld( &origPort
, &origDev
) ;
263 RgnHandle clipRgn
= NULL
;
268 LockPixels( GetGWorldPixMap( mask
) ) ;
269 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
270 UnlockPixels( GetGWorldPixMap( mask
) ) ;
273 SetGWorld( wp
, NULL
) ;
276 GetRegionBounds( clipRgn
, &portRect
) ;
278 GetPortBounds( wp
, &portRect
) ;
279 pict
= OpenPicture(&portRect
);
282 RGBForeColor( &black
) ;
283 RGBBackColor( &white
) ;
288 LockPixels( GetGWorldPixMap( wp
) ) ;
289 CopyBits(GetPortBitMapForCopyBits(wp
),
290 GetPortBitMapForCopyBits(wp
),
294 UnlockPixels( GetGWorldPixMap( wp
) ) ;
297 SetGWorld( origPort
, origDev
) ;
299 DisposeRgn( clipRgn
) ;
303 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
305 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
308 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
312 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
314 info
->contentType
= kControlContentPictHandle
;
315 info
->u
.picture
= MAC_WXHMETAFILE(bmap
->m_hPict
) ;
317 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
319 if ( (forceType
== kControlContentCIconHandle
|| ( bmap
->m_width
== bmap
->m_height
&& forceType
!= kControlContentPictHandle
) ) && ((bmap
->m_width
& 0x3) == 0) )
321 info
->contentType
= kControlContentCIconHandle
;
322 if ( bitmap
.GetMask() )
324 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap()) ,
325 8 , bmap
->m_width
) ;
329 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
,
330 8 , bmap
->m_width
) ;
335 info
->contentType
= kControlContentPictHandle
;
336 if ( bitmap
.GetMask() )
338 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap() ) ) ;
342 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
) ;
346 else if ( bmap
->m_bitmapType
== kMacBitmapTypeIcon
)
348 info
->contentType
= kControlContentCIconHandle
;
349 info
->u
.cIconHandle
= MAC_WXHICON(bmap
->m_hIcon
) ;
354 wxBitmapRefData::wxBitmapRefData()
366 m_bitmapType
= kMacBitmapTypeUnknownType
;
369 // TODO move this to a public function of Bitmap Ref
370 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
375 switch (data
->m_bitmapType
)
377 case kMacBitmapTypePict
:
381 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
382 data
->m_hPict
= NULL
;
386 case kMacBitmapTypeGrafWorld
:
388 if ( data
->m_hBitmap
)
390 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
391 data
->m_hBitmap
= NULL
;
395 case kMacBitmapTypeIcon
:
398 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
399 data
->m_hIcon
= NULL
;
407 if (data
->m_bitmapMask
)
409 delete data
->m_bitmapMask
;
410 data
->m_bitmapMask
= NULL
;
414 wxBitmapRefData::~wxBitmapRefData()
416 DisposeBitmapRefData( this ) ;
419 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
430 wxBitmap::~wxBitmap()
434 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
436 m_refData
= new wxBitmapRefData
;
438 M_BITMAPDATA
->m_width
= the_width
;
439 M_BITMAPDATA
->m_height
= the_height
;
440 M_BITMAPDATA
->m_depth
= no_bits
;
441 M_BITMAPDATA
->m_numColors
= 0;
444 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
445 MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) = wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
446 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
449 GDHandle origDevice
;
451 GetGWorld( &origPort
, &origDevice
) ;
452 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
453 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
455 // bits is a char array
457 unsigned char* linestart
= (unsigned char*) bits
;
458 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
459 if ( the_width
% (sizeof(unsigned char) * 8) ) {
460 linesize
+= sizeof(unsigned char);
463 RGBColor colors
[2] = {
464 { 0xFFFF , 0xFFFF , 0xFFFF } ,
468 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
470 for ( int x
= 0 ; x
< the_width
; ++x
)
474 int mask
= 1 << bit
;
475 if ( linestart
[index
] & mask
)
477 SetCPixel( x
, y
, &colors
[1] ) ;
481 SetCPixel( x
, y
, &colors
[0] ) ;
485 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
487 SetGWorld( origPort
, origDevice
) ;
491 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
495 wxBitmap::wxBitmap(int w
, int h
, int d
)
497 (void)Create(w
, h
, d
);
500 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
502 (void) Create(data
, type
, width
, height
, depth
);
505 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
507 LoadFile(filename
, type
);
510 bool wxBitmap::CreateFromXpm(const char **bits
)
512 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
513 wxXPMDecoder decoder
;
514 wxImage img
= decoder
.ReadData(bits
);
515 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
516 *this = wxBitmap(img
);
520 wxBitmap::wxBitmap(const char **bits
)
522 (void) CreateFromXpm(bits
);
525 wxBitmap::wxBitmap(char **bits
)
527 (void) CreateFromXpm((const char **)bits
);
530 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
533 (rect
.x
>= 0) && (rect
.y
>= 0) &&
534 (rect
.x
+rect
.width
<= GetWidth()) &&
535 (rect
.y
+rect
.height
<= GetHeight()),
536 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
539 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
540 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
545 GetGWorld( &origPort
, &origDevice
);
547 // Update the subbitmaps reference data
548 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
550 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
551 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
552 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
554 // Copy sub region of this bitmap
555 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
557 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
559 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
564 GWorldPtr submask
, mask
;
567 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
568 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetMask()->GetDepth() );
569 LockPixels(GetGWorldPixMap(mask
));
570 LockPixels(GetGWorldPixMap(submask
));
572 for(int yy
= 0; yy
< rect
.height
; yy
++)
574 for(int xx
= 0; xx
< rect
.width
; xx
++)
576 SetGWorld(mask
, NULL
);
577 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
578 SetGWorld(submask
, NULL
);
579 SetCPixel(xx
,yy
, &color
);
582 UnlockPixels(GetGWorldPixMap(mask
));
583 UnlockPixels(GetGWorldPixMap(submask
));
584 ref
->m_bitmapMask
= new wxMask
;
585 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
591 GWorldPtr subbitmap
, bitmap
;
594 bitmap
= (GWorldPtr
) GetHBITMAP();
595 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
596 LockPixels(GetGWorldPixMap(bitmap
));
597 LockPixels(GetGWorldPixMap(subbitmap
));
599 for(int yy
= 0; yy
< rect
.height
; yy
++)
601 for(int xx
= 0; xx
< rect
.width
; xx
++)
603 SetGWorld(bitmap
, NULL
);
604 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
605 SetGWorld(subbitmap
, NULL
);
606 SetCPixel(xx
, yy
, &color
);
609 UnlockPixels(GetGWorldPixMap(bitmap
));
610 UnlockPixels(GetGWorldPixMap(subbitmap
));
613 SetGWorld( origPort
, origDevice
);
618 bool wxBitmap::Create(int w
, int h
, int d
)
622 m_refData
= new wxBitmapRefData
;
624 M_BITMAPDATA
->m_width
= w
;
625 M_BITMAPDATA
->m_height
= h
;
626 M_BITMAPDATA
->m_depth
= d
;
628 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
629 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
630 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
631 return M_BITMAPDATA
->m_ok
;
634 int wxBitmap::GetBitmapType() const
636 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
638 return M_BITMAPDATA
->m_bitmapType
;
641 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
644 m_refData
= new wxBitmapRefData
;
646 DisposeBitmapRefData( M_BITMAPDATA
) ;
648 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
649 M_BITMAPDATA
->m_hBitmap
= bmp
;
650 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
653 void wxBitmap::SetHICON(WXHICON ico
)
656 m_refData
= new wxBitmapRefData
;
658 DisposeBitmapRefData( M_BITMAPDATA
) ;
660 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeIcon
;
661 M_BITMAPDATA
->m_hIcon
= ico
;
662 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hIcon
!= NULL
) ;
665 void wxBitmap::SetPict(WXHMETAFILE pict
)
668 m_refData
= new wxBitmapRefData
;
670 DisposeBitmapRefData( M_BITMAPDATA
) ;
672 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypePict
;
673 M_BITMAPDATA
->m_hPict
= pict
;
674 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hPict
!= NULL
) ;
677 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
681 wxBitmapHandler
*handler
= FindHandler(type
);
685 m_refData
= new wxBitmapRefData
;
687 return handler
->LoadFile(this, filename
, type
, -1, -1);
691 wxImage
loadimage(filename
, type
);
692 if (loadimage
.Ok()) {
697 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
701 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
705 m_refData
= new wxBitmapRefData
;
707 wxBitmapHandler
*handler
= FindHandler(type
);
709 if ( handler
== NULL
) {
710 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
715 return handler
->Create(this, data
, type
, width
, height
, depth
);
718 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
720 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
721 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
723 m_refData
= new wxBitmapRefData();
725 // width and height of the device-dependent bitmap
726 int width
= image
.GetWidth();
727 int height
= image
.GetHeight();
731 Create( width
, height
, 32 ) ;
734 GDHandle origDevice
;
736 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
737 LockPixels( pixMap
);
739 GetGWorld( &origPort
, &origDevice
) ;
740 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
743 register unsigned char* data
= image
.GetData();
744 char* destinationBase
= GetPixBaseAddr( pixMap
);
745 register unsigned char* destination
= (unsigned char*) destinationBase
;
746 for (int y
= 0; y
< height
; y
++)
748 for (int x
= 0; x
< width
; x
++)
751 *destination
++ = *data
++ ;
752 *destination
++ = *data
++ ;
753 *destination
++ = *data
++ ;
755 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
756 destination
= (unsigned char*) destinationBase
;
758 if ( image
.HasAlpha() )
760 unsigned char *alpha
= image
.GetAlpha();
762 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
764 wxBitmap maskBitmap
;
766 maskBitmap
.Create( width
, height
, 24);
767 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
768 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
770 for (int y
= 0; y
< height
; y
++)
772 for (int x
= 0; x
< width
; x
++)
774 memset( &color
, 255 - *alpha
, sizeof( color
) );
775 SetCPixel(x
,y
, &color
);
780 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
781 SetMask(new wxMask( maskBitmap
));
782 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
784 else if ( image
.HasMask() )
786 data
= image
.GetData();
788 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
789 RGBColor white
= { 0xffff, 0xffff, 0xffff };
790 RGBColor black
= { 0 , 0 , 0 };
791 wxBitmap maskBitmap
;
793 maskBitmap
.Create( width
, height
, 1);
794 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
795 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
797 for (int y
= 0; y
< height
; y
++)
799 for (int x
= 0; x
< width
; x
++)
801 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
803 SetCPixel(x
,y
, &white
);
806 SetCPixel(x
,y
, &black
);
811 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
812 SetMask(new wxMask( maskBitmap
));
813 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
816 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
817 SetGWorld( origPort
, origDevice
);
820 wxImage
wxBitmap::ConvertToImage() const
824 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
826 // create an wxImage object
827 int width
= GetWidth();
828 int height
= GetHeight();
829 image
.Create( width
, height
);
831 unsigned char *data
= image
.GetData();
833 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
837 RgnHandle maskRgn
= NULL
;
838 GWorldPtr tempPort
= NULL
;
841 // background color set to RGB(16,16,16) in consistent with wxGTK
842 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
844 wxMask
*mask
= GetMask();
846 GetGWorld( &origPort
, &origDevice
);
847 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
849 tempPort
= wxMacCreateGWorld( width
, height
, -1) ;
853 tempPort
= (GWorldPtr
) GetHBITMAP() ;
855 LockPixels(GetGWorldPixMap(tempPort
));
856 SetGWorld( tempPort
, NULL
);
857 if ( GetBitmapType() == kMacBitmapTypePict
|| GetBitmapType() == kMacBitmapTypeIcon
)
859 Rect bitmaprect
= { 0 , 0 , height
, width
};
860 if ( GetBitmapType() == kMacBitmapTypeIcon
)
862 ::PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, MAC_WXHICON(GetHICON()) ) ;
864 BitMapToRegion( maskRgn
, &(**(MAC_WXHICON(GetHICON()))).iconMask
) ;
867 ::DrawPicture( (PicHandle
) GetPict(), &bitmaprect
) ;
869 // Copy data into image
871 for (int yy
= 0; yy
< height
; yy
++)
873 for (int xx
= 0; xx
< width
; xx
++)
875 GetCPixel(xx
,yy
, &color
);
876 r
= ((color
.red
) >> 8);
877 g
= ((color
.green
) >> 8);
878 b
= ((color
.blue
) >> 8);
887 if ( !PtInRgn( pt
, maskRgn
) )
889 data
[index
] = mask_r
;
890 data
[index
+ 1] = mask_g
;
891 data
[index
+ 2] = mask_b
;
898 if (mask
->PointMasked(xx
,yy
))
900 data
[index
] = mask_r
;
901 data
[index
+ 1] = mask_g
;
902 data
[index
+ 2] = mask_b
;
909 if (mask
|| maskRgn
)
911 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
912 image
.SetMask( true );
916 UnlockPixels(GetGWorldPixMap( tempPort
));
917 SetGWorld(origPort
, origDevice
);
918 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
920 wxMacDestroyGWorld( tempPort
) ;
924 DisposeRgn( maskRgn
) ;
931 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
932 const wxPalette
*palette
) const
934 wxBitmapHandler
*handler
= FindHandler(type
);
938 return handler
->SaveFile(this, filename
, type
, palette
);
942 wxImage image
= ConvertToImage();
944 return image
.SaveFile(filename
, type
);
947 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
951 bool wxBitmap::Ok() const
953 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
956 int wxBitmap::GetHeight() const
958 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
960 return M_BITMAPDATA
->m_height
;
963 int wxBitmap::GetWidth() const
965 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
967 return M_BITMAPDATA
->m_width
;
970 int wxBitmap::GetDepth() const
972 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
974 return M_BITMAPDATA
->m_depth
;
977 int wxBitmap::GetQuality() const
979 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
981 return M_BITMAPDATA
->m_quality
;
984 wxMask
*wxBitmap::GetMask() const
986 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
988 return M_BITMAPDATA
->m_bitmapMask
;
991 void wxBitmap::SetWidth(int w
)
994 m_refData
= new wxBitmapRefData
;
996 M_BITMAPDATA
->m_width
= w
;
999 void wxBitmap::SetHeight(int h
)
1002 m_refData
= new wxBitmapRefData
;
1004 M_BITMAPDATA
->m_height
= h
;
1007 void wxBitmap::SetDepth(int d
)
1010 m_refData
= new wxBitmapRefData
;
1012 M_BITMAPDATA
->m_depth
= d
;
1015 void wxBitmap::SetQuality(int q
)
1018 m_refData
= new wxBitmapRefData
;
1020 M_BITMAPDATA
->m_quality
= q
;
1023 void wxBitmap::SetOk(bool isOk
)
1026 m_refData
= new wxBitmapRefData
;
1028 M_BITMAPDATA
->m_ok
= isOk
;
1031 wxPalette
*wxBitmap::GetPalette() const
1033 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1035 return &M_BITMAPDATA
->m_bitmapPalette
;
1038 void wxBitmap::SetPalette(const wxPalette
& palette
)
1041 m_refData
= new wxBitmapRefData
;
1043 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1046 void wxBitmap::SetMask(wxMask
*mask
)
1049 m_refData
= new wxBitmapRefData
;
1051 // Remove existing mask if there is one.
1052 if (M_BITMAPDATA
->m_bitmapMask
)
1053 delete M_BITMAPDATA
->m_bitmapMask
;
1055 M_BITMAPDATA
->m_bitmapMask
= mask
;
1058 WXHBITMAP
wxBitmap::GetHBITMAP() const
1060 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1062 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1065 WXHMETAFILE
wxBitmap::GetPict( bool *created
) const
1067 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1069 PicHandle picture
= NULL
; // This is the returned picture
1071 (*created
) = false ;
1072 // If bitmap already in Pict format return pointer
1073 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
1074 return M_BITMAPDATA
->m_hPict
;
1076 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
1084 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
1088 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
1090 if ( created
&& picture
)
1101 : m_maskBitmap(NULL
)
1105 // Construct a mask from a bitmap and a colour indicating
1106 // the transparent area
1107 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1108 : m_maskBitmap(NULL
)
1110 Create(bitmap
, colour
);
1113 // Construct a mask from a bitmap and a palette index indicating
1114 // the transparent area
1115 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1116 : m_maskBitmap(NULL
)
1118 Create(bitmap
, paletteIndex
);
1121 // Construct a mask from a mono bitmap (copies the bitmap).
1122 wxMask::wxMask(const wxBitmap
& bitmap
)
1123 : m_maskBitmap(NULL
)
1132 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1133 m_maskBitmap
= NULL
;
1137 // Create a mask from a mono bitmap (copies the bitmap).
1138 bool wxMask::Create(const wxBitmap
& bitmap
)
1142 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1143 m_maskBitmap
= NULL
;
1145 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1146 wxT("Cannot create mask from this bitmap type (TODO)"));
1147 // other types would require a temporary bitmap. not yet implemented
1149 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1151 m_depth
= bitmap
.GetDepth() ;
1152 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), bitmap
.GetDepth() );
1153 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1155 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1156 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1157 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1158 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1159 &rect
, &rect
, srcCopy
, 0);
1160 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1161 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1166 // Create a mask from a bitmap and a palette index indicating
1167 // the transparent area
1168 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1171 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1175 // Create a mask from a bitmap and a colour indicating
1176 // the transparent area
1177 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1181 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1182 m_maskBitmap
= NULL
;
1184 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1185 wxT("Cannot create mask from this bitmap type (TODO)"));
1186 // other types would require a temporary bitmap. not yet implemented
1188 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1190 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1192 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1193 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1194 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1196 // this is not very efficient, but I can't think
1197 // of a better way of doing it
1199 GDHandle origDevice
;
1201 RGBColor colors
[2] = {
1202 { 0xFFFF, 0xFFFF, 0xFFFF },
1205 GetGWorld( &origPort
, &origDevice
) ;
1206 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1208 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1210 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1211 GetCPixel( w
, h
, &col
) ;
1212 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1213 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1215 SetCPixel( w
, h
, &colors
[0] ) ;
1219 SetCPixel( w
, h
, &colors
[1] ) ;
1223 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1224 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1225 SetGWorld( origPort
, origDevice
) ;
1230 bool wxMask::PointMasked(int x
, int y
)
1233 GDHandle origDevice
;
1237 GetGWorld( &origPort
, &origDevice
);
1239 //Set port to mask and see if it masked (1) or not ( 0 )
1240 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1241 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1242 GetCPixel(x
,y
, &color
);
1243 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1244 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1246 SetGWorld( origPort
, origDevice
);
1255 wxBitmapHandler::~wxBitmapHandler()
1259 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1264 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1265 int desiredWidth
, int desiredHeight
)
1270 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1279 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1281 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1283 inline wxPICTResourceHandler()
1285 m_name
= wxT("Macintosh Pict resource");
1286 m_extension
= wxEmptyString
;
1287 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1290 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1291 int desiredWidth
, int desiredHeight
);
1293 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1295 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1296 int desiredWidth
, int desiredHeight
)
1299 wxMacStringToPascal( name
, theName
) ;
1301 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1306 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1307 DetachResource( (Handle
) thePict
) ;
1308 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1309 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1310 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1311 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1313 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1314 M_BITMAPHANDLERDATA
->m_ok
= true ;
1315 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1316 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1317 // M_BITMAPHANDLERDATA->m_quality;
1323 void wxBitmap::InitStandardHandlers()
1325 AddHandler(new wxPICTResourceHandler
) ;
1326 AddHandler(new wxICONResourceHandler
) ;