1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/bitmap.h"
18 #include "wx/xpmdecod.h"
20 #include "wx/rawbmp.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
23 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
24 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
27 #include <ApplicationServices/ApplicationServices.h>
29 #include <PictUtils.h>
32 #include "wx/mac/uma.h"
34 CTabHandle
wxMacCreateColorTable( int numColors
)
36 CTabHandle newColors
; /* Handle to the new color table */
38 /* Allocate memory for the color table */
39 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
40 sizeof (ColorSpec
) * (numColors
- 1) );
43 /* Initialize the fields */
44 (**newColors
).ctSeed
= GetCTSeed();
45 (**newColors
).ctFlags
= 0;
46 (**newColors
).ctSize
= numColors
- 1;
47 /* Initialize the table of colors */
52 void wxMacDestroyColorTable( CTabHandle colors
)
54 DisposeHandle( (Handle
) colors
) ;
57 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
59 (**newColors
).ctTable
[index
].value
= index
;
60 (**newColors
).ctTable
[index
].rgb
.red
= red
; // someRedValue;
61 (**newColors
).ctTable
[index
].rgb
.green
= green
; // someGreenValue;
62 (**newColors
).ctTable
[index
].rgb
.blue
= blue
; // someBlueValue;
65 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
69 Rect rect
= { 0 , 0 , height
, width
} ;
73 depth
= wxDisplayDepth() ;
76 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
84 void wxMacDestroyGWorld( GWorldPtr gw
)
90 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
92 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
94 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
95 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
96 OSErr error
; /* Returns error code */
97 short bytesPerRow
; /* Number of bytes per row in the PixMap */
104 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
106 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
110 error
= HandToHand((Handle
*) &newColors
);
114 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
120 /* Allocate pixel image; long integer multiplication avoids overflow */
121 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
123 if ((**icon
).iconData
!= nil
)
125 /* Initialize fields common to indexed and direct PixMaps */
126 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
127 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
129 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
130 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
131 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
132 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
133 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
134 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
135 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
137 /* Initialize fields specific to indexed and direct PixMaps */
140 /* PixMap is indexed */
141 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
142 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
143 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
144 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
148 /* PixMap is direct */
149 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
150 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
152 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
154 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
155 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
156 (**newColors
).ctFlags
= 0;
157 (**newColors
).ctSize
= 0;
158 (**icon
).iconPMap
.pmTable
= newColors
;
167 /* If no errors occurred, return a handle to the new off-screen PixMap */
170 if (newColors
!= nil
)
171 DisposeCTable(newColors
);
174 /* Return the error code */
178 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
183 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
184 SetGWorld(image
,nil
);
186 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
187 Rect imageBounds
= frame
;
188 GetPortBounds( image
, &imageBounds
) ;
190 int bwSize
= iconSize
/ 8 * iconSize
;
191 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
192 HLock((Handle
)icon
) ;
193 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
194 HLock( (**icon
).iconData
) ;
195 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
197 LockPixels(GetGWorldPixMap(image
));
199 CopyBits(GetPortBitMapForCopyBits(image
),
200 (BitMapPtr
)&((**icon
).iconPMap
),
203 srcCopy
| ditherCopy
, nil
);
206 UnlockPixels(GetGWorldPixMap(image
));
207 HUnlock( (**icon
).iconData
) ;
209 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
210 (**icon
).iconMask
.bounds
= frame
;
212 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
213 (**icon
).iconBMap
.bounds
= frame
;
214 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
215 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
220 GetPortBounds( image
, &r
) ;
221 LockPixels(GetGWorldPixMap(mask
) ) ;
222 CopyBits(GetPortBitMapForCopyBits(mask
) ,
223 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
224 CopyBits(GetPortBitMapForCopyBits(mask
) ,
225 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
226 UnlockPixels(GetGWorldPixMap( mask
) ) ;
231 GetPortBounds( image
, &r
) ;
232 LockPixels(GetGWorldPixMap(image
));
233 CopyBits(GetPortBitMapForCopyBits(image
) ,
234 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
235 CopyBits(GetPortBitMapForCopyBits(image
) ,
236 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
237 UnlockPixels(GetGWorldPixMap(image
));
240 (**icon
).iconMask
.baseAddr
= NULL
;
241 (**icon
).iconBMap
.baseAddr
= NULL
;
242 (**icon
).iconPMap
.baseAddr
= NULL
;
243 HUnlock((Handle
)icon
) ;
244 SetGWorld(saveWorld
,saveHandle
);
249 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
256 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
257 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
259 GetGWorld( &origPort
, &origDev
) ;
261 RgnHandle clipRgn
= NULL
;
266 LockPixels( GetGWorldPixMap( mask
) ) ;
267 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
268 UnlockPixels( GetGWorldPixMap( mask
) ) ;
271 SetGWorld( wp
, NULL
) ;
274 GetRegionBounds( clipRgn
, &portRect
) ;
276 GetPortBounds( wp
, &portRect
) ;
277 pict
= OpenPicture(&portRect
);
280 RGBForeColor( &black
) ;
281 RGBBackColor( &white
) ;
286 LockPixels( GetGWorldPixMap( wp
) ) ;
287 CopyBits(GetPortBitMapForCopyBits(wp
),
288 GetPortBitMapForCopyBits(wp
),
292 UnlockPixels( GetGWorldPixMap( wp
) ) ;
295 SetGWorld( origPort
, origDev
) ;
297 DisposeRgn( clipRgn
) ;
301 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
303 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
306 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
310 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
312 info
->contentType
= kControlContentPictHandle
;
313 info
->u
.picture
= MAC_WXHMETAFILE(bmap
->m_hPict
) ;
315 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
317 if ( (forceType
== kControlContentCIconHandle
|| ( bmap
->m_width
== bmap
->m_height
&& forceType
!= kControlContentPictHandle
) ) && ((bmap
->m_width
& 0x3) == 0) )
319 info
->contentType
= kControlContentCIconHandle
;
320 if ( bitmap
.GetMask() )
322 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap()) ,
323 8 , bmap
->m_width
) ;
327 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
,
328 8 , bmap
->m_width
) ;
333 info
->contentType
= kControlContentPictHandle
;
334 if ( bitmap
.GetMask() )
336 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap() ) ) ;
340 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
) ;
344 else if ( bmap
->m_bitmapType
== kMacBitmapTypeIcon
)
346 info
->contentType
= kControlContentCIconHandle
;
347 info
->u
.cIconHandle
= MAC_WXHICON(bmap
->m_hIcon
) ;
352 wxBitmapRefData::wxBitmapRefData()
364 m_bitmapType
= kMacBitmapTypeUnknownType
;
368 // TODO move this to a public function of Bitmap Ref
369 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
374 switch (data
->m_bitmapType
)
376 case kMacBitmapTypePict
:
380 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
381 data
->m_hPict
= NULL
;
385 case kMacBitmapTypeGrafWorld
:
387 if ( data
->m_hBitmap
)
389 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
390 data
->m_hBitmap
= NULL
;
394 case kMacBitmapTypeIcon
:
397 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
398 data
->m_hIcon
= NULL
;
406 if (data
->m_bitmapMask
)
408 delete data
->m_bitmapMask
;
409 data
->m_bitmapMask
= NULL
;
413 wxBitmapRefData::~wxBitmapRefData()
415 DisposeBitmapRefData( this ) ;
418 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
429 wxBitmap::~wxBitmap()
433 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
435 m_refData
= new wxBitmapRefData
;
437 M_BITMAPDATA
->m_width
= the_width
;
438 M_BITMAPDATA
->m_height
= the_height
;
439 M_BITMAPDATA
->m_depth
= no_bits
;
440 M_BITMAPDATA
->m_numColors
= 0;
443 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
444 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
445 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
448 GDHandle origDevice
;
450 GetGWorld( &origPort
, &origDevice
) ;
451 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
452 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
454 // bits is a char array
456 unsigned char* linestart
= (unsigned char*) bits
;
457 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
458 if ( the_width
% (sizeof(unsigned char) * 8) ) {
459 linesize
+= sizeof(unsigned char);
462 RGBColor colors
[2] = {
463 { 0xFFFF , 0xFFFF , 0xFFFF } ,
467 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
469 for ( int x
= 0 ; x
< the_width
; ++x
)
473 int mask
= 1 << bit
;
474 if ( linestart
[index
] & mask
)
476 SetCPixel( x
, y
, &colors
[1] ) ;
480 SetCPixel( x
, y
, &colors
[0] ) ;
484 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
486 SetGWorld( origPort
, origDevice
) ;
490 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
494 wxBitmap::wxBitmap(int w
, int h
, int d
)
496 (void)Create(w
, h
, d
);
499 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
501 (void) Create(data
, type
, width
, height
, depth
);
504 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
506 LoadFile(filename
, type
);
509 bool wxBitmap::CreateFromXpm(const char **bits
)
511 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
512 wxXPMDecoder decoder
;
513 wxImage img
= decoder
.ReadData(bits
);
514 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
515 *this = wxBitmap(img
);
519 wxBitmap::wxBitmap(const char **bits
)
521 (void) CreateFromXpm(bits
);
524 wxBitmap::wxBitmap(char **bits
)
526 (void) CreateFromXpm((const char **)bits
);
529 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
532 (rect
.x
>= 0) && (rect
.y
>= 0) &&
533 (rect
.x
+rect
.width
<= GetWidth()) &&
534 (rect
.y
+rect
.height
<= GetHeight()),
535 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
538 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
539 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
544 GetGWorld( &origPort
, &origDevice
);
546 // Update the subbitmaps reference data
547 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
549 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
551 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
552 #endif // wxUSE_PALETTE
553 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
555 // Copy sub region of this bitmap
556 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
558 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
560 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
565 GWorldPtr submask
, mask
;
568 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
569 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetMask()->GetDepth() );
570 LockPixels(GetGWorldPixMap(mask
));
571 LockPixels(GetGWorldPixMap(submask
));
573 for(int yy
= 0; yy
< rect
.height
; yy
++)
575 for(int xx
= 0; xx
< rect
.width
; xx
++)
577 SetGWorld(mask
, NULL
);
578 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
579 SetGWorld(submask
, NULL
);
580 SetCPixel(xx
,yy
, &color
);
583 UnlockPixels(GetGWorldPixMap(mask
));
584 UnlockPixels(GetGWorldPixMap(submask
));
585 ref
->m_bitmapMask
= new wxMask
;
586 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
592 GWorldPtr subbitmap
, bitmap
;
595 bitmap
= (GWorldPtr
) GetHBITMAP();
596 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
597 LockPixels(GetGWorldPixMap(bitmap
));
598 LockPixels(GetGWorldPixMap(subbitmap
));
600 for(int yy
= 0; yy
< rect
.height
; yy
++)
602 for(int xx
= 0; xx
< rect
.width
; xx
++)
604 SetGWorld(bitmap
, NULL
);
605 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
606 SetGWorld(subbitmap
, NULL
);
607 SetCPixel(xx
, yy
, &color
);
610 UnlockPixels(GetGWorldPixMap(bitmap
));
611 UnlockPixels(GetGWorldPixMap(subbitmap
));
614 SetGWorld( origPort
, origDevice
);
619 bool wxBitmap::Create(int w
, int h
, int d
)
623 m_refData
= new wxBitmapRefData
;
625 M_BITMAPDATA
->m_width
= w
;
626 M_BITMAPDATA
->m_height
= h
;
627 M_BITMAPDATA
->m_depth
= d
;
629 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
630 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
631 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
632 return M_BITMAPDATA
->m_ok
;
635 int wxBitmap::GetBitmapType() const
637 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
639 return M_BITMAPDATA
->m_bitmapType
;
642 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
645 m_refData
= new wxBitmapRefData
;
647 DisposeBitmapRefData( M_BITMAPDATA
) ;
649 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
650 M_BITMAPDATA
->m_hBitmap
= bmp
;
651 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
654 void wxBitmap::SetHICON(WXHICON ico
)
657 m_refData
= new wxBitmapRefData
;
659 DisposeBitmapRefData( M_BITMAPDATA
) ;
661 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeIcon
;
662 M_BITMAPDATA
->m_hIcon
= ico
;
663 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hIcon
!= NULL
) ;
666 void wxBitmap::SetPict(WXHMETAFILE pict
)
669 m_refData
= new wxBitmapRefData
;
671 DisposeBitmapRefData( M_BITMAPDATA
) ;
673 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypePict
;
674 M_BITMAPDATA
->m_hPict
= pict
;
675 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hPict
!= NULL
) ;
678 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
682 wxBitmapHandler
*handler
= FindHandler(type
);
686 m_refData
= new wxBitmapRefData
;
688 return handler
->LoadFile(this, filename
, type
, -1, -1);
692 wxImage
loadimage(filename
, type
);
693 if (loadimage
.Ok()) {
698 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
702 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
706 m_refData
= new wxBitmapRefData
;
708 wxBitmapHandler
*handler
= FindHandler(type
);
710 if ( handler
== NULL
) {
711 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
716 return handler
->Create(this, data
, type
, width
, height
, depth
);
719 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
721 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
722 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
724 m_refData
= new wxBitmapRefData();
726 // width and height of the device-dependent bitmap
727 int width
= image
.GetWidth();
728 int height
= image
.GetHeight();
732 Create( width
, height
, 32 ) ;
735 GDHandle origDevice
;
737 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
738 LockPixels( pixMap
);
740 GetGWorld( &origPort
, &origDevice
) ;
741 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
744 register unsigned char* data
= image
.GetData();
745 char* destinationBase
= GetPixBaseAddr( pixMap
);
746 register unsigned char* destination
= (unsigned char*) destinationBase
;
747 for (int y
= 0; y
< height
; y
++)
749 for (int x
= 0; x
< width
; x
++)
752 *destination
++ = *data
++ ;
753 *destination
++ = *data
++ ;
754 *destination
++ = *data
++ ;
756 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
757 destination
= (unsigned char*) destinationBase
;
759 if ( image
.HasAlpha() )
761 unsigned char *alpha
= image
.GetAlpha();
763 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
765 wxBitmap maskBitmap
;
767 maskBitmap
.Create( width
, height
, 24);
768 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
769 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
771 for (int y
= 0; y
< height
; y
++)
773 for (int x
= 0; x
< width
; x
++)
775 memset( &color
, 255 - *alpha
, sizeof( color
) );
776 SetCPixel(x
,y
, &color
);
781 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
782 SetMask(new wxMask( maskBitmap
));
783 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
785 else if ( image
.HasMask() )
787 data
= image
.GetData();
789 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
790 RGBColor white
= { 0xffff, 0xffff, 0xffff };
791 RGBColor black
= { 0 , 0 , 0 };
792 wxBitmap maskBitmap
;
794 maskBitmap
.Create( width
, height
, 1);
795 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
796 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
798 for (int y
= 0; y
< height
; y
++)
800 for (int x
= 0; x
< width
; x
++)
802 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
804 SetCPixel(x
,y
, &white
);
807 SetCPixel(x
,y
, &black
);
812 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
813 SetMask(new wxMask( maskBitmap
));
814 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
817 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
818 SetGWorld( origPort
, origDevice
);
821 wxImage
wxBitmap::ConvertToImage() const
825 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
827 // create an wxImage object
828 int width
= GetWidth();
829 int height
= GetHeight();
830 image
.Create( width
, height
);
832 unsigned char *data
= image
.GetData();
834 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
838 RgnHandle maskRgn
= NULL
;
839 GWorldPtr tempPort
= NULL
;
842 // background color set to RGB(16,16,16) in consistent with wxGTK
843 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
845 wxMask
*mask
= GetMask();
847 GetGWorld( &origPort
, &origDevice
);
848 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
850 tempPort
= wxMacCreateGWorld( width
, height
, -1) ;
854 tempPort
= (GWorldPtr
) GetHBITMAP() ;
856 LockPixels(GetGWorldPixMap(tempPort
));
857 SetGWorld( tempPort
, NULL
);
858 if ( GetBitmapType() == kMacBitmapTypePict
|| GetBitmapType() == kMacBitmapTypeIcon
)
860 Rect bitmaprect
= { 0 , 0 , height
, width
};
861 if ( GetBitmapType() == kMacBitmapTypeIcon
)
863 ::PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, MAC_WXHICON(GetHICON()) ) ;
865 BitMapToRegion( maskRgn
, &(**(MAC_WXHICON(GetHICON()))).iconMask
) ;
868 ::DrawPicture( (PicHandle
) GetPict(), &bitmaprect
) ;
870 // Copy data into image
872 for (int yy
= 0; yy
< height
; yy
++)
874 for (int xx
= 0; xx
< width
; xx
++)
876 GetCPixel(xx
,yy
, &color
);
877 r
= ((color
.red
) >> 8);
878 g
= ((color
.green
) >> 8);
879 b
= ((color
.blue
) >> 8);
888 if ( !PtInRgn( pt
, maskRgn
) )
890 data
[index
] = mask_r
;
891 data
[index
+ 1] = mask_g
;
892 data
[index
+ 2] = mask_b
;
899 if (mask
->PointMasked(xx
,yy
))
901 data
[index
] = mask_r
;
902 data
[index
+ 1] = mask_g
;
903 data
[index
+ 2] = mask_b
;
910 if (mask
|| maskRgn
)
912 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
913 image
.SetMask( true );
917 UnlockPixels(GetGWorldPixMap( tempPort
));
918 SetGWorld(origPort
, origDevice
);
919 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
921 wxMacDestroyGWorld( tempPort
) ;
925 DisposeRgn( maskRgn
) ;
932 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
933 const wxPalette
*palette
) const
935 wxBitmapHandler
*handler
= FindHandler(type
);
939 return handler
->SaveFile(this, filename
, type
, palette
);
943 wxImage image
= ConvertToImage();
945 return image
.SaveFile(filename
, type
);
948 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
952 bool wxBitmap::Ok() const
954 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
957 int wxBitmap::GetHeight() const
959 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
961 return M_BITMAPDATA
->m_height
;
964 int wxBitmap::GetWidth() const
966 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
968 return M_BITMAPDATA
->m_width
;
971 int wxBitmap::GetDepth() const
973 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
975 return M_BITMAPDATA
->m_depth
;
978 int wxBitmap::GetQuality() const
980 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
982 return M_BITMAPDATA
->m_quality
;
985 wxMask
*wxBitmap::GetMask() const
987 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
989 return M_BITMAPDATA
->m_bitmapMask
;
992 void wxBitmap::SetWidth(int w
)
995 m_refData
= new wxBitmapRefData
;
997 M_BITMAPDATA
->m_width
= w
;
1000 void wxBitmap::SetHeight(int h
)
1003 m_refData
= new wxBitmapRefData
;
1005 M_BITMAPDATA
->m_height
= h
;
1008 void wxBitmap::SetDepth(int d
)
1011 m_refData
= new wxBitmapRefData
;
1013 M_BITMAPDATA
->m_depth
= d
;
1016 void wxBitmap::SetQuality(int q
)
1019 m_refData
= new wxBitmapRefData
;
1021 M_BITMAPDATA
->m_quality
= q
;
1024 void wxBitmap::SetOk(bool isOk
)
1027 m_refData
= new wxBitmapRefData
;
1029 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
;
1047 #endif // wxUSE_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;