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
;
222 GetPortBounds( image
, &r
) ;
223 LockPixels(GetGWorldPixMap(mask
) ) ;
224 CopyBits(GetPortBitMapForCopyBits(mask
) ,
225 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
226 CopyBits(GetPortBitMapForCopyBits(mask
) ,
227 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
228 UnlockPixels(GetGWorldPixMap( mask
) ) ;
233 GetPortBounds( image
, &r
) ;
234 LockPixels(GetGWorldPixMap(image
));
235 CopyBits(GetPortBitMapForCopyBits(image
) ,
236 &(**icon
).iconBMap
, &r
, &r
, srcCopy
, nil
) ;
237 CopyBits(GetPortBitMapForCopyBits(image
) ,
238 &(**icon
).iconMask
, &r
, &r
, srcCopy
, nil
) ;
239 UnlockPixels(GetGWorldPixMap(image
));
242 (**icon
).iconMask
.baseAddr
= NULL
;
243 (**icon
).iconBMap
.baseAddr
= NULL
;
244 (**icon
).iconPMap
.baseAddr
= NULL
;
245 HUnlock((Handle
)icon
) ;
246 SetGWorld(saveWorld
,saveHandle
);
251 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
258 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
259 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
261 GetGWorld( &origPort
, &origDev
) ;
263 RgnHandle clipRgn
= NULL
;
268 LockPixels( GetGWorldPixMap( mask
) ) ;
269 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
270 UnlockPixels( GetGWorldPixMap( mask
) ) ;
273 SetGWorld( wp
, NULL
) ;
275 GetPortBounds( wp
, &portRect
) ;
277 pict
= OpenPicture(&portRect
);
280 RGBForeColor( &black
) ;
281 RGBBackColor( &white
) ;
283 LockPixels( GetGWorldPixMap( wp
) ) ;
284 CopyBits(GetPortBitMapForCopyBits(wp
),
285 GetPortBitMapForCopyBits(wp
),
289 UnlockPixels( GetGWorldPixMap( wp
) ) ;
292 SetGWorld( origPort
, origDev
) ;
296 void wxMacCreateBitmapButton( ControlButtonContentInfo
*info
, const wxBitmap
& bitmap
, bool forceColorIcon
)
298 memset( info
, 0 , sizeof(ControlButtonContentInfo
) ) ;
301 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( bitmap
.GetRefData()) ;
305 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
307 info
->contentType
= kControlContentPictHandle
;
308 info
->u
.picture
= MAC_WXHMETAFILE(bmap
->m_hPict
) ;
310 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
312 if ( (forceColorIcon
|| bmap
->m_width
== bmap
->m_height
) && ((bmap
->m_width
& 0x3) == 0) )
314 info
->contentType
= kControlContentCIconHandle
;
315 if ( bitmap
.GetMask() )
317 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap()) ,
318 8 , bmap
->m_width
) ;
322 info
->u
.cIconHandle
= wxMacCreateCIcon( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
,
323 8 , bmap
->m_width
) ;
328 info
->contentType
= kControlContentPictHandle
;
329 if ( bitmap
.GetMask() )
331 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , MAC_WXHBITMAP(bitmap
.GetMask()->GetMaskBitmap() ) ) ;
335 info
->u
.picture
= wxMacCreatePict( MAC_WXHBITMAP(bmap
->m_hBitmap
) , NULL
) ;
339 else if ( bmap
->m_bitmapType
== kMacBitmapTypeIcon
)
341 info
->contentType
= kControlContentCIconHandle
;
342 info
->u
.cIconHandle
= MAC_WXHICON(bmap
->m_hIcon
) ;
347 wxBitmapRefData::wxBitmapRefData()
359 m_bitmapType
= kMacBitmapTypeUnknownType
;
362 // TODO move this to a public function of Bitmap Ref
363 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
365 switch (data
->m_bitmapType
)
367 case kMacBitmapTypePict
:
371 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
372 data
->m_hPict
= NULL
;
376 case kMacBitmapTypeGrafWorld
:
378 if ( data
->m_hBitmap
)
380 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
381 data
->m_hBitmap
= NULL
;
385 case kMacBitmapTypeIcon
:
388 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
389 data
->m_hIcon
= NULL
;
397 if (data
->m_bitmapMask
)
399 delete data
->m_bitmapMask
;
400 data
->m_bitmapMask
= NULL
;
404 wxBitmapRefData::~wxBitmapRefData()
406 DisposeBitmapRefData( this ) ;
409 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
420 wxBitmap::~wxBitmap()
424 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
426 m_refData
= new wxBitmapRefData
;
428 M_BITMAPDATA
->m_width
= the_width
;
429 M_BITMAPDATA
->m_height
= the_height
;
430 M_BITMAPDATA
->m_depth
= no_bits
;
431 M_BITMAPDATA
->m_numColors
= 0;
434 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
435 MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) = wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
436 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
439 GDHandle origDevice
;
441 GetGWorld( &origPort
, &origDevice
) ;
442 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
443 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
445 // bits is a char array
447 unsigned char* linestart
= (unsigned char*) bits
;
448 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
449 if ( the_width
% (sizeof(unsigned char) * 8) ) {
450 linesize
+= sizeof(unsigned char);
453 RGBColor colors
[2] = {
454 { 0xFFFF , 0xFFFF , 0xFFFF } ,
458 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
460 for ( int x
= 0 ; x
< the_width
; ++x
)
464 int mask
= 1 << bit
;
465 if ( linestart
[index
] & mask
)
467 SetCPixel( x
, y
, &colors
[1] ) ;
471 SetCPixel( x
, y
, &colors
[0] ) ;
475 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
477 SetGWorld( origPort
, origDevice
) ;
481 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
485 wxBitmap::wxBitmap(int w
, int h
, int d
)
487 (void)Create(w
, h
, d
);
490 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
492 (void) Create(data
, type
, width
, height
, depth
);
495 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
497 LoadFile(filename
, type
);
500 bool wxBitmap::CreateFromXpm(const char **bits
)
502 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
503 wxXPMDecoder decoder
;
504 wxImage img
= decoder
.ReadData(bits
);
505 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
506 *this = wxBitmap(img
);
510 wxBitmap::wxBitmap(const char **bits
)
512 (void) CreateFromXpm(bits
);
515 wxBitmap::wxBitmap(char **bits
)
517 (void) CreateFromXpm((const char **)bits
);
520 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
523 (rect
.x
>= 0) && (rect
.y
>= 0) &&
524 (rect
.x
+rect
.width
<= GetWidth()) &&
525 (rect
.y
+rect
.height
<= GetHeight()),
526 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
529 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
530 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
535 GetGWorld( &origPort
, &origDevice
);
537 // Update the subbitmaps reference data
538 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
540 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
541 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
542 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
544 // Copy sub region of this bitmap
545 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
547 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
549 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
554 GWorldPtr submask
, mask
;
557 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
558 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
559 LockPixels(GetGWorldPixMap(mask
));
560 LockPixels(GetGWorldPixMap(submask
));
562 for(int yy
= 0; yy
< rect
.height
; yy
++)
564 for(int xx
= 0; xx
< rect
.width
; xx
++)
566 SetGWorld(mask
, NULL
);
567 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
568 SetGWorld(submask
, NULL
);
569 SetCPixel(xx
,yy
, &color
);
572 UnlockPixels(GetGWorldPixMap(mask
));
573 UnlockPixels(GetGWorldPixMap(submask
));
574 ref
->m_bitmapMask
= new wxMask
;
575 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
581 GWorldPtr subbitmap
, bitmap
;
584 bitmap
= (GWorldPtr
) GetHBITMAP();
585 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
586 LockPixels(GetGWorldPixMap(bitmap
));
587 LockPixels(GetGWorldPixMap(subbitmap
));
589 for(int yy
= 0; yy
< rect
.height
; yy
++)
591 for(int xx
= 0; xx
< rect
.width
; xx
++)
593 SetGWorld(bitmap
, NULL
);
594 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
595 SetGWorld(subbitmap
, NULL
);
596 SetCPixel(xx
, yy
, &color
);
599 UnlockPixels(GetGWorldPixMap(bitmap
));
600 UnlockPixels(GetGWorldPixMap(subbitmap
));
603 SetGWorld( origPort
, origDevice
);
608 bool wxBitmap::Create(int w
, int h
, int d
)
612 m_refData
= new wxBitmapRefData
;
614 M_BITMAPDATA
->m_width
= w
;
615 M_BITMAPDATA
->m_height
= h
;
616 M_BITMAPDATA
->m_depth
= d
;
618 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
619 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
620 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
621 return M_BITMAPDATA
->m_ok
;
624 int wxBitmap::GetBitmapType() const
626 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
628 return M_BITMAPDATA
->m_bitmapType
;
631 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
633 DisposeBitmapRefData( M_BITMAPDATA
) ;
635 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
636 M_BITMAPDATA
->m_hBitmap
= bmp
;
637 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
640 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
644 wxBitmapHandler
*handler
= FindHandler(type
);
648 m_refData
= new wxBitmapRefData
;
650 return handler
->LoadFile(this, filename
, type
, -1, -1);
654 wxImage
loadimage(filename
, type
);
655 if (loadimage
.Ok()) {
660 wxLogWarning("no bitmap handler for type %d defined.", type
);
664 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
668 m_refData
= new wxBitmapRefData
;
670 wxBitmapHandler
*handler
= FindHandler(type
);
672 if ( handler
== NULL
) {
673 wxLogWarning("no bitmap handler for type %d defined.", type
);
678 return handler
->Create(this, data
, type
, width
, height
, depth
);
681 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
683 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
684 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
686 m_refData
= new wxBitmapRefData();
688 // width and height of the device-dependent bitmap
689 int width
= image
.GetWidth();
690 int height
= image
.GetHeight();
694 Create( width
, height
, 32 ) ;
697 GDHandle origDevice
;
699 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
700 LockPixels( pixMap
);
702 GetGWorld( &origPort
, &origDevice
) ;
703 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
706 register unsigned char* data
= image
.GetData();
707 char* destinationBase
= GetPixBaseAddr( pixMap
);
708 register unsigned char* destination
= (unsigned char*) destinationBase
;
709 for (int y
= 0; y
< height
; y
++)
711 for (int x
= 0; x
< width
; x
++)
714 *destination
++ = *data
++ ;
715 *destination
++ = *data
++ ;
716 *destination
++ = *data
++ ;
718 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
719 destination
= (unsigned char*) destinationBase
;
721 if ( image
.HasMask() )
723 data
= image
.GetData();
725 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
726 RGBColor white
= { 0xffff, 0xffff, 0xffff };
727 RGBColor black
= { 0 , 0 , 0 };
728 wxBitmap maskBitmap
;
730 maskBitmap
.Create( width
, height
, 1);
731 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
732 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
734 for (int y
= 0; y
< height
; y
++)
736 for (int x
= 0; x
< width
; x
++)
738 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
740 SetCPixel(x
,y
, &white
);
743 SetCPixel(x
,y
, &black
);
748 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
749 SetMask(new wxMask( maskBitmap
));
750 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
753 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
754 SetGWorld( origPort
, origDevice
);
757 wxImage
wxBitmap::ConvertToImage() const
761 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
763 // create an wxImage object
764 int width
= GetWidth();
765 int height
= GetHeight();
766 image
.Create( width
, height
);
768 unsigned char *data
= image
.GetData();
770 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
776 // background color set to RGB(16,16,16) in consistent with wxGTK
777 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
779 wxMask
*mask
= GetMask();
781 GetGWorld( &origPort
, &origDevice
);
782 LockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
783 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
785 // Copy data into image
787 for (int yy
= 0; yy
< height
; yy
++)
789 for (int xx
= 0; xx
< width
; xx
++)
791 GetCPixel(xx
,yy
, &color
);
792 r
= ((color
.red
) >> 8);
793 g
= ((color
.green
) >> 8);
794 b
= ((color
.blue
) >> 8);
800 if (mask
->PointMasked(xx
,yy
))
802 data
[index
] = mask_r
;
803 data
[index
+ 1] = mask_g
;
804 data
[index
+ 2] = mask_b
;
812 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
813 image
.SetMask( true );
817 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
818 SetGWorld(origPort
, origDevice
);
824 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
825 const wxPalette
*palette
) const
827 wxBitmapHandler
*handler
= FindHandler(type
);
831 return handler
->SaveFile(this, filename
, type
, palette
);
835 wxImage image
= ConvertToImage();
837 return image
.SaveFile(filename
, type
);
840 wxLogWarning("no bitmap handler for type %d defined.", type
);
844 bool wxBitmap::Ok() const
846 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
849 int wxBitmap::GetHeight() const
851 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
853 return M_BITMAPDATA
->m_height
;
856 int wxBitmap::GetWidth() const
858 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
860 return M_BITMAPDATA
->m_width
;
863 int wxBitmap::GetDepth() const
865 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
867 return M_BITMAPDATA
->m_depth
;
870 int wxBitmap::GetQuality() const
872 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
874 return M_BITMAPDATA
->m_quality
;
877 wxMask
*wxBitmap::GetMask() const
879 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
881 return M_BITMAPDATA
->m_bitmapMask
;
884 void wxBitmap::SetWidth(int w
)
887 m_refData
= new wxBitmapRefData
;
889 M_BITMAPDATA
->m_width
= w
;
892 void wxBitmap::SetHeight(int h
)
895 m_refData
= new wxBitmapRefData
;
897 M_BITMAPDATA
->m_height
= h
;
900 void wxBitmap::SetDepth(int d
)
903 m_refData
= new wxBitmapRefData
;
905 M_BITMAPDATA
->m_depth
= d
;
908 void wxBitmap::SetQuality(int q
)
911 m_refData
= new wxBitmapRefData
;
913 M_BITMAPDATA
->m_quality
= q
;
916 void wxBitmap::SetOk(bool isOk
)
919 m_refData
= new wxBitmapRefData
;
921 M_BITMAPDATA
->m_ok
= isOk
;
924 wxPalette
*wxBitmap::GetPalette() const
926 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
928 return &M_BITMAPDATA
->m_bitmapPalette
;
931 void wxBitmap::SetPalette(const wxPalette
& palette
)
934 m_refData
= new wxBitmapRefData
;
936 M_BITMAPDATA
->m_bitmapPalette
= palette
;
939 void wxBitmap::SetMask(wxMask
*mask
)
942 m_refData
= new wxBitmapRefData
;
944 // Remove existing mask if there is one.
945 if (M_BITMAPDATA
->m_bitmapMask
)
946 delete M_BITMAPDATA
->m_bitmapMask
;
948 M_BITMAPDATA
->m_bitmapMask
= mask
;
951 WXHBITMAP
wxBitmap::GetHBITMAP() const
953 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
955 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
958 WXHMETAFILE
wxBitmap::GetPict() const
960 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
962 PicHandle picture
; // This is the returned picture
964 // If bitmap already in Pict format return pointer
965 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
966 return M_BITMAPDATA
->m_hPict
;
968 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
973 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
974 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
975 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
981 GetPortBounds( (GWorldPtr
) GetHBITMAP() , &portRect
) ;
982 int width
= portRect
.right
- portRect
.left
;
983 int height
= portRect
.bottom
- portRect
.top
;
985 LockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
986 GetGWorld( &origPort
, &origDev
) ;
990 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
992 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
1000 RGBColor trans
= white
;
1002 RGBBackColor( &gray
);
1003 EraseRect( &portRect
);
1004 RGBColor trans
= gray
;
1006 RGBForeColor( &black
) ;
1007 RGBBackColor( &white
) ;
1008 PenMode(transparent
);
1010 for ( int y
= 0 ; y
< height
; ++y
)
1012 for( int x
= 0 ; x
< width
; ++x
)
1014 if ( !mask
->PointMasked(x
,y
) )
1018 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
1019 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
1022 // With transparency this sets a blank pixel
1023 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
1030 RGBBackColor( &gray
) ;
1031 EraseRect(&portRect
);
1032 RGBForeColor( &black
) ;
1033 RGBBackColor( &white
) ;
1035 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
1036 // src PixMap - we copy image over itself -
1037 GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
1038 // dst PixMap - no drawing occurs
1039 &portRect
, // srcRect - it will be recorded and compressed -
1040 &portRect
, // dstRect - into the picture that is open -
1041 srcCopy
,NULL
); // copyMode and no clip region
1043 ClosePicture(); // We are done recording the picture
1044 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
1045 SetGWorld( origPort
, origDev
) ;
1047 return picture
; // return our groovy pict handle
1055 : m_maskBitmap(NULL
)
1059 // Construct a mask from a bitmap and a colour indicating
1060 // the transparent area
1061 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1062 : m_maskBitmap(NULL
)
1064 Create(bitmap
, colour
);
1067 // Construct a mask from a bitmap and a palette index indicating
1068 // the transparent area
1069 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1070 : m_maskBitmap(NULL
)
1072 Create(bitmap
, paletteIndex
);
1075 // Construct a mask from a mono bitmap (copies the bitmap).
1076 wxMask::wxMask(const wxBitmap
& bitmap
)
1077 : m_maskBitmap(NULL
)
1086 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1087 m_maskBitmap
= NULL
;
1091 // Create a mask from a mono bitmap (copies the bitmap).
1092 bool wxMask::Create(const wxBitmap
& bitmap
)
1096 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1097 m_maskBitmap
= NULL
;
1099 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1100 wxT("Cannot create mask from this bitmap type (TODO)"));
1101 // other types would require a temporary bitmap. not yet implemented
1103 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1105 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1106 wxT("Cannot create mask from colour bitmap"));
1108 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1109 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1111 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1112 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1113 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1114 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1115 &rect
, &rect
, srcCopy
, 0);
1116 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1117 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1122 // Create a mask from a bitmap and a palette index indicating
1123 // the transparent area
1124 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1127 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
1131 // Create a mask from a bitmap and a colour indicating
1132 // the transparent area
1133 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1137 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1138 m_maskBitmap
= NULL
;
1140 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1141 wxT("Cannot create mask from this bitmap type (TODO)"));
1142 // other types would require a temporary bitmap. not yet implemented
1144 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1146 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1147 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1148 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1149 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1151 // this is not very efficient, but I can't think
1152 // of a better way of doing it
1154 GDHandle origDevice
;
1156 RGBColor colors
[2] = {
1157 { 0xFFFF, 0xFFFF, 0xFFFF },
1160 GetGWorld( &origPort
, &origDevice
) ;
1161 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1163 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1165 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1166 GetCPixel( w
, h
, &col
) ;
1167 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1168 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1170 SetCPixel( w
, h
, &colors
[0] ) ;
1174 SetCPixel( w
, h
, &colors
[1] ) ;
1178 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1179 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1180 SetGWorld( origPort
, origDevice
) ;
1185 bool wxMask::PointMasked(int x
, int y
)
1188 GDHandle origDevice
;
1192 GetGWorld( &origPort
, &origDevice
);
1194 //Set port to mask and see if it masked (1) or not ( 0 )
1195 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1196 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1197 GetCPixel(x
,y
, &color
);
1198 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1199 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1201 SetGWorld( origPort
, origDevice
);
1210 wxBitmapHandler::~wxBitmapHandler()
1214 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1219 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1220 int desiredWidth
, int desiredHeight
)
1225 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1234 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1236 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1238 inline wxPICTResourceHandler()
1240 m_name
= "Macintosh Pict resource";
1242 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1245 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1246 int desiredWidth
, int desiredHeight
);
1248 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1250 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1251 int desiredWidth
, int desiredHeight
)
1256 c2pstrcpy( (StringPtr
) theName
, name
) ;
1258 strcpy( (char *) theName
, name
) ;
1259 c2pstr( (char *)theName
) ;
1262 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1267 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1268 DetachResource( (Handle
) thePict
) ;
1269 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1270 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1271 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1272 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1274 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1275 M_BITMAPHANDLERDATA
->m_ok
= true ;
1276 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1277 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1278 // M_BITMAPHANDLERDATA->m_quality;
1284 void wxBitmap::InitStandardHandlers()
1286 AddHandler(new wxPICTResourceHandler
) ;
1287 AddHandler(new wxICONResourceHandler
) ;