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
;
372 // TODO move this to a public function of Bitmap Ref
373 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
378 switch (data
->m_bitmapType
)
380 case kMacBitmapTypePict
:
384 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
385 data
->m_hPict
= NULL
;
389 case kMacBitmapTypeGrafWorld
:
391 if ( data
->m_hBitmap
)
393 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
394 data
->m_hBitmap
= NULL
;
398 case kMacBitmapTypeIcon
:
401 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
402 data
->m_hIcon
= NULL
;
410 if (data
->m_bitmapMask
)
412 delete data
->m_bitmapMask
;
413 data
->m_bitmapMask
= NULL
;
417 wxBitmapRefData::~wxBitmapRefData()
419 DisposeBitmapRefData( this ) ;
422 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
433 wxBitmap::~wxBitmap()
437 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
439 m_refData
= new wxBitmapRefData
;
441 M_BITMAPDATA
->m_width
= the_width
;
442 M_BITMAPDATA
->m_height
= the_height
;
443 M_BITMAPDATA
->m_depth
= no_bits
;
444 M_BITMAPDATA
->m_numColors
= 0;
447 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
448 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
449 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
452 GDHandle origDevice
;
454 GetGWorld( &origPort
, &origDevice
) ;
455 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
456 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
458 // bits is a char array
460 unsigned char* linestart
= (unsigned char*) bits
;
461 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
462 if ( the_width
% (sizeof(unsigned char) * 8) ) {
463 linesize
+= sizeof(unsigned char);
466 RGBColor colors
[2] = {
467 { 0xFFFF , 0xFFFF , 0xFFFF } ,
471 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
473 for ( int x
= 0 ; x
< the_width
; ++x
)
477 int mask
= 1 << bit
;
478 if ( linestart
[index
] & mask
)
480 SetCPixel( x
, y
, &colors
[1] ) ;
484 SetCPixel( x
, y
, &colors
[0] ) ;
488 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
490 SetGWorld( origPort
, origDevice
) ;
494 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
498 wxBitmap::wxBitmap(int w
, int h
, int d
)
500 (void)Create(w
, h
, d
);
503 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
505 (void) Create(data
, type
, width
, height
, depth
);
508 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
510 LoadFile(filename
, type
);
513 bool wxBitmap::CreateFromXpm(const char **bits
)
515 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
516 wxXPMDecoder decoder
;
517 wxImage img
= decoder
.ReadData(bits
);
518 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
519 *this = wxBitmap(img
);
523 wxBitmap::wxBitmap(const char **bits
)
525 (void) CreateFromXpm(bits
);
528 wxBitmap::wxBitmap(char **bits
)
530 (void) CreateFromXpm((const char **)bits
);
533 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
536 (rect
.x
>= 0) && (rect
.y
>= 0) &&
537 (rect
.x
+rect
.width
<= GetWidth()) &&
538 (rect
.y
+rect
.height
<= GetHeight()),
539 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
542 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
543 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
548 GetGWorld( &origPort
, &origDevice
);
550 // Update the subbitmaps reference data
551 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
553 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
554 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
555 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
557 // Copy sub region of this bitmap
558 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
560 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
562 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
567 GWorldPtr submask
, mask
;
570 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
571 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetMask()->GetDepth() );
572 LockPixels(GetGWorldPixMap(mask
));
573 LockPixels(GetGWorldPixMap(submask
));
575 for(int yy
= 0; yy
< rect
.height
; yy
++)
577 for(int xx
= 0; xx
< rect
.width
; xx
++)
579 SetGWorld(mask
, NULL
);
580 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
581 SetGWorld(submask
, NULL
);
582 SetCPixel(xx
,yy
, &color
);
585 UnlockPixels(GetGWorldPixMap(mask
));
586 UnlockPixels(GetGWorldPixMap(submask
));
587 ref
->m_bitmapMask
= new wxMask
;
588 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
594 GWorldPtr subbitmap
, bitmap
;
597 bitmap
= (GWorldPtr
) GetHBITMAP();
598 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
599 LockPixels(GetGWorldPixMap(bitmap
));
600 LockPixels(GetGWorldPixMap(subbitmap
));
602 for(int yy
= 0; yy
< rect
.height
; yy
++)
604 for(int xx
= 0; xx
< rect
.width
; xx
++)
606 SetGWorld(bitmap
, NULL
);
607 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
608 SetGWorld(subbitmap
, NULL
);
609 SetCPixel(xx
, yy
, &color
);
612 UnlockPixels(GetGWorldPixMap(bitmap
));
613 UnlockPixels(GetGWorldPixMap(subbitmap
));
616 SetGWorld( origPort
, origDevice
);
621 bool wxBitmap::Create(int w
, int h
, int d
)
625 m_refData
= new wxBitmapRefData
;
627 M_BITMAPDATA
->m_width
= w
;
628 M_BITMAPDATA
->m_height
= h
;
629 M_BITMAPDATA
->m_depth
= d
;
631 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
632 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
633 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
634 return M_BITMAPDATA
->m_ok
;
637 int wxBitmap::GetBitmapType() const
639 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
641 return M_BITMAPDATA
->m_bitmapType
;
644 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
647 m_refData
= new wxBitmapRefData
;
649 DisposeBitmapRefData( M_BITMAPDATA
) ;
651 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
652 M_BITMAPDATA
->m_hBitmap
= bmp
;
653 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
656 void wxBitmap::SetHICON(WXHICON ico
)
659 m_refData
= new wxBitmapRefData
;
661 DisposeBitmapRefData( M_BITMAPDATA
) ;
663 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeIcon
;
664 M_BITMAPDATA
->m_hIcon
= ico
;
665 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hIcon
!= NULL
) ;
668 void wxBitmap::SetPict(WXHMETAFILE pict
)
671 m_refData
= new wxBitmapRefData
;
673 DisposeBitmapRefData( M_BITMAPDATA
) ;
675 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypePict
;
676 M_BITMAPDATA
->m_hPict
= pict
;
677 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hPict
!= NULL
) ;
680 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
684 wxBitmapHandler
*handler
= FindHandler(type
);
688 m_refData
= new wxBitmapRefData
;
690 return handler
->LoadFile(this, filename
, type
, -1, -1);
694 wxImage
loadimage(filename
, type
);
695 if (loadimage
.Ok()) {
700 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
704 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
708 m_refData
= new wxBitmapRefData
;
710 wxBitmapHandler
*handler
= FindHandler(type
);
712 if ( handler
== NULL
) {
713 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
718 return handler
->Create(this, data
, type
, width
, height
, depth
);
721 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
723 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
724 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
726 m_refData
= new wxBitmapRefData();
728 // width and height of the device-dependent bitmap
729 int width
= image
.GetWidth();
730 int height
= image
.GetHeight();
734 Create( width
, height
, 32 ) ;
737 GDHandle origDevice
;
739 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
740 LockPixels( pixMap
);
742 GetGWorld( &origPort
, &origDevice
) ;
743 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
746 register unsigned char* data
= image
.GetData();
747 char* destinationBase
= GetPixBaseAddr( pixMap
);
748 register unsigned char* destination
= (unsigned char*) destinationBase
;
749 for (int y
= 0; y
< height
; y
++)
751 for (int x
= 0; x
< width
; x
++)
754 *destination
++ = *data
++ ;
755 *destination
++ = *data
++ ;
756 *destination
++ = *data
++ ;
758 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
759 destination
= (unsigned char*) destinationBase
;
761 if ( image
.HasAlpha() )
763 unsigned char *alpha
= image
.GetAlpha();
765 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
767 wxBitmap maskBitmap
;
769 maskBitmap
.Create( width
, height
, 24);
770 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
771 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
773 for (int y
= 0; y
< height
; y
++)
775 for (int x
= 0; x
< width
; x
++)
777 memset( &color
, 255 - *alpha
, sizeof( color
) );
778 SetCPixel(x
,y
, &color
);
783 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
784 SetMask(new wxMask( maskBitmap
));
785 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
787 else if ( image
.HasMask() )
789 data
= image
.GetData();
791 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
792 RGBColor white
= { 0xffff, 0xffff, 0xffff };
793 RGBColor black
= { 0 , 0 , 0 };
794 wxBitmap maskBitmap
;
796 maskBitmap
.Create( width
, height
, 1);
797 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
798 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
800 for (int y
= 0; y
< height
; y
++)
802 for (int x
= 0; x
< width
; x
++)
804 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
806 SetCPixel(x
,y
, &white
);
809 SetCPixel(x
,y
, &black
);
814 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
815 SetMask(new wxMask( maskBitmap
));
816 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
819 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
820 SetGWorld( origPort
, origDevice
);
823 wxImage
wxBitmap::ConvertToImage() const
827 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
829 // create an wxImage object
830 int width
= GetWidth();
831 int height
= GetHeight();
832 image
.Create( width
, height
);
834 unsigned char *data
= image
.GetData();
836 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
840 RgnHandle maskRgn
= NULL
;
841 GWorldPtr tempPort
= NULL
;
844 // background color set to RGB(16,16,16) in consistent with wxGTK
845 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
847 wxMask
*mask
= GetMask();
849 GetGWorld( &origPort
, &origDevice
);
850 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
852 tempPort
= wxMacCreateGWorld( width
, height
, -1) ;
856 tempPort
= (GWorldPtr
) GetHBITMAP() ;
858 LockPixels(GetGWorldPixMap(tempPort
));
859 SetGWorld( tempPort
, NULL
);
860 if ( GetBitmapType() == kMacBitmapTypePict
|| GetBitmapType() == kMacBitmapTypeIcon
)
862 Rect bitmaprect
= { 0 , 0 , height
, width
};
863 if ( GetBitmapType() == kMacBitmapTypeIcon
)
865 ::PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, MAC_WXHICON(GetHICON()) ) ;
867 BitMapToRegion( maskRgn
, &(**(MAC_WXHICON(GetHICON()))).iconMask
) ;
870 ::DrawPicture( (PicHandle
) GetPict(), &bitmaprect
) ;
872 // Copy data into image
874 for (int yy
= 0; yy
< height
; yy
++)
876 for (int xx
= 0; xx
< width
; xx
++)
878 GetCPixel(xx
,yy
, &color
);
879 r
= ((color
.red
) >> 8);
880 g
= ((color
.green
) >> 8);
881 b
= ((color
.blue
) >> 8);
890 if ( !PtInRgn( pt
, maskRgn
) )
892 data
[index
] = mask_r
;
893 data
[index
+ 1] = mask_g
;
894 data
[index
+ 2] = mask_b
;
901 if (mask
->PointMasked(xx
,yy
))
903 data
[index
] = mask_r
;
904 data
[index
+ 1] = mask_g
;
905 data
[index
+ 2] = mask_b
;
912 if (mask
|| maskRgn
)
914 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
915 image
.SetMask( true );
919 UnlockPixels(GetGWorldPixMap( tempPort
));
920 SetGWorld(origPort
, origDevice
);
921 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
923 wxMacDestroyGWorld( tempPort
) ;
927 DisposeRgn( maskRgn
) ;
934 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
935 const wxPalette
*palette
) const
937 wxBitmapHandler
*handler
= FindHandler(type
);
941 return handler
->SaveFile(this, filename
, type
, palette
);
945 wxImage image
= ConvertToImage();
947 return image
.SaveFile(filename
, type
);
950 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
954 bool wxBitmap::Ok() const
956 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
959 int wxBitmap::GetHeight() const
961 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
963 return M_BITMAPDATA
->m_height
;
966 int wxBitmap::GetWidth() const
968 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
970 return M_BITMAPDATA
->m_width
;
973 int wxBitmap::GetDepth() const
975 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
977 return M_BITMAPDATA
->m_depth
;
980 int wxBitmap::GetQuality() const
982 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
984 return M_BITMAPDATA
->m_quality
;
987 wxMask
*wxBitmap::GetMask() const
989 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
991 return M_BITMAPDATA
->m_bitmapMask
;
994 void wxBitmap::SetWidth(int w
)
997 m_refData
= new wxBitmapRefData
;
999 M_BITMAPDATA
->m_width
= w
;
1002 void wxBitmap::SetHeight(int h
)
1005 m_refData
= new wxBitmapRefData
;
1007 M_BITMAPDATA
->m_height
= h
;
1010 void wxBitmap::SetDepth(int d
)
1013 m_refData
= new wxBitmapRefData
;
1015 M_BITMAPDATA
->m_depth
= d
;
1018 void wxBitmap::SetQuality(int q
)
1021 m_refData
= new wxBitmapRefData
;
1023 M_BITMAPDATA
->m_quality
= q
;
1026 void wxBitmap::SetOk(bool isOk
)
1029 m_refData
= new wxBitmapRefData
;
1031 M_BITMAPDATA
->m_ok
= isOk
;
1034 wxPalette
*wxBitmap::GetPalette() const
1036 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1038 return &M_BITMAPDATA
->m_bitmapPalette
;
1041 void wxBitmap::SetPalette(const wxPalette
& palette
)
1044 m_refData
= new wxBitmapRefData
;
1046 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1049 void wxBitmap::SetMask(wxMask
*mask
)
1052 m_refData
= new wxBitmapRefData
;
1054 // Remove existing mask if there is one.
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 M_BITMAPDATA
->m_hasAlpha
= false;
1371 return GetPixBaseAddr(hPixMap
);
1374 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1379 if ( M_BITMAPDATA
->m_hasAlpha
)
1381 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1383 int w
= data
.GetWidth(),
1384 h
= data
.GetHeight();
1386 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1387 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1388 wxAlphaPixelData::Iterator
pMask(dataMask
),
1390 for ( int y
= 0; y
< h
; y
++ )
1392 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1395 for ( int x
= 0; x
< w
; x
++ )
1397 const wxAlphaPixelData::Iterator::ChannelType
1400 pMask
.Red() = alpha
;
1401 pMask
.Green() = alpha
;
1402 pMask
.Blue() = alpha
;
1411 pMask
= rowStartMask
;
1412 pMask
.OffsetY(dataMask
, 1);
1415 SetMask(new wxMask(bmpMask
));
1418 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1419 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1422 UnlockPixels(hPixMap
);
1426 void wxBitmap::UseAlpha()
1428 // remember that we are using alpha channel, we'll need to create a proper
1429 // mask in UngetRawData()
1430 M_BITMAPDATA
->m_hasAlpha
= true;