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::IsOk() 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_BITMAPDATA
->m_width
= w
;
1006 void wxBitmap::SetHeight(int h
)
1009 M_BITMAPDATA
->m_height
= h
;
1012 void wxBitmap::SetDepth(int d
)
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
;
1035 wxPalette
*wxBitmap::GetPalette() const
1037 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
1039 return &M_BITMAPDATA
->m_bitmapPalette
;
1042 void wxBitmap::SetPalette(const wxPalette
& palette
)
1045 m_refData
= new wxBitmapRefData
;
1047 M_BITMAPDATA
->m_bitmapPalette
= palette
;
1049 #endif // wxUSE_PALETTE
1051 void wxBitmap::SetMask(wxMask
*mask
)
1054 m_refData
= new wxBitmapRefData
;
1056 // Remove existing mask if there is one.
1057 delete M_BITMAPDATA
->m_bitmapMask
;
1059 M_BITMAPDATA
->m_bitmapMask
= mask
;
1062 WXHBITMAP
wxBitmap::GetHBITMAP() const
1064 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1066 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1069 WXHMETAFILE
wxBitmap::GetPict( bool *created
) const
1071 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1073 PicHandle picture
= NULL
; // This is the returned picture
1075 (*created
) = false ;
1076 // If bitmap already in Pict format return pointer
1077 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
1078 return M_BITMAPDATA
->m_hPict
;
1080 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
1088 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
1092 picture
= wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
1094 if ( created
&& picture
)
1105 : m_maskBitmap(NULL
)
1109 // Construct a mask from a bitmap and a colour indicating
1110 // the transparent area
1111 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1112 : m_maskBitmap(NULL
)
1114 Create(bitmap
, colour
);
1117 // Construct a mask from a bitmap and a palette index indicating
1118 // the transparent area
1119 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1120 : m_maskBitmap(NULL
)
1122 Create(bitmap
, paletteIndex
);
1125 // Construct a mask from a mono bitmap (copies the bitmap).
1126 wxMask::wxMask(const wxBitmap
& bitmap
)
1127 : m_maskBitmap(NULL
)
1136 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1137 m_maskBitmap
= NULL
;
1141 // Create a mask from a mono bitmap (copies the bitmap).
1142 bool wxMask::Create(const wxBitmap
& bitmap
)
1146 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1147 m_maskBitmap
= NULL
;
1149 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1150 wxT("Cannot create mask from this bitmap type (TODO)"));
1151 // other types would require a temporary bitmap. not yet implemented
1153 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1155 m_depth
= bitmap
.GetDepth() ;
1156 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), bitmap
.GetDepth() );
1157 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1159 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1160 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1161 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1162 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1163 &rect
, &rect
, srcCopy
, 0);
1164 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1165 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1170 // Create a mask from a bitmap and a palette index indicating
1171 // the transparent area
1172 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1175 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1179 // Create a mask from a bitmap and a colour indicating
1180 // the transparent area
1181 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1185 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1186 m_maskBitmap
= NULL
;
1188 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1189 wxT("Cannot create mask from this bitmap type (TODO)"));
1190 // other types would require a temporary bitmap. not yet implemented
1192 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1194 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1196 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1197 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1198 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1200 // this is not very efficient, but I can't think
1201 // of a better way of doing it
1203 GDHandle origDevice
;
1205 RGBColor colors
[2] = {
1206 { 0xFFFF, 0xFFFF, 0xFFFF },
1209 GetGWorld( &origPort
, &origDevice
) ;
1210 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1212 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1214 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1215 GetCPixel( w
, h
, &col
) ;
1216 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1217 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1219 SetCPixel( w
, h
, &colors
[0] ) ;
1223 SetCPixel( w
, h
, &colors
[1] ) ;
1227 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1228 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1229 SetGWorld( origPort
, origDevice
) ;
1234 bool wxMask::PointMasked(int x
, int y
)
1237 GDHandle origDevice
;
1241 GetGWorld( &origPort
, &origDevice
);
1243 //Set port to mask and see if it masked (1) or not ( 0 )
1244 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1245 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1246 GetCPixel(x
,y
, &color
);
1247 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1248 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1250 SetGWorld( origPort
, origDevice
);
1259 wxBitmapHandler::~wxBitmapHandler()
1263 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1268 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1269 int desiredWidth
, int desiredHeight
)
1274 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1283 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1285 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1287 inline wxPICTResourceHandler()
1289 m_name
= wxT("Macintosh Pict resource");
1290 m_extension
= wxEmptyString
;
1291 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1294 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1295 int desiredWidth
, int desiredHeight
);
1297 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1299 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1300 int desiredWidth
, int desiredHeight
)
1303 wxMacStringToPascal( name
, theName
) ;
1305 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1310 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1311 DetachResource( (Handle
) thePict
) ;
1312 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1313 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1314 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1315 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1317 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1318 M_BITMAPHANDLERDATA
->m_ok
= true ;
1319 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1320 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1321 // M_BITMAPHANDLERDATA->m_quality;
1327 void wxBitmap::InitStandardHandlers()
1329 AddHandler(new wxPICTResourceHandler
) ;
1330 AddHandler(new wxICONResourceHandler
) ;
1333 // ----------------------------------------------------------------------------
1334 // raw bitmap access support
1335 // ----------------------------------------------------------------------------
1337 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1341 // no bitmap, no data (raw or otherwise)
1345 if ( M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
)
1347 wxFAIL_MSG( _T("GetRawData() only supported for GWorlds") );
1352 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1353 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1354 wxCHECK_MSG( hPixMap
&& *hPixMap
, NULL
,
1355 _T("GetRawData(): failed to get PixMap from GWorld?") );
1357 wxCHECK_MSG( (*hPixMap
)->pixelSize
== bpp
, NULL
,
1358 _T("GetRawData(): pixel format mismatch") );
1360 if ( !LockPixels(hPixMap
) )
1362 wxFAIL_MSG( _T("failed to lock PixMap in GetRawData()") );
1367 data
.m_width
= GetWidth();
1368 data
.m_height
= GetHeight();
1369 data
.m_stride
= (*hPixMap
)->rowBytes
& 0x7fff;
1371 M_BITMAPDATA
->m_hasAlpha
= false;
1373 return GetPixBaseAddr(hPixMap
);
1376 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1381 if ( M_BITMAPDATA
->m_hasAlpha
)
1383 wxAlphaPixelData
& data
= (wxAlphaPixelData
&)dataBase
;
1385 int w
= data
.GetWidth(),
1386 h
= data
.GetHeight();
1388 wxBitmap
bmpMask(GetWidth(), GetHeight(), 32);
1389 wxAlphaPixelData
dataMask(bmpMask
, data
.GetOrigin(), wxSize(w
, h
));
1390 wxAlphaPixelData::Iterator
pMask(dataMask
),
1392 for ( int y
= 0; y
< h
; y
++ )
1394 wxAlphaPixelData::Iterator rowStartMask
= pMask
,
1397 for ( int x
= 0; x
< w
; x
++ )
1399 const wxAlphaPixelData::Iterator::ChannelType
1402 pMask
.Red() = alpha
;
1403 pMask
.Green() = alpha
;
1404 pMask
.Blue() = alpha
;
1413 pMask
= rowStartMask
;
1414 pMask
.OffsetY(dataMask
, 1);
1417 SetMask(new wxMask(bmpMask
));
1420 GWorldPtr gworld
= MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
1421 PixMapHandle hPixMap
= GetGWorldPixMap(gworld
);
1424 UnlockPixels(hPixMap
);
1428 void wxBitmap::UseAlpha()
1430 // remember that we are using alpha channel, we'll need to create a proper
1431 // mask in UngetRawData()
1432 M_BITMAPDATA
->m_hasAlpha
= true;