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 do 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
) ;
704 register unsigned char* data
= image
.GetData();
705 char* destinationBase
= GetPixBaseAddr( pixMap
);
706 register unsigned char* destination
= (unsigned char*) destinationBase
;
707 for (int y
= 0; y
< height
; y
++)
709 for (int x
= 0; x
< width
; x
++)
712 *destination
++ = *data
++ ;
713 *destination
++ = *data
++ ;
714 *destination
++ = *data
++ ;
716 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
717 destination
= (unsigned char*) destinationBase
;
719 if ( image
.HasMask() )
721 data
= image
.GetData();
723 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
724 RGBColor white
= { 0xffff, 0xffff, 0xffff };
725 RGBColor black
= { 0 , 0 , 0 };
726 wxBitmap maskBitmap
;
728 maskBitmap
.Create( width
, height
, 1);
729 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
730 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
732 for (int y
= 0; y
< height
; y
++)
734 for (int x
= 0; x
< width
; x
++)
736 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
738 SetCPixel(x
,y
, &white
);
741 SetCPixel(x
,y
, &black
);
746 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
747 SetMask(new wxMask( maskBitmap
));
748 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
751 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
752 SetGWorld( origPort
, origDevice
);
755 wxImage
wxBitmap::ConvertToImage() const
759 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
761 // create an wxImage object
762 int width
= GetWidth();
763 int height
= GetHeight();
764 image
.Create( width
, height
);
766 unsigned char *data
= image
.GetData();
768 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
774 // background color set to RGB(16,16,16) in consistent with wxGTK
775 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
777 wxMask
*mask
= GetMask();
779 GetGWorld( &origPort
, &origDevice
);
780 LockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
781 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
783 // Copy data into image
785 for (int yy
= 0; yy
< height
; yy
++)
787 for (int xx
= 0; xx
< width
; xx
++)
789 GetCPixel(xx
,yy
, &color
);
790 r
= ((color
.red
) >> 8);
791 g
= ((color
.green
) >> 8);
792 b
= ((color
.blue
) >> 8);
798 if (mask
->PointMasked(xx
,yy
))
800 data
[index
] = mask_r
;
801 data
[index
+ 1] = mask_g
;
802 data
[index
+ 2] = mask_b
;
810 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
811 image
.SetMask( true );
815 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
816 SetGWorld(origPort
, origDevice
);
822 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
823 const wxPalette
*palette
) const
825 wxBitmapHandler
*handler
= FindHandler(type
);
829 return handler
->SaveFile(this, filename
, type
, palette
);
833 wxImage image
= ConvertToImage();
835 return image
.SaveFile(filename
, type
);
838 wxLogWarning("no bitmap handler for type %d defined.", type
);
842 bool wxBitmap::Ok() const
844 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
847 int wxBitmap::GetHeight() const
849 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
851 return M_BITMAPDATA
->m_height
;
854 int wxBitmap::GetWidth() const
856 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
858 return M_BITMAPDATA
->m_width
;
861 int wxBitmap::GetDepth() const
863 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
865 return M_BITMAPDATA
->m_depth
;
868 int wxBitmap::GetQuality() const
870 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
872 return M_BITMAPDATA
->m_quality
;
875 wxMask
*wxBitmap::GetMask() const
877 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
879 return M_BITMAPDATA
->m_bitmapMask
;
882 void wxBitmap::SetWidth(int w
)
885 m_refData
= new wxBitmapRefData
;
887 M_BITMAPDATA
->m_width
= w
;
890 void wxBitmap::SetHeight(int h
)
893 m_refData
= new wxBitmapRefData
;
895 M_BITMAPDATA
->m_height
= h
;
898 void wxBitmap::SetDepth(int d
)
901 m_refData
= new wxBitmapRefData
;
903 M_BITMAPDATA
->m_depth
= d
;
906 void wxBitmap::SetQuality(int q
)
909 m_refData
= new wxBitmapRefData
;
911 M_BITMAPDATA
->m_quality
= q
;
914 void wxBitmap::SetOk(bool isOk
)
917 m_refData
= new wxBitmapRefData
;
919 M_BITMAPDATA
->m_ok
= isOk
;
922 wxPalette
*wxBitmap::GetPalette() const
924 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
926 return &M_BITMAPDATA
->m_bitmapPalette
;
929 void wxBitmap::SetPalette(const wxPalette
& palette
)
932 m_refData
= new wxBitmapRefData
;
934 M_BITMAPDATA
->m_bitmapPalette
= palette
;
937 void wxBitmap::SetMask(wxMask
*mask
)
940 m_refData
= new wxBitmapRefData
;
942 // Remove existing mask if there is one.
943 if (M_BITMAPDATA
->m_bitmapMask
)
944 delete M_BITMAPDATA
->m_bitmapMask
;
946 M_BITMAPDATA
->m_bitmapMask
= mask
;
949 WXHBITMAP
wxBitmap::GetHBITMAP() const
951 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
953 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
956 WXHMETAFILE
wxBitmap::GetPict() const
958 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
960 PicHandle picture
; // This is the returned picture
962 // If bitmap already in Pict format return pointer
963 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
964 return M_BITMAPDATA
->m_hPict
;
966 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
971 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
972 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
973 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
979 GetPortBounds( (GWorldPtr
) GetHBITMAP() , &portRect
) ;
980 int width
= portRect
.right
- portRect
.left
;
981 int height
= portRect
.bottom
- portRect
.top
;
983 LockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
984 GetGWorld( &origPort
, &origDev
) ;
988 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
990 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
998 RGBColor trans
= white
;
1000 RGBBackColor( &gray
);
1001 EraseRect( &portRect
);
1002 RGBColor trans
= gray
;
1004 RGBForeColor( &black
) ;
1005 RGBBackColor( &white
) ;
1006 PenMode(transparent
);
1008 for ( int y
= 0 ; y
< height
; ++y
)
1010 for( int x
= 0 ; x
< width
; ++x
)
1012 if ( !mask
->PointMasked(x
,y
) )
1016 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
1017 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
1020 // With transparency this sets a blank pixel
1021 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
1028 RGBBackColor( &gray
) ;
1029 EraseRect(&portRect
);
1030 RGBForeColor( &black
) ;
1031 RGBBackColor( &white
) ;
1033 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
1034 // src PixMap - we copy image over itself -
1035 GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
1036 // dst PixMap - no drawing occurs
1037 &portRect
, // srcRect - it will be recorded and compressed -
1038 &portRect
, // dstRect - into the picture that is open -
1039 srcCopy
,NULL
); // copyMode and no clip region
1041 ClosePicture(); // We are done recording the picture
1042 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
1043 SetGWorld( origPort
, origDev
) ;
1045 return picture
; // return our groovy pict handle
1057 // Construct a mask from a bitmap and a colour indicating
1058 // the transparent area
1059 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1062 Create(bitmap
, colour
);
1065 // Construct a mask from a bitmap and a palette index indicating
1066 // the transparent area
1067 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1070 Create(bitmap
, paletteIndex
);
1073 // Construct a mask from a mono bitmap (copies the bitmap).
1074 wxMask::wxMask(const wxBitmap
& bitmap
)
1084 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1085 m_maskBitmap
= NULL
;
1089 // Create a mask from a mono bitmap (copies the bitmap).
1090 bool wxMask::Create(const wxBitmap
& bitmap
)
1094 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1095 m_maskBitmap
= NULL
;
1097 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1098 wxT("Cannot create mask from this bitmap type (TODO)"));
1099 // other types would require a temporary bitmap. not yet implemented
1101 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1103 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1104 wxT("Cannot create mask from colour bitmap"));
1106 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1107 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1109 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1110 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1111 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1112 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1113 &rect
, &rect
, srcCopy
, 0);
1114 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1115 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1120 // Create a mask from a bitmap and a palette index indicating
1121 // the transparent area
1122 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1125 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1129 // Create a mask from a bitmap and a colour indicating
1130 // the transparent area
1131 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1135 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1136 m_maskBitmap
= NULL
;
1138 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1139 wxT("Cannot create mask from this bitmap type (TODO)"));
1140 // other types would require a temporary bitmap. not yet implemented
1142 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1144 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1145 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1146 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1147 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1149 // this is not very efficient, but I can't think
1150 // of a better way of doing it
1152 GDHandle origDevice
;
1154 RGBColor colors
[2] = {
1155 { 0xFFFF, 0xFFFF, 0xFFFF },
1158 GetGWorld( &origPort
, &origDevice
) ;
1159 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1161 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1163 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1164 GetCPixel( w
, h
, &col
) ;
1165 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1166 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1168 SetCPixel( w
, h
, &colors
[0] ) ;
1172 SetCPixel( w
, h
, &colors
[1] ) ;
1176 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1177 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1178 SetGWorld( origPort
, origDevice
) ;
1183 bool wxMask::PointMasked(int x
, int y
)
1186 GDHandle origDevice
;
1190 GetGWorld( &origPort
, &origDevice
);
1192 //Set port to mask and see if it masked (1) or not ( 0 )
1193 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1194 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1195 GetCPixel(x
,y
, &color
);
1196 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1197 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1199 SetGWorld( origPort
, origDevice
);
1208 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1213 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1214 int desiredWidth
, int desiredHeight
)
1219 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1228 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1230 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1232 inline wxPICTResourceHandler()
1234 m_name
= "Macintosh Pict resource";
1236 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1239 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1240 int desiredWidth
, int desiredHeight
);
1242 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1244 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1245 int desiredWidth
, int desiredHeight
)
1250 c2pstrcpy( (StringPtr
) theName
, name
) ;
1252 strcpy( (char *) theName
, name
) ;
1253 c2pstr( (char *)theName
) ;
1256 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1261 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1262 DetachResource( (Handle
) thePict
) ;
1263 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1264 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1265 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1266 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1268 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1269 M_BITMAPHANDLERDATA
->m_ok
= true ;
1270 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1271 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1272 // M_BITMAPHANDLERDATA->m_quality;
1278 void wxBitmap::InitStandardHandlers()
1280 AddHandler(new wxPICTResourceHandler
) ;
1281 AddHandler(new wxICONResourceHandler
) ;