1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmapbase.h"
14 #pragma implementation "bitmap.h"
19 #include "wx/bitmap.h"
23 #include "wx/xpmdecod.h"
25 #if !USE_SHARED_LIBRARIES
26 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
27 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
28 IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase
, wxGDIObject
)
29 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandlerBase
, wxObject
)
33 #include <ApplicationServices/ApplicationServices.h>
35 #include <PictUtils.h>
38 #include "wx/mac/uma.h"
40 CTabHandle
wxMacCreateColorTable( int numColors
)
42 CTabHandle newColors
; /* Handle to the new color table */
44 /* Allocate memory for the color table */
45 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
46 sizeof (ColorSpec
) * (numColors
- 1) );
49 /* Initialize the fields */
50 (**newColors
).ctSeed
= GetCTSeed();
51 (**newColors
).ctFlags
= 0;
52 (**newColors
).ctSize
= numColors
- 1;
53 /* Initialize the table of colors */
58 void wxMacDestroyColorTable( CTabHandle colors
)
60 DisposeHandle( (Handle
) colors
) ;
63 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
65 (**newColors
).ctTable
[index
].value
= index
;
66 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
67 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
68 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
71 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
75 Rect rect
= { 0 , 0 , height
, width
} ;
79 depth
= wxDisplayDepth() ;
82 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
90 void wxMacDestroyGWorld( GWorldPtr gw
)
96 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
98 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
100 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
101 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
102 OSErr error
; /* Returns error code */
103 short bytesPerRow
; /* Number of bytes per row in the PixMap */
110 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
112 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
116 error
= HandToHand((Handle
*) &newColors
);
120 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
126 /* Allocate pixel image; long integer multiplication avoids overflow */
127 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
129 if ((**icon
).iconData
!= nil
)
131 /* Initialize fields common to indexed and direct PixMaps */
132 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
133 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
135 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
136 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
137 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
138 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
139 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
140 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
141 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
143 /* Initialize fields specific to indexed and direct PixMaps */
146 /* PixMap is indexed */
147 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
148 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
149 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
150 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
154 /* PixMap is direct */
155 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
156 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
158 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
160 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
161 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
162 (**newColors
).ctFlags
= 0;
163 (**newColors
).ctSize
= 0;
164 (**icon
).iconPMap
.pmTable
= newColors
;
173 /* If no errors occured, return a handle to the new off-screen PixMap */
176 if (newColors
!= nil
)
177 DisposeCTable(newColors
);
180 /* Return the error code */
184 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
189 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
190 SetGWorld(image
,nil
);
192 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
193 Rect imageBounds
= frame
;
194 GetPortBounds( image
, &imageBounds
) ;
196 int bwSize
= iconSize
/ 8 * iconSize
;
197 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
198 HLock((Handle
)icon
) ;
199 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
200 HLock( (**icon
).iconData
) ;
201 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
203 LockPixels(GetGWorldPixMap(image
));
205 CopyBits(GetPortBitMapForCopyBits(image
),
206 (BitMapPtr
)&((**icon
).iconPMap
),
209 srcCopy
| ditherCopy
, nil
);
212 UnlockPixels(GetGWorldPixMap(image
));
213 HUnlock( (**icon
).iconData
) ;
215 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
216 (**icon
).iconMask
.bounds
= frame
;
218 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
219 (**icon
).iconBMap
.bounds
= frame
;
220 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
221 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
225 LockPixels(GetGWorldPixMap(mask
) ) ;
226 CopyBits(GetPortBitMapForCopyBits(mask
) ,
227 &(**icon
).iconBMap
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
228 CopyBits(GetPortBitMapForCopyBits(mask
) ,
229 &(**icon
).iconMask
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
230 UnlockPixels(GetGWorldPixMap( mask
) ) ;
234 LockPixels(GetGWorldPixMap(image
));
235 CopyBits(GetPortBitMapForCopyBits(image
) ,
236 &(**icon
).iconBMap
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
237 CopyBits(GetPortBitMapForCopyBits(image
) ,
238 &(**icon
).iconMask
, &imageBounds
, &imageBounds
, 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 wxBitmapRefData::wxBitmapRefData()
308 m_bitmapType
= kMacBitmapTypeUnknownType
;
311 // TODO move this do a public function of Bitmap Ref
312 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
314 switch (data
->m_bitmapType
)
316 case kMacBitmapTypePict
:
320 KillPicture( data
->m_hPict
) ;
321 data
->m_hPict
= NULL
;
325 case kMacBitmapTypeGrafWorld
:
327 if ( data
->m_hBitmap
)
329 wxMacDestroyGWorld( data
->m_hBitmap
) ;
330 data
->m_hBitmap
= NULL
;
334 case kMacBitmapTypeIcon
:
337 DisposeCIcon( data
->m_hIcon
) ;
338 data
->m_hIcon
= NULL
;
346 if (data
->m_bitmapMask
)
348 delete data
->m_bitmapMask
;
349 data
->m_bitmapMask
= NULL
;
353 wxBitmapRefData::~wxBitmapRefData()
355 DisposeBitmapRefData( this ) ;
358 wxList
wxBitmapBase::sm_handlers
;
361 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
372 wxBitmap::~wxBitmap()
376 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
378 m_refData
= new wxBitmapRefData
;
380 M_BITMAPDATA
->m_width
= the_width
;
381 M_BITMAPDATA
->m_height
= the_height
;
382 M_BITMAPDATA
->m_depth
= no_bits
;
383 M_BITMAPDATA
->m_numColors
= 0;
386 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
387 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
388 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
391 GDHandle origDevice
;
393 GetGWorld( &origPort
, &origDevice
) ;
394 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
395 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
397 // bits is a char array
399 unsigned char* linestart
= (unsigned char*) bits
;
400 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
401 if ( the_width
% (sizeof(unsigned char) * 8) ) {
402 linesize
+= sizeof(unsigned char);
405 RGBColor colors
[2] = {
406 { 0xFFFF , 0xFFFF , 0xFFFF } ,
410 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
412 for ( int x
= 0 ; x
< the_width
; ++x
)
416 int mask
= 1 << bit
;
417 if ( linestart
[index
] & mask
)
419 SetCPixel( x
, y
, &colors
[1] ) ;
423 SetCPixel( x
, y
, &colors
[0] ) ;
427 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
429 SetGWorld( origPort
, origDevice
) ;
433 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
437 wxBitmap::wxBitmap(int w
, int h
, int d
)
439 (void)Create(w
, h
, d
);
442 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
444 (void) Create(data
, type
, width
, height
, depth
);
447 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
449 LoadFile(filename
, type
);
452 bool wxBitmap::CreateFromXpm(const char **bits
)
454 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
455 wxXPMDecoder decoder
;
456 wxImage img
= decoder
.ReadData(bits
);
457 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
458 *this = wxBitmap(img
);
462 wxBitmap::wxBitmap(const char **bits
)
464 (void) CreateFromXpm(bits
);
467 wxBitmap::wxBitmap(char **bits
)
469 (void) CreateFromXpm((const char **)bits
);
472 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
475 (rect
.x
>= 0) && (rect
.y
>= 0) &&
476 (rect
.x
+rect
.width
<= GetWidth()) &&
477 (rect
.y
+rect
.height
<= GetHeight()),
478 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
481 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
482 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
487 GetGWorld( &origPort
, &origDevice
);
489 // Update the subbitmaps reference data
490 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
492 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
493 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
494 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
496 // Copy sub region of this bitmap
497 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
499 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
501 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
506 WXHBITMAP submask
, mask
;
509 mask
= GetMask()->GetMaskBitmap();
510 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
511 LockPixels(GetGWorldPixMap(mask
));
512 LockPixels(GetGWorldPixMap(submask
));
514 for(int yy
= 0; yy
< rect
.height
; yy
++)
516 for(int xx
= 0; xx
< rect
.width
; xx
++)
518 SetGWorld(mask
, NULL
);
519 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
520 SetGWorld(submask
, NULL
);
521 SetCPixel(xx
,yy
, &color
);
524 UnlockPixels(GetGWorldPixMap(mask
));
525 UnlockPixels(GetGWorldPixMap(submask
));
526 ref
->m_bitmapMask
= new wxMask
;
527 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
533 WXHBITMAP subbitmap
, bitmap
;
536 bitmap
= GetHBITMAP();
537 subbitmap
= ref
->m_hBitmap
;
538 LockPixels(GetGWorldPixMap(bitmap
));
539 LockPixels(GetGWorldPixMap(subbitmap
));
541 for(int yy
= 0; yy
< rect
.height
; yy
++)
543 for(int xx
= 0; xx
< rect
.width
; xx
++)
545 SetGWorld(bitmap
, NULL
);
546 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
547 SetGWorld(subbitmap
, NULL
);
548 SetCPixel(xx
, yy
, &color
);
551 UnlockPixels(GetGWorldPixMap(bitmap
));
552 UnlockPixels(GetGWorldPixMap(subbitmap
));
555 SetGWorld( origPort
, origDevice
);
560 bool wxBitmap::Create(int w
, int h
, int d
)
564 m_refData
= new wxBitmapRefData
;
566 M_BITMAPDATA
->m_width
= w
;
567 M_BITMAPDATA
->m_height
= h
;
568 M_BITMAPDATA
->m_depth
= d
;
570 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
571 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
572 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
573 return M_BITMAPDATA
->m_ok
;
576 int wxBitmap::GetBitmapType() const
578 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
580 return M_BITMAPDATA
->m_bitmapType
;
583 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
585 DisposeBitmapRefData( M_BITMAPDATA
) ;
587 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
588 M_BITMAPDATA
->m_hBitmap
= bmp
;
589 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
592 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
596 wxBitmapHandler
*handler
= FindHandler(type
);
600 m_refData
= new wxBitmapRefData
;
602 return handler
->LoadFile(this, filename
, type
, -1, -1);
606 wxImage
loadimage(filename
, type
);
607 if (loadimage
.Ok()) {
612 wxLogWarning("no bitmap handler for type %d defined.", type
);
616 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
620 m_refData
= new wxBitmapRefData
;
622 wxBitmapHandler
*handler
= FindHandler(type
);
624 if ( handler
== NULL
) {
625 wxLogWarning("no bitmap handler for type %d defined.", type
);
630 return handler
->Create(this, data
, type
, width
, height
, depth
);
633 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
635 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
636 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
638 m_refData
= new wxBitmapRefData();
640 // width and height of the device-dependent bitmap
641 int width
= image
.GetWidth();
642 int height
= image
.GetHeight();
646 Create( width
, height
, 32 ) ;
649 GDHandle origDevice
;
651 PixMapHandle pixMap
= GetGWorldPixMap(GetHBITMAP()) ;
652 LockPixels( pixMap
);
654 GetGWorld( &origPort
, &origDevice
) ;
655 SetGWorld( GetHBITMAP() , NULL
) ;
660 register unsigned char* data
= image
.GetData();
661 char* destinationBase
= GetPixBaseAddr( pixMap
);
662 register unsigned char* destination
= (unsigned char*) destinationBase
;
663 for (int y
= 0; y
< height
; y
++)
665 for (int x
= 0; x
< width
; x
++)
668 *destination
++ = *data
++ ;
669 *destination
++ = *data
++ ;
670 *destination
++ = *data
++ ;
672 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
673 destination
= (unsigned char*) destinationBase
;
675 if ( image
.HasMask() )
677 data
= image
.GetData();
679 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
680 RGBColor white
= { 0xffff, 0xffff, 0xffff };
681 RGBColor black
= { 0 , 0 , 0 };
682 wxBitmap maskBitmap
;
684 maskBitmap
.Create( width
, height
, 1);
685 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
686 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
688 for (int y
= 0; y
< height
; y
++)
690 for (int x
= 0; x
< width
; x
++)
692 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
694 SetCPixel(x
,y
, &white
);
697 SetCPixel(x
,y
, &black
);
702 SetGWorld(GetHBITMAP(), NULL
);
703 SetMask(new wxMask( maskBitmap
));
704 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
707 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
708 SetGWorld( origPort
, origDevice
);
711 wxImage
wxBitmap::ConvertToImage() const
715 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
717 // create an wxImage object
718 int width
= GetWidth();
719 int height
= GetHeight();
720 image
.Create( width
, height
);
722 unsigned char *data
= image
.GetData();
724 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
730 // background color set to RGB(16,16,16) in consistent with wxGTK
731 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
733 wxMask
*mask
= GetMask();
735 GetGWorld( &origPort
, &origDevice
);
736 LockPixels(GetGWorldPixMap(GetHBITMAP()));
737 SetGWorld( GetHBITMAP(), NULL
);
739 // Copy data into image
741 for (int yy
= 0; yy
< height
; yy
++)
743 for (int xx
= 0; xx
< width
; xx
++)
745 GetCPixel(xx
,yy
, &color
);
746 r
= ((color
.red
) >> 8);
747 g
= ((color
.green
) >> 8);
748 b
= ((color
.blue
) >> 8);
754 if (mask
->PointMasked(xx
,yy
))
756 data
[index
] = mask_r
;
757 data
[index
+ 1] = mask_g
;
758 data
[index
+ 2] = mask_b
;
766 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
767 image
.SetMask( true );
771 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
772 SetGWorld(origPort
, origDevice
);
778 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
779 const wxPalette
*palette
) const
781 wxBitmapHandler
*handler
= FindHandler(type
);
785 return handler
->SaveFile(this, filename
, type
, palette
);
789 wxImage image
= ConvertToImage();
791 return image
.SaveFile(filename
, type
);
794 wxLogWarning("no bitmap handler for type %d defined.", type
);
798 bool wxBitmap::Ok() const
800 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
803 int wxBitmap::GetHeight() const
805 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
807 return M_BITMAPDATA
->m_height
;
810 int wxBitmap::GetWidth() const
812 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
814 return M_BITMAPDATA
->m_width
;
817 int wxBitmap::GetDepth() const
819 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
821 return M_BITMAPDATA
->m_depth
;
824 int wxBitmap::GetQuality() const
826 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
828 return M_BITMAPDATA
->m_quality
;
831 wxMask
*wxBitmap::GetMask() const
833 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
835 return M_BITMAPDATA
->m_bitmapMask
;
838 void wxBitmap::SetWidth(int w
)
841 m_refData
= new wxBitmapRefData
;
843 M_BITMAPDATA
->m_width
= w
;
846 void wxBitmap::SetHeight(int h
)
849 m_refData
= new wxBitmapRefData
;
851 M_BITMAPDATA
->m_height
= h
;
854 void wxBitmap::SetDepth(int d
)
857 m_refData
= new wxBitmapRefData
;
859 M_BITMAPDATA
->m_depth
= d
;
862 void wxBitmap::SetQuality(int q
)
865 m_refData
= new wxBitmapRefData
;
867 M_BITMAPDATA
->m_quality
= q
;
870 void wxBitmap::SetOk(bool isOk
)
873 m_refData
= new wxBitmapRefData
;
875 M_BITMAPDATA
->m_ok
= isOk
;
878 wxPalette
*wxBitmap::GetPalette() const
880 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
882 return &M_BITMAPDATA
->m_bitmapPalette
;
885 void wxBitmap::SetPalette(const wxPalette
& palette
)
888 m_refData
= new wxBitmapRefData
;
890 M_BITMAPDATA
->m_bitmapPalette
= palette
;
893 void wxBitmap::SetMask(wxMask
*mask
)
896 m_refData
= new wxBitmapRefData
;
898 // Remove existing mask if there is one.
899 if (M_BITMAPDATA
->m_bitmapMask
)
900 delete M_BITMAPDATA
->m_bitmapMask
;
902 M_BITMAPDATA
->m_bitmapMask
= mask
;
905 WXHBITMAP
wxBitmap::GetHBITMAP() const
907 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
909 return M_BITMAPDATA
->m_hBitmap
;
912 PicHandle
wxBitmap::GetPict() const
914 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
916 PicHandle picture
; // This is the returned picture
918 // If bitmap already in Pict format return pointer
919 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
920 return M_BITMAPDATA
->m_hPict
;
922 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
927 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
928 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
929 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
935 GetPortBounds( GetHBITMAP() , &portRect
) ;
936 int width
= portRect
.right
- portRect
.left
;
937 int height
= portRect
.bottom
- portRect
.top
;
939 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
940 GetGWorld( &origPort
, &origDev
) ;
944 SetGWorld( GetHBITMAP() , NULL
) ;
946 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
954 RGBColor trans
= white
;
956 RGBBackColor( &gray
);
957 EraseRect( &portRect
);
958 RGBColor trans
= gray
;
960 RGBForeColor( &black
) ;
961 RGBBackColor( &white
) ;
962 PenMode(transparent
);
964 for ( int y
= 0 ; y
< height
; ++y
)
966 for( int x
= 0 ; x
< width
; ++x
)
968 if ( !mask
->PointMasked(x
,y
) )
972 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
973 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
976 // With transparency this sets a blank pixel
977 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
984 RGBBackColor( &gray
) ;
985 EraseRect(&portRect
);
986 RGBForeColor( &black
) ;
987 RGBBackColor( &white
) ;
989 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
990 // src PixMap - we copy image over itself -
991 GetPortBitMapForCopyBits(GetHBITMAP()),
992 // dst PixMap - no drawing occurs
993 &portRect
, // srcRect - it will be recorded and compressed -
994 &portRect
, // dstRect - into the picture that is open -
995 srcCopy
,NULL
); // copyMode and no clip region
997 ClosePicture(); // We are done recording the picture
998 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
999 SetGWorld( origPort
, origDev
) ;
1001 return picture
; // return our groovy pict handle
1004 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
1006 sm_handlers
.Append(handler
);
1009 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
1011 sm_handlers
.Insert(handler
);
1014 bool wxBitmap::RemoveHandler(const wxString
& name
)
1016 wxBitmapHandler
*handler
= FindHandler(name
);
1019 sm_handlers
.DeleteObject(handler
);
1026 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
1028 wxNode
*node
= sm_handlers
.First();
1031 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1032 if ( handler
->GetName() == name
)
1034 node
= node
->Next();
1039 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, wxBitmapType type
)
1041 wxNode
*node
= sm_handlers
.First();
1044 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1045 if ( handler
->GetExtension() == extension
&&
1046 (type
== -1 || handler
->GetType() == type
) )
1048 node
= node
->Next();
1053 wxBitmapHandler
*wxBitmap::FindHandler(wxBitmapType type
)
1055 wxNode
*node
= sm_handlers
.First();
1058 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1059 if (handler
->GetType() == type
)
1061 node
= node
->Next();
1075 // Construct a mask from a bitmap and a colour indicating
1076 // the transparent area
1077 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1080 Create(bitmap
, colour
);
1083 // Construct a mask from a bitmap and a palette index indicating
1084 // the transparent area
1085 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1088 Create(bitmap
, paletteIndex
);
1091 // Construct a mask from a mono bitmap (copies the bitmap).
1092 wxMask::wxMask(const wxBitmap
& bitmap
)
1102 wxMacDestroyGWorld( m_maskBitmap
) ;
1103 m_maskBitmap
= NULL
;
1107 // Create a mask from a mono bitmap (copies the bitmap).
1108 bool wxMask::Create(const wxBitmap
& bitmap
)
1112 wxMacDestroyGWorld( m_maskBitmap
) ;
1113 m_maskBitmap
= NULL
;
1115 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1116 wxT("Cannot create mask from this bitmap type (TODO)"));
1117 // other types would require a temporary bitmap. not yet implemented
1119 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1121 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1122 wxT("Cannot create mask from colour bitmap"));
1124 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1125 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1127 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1128 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1129 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1130 GetPortBitMapForCopyBits(m_maskBitmap
),
1131 &rect
, &rect
, srcCopy
, 0);
1132 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1133 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1138 // Create a mask from a bitmap and a palette index indicating
1139 // the transparent area
1140 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1143 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1147 // Create a mask from a bitmap and a colour indicating
1148 // the transparent area
1149 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1153 wxMacDestroyGWorld( m_maskBitmap
) ;
1154 m_maskBitmap
= NULL
;
1156 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1157 wxT("Cannot create mask from this bitmap type (TODO)"));
1158 // other types would require a temporary bitmap. not yet implemented
1160 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1162 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1163 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1164 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1165 RGBColor maskColor
= colour
.GetPixel();
1167 // this is not very efficient, but I can't think
1168 // of a better way of doing it
1170 GDHandle origDevice
;
1172 RGBColor colors
[2] = {
1173 { 0xFFFF, 0xFFFF, 0xFFFF },
1176 GetGWorld( &origPort
, &origDevice
) ;
1177 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1179 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1181 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1182 GetCPixel( w
, h
, &col
) ;
1183 SetGWorld( m_maskBitmap
, NULL
) ;
1184 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1186 SetCPixel( w
, h
, &colors
[0] ) ;
1190 SetCPixel( w
, h
, &colors
[1] ) ;
1194 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1195 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1196 SetGWorld( origPort
, origDevice
) ;
1201 bool wxMask::PointMasked(int x
, int y
)
1204 GDHandle origDevice
;
1208 GetGWorld( &origPort
, &origDevice
);
1210 //Set port to mask and see if it masked (1) or not ( 0 )
1211 SetGWorld(m_maskBitmap
, NULL
);
1212 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1213 GetCPixel(x
,y
, &color
);
1214 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1215 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1217 SetGWorld( origPort
, origDevice
);
1226 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1228 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1233 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1234 int desiredWidth
, int desiredHeight
)
1239 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1248 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1250 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1252 inline wxPICTResourceHandler()
1254 m_name
= "Macintosh Pict resource";
1256 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1259 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1260 int desiredWidth
, int desiredHeight
);
1262 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1264 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1265 int desiredWidth
, int desiredHeight
)
1270 c2pstrcpy( (StringPtr
) theName
, name
) ;
1272 strcpy( (char *) theName
, name
) ;
1273 c2pstr( (char *)theName
) ;
1276 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1281 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1282 DetachResource( (Handle
) thePict
) ;
1283 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1284 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1285 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1286 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1288 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1289 M_BITMAPHANDLERDATA
->m_ok
= true ;
1290 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1291 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1292 // M_BITMAPHANDLERDATA->m_quality;
1298 #if 0 // The following is an example for creating a bitmap handler
1300 // TODO: bitmap handlers, a bit like this:
1301 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1303 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1305 inline wxBMPResourceHandler()
1307 m_name
= "Windows bitmap resource";
1309 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1312 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1313 int desiredWidth
, int desiredHeight
);
1315 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1319 void wxBitmap::CleanUpHandlers()
1321 wxNode
*node
= sm_handlers
.First();
1324 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1325 wxNode
*next
= node
->Next();
1332 void wxBitmap::InitStandardHandlers()
1334 AddHandler(new wxPICTResourceHandler
) ;
1335 AddHandler(new wxICONResourceHandler
) ;