1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
18 #include "wx/bitmap.h"
22 #include "wx/xpmdecod.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
25 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
26 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
29 #include <ApplicationServices/ApplicationServices.h>
31 #include <PictUtils.h>
34 #include "wx/mac/uma.h"
36 CTabHandle
wxMacCreateColorTable( int numColors
)
38 CTabHandle newColors
; /* Handle to the new color table */
40 /* Allocate memory for the color table */
41 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
42 sizeof (ColorSpec
) * (numColors
- 1) );
45 /* Initialize the fields */
46 (**newColors
).ctSeed
= GetCTSeed();
47 (**newColors
).ctFlags
= 0;
48 (**newColors
).ctSize
= numColors
- 1;
49 /* Initialize the table of colors */
54 void wxMacDestroyColorTable( CTabHandle colors
)
56 DisposeHandle( (Handle
) colors
) ;
59 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
61 (**newColors
).ctTable
[index
].value
= index
;
62 (**newColors
).ctTable
[index
].rgb
.red
= red
; // someRedValue;
63 (**newColors
).ctTable
[index
].rgb
.green
= green
; // someGreenValue;
64 (**newColors
).ctTable
[index
].rgb
.blue
= blue
; // someBlueValue;
67 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
71 Rect rect
= { 0 , 0 , height
, width
} ;
75 depth
= wxDisplayDepth() ;
78 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
86 void wxMacDestroyGWorld( GWorldPtr gw
)
92 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
94 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
96 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
97 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
98 OSErr error
; /* Returns error code */
99 short bytesPerRow
; /* Number of bytes per row in the PixMap */
106 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
108 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
112 error
= HandToHand((Handle
*) &newColors
);
116 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
122 /* Allocate pixel image; long integer multiplication avoids overflow */
123 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
125 if ((**icon
).iconData
!= nil
)
127 /* Initialize fields common to indexed and direct PixMaps */
128 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
129 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
131 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
132 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
133 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
134 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
135 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
136 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
137 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
139 /* Initialize fields specific to indexed and direct PixMaps */
142 /* PixMap is indexed */
143 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
144 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
145 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
146 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
150 /* PixMap is direct */
151 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
152 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
154 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
156 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
157 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
158 (**newColors
).ctFlags
= 0;
159 (**newColors
).ctSize
= 0;
160 (**icon
).iconPMap
.pmTable
= newColors
;
169 /* If no errors occured, return a handle to the new off-screen PixMap */
172 if (newColors
!= nil
)
173 DisposeCTable(newColors
);
176 /* Return the error code */
180 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
185 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
186 SetGWorld(image
,nil
);
188 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
189 Rect imageBounds
= frame
;
190 GetPortBounds( image
, &imageBounds
) ;
192 int bwSize
= iconSize
/ 8 * iconSize
;
193 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
194 HLock((Handle
)icon
) ;
195 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
196 HLock( (**icon
).iconData
) ;
197 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
199 LockPixels(GetGWorldPixMap(image
));
201 CopyBits(GetPortBitMapForCopyBits(image
),
202 (BitMapPtr
)&((**icon
).iconPMap
),
205 srcCopy
| ditherCopy
, nil
);
208 UnlockPixels(GetGWorldPixMap(image
));
209 HUnlock( (**icon
).iconData
) ;
211 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
212 (**icon
).iconMask
.bounds
= frame
;
214 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
215 (**icon
).iconBMap
.bounds
= frame
;
216 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
217 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
221 LockPixels(GetGWorldPixMap(mask
) ) ;
222 CopyBits(GetPortBitMapForCopyBits(mask
) ,
223 &(**icon
).iconBMap
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
224 CopyBits(GetPortBitMapForCopyBits(mask
) ,
225 &(**icon
).iconMask
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
226 UnlockPixels(GetGWorldPixMap( mask
) ) ;
230 LockPixels(GetGWorldPixMap(image
));
231 CopyBits(GetPortBitMapForCopyBits(image
) ,
232 &(**icon
).iconBMap
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
233 CopyBits(GetPortBitMapForCopyBits(image
) ,
234 &(**icon
).iconMask
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
235 UnlockPixels(GetGWorldPixMap(image
));
238 (**icon
).iconMask
.baseAddr
= NULL
;
239 (**icon
).iconBMap
.baseAddr
= NULL
;
240 (**icon
).iconPMap
.baseAddr
= NULL
;
241 HUnlock((Handle
)icon
) ;
242 SetGWorld(saveWorld
,saveHandle
);
247 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
254 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
255 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
257 GetGWorld( &origPort
, &origDev
) ;
259 RgnHandle clipRgn
= NULL
;
264 LockPixels( GetGWorldPixMap( mask
) ) ;
265 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
266 UnlockPixels( GetGWorldPixMap( mask
) ) ;
269 SetGWorld( wp
, NULL
) ;
271 GetPortBounds( wp
, &portRect
) ;
273 pict
= OpenPicture(&portRect
);
276 RGBForeColor( &black
) ;
277 RGBBackColor( &white
) ;
279 LockPixels( GetGWorldPixMap( wp
) ) ;
280 CopyBits(GetPortBitMapForCopyBits(wp
),
281 GetPortBitMapForCopyBits(wp
),
285 UnlockPixels( GetGWorldPixMap( wp
) ) ;
288 SetGWorld( origPort
, origDev
) ;
292 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
)
294 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
297 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
301 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
303 info
->contentType
= kControlContentPictHandle
;
304 info
->u
.picture
= MAC_WXHMETAFILE(bmap
->m_hPict
) ;
306 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
308 if ( (bmap
->m_width
== bmap
->m_height
) && (bmap
->m_width
& 0x3 == 0) )
310 info
->contentType
= kControlContentCIconHandle
;
311 if ( bitmap
.GetMask() )
313 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap()) ,
314 8 , bmap
->m_width
) ;
318 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
,
319 8 , bmap
->m_width
) ;
324 info
->contentType
= kControlContentPictHandle
;
325 if ( bitmap
.GetMask() )
327 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap() ) ) ;
331 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
) ;
335 else if ( bmap
->m_bitmapType
== kMacBitmapTypeIcon
)
337 info
->contentType
= kControlContentCIconHandle
;
338 info
->u
.cIconHandle
= MAC_WXHICON(bmap
->m_hIcon
) ;
343 wxBitmapRefData::wxBitmapRefData()
355 m_bitmapType
= kMacBitmapTypeUnknownType
;
358 // TODO move this to a public function of Bitmap Ref
359 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
361 switch (data
->m_bitmapType
)
363 case kMacBitmapTypePict
:
367 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
368 data
->m_hPict
= NULL
;
372 case kMacBitmapTypeGrafWorld
:
374 if ( data
->m_hBitmap
)
376 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
377 data
->m_hBitmap
= NULL
;
381 case kMacBitmapTypeIcon
:
384 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
385 data
->m_hIcon
= NULL
;
393 if (data
->m_bitmapMask
)
395 delete data
->m_bitmapMask
;
396 data
->m_bitmapMask
= NULL
;
400 wxBitmapRefData::~wxBitmapRefData()
402 DisposeBitmapRefData( this ) ;
405 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
416 wxBitmap::~wxBitmap()
420 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
422 m_refData
= new wxBitmapRefData
;
424 M_BITMAPDATA
->m_width
= the_width
;
425 M_BITMAPDATA
->m_height
= the_height
;
426 M_BITMAPDATA
->m_depth
= no_bits
;
427 M_BITMAPDATA
->m_numColors
= 0;
430 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
431 MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) = wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
432 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
435 GDHandle origDevice
;
437 GetGWorld( &origPort
, &origDevice
) ;
438 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
439 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
441 // bits is a char array
443 unsigned char* linestart
= (unsigned char*) bits
;
444 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
445 if ( the_width
% (sizeof(unsigned char) * 8) ) {
446 linesize
+= sizeof(unsigned char);
449 RGBColor colors
[2] = {
450 { 0xFFFF , 0xFFFF , 0xFFFF } ,
454 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
456 for ( int x
= 0 ; x
< the_width
; ++x
)
460 int mask
= 1 << bit
;
461 if ( linestart
[index
] & mask
)
463 SetCPixel( x
, y
, &colors
[1] ) ;
467 SetCPixel( x
, y
, &colors
[0] ) ;
471 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
473 SetGWorld( origPort
, origDevice
) ;
477 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
481 wxBitmap::wxBitmap(int w
, int h
, int d
)
483 (void)Create(w
, h
, d
);
486 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
488 (void) Create(data
, type
, width
, height
, depth
);
491 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
493 LoadFile(filename
, type
);
496 bool wxBitmap::CreateFromXpm(const char **bits
)
498 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
499 wxXPMDecoder decoder
;
500 wxImage img
= decoder
.ReadData(bits
);
501 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
502 *this = wxBitmap(img
);
506 wxBitmap::wxBitmap(const char **bits
)
508 (void) CreateFromXpm(bits
);
511 wxBitmap::wxBitmap(char **bits
)
513 (void) CreateFromXpm((const char **)bits
);
516 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
519 (rect
.x
>= 0) && (rect
.y
>= 0) &&
520 (rect
.x
+rect
.width
<= GetWidth()) &&
521 (rect
.y
+rect
.height
<= GetHeight()),
522 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
525 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
526 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
531 GetGWorld( &origPort
, &origDevice
);
533 // Update the subbitmaps reference data
534 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
536 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
537 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
538 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
540 // Copy sub region of this bitmap
541 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
543 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
545 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
550 GWorldPtr submask
, mask
;
553 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
554 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
555 LockPixels(GetGWorldPixMap(mask
));
556 LockPixels(GetGWorldPixMap(submask
));
558 for(int yy
= 0; yy
< rect
.height
; yy
++)
560 for(int xx
= 0; xx
< rect
.width
; xx
++)
562 SetGWorld(mask
, NULL
);
563 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
564 SetGWorld(submask
, NULL
);
565 SetCPixel(xx
,yy
, &color
);
568 UnlockPixels(GetGWorldPixMap(mask
));
569 UnlockPixels(GetGWorldPixMap(submask
));
570 ref
->m_bitmapMask
= new wxMask
;
571 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
577 GWorldPtr subbitmap
, bitmap
;
580 bitmap
= (GWorldPtr
) GetHBITMAP();
581 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
582 LockPixels(GetGWorldPixMap(bitmap
));
583 LockPixels(GetGWorldPixMap(subbitmap
));
585 for(int yy
= 0; yy
< rect
.height
; yy
++)
587 for(int xx
= 0; xx
< rect
.width
; xx
++)
589 SetGWorld(bitmap
, NULL
);
590 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
591 SetGWorld(subbitmap
, NULL
);
592 SetCPixel(xx
, yy
, &color
);
595 UnlockPixels(GetGWorldPixMap(bitmap
));
596 UnlockPixels(GetGWorldPixMap(subbitmap
));
599 SetGWorld( origPort
, origDevice
);
604 bool wxBitmap::Create(int w
, int h
, int d
)
608 m_refData
= new wxBitmapRefData
;
610 M_BITMAPDATA
->m_width
= w
;
611 M_BITMAPDATA
->m_height
= h
;
612 M_BITMAPDATA
->m_depth
= d
;
614 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
615 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
616 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
617 return M_BITMAPDATA
->m_ok
;
620 int wxBitmap::GetBitmapType() const
622 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
624 return M_BITMAPDATA
->m_bitmapType
;
627 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
629 DisposeBitmapRefData( M_BITMAPDATA
) ;
631 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
632 M_BITMAPDATA
->m_hBitmap
= bmp
;
633 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
636 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
640 wxBitmapHandler
*handler
= FindHandler(type
);
644 m_refData
= new wxBitmapRefData
;
646 return handler
->LoadFile(this, filename
, type
, -1, -1);
650 wxImage
loadimage(filename
, type
);
651 if (loadimage
.Ok()) {
656 wxLogWarning("no bitmap handler for type %d defined.", type
);
660 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
664 m_refData
= new wxBitmapRefData
;
666 wxBitmapHandler
*handler
= FindHandler(type
);
668 if ( handler
== NULL
) {
669 wxLogWarning("no bitmap handler for type %d defined.", type
);
674 return handler
->Create(this, data
, type
, width
, height
, depth
);
677 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
679 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
680 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
682 m_refData
= new wxBitmapRefData();
684 // width and height of the device-dependent bitmap
685 int width
= image
.GetWidth();
686 int height
= image
.GetHeight();
690 Create( width
, height
, 32 ) ;
693 GDHandle origDevice
;
695 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
696 LockPixels( pixMap
);
698 GetGWorld( &origPort
, &origDevice
) ;
699 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
702 register unsigned char* data
= image
.GetData();
703 char* destinationBase
= GetPixBaseAddr( pixMap
);
704 register unsigned char* destination
= (unsigned char*) destinationBase
;
705 for (int y
= 0; y
< height
; y
++)
707 for (int x
= 0; x
< width
; x
++)
710 *destination
++ = *data
++ ;
711 *destination
++ = *data
++ ;
712 *destination
++ = *data
++ ;
714 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
715 destination
= (unsigned char*) destinationBase
;
717 if ( image
.HasMask() )
719 data
= image
.GetData();
721 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
722 RGBColor white
= { 0xffff, 0xffff, 0xffff };
723 RGBColor black
= { 0 , 0 , 0 };
724 wxBitmap maskBitmap
;
726 maskBitmap
.Create( width
, height
, 1);
727 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
728 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
730 for (int y
= 0; y
< height
; y
++)
732 for (int x
= 0; x
< width
; x
++)
734 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
736 SetCPixel(x
,y
, &white
);
739 SetCPixel(x
,y
, &black
);
744 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
745 SetMask(new wxMask( maskBitmap
));
746 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
749 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
750 SetGWorld( origPort
, origDevice
);
753 wxImage
wxBitmap::ConvertToImage() const
757 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
759 // create an wxImage object
760 int width
= GetWidth();
761 int height
= GetHeight();
762 image
.Create( width
, height
);
764 unsigned char *data
= image
.GetData();
766 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
772 // background color set to RGB(16,16,16) in consistent with wxGTK
773 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
775 wxMask
*mask
= GetMask();
777 GetGWorld( &origPort
, &origDevice
);
778 LockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
779 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
781 // Copy data into image
783 for (int yy
= 0; yy
< height
; yy
++)
785 for (int xx
= 0; xx
< width
; xx
++)
787 GetCPixel(xx
,yy
, &color
);
788 r
= ((color
.red
) >> 8);
789 g
= ((color
.green
) >> 8);
790 b
= ((color
.blue
) >> 8);
796 if (mask
->PointMasked(xx
,yy
))
798 data
[index
] = mask_r
;
799 data
[index
+ 1] = mask_g
;
800 data
[index
+ 2] = mask_b
;
808 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
809 image
.SetMask( true );
813 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
814 SetGWorld(origPort
, origDevice
);
820 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
821 const wxPalette
*palette
) const
823 wxBitmapHandler
*handler
= FindHandler(type
);
827 return handler
->SaveFile(this, filename
, type
, palette
);
831 wxImage image
= ConvertToImage();
833 return image
.SaveFile(filename
, type
);
836 wxLogWarning("no bitmap handler for type %d defined.", type
);
840 bool wxBitmap::Ok() const
842 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
845 int wxBitmap::GetHeight() const
847 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
849 return M_BITMAPDATA
->m_height
;
852 int wxBitmap::GetWidth() const
854 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
856 return M_BITMAPDATA
->m_width
;
859 int wxBitmap::GetDepth() const
861 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
863 return M_BITMAPDATA
->m_depth
;
866 int wxBitmap::GetQuality() const
868 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
870 return M_BITMAPDATA
->m_quality
;
873 wxMask
*wxBitmap::GetMask() const
875 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
877 return M_BITMAPDATA
->m_bitmapMask
;
880 void wxBitmap::SetWidth(int w
)
883 m_refData
= new wxBitmapRefData
;
885 M_BITMAPDATA
->m_width
= w
;
888 void wxBitmap::SetHeight(int h
)
891 m_refData
= new wxBitmapRefData
;
893 M_BITMAPDATA
->m_height
= h
;
896 void wxBitmap::SetDepth(int d
)
899 m_refData
= new wxBitmapRefData
;
901 M_BITMAPDATA
->m_depth
= d
;
904 void wxBitmap::SetQuality(int q
)
907 m_refData
= new wxBitmapRefData
;
909 M_BITMAPDATA
->m_quality
= q
;
912 void wxBitmap::SetOk(bool isOk
)
915 m_refData
= new wxBitmapRefData
;
917 M_BITMAPDATA
->m_ok
= isOk
;
920 wxPalette
*wxBitmap::GetPalette() const
922 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
924 return &M_BITMAPDATA
->m_bitmapPalette
;
927 void wxBitmap::SetPalette(const wxPalette
& palette
)
930 m_refData
= new wxBitmapRefData
;
932 M_BITMAPDATA
->m_bitmapPalette
= palette
;
935 void wxBitmap::SetMask(wxMask
*mask
)
938 m_refData
= new wxBitmapRefData
;
940 // Remove existing mask if there is one.
941 if (M_BITMAPDATA
->m_bitmapMask
)
942 delete M_BITMAPDATA
->m_bitmapMask
;
944 M_BITMAPDATA
->m_bitmapMask
= mask
;
947 WXHBITMAP
wxBitmap::GetHBITMAP() const
949 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
951 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
954 WXHMETAFILE
wxBitmap::GetPict() const
956 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
958 PicHandle picture
; // This is the returned picture
960 // If bitmap already in Pict format return pointer
961 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
962 return M_BITMAPDATA
->m_hPict
;
964 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
969 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
970 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
971 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
977 GetPortBounds( (GWorldPtr
) GetHBITMAP() , &portRect
) ;
978 int width
= portRect
.right
- portRect
.left
;
979 int height
= portRect
.bottom
- portRect
.top
;
981 LockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
982 GetGWorld( &origPort
, &origDev
) ;
986 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
988 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
996 RGBColor trans
= white
;
998 RGBBackColor( &gray
);
999 EraseRect( &portRect
);
1000 RGBColor trans
= gray
;
1002 RGBForeColor( &black
) ;
1003 RGBBackColor( &white
) ;
1004 PenMode(transparent
);
1006 for ( int y
= 0 ; y
< height
; ++y
)
1008 for( int x
= 0 ; x
< width
; ++x
)
1010 if ( !mask
->PointMasked(x
,y
) )
1014 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
1015 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
1018 // With transparency this sets a blank pixel
1019 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
1026 RGBBackColor( &gray
) ;
1027 EraseRect(&portRect
);
1028 RGBForeColor( &black
) ;
1029 RGBBackColor( &white
) ;
1031 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
1032 // src PixMap - we copy image over itself -
1033 GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
1034 // dst PixMap - no drawing occurs
1035 &portRect
, // srcRect - it will be recorded and compressed -
1036 &portRect
, // dstRect - into the picture that is open -
1037 srcCopy
,NULL
); // copyMode and no clip region
1039 ClosePicture(); // We are done recording the picture
1040 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
1041 SetGWorld( origPort
, origDev
) ;
1043 return picture
; // return our groovy pict handle
1051 : m_maskBitmap(NULL
)
1055 // Construct a mask from a bitmap and a colour indicating
1056 // the transparent area
1057 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1058 : m_maskBitmap(NULL
)
1060 Create(bitmap
, colour
);
1063 // Construct a mask from a bitmap and a palette index indicating
1064 // the transparent area
1065 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1066 : m_maskBitmap(NULL
)
1068 Create(bitmap
, paletteIndex
);
1071 // Construct a mask from a mono bitmap (copies the bitmap).
1072 wxMask::wxMask(const wxBitmap
& bitmap
)
1073 : m_maskBitmap(NULL
)
1082 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1083 m_maskBitmap
= NULL
;
1087 // Create a mask from a mono bitmap (copies the bitmap).
1088 bool wxMask::Create(const wxBitmap
& bitmap
)
1092 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1093 m_maskBitmap
= NULL
;
1095 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1096 wxT("Cannot create mask from this bitmap type (TODO)"));
1097 // other types would require a temporary bitmap. not yet implemented
1099 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1101 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1102 wxT("Cannot create mask from colour bitmap"));
1104 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1105 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1107 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1108 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1109 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1110 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1111 &rect
, &rect
, srcCopy
, 0);
1112 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1113 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1118 // Create a mask from a bitmap and a palette index indicating
1119 // the transparent area
1120 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1123 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1127 // Create a mask from a bitmap and a colour indicating
1128 // the transparent area
1129 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1133 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1134 m_maskBitmap
= NULL
;
1136 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1137 wxT("Cannot create mask from this bitmap type (TODO)"));
1138 // other types would require a temporary bitmap. not yet implemented
1140 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1142 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1143 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1144 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1145 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1147 // this is not very efficient, but I can't think
1148 // of a better way of doing it
1150 GDHandle origDevice
;
1152 RGBColor colors
[2] = {
1153 { 0xFFFF, 0xFFFF, 0xFFFF },
1156 GetGWorld( &origPort
, &origDevice
) ;
1157 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1159 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1161 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1162 GetCPixel( w
, h
, &col
) ;
1163 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1164 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1166 SetCPixel( w
, h
, &colors
[0] ) ;
1170 SetCPixel( w
, h
, &colors
[1] ) ;
1174 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1175 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1176 SetGWorld( origPort
, origDevice
) ;
1181 bool wxMask::PointMasked(int x
, int y
)
1184 GDHandle origDevice
;
1188 GetGWorld( &origPort
, &origDevice
);
1190 //Set port to mask and see if it masked (1) or not ( 0 )
1191 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1192 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1193 GetCPixel(x
,y
, &color
);
1194 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1195 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1197 SetGWorld( origPort
, origDevice
);
1206 wxBitmapHandler::~wxBitmapHandler()
1210 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1215 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1216 int desiredWidth
, int desiredHeight
)
1221 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1230 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1232 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1234 inline wxPICTResourceHandler()
1236 m_name
= "Macintosh Pict resource";
1238 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1241 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1242 int desiredWidth
, int desiredHeight
);
1244 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1246 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1247 int desiredWidth
, int desiredHeight
)
1252 c2pstrcpy( (StringPtr
) theName
, name
) ;
1254 strcpy( (char *) theName
, name
) ;
1255 c2pstr( (char *)theName
) ;
1258 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1263 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1264 DetachResource( (Handle
) thePict
) ;
1265 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1266 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1267 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1268 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1270 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1271 M_BITMAPHANDLERDATA
->m_ok
= true ;
1272 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1273 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1274 // M_BITMAPHANDLERDATA->m_quality;
1280 void wxBitmap::InitStandardHandlers()
1282 AddHandler(new wxPICTResourceHandler
) ;
1283 AddHandler(new wxICONResourceHandler
) ;