1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/bitmap.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
26 #include "wx/xpmdecod.h"
28 #include "wx/rawbmp.h"
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
32 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
35 #include <ApplicationServices/ApplicationServices.h>
37 #include <PictUtils.h>
40 #include "wx/mac/uma.h"
42 CTabHandle
wxMacCreateColorTable( int numColors
)
44 CTabHandle newColors
; /* Handle to the new color table */
46 /* Allocate memory for the color table */
47 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
48 sizeof (ColorSpec
) * (numColors
- 1) );
51 /* Initialize the fields */
52 (**newColors
).ctSeed
= GetCTSeed();
53 (**newColors
).ctFlags
= 0;
54 (**newColors
).ctSize
= numColors
- 1;
55 /* Initialize the table of colors */
60 void wxMacDestroyColorTable( CTabHandle colors
)
62 DisposeHandle( (Handle
) colors
) ;
65 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
67 (**newColors
).ctTable
[index
].value
= index
;
68 (**newColors
).ctTable
[index
].rgb
.red
= red
; // someRedValue;
69 (**newColors
).ctTable
[index
].rgb
.green
= green
; // someGreenValue;
70 (**newColors
).ctTable
[index
].rgb
.blue
= blue
; // someBlueValue;
73 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
77 Rect rect
= { 0 , 0 , height
, width
} ;
81 depth
= wxDisplayDepth() ;
84 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
92 void wxMacDestroyGWorld( GWorldPtr gw
)
98 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
100 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
102 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
103 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
104 OSErr error
; /* Returns error code */
105 short bytesPerRow
; /* Number of bytes per row in the PixMap */
112 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
114 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
118 error
= HandToHand((Handle
*) &newColors
);
122 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
128 /* Allocate pixel image; long integer multiplication avoids overflow */
129 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
131 if ((**icon
).iconData
!= nil
)
133 /* Initialize fields common to indexed and direct PixMaps */
134 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
135 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
137 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
138 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
139 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
140 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
141 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
142 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
143 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
145 /* Initialize fields specific to indexed and direct PixMaps */
148 /* PixMap is indexed */
149 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
150 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
151 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
152 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
156 /* PixMap is direct */
157 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
158 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
160 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
162 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
163 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
164 (**newColors
).ctFlags
= 0;
165 (**newColors
).ctSize
= 0;
166 (**icon
).iconPMap
.pmTable
= newColors
;
175 /* If no errors occurred, return a handle to the new off-screen PixMap */
178 if (newColors
!= nil
)
179 DisposeCTable(newColors
);
182 /* Return the error code */
186 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
191 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
192 SetGWorld(image
,nil
);
194 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
195 Rect imageBounds
= frame
;
196 GetPortBounds( image
, &imageBounds
) ;
198 int bwSize
= iconSize
/ 8 * iconSize
;
199 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
200 HLock((Handle
)icon
) ;
201 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
202 HLock( (**icon
).iconData
) ;
203 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
205 LockPixels(GetGWorldPixMap(image
));
207 CopyBits(GetPortBitMapForCopyBits(image
),
208 (BitMapPtr
)&((**icon
).iconPMap
),
211 srcCopy
| ditherCopy
, nil
);
214 UnlockPixels(GetGWorldPixMap(image
));
215 HUnlock( (**icon
).iconData
) ;
217 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
218 (**icon
).iconMask
.bounds
= frame
;
220 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
221 (**icon
).iconBMap
.bounds
= frame
;
222 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
223 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
228 GetPortBounds( image
, &r
) ;
229 LockPixels(GetGWorldPixMap(mask
) ) ;
230 CopyBits(GetPortBitMapForCopyBits(mask
) ,
231 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
232 CopyBits(GetPortBitMapForCopyBits(mask
) ,
233 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
234 UnlockPixels(GetGWorldPixMap( mask
) ) ;
239 GetPortBounds( image
, &r
) ;
240 LockPixels(GetGWorldPixMap(image
));
241 CopyBits(GetPortBitMapForCopyBits(image
) ,
242 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
243 CopyBits(GetPortBitMapForCopyBits(image
) ,
244 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
245 UnlockPixels(GetGWorldPixMap(image
));
248 (**icon
).iconMask
.baseAddr
= NULL
;
249 (**icon
).iconBMap
.baseAddr
= NULL
;
250 (**icon
).iconPMap
.baseAddr
= NULL
;
251 HUnlock((Handle
)icon
) ;
252 SetGWorld(saveWorld
,saveHandle
);
257 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
264 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
265 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
267 GetGWorld( &origPort
, &origDev
) ;
269 RgnHandle clipRgn
= NULL
;
274 LockPixels( GetGWorldPixMap( mask
) ) ;
275 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
276 UnlockPixels( GetGWorldPixMap( mask
) ) ;
279 SetGWorld( wp
, NULL
) ;
282 GetRegionBounds( clipRgn
, &portRect
) ;
284 GetPortBounds( wp
, &portRect
) ;
285 pict
= OpenPicture(&portRect
);
288 RGBForeColor( &black
) ;
289 RGBBackColor( &white
) ;
294 LockPixels( GetGWorldPixMap( wp
) ) ;
295 CopyBits(GetPortBitMapForCopyBits(wp
),
296 GetPortBitMapForCopyBits(wp
),
300 UnlockPixels( GetGWorldPixMap( wp
) ) ;
303 SetGWorld( origPort
, origDev
) ;
305 DisposeRgn( clipRgn
) ;
309 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, int forceType
)
311 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
314 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
318 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
320 info
->contentType
= kControlContentPictHandle
;
321 info
->u
.picture
= MAC_WXHMETAFILE(bmap
->m_hPict
) ;
323 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
325 if ( (forceType
== kControlContentCIconHandle
|| ( bmap
->m_width
== bmap
->m_height
&& forceType
!= kControlContentPictHandle
) ) && ((bmap
->m_width
& 0x3) == 0) )
327 info
->contentType
= kControlContentCIconHandle
;
328 if ( bitmap
.GetMask() )
330 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap()) ,
331 8 , bmap
->m_width
) ;
335 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
,
336 8 , bmap
->m_width
) ;
341 info
->contentType
= kControlContentPictHandle
;
342 if ( bitmap
.GetMask() )
344 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap() ) ) ;
348 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
) ;
352 else if ( bmap
->m_bitmapType
== kMacBitmapTypeIcon
)
354 info
->contentType
= kControlContentCIconHandle
;
355 info
->u
.cIconHandle
= MAC_WXHICON(bmap
->m_hIcon
) ;
360 wxBitmapRefData::wxBitmapRefData()
372 m_bitmapType
= kMacBitmapTypeUnknownType
;
376 // TODO move this to a public function of Bitmap Ref
377 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
382 switch (data
->m_bitmapType
)
384 case kMacBitmapTypePict
:
388 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
389 data
->m_hPict
= NULL
;
393 case kMacBitmapTypeGrafWorld
:
395 if ( data
->m_hBitmap
)
397 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
398 data
->m_hBitmap
= NULL
;
402 case kMacBitmapTypeIcon
:
405 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
406 data
->m_hIcon
= NULL
;
414 if (data
->m_bitmapMask
)
416 delete data
->m_bitmapMask
;
417 data
->m_bitmapMask
= NULL
;
421 wxBitmapRefData::~wxBitmapRefData()
423 DisposeBitmapRefData( this ) ;
426 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
437 wxBitmap::~wxBitmap()
441 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
443 m_refData
= new wxBitmapRefData
;
445 M_BITMAPDATA
->m_width
= the_width
;
446 M_BITMAPDATA
->m_height
= the_height
;
447 M_BITMAPDATA
->m_depth
= no_bits
;
448 M_BITMAPDATA
->m_numColors
= 0;
451 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
452 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
453 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
456 GDHandle origDevice
;
458 GetGWorld( &origPort
, &origDevice
) ;
459 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
460 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
462 // bits is a char array
464 unsigned char* linestart
= (unsigned char*) bits
;
465 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
466 if ( the_width
% (sizeof(unsigned char) * 8) ) {
467 linesize
+= sizeof(unsigned char);
470 RGBColor colors
[2] = {
471 { 0xFFFF , 0xFFFF , 0xFFFF } ,
475 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
477 for ( int x
= 0 ; x
< the_width
; ++x
)
481 int mask
= 1 << bit
;
482 if ( linestart
[index
] & mask
)
484 SetCPixel( x
, y
, &colors
[1] ) ;
488 SetCPixel( x
, y
, &colors
[0] ) ;
492 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
494 SetGWorld( origPort
, origDevice
) ;
498 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
502 wxBitmap::wxBitmap(int w
, int h
, int d
)
504 (void)Create(w
, h
, d
);
507 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
509 (void) Create(data
, type
, width
, height
, depth
);
512 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
514 LoadFile(filename
, type
);
517 bool wxBitmap::CreateFromXpm(const char **bits
)
519 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
520 wxXPMDecoder decoder
;
521 wxImage img
= decoder
.ReadData(bits
);
522 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
523 *this = wxBitmap(img
);
527 wxBitmap::wxBitmap(const char **bits
)
529 (void) CreateFromXpm(bits
);
532 wxBitmap::wxBitmap(char **bits
)
534 (void) CreateFromXpm((const char **)bits
);
537 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
540 (rect
.x
>= 0) && (rect
.y
>= 0) &&
541 (rect
.x
+rect
.width
<= GetWidth()) &&
542 (rect
.y
+rect
.height
<= GetHeight()),
543 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
546 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
547 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
552 GetGWorld( &origPort
, &origDevice
);
554 // Update the subbitmaps reference data
555 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
557 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
559 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
560 #endif // wxUSE_PALETTE
561 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
563 // Copy sub region of this bitmap
564 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
566 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
568 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
573 GWorldPtr submask
, mask
;
576 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
577 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, GetMask()->GetDepth() );
578 LockPixels(GetGWorldPixMap(mask
));
579 LockPixels(GetGWorldPixMap(submask
));
581 for(int yy
= 0; yy
< rect
.height
; yy
++)
583 for(int xx
= 0; xx
< rect
.width
; xx
++)
585 SetGWorld(mask
, NULL
);
586 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
587 SetGWorld(submask
, NULL
);
588 SetCPixel(xx
,yy
, &color
);
591 UnlockPixels(GetGWorldPixMap(mask
));
592 UnlockPixels(GetGWorldPixMap(submask
));
593 ref
->m_bitmapMask
= new wxMask
;
594 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
600 GWorldPtr subbitmap
, bitmap
;
603 bitmap
= (GWorldPtr
) GetHBITMAP();
604 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
605 LockPixels(GetGWorldPixMap(bitmap
));
606 LockPixels(GetGWorldPixMap(subbitmap
));
608 for(int yy
= 0; yy
< rect
.height
; yy
++)
610 for(int xx
= 0; xx
< rect
.width
; xx
++)
612 SetGWorld(bitmap
, NULL
);
613 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
614 SetGWorld(subbitmap
, NULL
);
615 SetCPixel(xx
, yy
, &color
);
618 UnlockPixels(GetGWorldPixMap(bitmap
));
619 UnlockPixels(GetGWorldPixMap(subbitmap
));
622 SetGWorld( origPort
, origDevice
);
627 bool wxBitmap::Create(int w
, int h
, int d
)
631 m_refData
= new wxBitmapRefData
;
633 M_BITMAPDATA
->m_width
= w
;
634 M_BITMAPDATA
->m_height
= h
;
635 M_BITMAPDATA
->m_depth
= d
;
637 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
638 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
639 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
640 return M_BITMAPDATA
->m_ok
;
643 int wxBitmap::GetBitmapType() const
645 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
647 return M_BITMAPDATA
->m_bitmapType
;
650 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
653 m_refData
= new wxBitmapRefData
;
655 DisposeBitmapRefData( M_BITMAPDATA
) ;
657 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
658 M_BITMAPDATA
->m_hBitmap
= bmp
;
659 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
662 void wxBitmap::SetHICON(WXHICON ico
)
665 m_refData
= new wxBitmapRefData
;
667 DisposeBitmapRefData( M_BITMAPDATA
) ;
669 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeIcon
;
670 M_BITMAPDATA
->m_hIcon
= ico
;
671 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hIcon
!= NULL
) ;
674 void wxBitmap::SetPict(WXHMETAFILE pict
)
677 m_refData
= new wxBitmapRefData
;
679 DisposeBitmapRefData( M_BITMAPDATA
) ;
681 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypePict
;
682 M_BITMAPDATA
->m_hPict
= pict
;
683 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hPict
!= NULL
) ;
686 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
690 wxBitmapHandler
*handler
= FindHandler(type
);
694 m_refData
= new wxBitmapRefData
;
696 return handler
->LoadFile(this, filename
, type
, -1, -1);
700 wxImage
loadimage(filename
, type
);
701 if (loadimage
.Ok()) {
706 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
710 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
714 m_refData
= new wxBitmapRefData
;
716 wxBitmapHandler
*handler
= FindHandler(type
);
718 if ( handler
== NULL
) {
719 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
724 return handler
->Create(this, data
, type
, width
, height
, depth
);
727 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
729 wxCHECK_RET( image
.Ok(), wxT("invalid image") );
730 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") );
732 m_refData
= new wxBitmapRefData();
734 // width and height of the device-dependent bitmap
735 int width
= image
.GetWidth();
736 int height
= image
.GetHeight();
740 Create( width
, height
, 32 ) ;
743 GDHandle origDevice
;
745 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
746 LockPixels( pixMap
);
748 GetGWorld( &origPort
, &origDevice
) ;
749 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
752 register unsigned char* data
= image
.GetData();
753 char* destinationBase
= GetPixBaseAddr( pixMap
);
754 register unsigned char* destination
= (unsigned char*) destinationBase
;
755 for (int y
= 0; y
< height
; y
++)
757 for (int x
= 0; x
< width
; x
++)
760 *destination
++ = *data
++ ;
761 *destination
++ = *data
++ ;
762 *destination
++ = *data
++ ;
764 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
765 destination
= (unsigned char*) destinationBase
;
767 if ( image
.HasAlpha() )
769 unsigned char *alpha
= image
.GetAlpha();
771 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
773 wxBitmap maskBitmap
;
775 maskBitmap
.Create( width
, height
, 24);
776 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
777 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
779 for (int y
= 0; y
< height
; y
++)
781 for (int x
= 0; x
< width
; x
++)
783 memset( &color
, 255 - *alpha
, sizeof( color
) );
784 SetCPixel(x
,y
, &color
);
789 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
790 SetMask(new wxMask( maskBitmap
));
791 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
793 else if ( image
.HasMask() )
795 data
= image
.GetData();
797 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
798 RGBColor white
= { 0xffff, 0xffff, 0xffff };
799 RGBColor black
= { 0 , 0 , 0 };
800 wxBitmap maskBitmap
;
802 maskBitmap
.Create( width
, height
, 1);
803 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
804 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
806 for (int y
= 0; y
< height
; y
++)
808 for (int x
= 0; x
< width
; x
++)
810 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
812 SetCPixel(x
,y
, &white
);
815 SetCPixel(x
,y
, &black
);
820 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
821 SetMask(new wxMask( maskBitmap
));
822 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
825 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
826 SetGWorld( origPort
, origDevice
);
829 wxImage
wxBitmap::ConvertToImage() const
833 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
835 // create an wxImage object
836 int width
= GetWidth();
837 int height
= GetHeight();
838 image
.Create( width
, height
);
840 unsigned char *data
= image
.GetData();
842 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
846 RgnHandle maskRgn
= NULL
;
847 GWorldPtr tempPort
= NULL
;
850 // background color set to RGB(16,16,16) in consistent with wxGTK
851 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
853 wxMask
*mask
= GetMask();
855 GetGWorld( &origPort
, &origDevice
);
856 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
858 tempPort
= wxMacCreateGWorld( width
, height
, -1) ;
862 tempPort
= (GWorldPtr
) GetHBITMAP() ;
864 LockPixels(GetGWorldPixMap(tempPort
));
865 SetGWorld( tempPort
, NULL
);
866 if ( GetBitmapType() == kMacBitmapTypePict
|| GetBitmapType() == kMacBitmapTypeIcon
)
868 Rect bitmaprect
= { 0 , 0 , height
, width
};
869 if ( GetBitmapType() == kMacBitmapTypeIcon
)
871 ::PlotCIconHandle( &bitmaprect
, atNone
, ttNone
, MAC_WXHICON(GetHICON()) ) ;
873 BitMapToRegion( maskRgn
, &(**(MAC_WXHICON(GetHICON()))).iconMask
) ;
876 ::DrawPicture( (PicHandle
) GetPict(), &bitmaprect
) ;
878 // Copy data into image
880 for (int yy
= 0; yy
< height
; yy
++)
882 for (int xx
= 0; xx
< width
; xx
++)
884 GetCPixel(xx
,yy
, &color
);
885 r
= ((color
.red
) >> 8);
886 g
= ((color
.green
) >> 8);
887 b
= ((color
.blue
) >> 8);
896 if ( !PtInRgn( pt
, maskRgn
) )
898 data
[index
] = mask_r
;
899 data
[index
+ 1] = mask_g
;
900 data
[index
+ 2] = mask_b
;
907 if (mask
->PointMasked(xx
,yy
))
909 data
[index
] = mask_r
;
910 data
[index
+ 1] = mask_g
;
911 data
[index
+ 2] = mask_b
;
918 if (mask
|| maskRgn
)
920 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
921 image
.SetMask( true );
925 UnlockPixels(GetGWorldPixMap( tempPort
));
926 SetGWorld(origPort
, origDevice
);
927 if ( GetBitmapType() != kMacBitmapTypeGrafWorld
)
929 wxMacDestroyGWorld( tempPort
) ;
933 DisposeRgn( maskRgn
) ;
940 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
941 const wxPalette
*palette
) const
943 wxBitmapHandler
*handler
= FindHandler(type
);
947 return handler
->SaveFile(this, filename
, type
, palette
);
951 wxImage image
= ConvertToImage();
953 return image
.SaveFile(filename
, type
);
956 wxLogWarning(wxT("no bitmap handler for type %d defined."), type
);
960 bool wxBitmap::Ok() const
962 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
965 int wxBitmap::GetHeight() const
967 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
969 return M_BITMAPDATA
->m_height
;
972 int wxBitmap::GetWidth() const
974 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
976 return M_BITMAPDATA
->m_width
;
979 int wxBitmap::GetDepth() const
981 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
983 return M_BITMAPDATA
->m_depth
;
986 int wxBitmap::GetQuality() const
988 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
990 return M_BITMAPDATA
->m_quality
;
993 wxMask
*wxBitmap::GetMask() const
995 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
997 return M_BITMAPDATA
->m_bitmapMask
;
1000 void wxBitmap::SetWidth(int w
)
1003 m_refData
= new wxBitmapRefData
;
1005 M_BITMAPDATA
->m_width
= w
;
1008 void wxBitmap::SetHeight(int h
)
1011 m_refData
= new wxBitmapRefData
;
1013 M_BITMAPDATA
->m_height
= h
;
1016 void wxBitmap::SetDepth(int d
)
1019 m_refData
= new wxBitmapRefData
;
1021 M_BITMAPDATA
->m_depth
= d
;
1024 void wxBitmap::SetQuality(int q
)
1027 m_refData
= new wxBitmapRefData
;
1029 M_BITMAPDATA
->m_quality
= q
;
1032 void wxBitmap::SetOk(bool isOk
)
1035 m_refData
= new wxBitmapRefData
;
1037 M_BITMAPDATA
->m_ok
= isOk
;
1041 wxPalette
*wxBitmap::GetPalette() const
1043 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1045 return &M_BITMAPDATA
->m_bitmapPalette
;
1048 void wxBitmap::SetPalette(const wxPalette
& palette
)
1051 m_refData
= new wxBitmapRefData
;
1053 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1055 #endif // wxUSE_PALETTE
1057 void wxBitmap::SetMask(wxMask
*mask
)
1060 m_refData
= new wxBitmapRefData
;
1062 // Remove existing mask if there is one.
1063 delete M_BITMAPDATA
->m_bitmapMask
;
1065 M_BITMAPDATA
->m_bitmapMask
= mask
;
1068 WXHBITMAP
wxBitmap::GetHBITMAP() const
1070 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1072 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1075 WXHMETAFILE
wxBitmap::GetPict( bool *created
) const
1077 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1079 PicHandle picture
= NULL
; // This is the returned picture
1081 (*created
) = false ;
1082 // If bitmap already in Pict format return pointer
1083 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
1084 return M_BITMAPDATA
->m_hPict
;
1086 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
1094 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
1098 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
1100 if ( created
&& picture
)
1111 : m_maskBitmap(NULL
)
1115 // Construct a mask from a bitmap and a colour indicating
1116 // the transparent area
1117 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1118 : m_maskBitmap(NULL
)
1120 Create(bitmap
, colour
);
1123 // Construct a mask from a bitmap and a palette index indicating
1124 // the transparent area
1125 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1126 : m_maskBitmap(NULL
)
1128 Create(bitmap
, paletteIndex
);
1131 // Construct a mask from a mono bitmap (copies the bitmap).
1132 wxMask::wxMask(const wxBitmap
& bitmap
)
1133 : m_maskBitmap(NULL
)
1142 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1143 m_maskBitmap
= NULL
;
1147 // Create a mask from a mono bitmap (copies the bitmap).
1148 bool wxMask::Create(const wxBitmap
& bitmap
)
1152 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1153 m_maskBitmap
= NULL
;
1155 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1156 wxT("Cannot create mask from this bitmap type (TODO)"));
1157 // other types would require a temporary bitmap. not yet implemented
1159 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1161 m_depth
= bitmap
.GetDepth() ;
1162 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), bitmap
.GetDepth() );
1163 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1165 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1166 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1167 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1168 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1169 &rect
, &rect
, srcCopy
, 0);
1170 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1171 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1176 // Create a mask from a bitmap and a palette index indicating
1177 // the transparent area
1178 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1181 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1185 // Create a mask from a bitmap and a colour indicating
1186 // the transparent area
1187 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1191 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1192 m_maskBitmap
= NULL
;
1194 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1195 wxT("Cannot create mask from this bitmap type (TODO)"));
1196 // other types would require a temporary bitmap. not yet implemented
1198 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1200 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1202 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1203 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1204 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1206 // this is not very efficient, but I can't think
1207 // of a better way of doing it
1209 GDHandle origDevice
;
1211 RGBColor colors
[2] = {
1212 { 0xFFFF, 0xFFFF, 0xFFFF },
1215 GetGWorld( &origPort
, &origDevice
) ;
1216 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1218 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1220 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1221 GetCPixel( w
, h
, &col
) ;
1222 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1223 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1225 SetCPixel( w
, h
, &colors
[0] ) ;
1229 SetCPixel( w
, h
, &colors
[1] ) ;
1233 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1234 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1235 SetGWorld( origPort
, origDevice
) ;
1240 bool wxMask::PointMasked(int x
, int y
)
1243 GDHandle origDevice
;
1247 GetGWorld( &origPort
, &origDevice
);
1249 //Set port to mask and see if it masked (1) or not ( 0 )
1250 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1251 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1252 GetCPixel(x
,y
, &color
);
1253 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1254 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1256 SetGWorld( origPort
, origDevice
);
1265 wxBitmapHandler::~wxBitmapHandler()
1269 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1274 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1275 int desiredWidth
, int desiredHeight
)
1280 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1289 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1291 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1293 inline wxPICTResourceHandler()
1295 m_name
= wxT("Macintosh Pict resource");
1296 m_extension
= wxEmptyString
;
1297 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1300 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1301 int desiredWidth
, int desiredHeight
);
1303 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1305 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1306 int desiredWidth
, int desiredHeight
)
1309 wxMacStringToPascal( name
, theName
) ;
1311 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1316 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1317 DetachResource( (Handle
) thePict
) ;
1318 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1319 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1320 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1321 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1323 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1324 M_BITMAPHANDLERDATA
->m_ok
= true ;
1325 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1326 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1327 // M_BITMAPHANDLERDATA->m_quality;
1333 void wxBitmap::InitStandardHandlers()
1335 AddHandler(new wxPICTResourceHandler
) ;
1336 AddHandler(new wxICONResourceHandler
) ;
1339 // ----------------------------------------------------------------------------
1340 // raw bitmap access support
1341 // ----------------------------------------------------------------------------
1343 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1347 // no bitmap, no data (raw or otherwise)
1351 if ( M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
)
1353 wxFAIL_MSG( _T("GetRawData() only supported for GWorlds") );
1358 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1359 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1360 wxCHECK_MSG( hPixMap
&& *hPixMap
, NULL
,
1361 _T("GetRawData(): failed to get PixMap from GWorld?") );
1363 wxCHECK_MSG( (*hPixMap
)->pixelSize
== bpp
, NULL
,
1364 _T("GetRawData(): pixel format mismatch") );
1366 if ( !LockPixels(hPixMap
) )
1368 wxFAIL_MSG( _T("failed to lock PixMap in GetRawData()") );
1373 data
.m_width
= GetWidth();
1374 data
.m_height
= GetHeight();
1375 data
.m_stride
= (*hPixMap
)->rowBytes
& 0x7fff;
1377 M_BITMAPDATA
->m_hasAlpha
= false;
1379 return GetPixBaseAddr(hPixMap
);
1382 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1387 if ( M_BITMAPDATA
->m_hasAlpha
)
1389 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1391 int w
= data
.GetWidth(),
1392 h
= data
.GetHeight();
1394 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1395 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1396 wxAlphaPixelData::Iterator
pMask(dataMask
),
1398 for ( int y
= 0; y
< h
; y
++ )
1400 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1403 for ( int x
= 0; x
< w
; x
++ )
1405 const wxAlphaPixelData::Iterator::ChannelType
1408 pMask
.Red() = alpha
;
1409 pMask
.Green() = alpha
;
1410 pMask
.Blue() = alpha
;
1419 pMask
= rowStartMask
;
1420 pMask
.OffsetY(dataMask
, 1);
1423 SetMask(new wxMask(bmpMask
));
1426 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1427 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1430 UnlockPixels(hPixMap
);
1434 void wxBitmap::UseAlpha()
1436 // remember that we are using alpha channel, we'll need to create a proper
1437 // mask in UngetRawData()
1438 M_BITMAPDATA
->m_hasAlpha
= true;