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
= 0 ;// someRedValue;
63 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
64 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // 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 wxBitmapRefData::wxBitmapRefData()
304 m_bitmapType
= kMacBitmapTypeUnknownType
;
307 // TODO move this do a public function of Bitmap Ref
308 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
310 switch (data
->m_bitmapType
)
312 case kMacBitmapTypePict
:
316 KillPicture( MAC_WXHMETAFILE( data
->m_hPict
) ) ;
317 data
->m_hPict
= NULL
;
321 case kMacBitmapTypeGrafWorld
:
323 if ( data
->m_hBitmap
)
325 wxMacDestroyGWorld( MAC_WXHBITMAP(data
->m_hBitmap
) ) ;
326 data
->m_hBitmap
= NULL
;
330 case kMacBitmapTypeIcon
:
333 DisposeCIcon( MAC_WXHICON(data
->m_hIcon
) ) ;
334 data
->m_hIcon
= NULL
;
342 if (data
->m_bitmapMask
)
344 delete data
->m_bitmapMask
;
345 data
->m_bitmapMask
= NULL
;
349 wxBitmapRefData::~wxBitmapRefData()
351 DisposeBitmapRefData( this ) ;
354 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
365 wxBitmap::~wxBitmap()
369 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
371 m_refData
= new wxBitmapRefData
;
373 M_BITMAPDATA
->m_width
= the_width
;
374 M_BITMAPDATA
->m_height
= the_height
;
375 M_BITMAPDATA
->m_depth
= no_bits
;
376 M_BITMAPDATA
->m_numColors
= 0;
379 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
380 MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) = wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
381 M_BITMAPDATA
->m_ok
= (MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) != NULL
) ;
384 GDHandle origDevice
;
386 GetGWorld( &origPort
, &origDevice
) ;
387 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) , NULL
) ;
388 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
390 // bits is a char array
392 unsigned char* linestart
= (unsigned char*) bits
;
393 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
394 if ( the_width
% (sizeof(unsigned char) * 8) ) {
395 linesize
+= sizeof(unsigned char);
398 RGBColor colors
[2] = {
399 { 0xFFFF , 0xFFFF , 0xFFFF } ,
403 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
405 for ( int x
= 0 ; x
< the_width
; ++x
)
409 int mask
= 1 << bit
;
410 if ( linestart
[index
] & mask
)
412 SetCPixel( x
, y
, &colors
[1] ) ;
416 SetCPixel( x
, y
, &colors
[0] ) ;
420 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
) ) ) ;
422 SetGWorld( origPort
, origDevice
) ;
426 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
430 wxBitmap::wxBitmap(int w
, int h
, int d
)
432 (void)Create(w
, h
, d
);
435 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
437 (void) Create(data
, type
, width
, height
, depth
);
440 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
442 LoadFile(filename
, type
);
445 bool wxBitmap::CreateFromXpm(const char **bits
)
447 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
448 wxXPMDecoder decoder
;
449 wxImage img
= decoder
.ReadData(bits
);
450 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
451 *this = wxBitmap(img
);
455 wxBitmap::wxBitmap(const char **bits
)
457 (void) CreateFromXpm(bits
);
460 wxBitmap::wxBitmap(char **bits
)
462 (void) CreateFromXpm((const char **)bits
);
465 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
468 (rect
.x
>= 0) && (rect
.y
>= 0) &&
469 (rect
.x
+rect
.width
<= GetWidth()) &&
470 (rect
.y
+rect
.height
<= GetHeight()),
471 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
474 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
475 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
480 GetGWorld( &origPort
, &origDevice
);
482 // Update the subbitmaps reference data
483 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
485 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
486 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
487 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
489 // Copy sub region of this bitmap
490 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
492 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
494 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
499 GWorldPtr submask
, mask
;
502 mask
= (GWorldPtr
) GetMask()->GetMaskBitmap();
503 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
504 LockPixels(GetGWorldPixMap(mask
));
505 LockPixels(GetGWorldPixMap(submask
));
507 for(int yy
= 0; yy
< rect
.height
; yy
++)
509 for(int xx
= 0; xx
< rect
.width
; xx
++)
511 SetGWorld(mask
, NULL
);
512 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
513 SetGWorld(submask
, NULL
);
514 SetCPixel(xx
,yy
, &color
);
517 UnlockPixels(GetGWorldPixMap(mask
));
518 UnlockPixels(GetGWorldPixMap(submask
));
519 ref
->m_bitmapMask
= new wxMask
;
520 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
526 GWorldPtr subbitmap
, bitmap
;
529 bitmap
= (GWorldPtr
) GetHBITMAP();
530 subbitmap
= (GWorldPtr
) ref
->m_hBitmap
;
531 LockPixels(GetGWorldPixMap(bitmap
));
532 LockPixels(GetGWorldPixMap(subbitmap
));
534 for(int yy
= 0; yy
< rect
.height
; yy
++)
536 for(int xx
= 0; xx
< rect
.width
; xx
++)
538 SetGWorld(bitmap
, NULL
);
539 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
540 SetGWorld(subbitmap
, NULL
);
541 SetCPixel(xx
, yy
, &color
);
544 UnlockPixels(GetGWorldPixMap(bitmap
));
545 UnlockPixels(GetGWorldPixMap(subbitmap
));
548 SetGWorld( origPort
, origDevice
);
553 bool wxBitmap::Create(int w
, int h
, int d
)
557 m_refData
= new wxBitmapRefData
;
559 M_BITMAPDATA
->m_width
= w
;
560 M_BITMAPDATA
->m_height
= h
;
561 M_BITMAPDATA
->m_depth
= d
;
563 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
564 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
565 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
566 return M_BITMAPDATA
->m_ok
;
569 int wxBitmap::GetBitmapType() const
571 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
573 return M_BITMAPDATA
->m_bitmapType
;
576 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
578 DisposeBitmapRefData( M_BITMAPDATA
) ;
580 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
581 M_BITMAPDATA
->m_hBitmap
= bmp
;
582 M_BITMAPDATA
->m_ok
= ( M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
585 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
589 wxBitmapHandler
*handler
= FindHandler(type
);
593 m_refData
= new wxBitmapRefData
;
595 return handler
->LoadFile(this, filename
, type
, -1, -1);
599 wxImage
loadimage(filename
, type
);
600 if (loadimage
.Ok()) {
605 wxLogWarning("no bitmap handler for type %d defined.", type
);
609 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
613 m_refData
= new wxBitmapRefData
;
615 wxBitmapHandler
*handler
= FindHandler(type
);
617 if ( handler
== NULL
) {
618 wxLogWarning("no bitmap handler for type %d defined.", type
);
623 return handler
->Create(this, data
, type
, width
, height
, depth
);
626 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
628 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
629 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
631 m_refData
= new wxBitmapRefData();
633 // width and height of the device-dependent bitmap
634 int width
= image
.GetWidth();
635 int height
= image
.GetHeight();
639 Create( width
, height
, 32 ) ;
642 GDHandle origDevice
;
644 PixMapHandle pixMap
= GetGWorldPixMap((GWorldPtr
)GetHBITMAP()) ;
645 LockPixels( pixMap
);
647 GetGWorld( &origPort
, &origDevice
) ;
648 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
653 register unsigned char* data
= image
.GetData();
654 char* destinationBase
= GetPixBaseAddr( pixMap
);
655 register unsigned char* destination
= (unsigned char*) destinationBase
;
656 for (int y
= 0; y
< height
; y
++)
658 for (int x
= 0; x
< width
; x
++)
661 *destination
++ = *data
++ ;
662 *destination
++ = *data
++ ;
663 *destination
++ = *data
++ ;
665 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
666 destination
= (unsigned char*) destinationBase
;
668 if ( image
.HasMask() )
670 data
= image
.GetData();
672 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
673 RGBColor white
= { 0xffff, 0xffff, 0xffff };
674 RGBColor black
= { 0 , 0 , 0 };
675 wxBitmap maskBitmap
;
677 maskBitmap
.Create( width
, height
, 1);
678 LockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
679 SetGWorld( (GWorldPtr
) maskBitmap
.GetHBITMAP(), NULL
);
681 for (int y
= 0; y
< height
; y
++)
683 for (int x
= 0; x
< width
; x
++)
685 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
687 SetCPixel(x
,y
, &white
);
690 SetCPixel(x
,y
, &black
);
695 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
696 SetMask(new wxMask( maskBitmap
));
697 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) maskBitmap
.GetHBITMAP()) );
700 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()) );
701 SetGWorld( origPort
, origDevice
);
704 wxImage
wxBitmap::ConvertToImage() const
708 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
710 // create an wxImage object
711 int width
= GetWidth();
712 int height
= GetHeight();
713 image
.Create( width
, height
);
715 unsigned char *data
= image
.GetData();
717 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
723 // background color set to RGB(16,16,16) in consistent with wxGTK
724 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
726 wxMask
*mask
= GetMask();
728 GetGWorld( &origPort
, &origDevice
);
729 LockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
730 SetGWorld( (GWorldPtr
) GetHBITMAP(), NULL
);
732 // Copy data into image
734 for (int yy
= 0; yy
< height
; yy
++)
736 for (int xx
= 0; xx
< width
; xx
++)
738 GetCPixel(xx
,yy
, &color
);
739 r
= ((color
.red
) >> 8);
740 g
= ((color
.green
) >> 8);
741 b
= ((color
.blue
) >> 8);
747 if (mask
->PointMasked(xx
,yy
))
749 data
[index
] = mask_r
;
750 data
[index
+ 1] = mask_g
;
751 data
[index
+ 2] = mask_b
;
759 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
760 image
.SetMask( true );
764 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) GetHBITMAP()));
765 SetGWorld(origPort
, origDevice
);
771 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
772 const wxPalette
*palette
) const
774 wxBitmapHandler
*handler
= FindHandler(type
);
778 return handler
->SaveFile(this, filename
, type
, palette
);
782 wxImage image
= ConvertToImage();
784 return image
.SaveFile(filename
, type
);
787 wxLogWarning("no bitmap handler for type %d defined.", type
);
791 bool wxBitmap::Ok() const
793 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
796 int wxBitmap::GetHeight() const
798 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
800 return M_BITMAPDATA
->m_height
;
803 int wxBitmap::GetWidth() const
805 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
807 return M_BITMAPDATA
->m_width
;
810 int wxBitmap::GetDepth() const
812 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
814 return M_BITMAPDATA
->m_depth
;
817 int wxBitmap::GetQuality() const
819 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
821 return M_BITMAPDATA
->m_quality
;
824 wxMask
*wxBitmap::GetMask() const
826 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
828 return M_BITMAPDATA
->m_bitmapMask
;
831 void wxBitmap::SetWidth(int w
)
834 m_refData
= new wxBitmapRefData
;
836 M_BITMAPDATA
->m_width
= w
;
839 void wxBitmap::SetHeight(int h
)
842 m_refData
= new wxBitmapRefData
;
844 M_BITMAPDATA
->m_height
= h
;
847 void wxBitmap::SetDepth(int d
)
850 m_refData
= new wxBitmapRefData
;
852 M_BITMAPDATA
->m_depth
= d
;
855 void wxBitmap::SetQuality(int q
)
858 m_refData
= new wxBitmapRefData
;
860 M_BITMAPDATA
->m_quality
= q
;
863 void wxBitmap::SetOk(bool isOk
)
866 m_refData
= new wxBitmapRefData
;
868 M_BITMAPDATA
->m_ok
= isOk
;
871 wxPalette
*wxBitmap::GetPalette() const
873 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
875 return &M_BITMAPDATA
->m_bitmapPalette
;
878 void wxBitmap::SetPalette(const wxPalette
& palette
)
881 m_refData
= new wxBitmapRefData
;
883 M_BITMAPDATA
->m_bitmapPalette
= palette
;
886 void wxBitmap::SetMask(wxMask
*mask
)
889 m_refData
= new wxBitmapRefData
;
891 // Remove existing mask if there is one.
892 if (M_BITMAPDATA
->m_bitmapMask
)
893 delete M_BITMAPDATA
->m_bitmapMask
;
895 M_BITMAPDATA
->m_bitmapMask
= mask
;
898 WXHBITMAP
wxBitmap::GetHBITMAP() const
900 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
902 return MAC_WXHBITMAP(M_BITMAPDATA
->m_hBitmap
);
905 WXHMETAFILE
wxBitmap::GetPict() const
907 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
909 PicHandle picture
; // This is the returned picture
911 // If bitmap already in Pict format return pointer
912 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
913 return M_BITMAPDATA
->m_hPict
;
915 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
920 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
921 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
922 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
928 GetPortBounds( (GWorldPtr
) GetHBITMAP() , &portRect
) ;
929 int width
= portRect
.right
- portRect
.left
;
930 int height
= portRect
.bottom
- portRect
.top
;
932 LockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
933 GetGWorld( &origPort
, &origDev
) ;
937 SetGWorld( (GWorldPtr
) GetHBITMAP() , NULL
) ;
939 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
947 RGBColor trans
= white
;
949 RGBBackColor( &gray
);
950 EraseRect( &portRect
);
951 RGBColor trans
= gray
;
953 RGBForeColor( &black
) ;
954 RGBBackColor( &white
) ;
955 PenMode(transparent
);
957 for ( int y
= 0 ; y
< height
; ++y
)
959 for( int x
= 0 ; x
< width
; ++x
)
961 if ( !mask
->PointMasked(x
,y
) )
965 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
966 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
969 // With transparency this sets a blank pixel
970 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
977 RGBBackColor( &gray
) ;
978 EraseRect(&portRect
);
979 RGBForeColor( &black
) ;
980 RGBBackColor( &white
) ;
982 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
983 // src PixMap - we copy image over itself -
984 GetPortBitMapForCopyBits( (GWorldPtr
) GetHBITMAP()),
985 // dst PixMap - no drawing occurs
986 &portRect
, // srcRect - it will be recorded and compressed -
987 &portRect
, // dstRect - into the picture that is open -
988 srcCopy
,NULL
); // copyMode and no clip region
990 ClosePicture(); // We are done recording the picture
991 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) GetHBITMAP() ) ) ;
992 SetGWorld( origPort
, origDev
) ;
994 return picture
; // return our groovy pict handle
1006 // Construct a mask from a bitmap and a colour indicating
1007 // the transparent area
1008 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1011 Create(bitmap
, colour
);
1014 // Construct a mask from a bitmap and a palette index indicating
1015 // the transparent area
1016 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1019 Create(bitmap
, paletteIndex
);
1022 // Construct a mask from a mono bitmap (copies the bitmap).
1023 wxMask::wxMask(const wxBitmap
& bitmap
)
1033 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1034 m_maskBitmap
= NULL
;
1038 // Create a mask from a mono bitmap (copies the bitmap).
1039 bool wxMask::Create(const wxBitmap
& bitmap
)
1043 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1044 m_maskBitmap
= NULL
;
1046 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1047 wxT("Cannot create mask from this bitmap type (TODO)"));
1048 // other types would require a temporary bitmap. not yet implemented
1050 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1052 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1053 wxT("Cannot create mask from colour bitmap"));
1055 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1056 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1058 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1059 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1060 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr
) bitmap
.GetHBITMAP()),
1061 GetPortBitMapForCopyBits( (GWorldPtr
) m_maskBitmap
),
1062 &rect
, &rect
, srcCopy
, 0);
1063 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1064 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP()) );
1069 // Create a mask from a bitmap and a palette index indicating
1070 // the transparent area
1071 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1074 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1078 // Create a mask from a bitmap and a colour indicating
1079 // the transparent area
1080 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1084 wxMacDestroyGWorld( (GWorldPtr
) m_maskBitmap
) ;
1085 m_maskBitmap
= NULL
;
1087 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1088 wxT("Cannot create mask from this bitmap type (TODO)"));
1089 // other types would require a temporary bitmap. not yet implemented
1091 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1093 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1094 LockPixels( GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
) );
1095 LockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) );
1096 RGBColor maskColor
= MAC_WXCOLORREF(colour
.GetPixel());
1098 // this is not very efficient, but I can't think
1099 // of a better way of doing it
1101 GDHandle origDevice
;
1103 RGBColor colors
[2] = {
1104 { 0xFFFF, 0xFFFF, 0xFFFF },
1107 GetGWorld( &origPort
, &origDevice
) ;
1108 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1110 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1112 SetGWorld( (GWorldPtr
) bitmap
.GetHBITMAP(), NULL
) ;
1113 GetCPixel( w
, h
, &col
) ;
1114 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
) ;
1115 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1117 SetCPixel( w
, h
, &colors
[0] ) ;
1121 SetCPixel( w
, h
, &colors
[1] ) ;
1125 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1126 UnlockPixels( GetGWorldPixMap( (GWorldPtr
) bitmap
.GetHBITMAP() ) ) ;
1127 SetGWorld( origPort
, origDevice
) ;
1132 bool wxMask::PointMasked(int x
, int y
)
1135 GDHandle origDevice
;
1139 GetGWorld( &origPort
, &origDevice
);
1141 //Set port to mask and see if it masked (1) or not ( 0 )
1142 SetGWorld( (GWorldPtr
) m_maskBitmap
, NULL
);
1143 LockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1144 GetCPixel(x
,y
, &color
);
1145 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1146 UnlockPixels(GetGWorldPixMap( (GWorldPtr
) m_maskBitmap
));
1148 SetGWorld( origPort
, origDevice
);
1157 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1162 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1163 int desiredWidth
, int desiredHeight
)
1168 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1177 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1179 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1181 inline wxPICTResourceHandler()
1183 m_name
= "Macintosh Pict resource";
1185 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1188 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1189 int desiredWidth
, int desiredHeight
);
1191 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1193 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1194 int desiredWidth
, int desiredHeight
)
1199 c2pstrcpy( (StringPtr
) theName
, name
) ;
1201 strcpy( (char *) theName
, name
) ;
1202 c2pstr( (char *)theName
) ;
1205 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1210 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1211 DetachResource( (Handle
) thePict
) ;
1212 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1213 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1214 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1215 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1217 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1218 M_BITMAPHANDLERDATA
->m_ok
= true ;
1219 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1220 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1221 // M_BITMAPHANDLERDATA->m_quality;
1227 void wxBitmap::InitStandardHandlers()
1229 AddHandler(new wxPICTResourceHandler
) ;
1230 AddHandler(new wxICONResourceHandler
) ;