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
)
371 if ( wxTheBitmapList
)
372 wxTheBitmapList
->AddBitmap(this);
375 wxBitmap::~wxBitmap()
378 wxTheBitmapList
->DeleteObject(this);
381 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
383 m_refData
= new wxBitmapRefData
;
385 M_BITMAPDATA
->m_width
= the_width
;
386 M_BITMAPDATA
->m_height
= the_height
;
387 M_BITMAPDATA
->m_depth
= no_bits
;
388 M_BITMAPDATA
->m_numColors
= 0;
391 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
392 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
393 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
396 GDHandle origDevice
;
398 GetGWorld( &origPort
, &origDevice
) ;
399 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
400 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
402 // bits is a char array
404 unsigned char* linestart
= (unsigned char*) bits
;
405 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
406 if ( the_width
% (sizeof(unsigned char) * 8) ) {
407 linesize
+= sizeof(unsigned char);
410 RGBColor colors
[2] = {
411 { 0xFFFF , 0xFFFF , 0xFFFF } ,
415 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
417 for ( int x
= 0 ; x
< the_width
; ++x
)
421 int mask
= 1 << bit
;
422 if ( linestart
[index
] & mask
)
424 SetCPixel( x
, y
, &colors
[1] ) ;
428 SetCPixel( x
, y
, &colors
[0] ) ;
432 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
434 SetGWorld( origPort
, origDevice
) ;
438 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
441 if ( wxTheBitmapList
) {
442 wxTheBitmapList
->AddBitmap(this);
446 wxBitmap::wxBitmap(int w
, int h
, int d
)
448 (void)Create(w
, h
, d
);
450 if ( wxTheBitmapList
)
451 wxTheBitmapList
->AddBitmap(this);
454 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
456 (void) Create(data
, type
, width
, height
, depth
);
458 if ( wxTheBitmapList
)
459 wxTheBitmapList
->AddBitmap(this);
462 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
464 LoadFile(filename
, type
);
466 if ( wxTheBitmapList
)
467 wxTheBitmapList
->AddBitmap(this);
470 bool wxBitmap::CreateFromXpm(const char **bits
)
472 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
473 wxXPMDecoder decoder
;
474 wxImage img
= decoder
.ReadData(bits
);
475 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
476 *this = wxBitmap(img
);
477 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
481 wxBitmap::wxBitmap(const char **bits
)
483 (void) CreateFromXpm(bits
);
486 wxBitmap::wxBitmap(char **bits
)
488 (void) CreateFromXpm((const char **)bits
);
491 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
494 (rect
.x
>= 0) && (rect
.y
>= 0) &&
495 (rect
.x
+rect
.width
<= GetWidth()) &&
496 (rect
.y
+rect
.height
<= GetHeight()),
497 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
500 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
501 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
506 GetGWorld( &origPort
, &origDevice
);
508 // Update the subbitmaps reference data
509 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
511 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
512 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
513 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
515 // Copy sub region of this bitmap
516 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
518 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
520 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
525 WXHBITMAP submask
, mask
;
528 mask
= GetMask()->GetMaskBitmap();
529 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
530 LockPixels(GetGWorldPixMap(mask
));
531 LockPixels(GetGWorldPixMap(submask
));
533 for(int yy
= 0; yy
< rect
.height
; yy
++)
535 for(int xx
= 0; xx
< rect
.width
; xx
++)
537 SetGWorld(mask
, NULL
);
538 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
539 SetGWorld(submask
, NULL
);
540 SetCPixel(xx
,yy
, &color
);
543 UnlockPixels(GetGWorldPixMap(mask
));
544 UnlockPixels(GetGWorldPixMap(submask
));
545 ref
->m_bitmapMask
= new wxMask
;
546 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
552 WXHBITMAP subbitmap
, bitmap
;
555 bitmap
= GetHBITMAP();
556 subbitmap
= ref
->m_hBitmap
;
557 LockPixels(GetGWorldPixMap(bitmap
));
558 LockPixels(GetGWorldPixMap(subbitmap
));
560 for(int yy
= 0; yy
< rect
.height
; yy
++)
562 for(int xx
= 0; xx
< rect
.width
; xx
++)
564 SetGWorld(bitmap
, NULL
);
565 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
566 SetGWorld(subbitmap
, NULL
);
567 SetCPixel(xx
, yy
, &color
);
570 UnlockPixels(GetGWorldPixMap(bitmap
));
571 UnlockPixels(GetGWorldPixMap(subbitmap
));
574 SetGWorld( origPort
, origDevice
);
579 bool wxBitmap::Create(int w
, int h
, int d
)
583 m_refData
= new wxBitmapRefData
;
585 M_BITMAPDATA
->m_width
= w
;
586 M_BITMAPDATA
->m_height
= h
;
587 M_BITMAPDATA
->m_depth
= d
;
589 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
590 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
591 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
592 return M_BITMAPDATA
->m_ok
;
595 int wxBitmap::GetBitmapType() const
597 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
599 return M_BITMAPDATA
->m_bitmapType
;
602 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
604 DisposeBitmapRefData( M_BITMAPDATA
) ;
606 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
607 M_BITMAPDATA
->m_hBitmap
= bmp
;
608 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
611 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
615 wxBitmapHandler
*handler
= FindHandler(type
);
619 m_refData
= new wxBitmapRefData
;
621 return handler
->LoadFile(this, filename
, type
, -1, -1);
625 wxImage
loadimage(filename
, type
);
626 if (loadimage
.Ok()) {
631 wxLogWarning("no bitmap handler for type %d defined.", type
);
635 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
639 m_refData
= new wxBitmapRefData
;
641 wxBitmapHandler
*handler
= FindHandler(type
);
643 if ( handler
== NULL
) {
644 wxLogWarning("no bitmap handler for type %d defined.", type
);
649 return handler
->Create(this, data
, type
, width
, height
, depth
);
652 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
654 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
655 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
657 m_refData
= new wxBitmapRefData();
659 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
661 // width and height of the device-dependent bitmap
662 int width
= image
.GetWidth();
663 int height
= image
.GetHeight();
667 Create( width
, height
, 32 ) ;
670 GDHandle origDevice
;
672 PixMapHandle pixMap
= GetGWorldPixMap(GetHBITMAP()) ;
673 LockPixels( pixMap
);
675 GetGWorld( &origPort
, &origDevice
) ;
676 SetGWorld( GetHBITMAP() , NULL
) ;
681 register unsigned char* data
= image
.GetData();
682 char* destinationBase
= GetPixBaseAddr( pixMap
);
683 register unsigned char* destination
= (unsigned char*) destinationBase
;
684 for (int y
= 0; y
< height
; y
++)
686 for (int x
= 0; x
< width
; x
++)
689 *destination
++ = *data
++ ;
690 *destination
++ = *data
++ ;
691 *destination
++ = *data
++ ;
693 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
694 destination
= (unsigned char*) destinationBase
;
696 if ( image
.HasMask() )
698 data
= image
.GetData();
700 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
701 RGBColor white
= { 0xffff, 0xffff, 0xffff };
702 RGBColor black
= { 0 , 0 , 0 };
703 wxBitmap maskBitmap
;
705 maskBitmap
.Create( width
, height
, 1);
706 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
707 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
709 for (int y
= 0; y
< height
; y
++)
711 for (int x
= 0; x
< width
; x
++)
713 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
715 SetCPixel(x
,y
, &white
);
718 SetCPixel(x
,y
, &black
);
723 SetGWorld(GetHBITMAP(), NULL
);
724 SetMask(new wxMask( maskBitmap
));
725 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
728 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
729 SetGWorld( origPort
, origDevice
);
732 wxImage
wxBitmap::ConvertToImage() const
736 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
738 // create an wxImage object
739 int width
= GetWidth();
740 int height
= GetHeight();
741 image
.Create( width
, height
);
743 unsigned char *data
= image
.GetData();
745 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
751 // background color set to RGB(16,16,16) in consistent with wxGTK
752 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
754 wxMask
*mask
= GetMask();
756 GetGWorld( &origPort
, &origDevice
);
757 LockPixels(GetGWorldPixMap(GetHBITMAP()));
758 SetGWorld( GetHBITMAP(), NULL
);
760 // Copy data into image
762 for (int yy
= 0; yy
< height
; yy
++)
764 for (int xx
= 0; xx
< width
; xx
++)
766 GetCPixel(xx
,yy
, &color
);
767 r
= ((color
.red
) >> 8);
768 g
= ((color
.green
) >> 8);
769 b
= ((color
.blue
) >> 8);
775 if (mask
->PointMasked(xx
,yy
))
777 data
[index
] = mask_r
;
778 data
[index
+ 1] = mask_g
;
779 data
[index
+ 2] = mask_b
;
787 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
788 image
.SetMask( true );
792 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
793 SetGWorld(origPort
, origDevice
);
799 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
800 const wxPalette
*palette
) const
802 wxBitmapHandler
*handler
= FindHandler(type
);
806 return handler
->SaveFile(this, filename
, type
, palette
);
810 wxImage image
= ConvertToImage();
812 return image
.SaveFile(filename
, type
);
815 wxLogWarning("no bitmap handler for type %d defined.", type
);
819 bool wxBitmap::Ok() const
821 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
824 int wxBitmap::GetHeight() const
826 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
828 return M_BITMAPDATA
->m_height
;
831 int wxBitmap::GetWidth() const
833 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
835 return M_BITMAPDATA
->m_width
;
838 int wxBitmap::GetDepth() const
840 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
842 return M_BITMAPDATA
->m_depth
;
845 int wxBitmap::GetQuality() const
847 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
849 return M_BITMAPDATA
->m_quality
;
852 wxMask
*wxBitmap::GetMask() const
854 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
856 return M_BITMAPDATA
->m_bitmapMask
;
859 void wxBitmap::SetWidth(int w
)
862 m_refData
= new wxBitmapRefData
;
864 M_BITMAPDATA
->m_width
= w
;
867 void wxBitmap::SetHeight(int h
)
870 m_refData
= new wxBitmapRefData
;
872 M_BITMAPDATA
->m_height
= h
;
875 void wxBitmap::SetDepth(int d
)
878 m_refData
= new wxBitmapRefData
;
880 M_BITMAPDATA
->m_depth
= d
;
883 void wxBitmap::SetQuality(int q
)
886 m_refData
= new wxBitmapRefData
;
888 M_BITMAPDATA
->m_quality
= q
;
891 void wxBitmap::SetOk(bool isOk
)
894 m_refData
= new wxBitmapRefData
;
896 M_BITMAPDATA
->m_ok
= isOk
;
899 wxPalette
*wxBitmap::GetPalette() const
901 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
903 return &M_BITMAPDATA
->m_bitmapPalette
;
906 void wxBitmap::SetPalette(const wxPalette
& palette
)
909 m_refData
= new wxBitmapRefData
;
911 M_BITMAPDATA
->m_bitmapPalette
= palette
;
914 void wxBitmap::SetMask(wxMask
*mask
)
917 m_refData
= new wxBitmapRefData
;
919 // Remove existing mask if there is one.
920 if (M_BITMAPDATA
->m_bitmapMask
)
921 delete M_BITMAPDATA
->m_bitmapMask
;
923 M_BITMAPDATA
->m_bitmapMask
= mask
;
926 WXHBITMAP
wxBitmap::GetHBITMAP() const
928 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
930 return M_BITMAPDATA
->m_hBitmap
;
933 PicHandle
wxBitmap::GetPict() const
935 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
937 PicHandle picture
; // This is the returned picture
939 // If bitmap already in Pict format return pointer
940 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
941 return M_BITMAPDATA
->m_hPict
;
943 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
948 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
949 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
950 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
956 GetPortBounds( GetHBITMAP() , &portRect
) ;
957 int width
= portRect
.right
- portRect
.left
;
958 int height
= portRect
.bottom
- portRect
.top
;
960 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
961 GetGWorld( &origPort
, &origDev
) ;
965 SetGWorld( GetHBITMAP() , NULL
) ;
967 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
975 RGBColor trans
= white
;
977 RGBBackColor( &gray
);
978 EraseRect( &portRect
);
979 RGBColor trans
= gray
;
981 RGBForeColor( &black
) ;
982 RGBBackColor( &white
) ;
983 PenMode(transparent
);
985 for ( int y
= 0 ; y
< height
; ++y
)
987 for( int x
= 0 ; x
< width
; ++x
)
989 if ( !mask
->PointMasked(x
,y
) )
993 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
994 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
997 // With transparency this sets a blank pixel
998 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
1005 RGBBackColor( &gray
) ;
1006 EraseRect(&portRect
);
1007 RGBForeColor( &black
) ;
1008 RGBBackColor( &white
) ;
1010 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
1011 // src PixMap - we copy image over itself -
1012 GetPortBitMapForCopyBits(GetHBITMAP()),
1013 // dst PixMap - no drawing occurs
1014 &portRect
, // srcRect - it will be recorded and compressed -
1015 &portRect
, // dstRect - into the picture that is open -
1016 srcCopy
,NULL
); // copyMode and no clip region
1018 ClosePicture(); // We are done recording the picture
1019 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
1020 SetGWorld( origPort
, origDev
) ;
1022 return picture
; // return our groovy pict handle
1025 void wxBitmap::AddHandler(wxBitmapHandler
*handler
)
1027 sm_handlers
.Append(handler
);
1030 void wxBitmap::InsertHandler(wxBitmapHandler
*handler
)
1032 sm_handlers
.Insert(handler
);
1035 bool wxBitmap::RemoveHandler(const wxString
& name
)
1037 wxBitmapHandler
*handler
= FindHandler(name
);
1040 sm_handlers
.DeleteObject(handler
);
1047 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& name
)
1049 wxNode
*node
= sm_handlers
.First();
1052 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1053 if ( handler
->GetName() == name
)
1055 node
= node
->Next();
1060 wxBitmapHandler
*wxBitmap::FindHandler(const wxString
& extension
, wxBitmapType type
)
1062 wxNode
*node
= sm_handlers
.First();
1065 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1066 if ( handler
->GetExtension() == extension
&&
1067 (type
== -1 || handler
->GetType() == type
) )
1069 node
= node
->Next();
1074 wxBitmapHandler
*wxBitmap::FindHandler(wxBitmapType type
)
1076 wxNode
*node
= sm_handlers
.First();
1079 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1080 if (handler
->GetType() == type
)
1082 node
= node
->Next();
1096 // Construct a mask from a bitmap and a colour indicating
1097 // the transparent area
1098 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1101 Create(bitmap
, colour
);
1104 // Construct a mask from a bitmap and a palette index indicating
1105 // the transparent area
1106 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1109 Create(bitmap
, paletteIndex
);
1112 // Construct a mask from a mono bitmap (copies the bitmap).
1113 wxMask::wxMask(const wxBitmap
& bitmap
)
1123 wxMacDestroyGWorld( m_maskBitmap
) ;
1124 m_maskBitmap
= NULL
;
1128 // Create a mask from a mono bitmap (copies the bitmap).
1129 bool wxMask::Create(const wxBitmap
& bitmap
)
1133 wxMacDestroyGWorld( m_maskBitmap
) ;
1134 m_maskBitmap
= NULL
;
1136 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1137 wxT("Cannot create mask from this bitmap type (TODO)"));
1138 // other types would require a temporary bitmap. not yet implemented
1140 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1142 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1143 wxT("Cannot create mask from colour bitmap"));
1145 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1146 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1148 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1149 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1150 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1151 GetPortBitMapForCopyBits(m_maskBitmap
),
1152 &rect
, &rect
, srcCopy
, 0);
1153 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1154 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1159 // Create a mask from a bitmap and a palette index indicating
1160 // the transparent area
1161 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1164 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1168 // Create a mask from a bitmap and a colour indicating
1169 // the transparent area
1170 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1174 wxMacDestroyGWorld( m_maskBitmap
) ;
1175 m_maskBitmap
= NULL
;
1177 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1178 wxT("Cannot create mask from this bitmap type (TODO)"));
1179 // other types would require a temporary bitmap. not yet implemented
1181 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1183 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1184 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1185 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1186 RGBColor maskColor
= colour
.GetPixel();
1188 // this is not very efficient, but I can't think
1189 // of a better way of doing it
1191 GDHandle origDevice
;
1193 RGBColor colors
[2] = {
1194 { 0xFFFF, 0xFFFF, 0xFFFF },
1197 GetGWorld( &origPort
, &origDevice
) ;
1198 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1200 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1202 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1203 GetCPixel( w
, h
, &col
) ;
1204 SetGWorld( m_maskBitmap
, NULL
) ;
1205 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1207 SetCPixel( w
, h
, &colors
[0] ) ;
1211 SetCPixel( w
, h
, &colors
[1] ) ;
1215 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1216 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1217 SetGWorld( origPort
, origDevice
) ;
1222 bool wxMask::PointMasked(int x
, int y
)
1225 GDHandle origDevice
;
1229 GetGWorld( &origPort
, &origDevice
);
1231 //Set port to mask and see if it masked (1) or not ( 0 )
1232 SetGWorld(m_maskBitmap
, NULL
);
1233 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1234 GetCPixel(x
,y
, &color
);
1235 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1236 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1238 SetGWorld( origPort
, origDevice
);
1247 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
1249 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1254 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1255 int desiredWidth
, int desiredHeight
)
1260 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1269 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1271 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1273 inline wxPICTResourceHandler()
1275 m_name
= "Macintosh Pict resource";
1277 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1280 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1281 int desiredWidth
, int desiredHeight
);
1283 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1285 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1286 int desiredWidth
, int desiredHeight
)
1291 c2pstrcpy( (StringPtr
) theName
, name
) ;
1293 strcpy( (char *) theName
, name
) ;
1294 c2pstr( (char *)theName
) ;
1297 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1302 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1303 DetachResource( (Handle
) thePict
) ;
1304 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1305 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1306 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1307 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1309 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1310 M_BITMAPHANDLERDATA
->m_ok
= true ;
1311 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1312 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1313 // M_BITMAPHANDLERDATA->m_quality;
1319 #if 0 // The following is an example for creating a bitmap handler
1321 // TODO: bitmap handlers, a bit like this:
1322 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
1324 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
1326 inline wxBMPResourceHandler()
1328 m_name
= "Windows bitmap resource";
1330 m_type
= wxBITMAP_TYPE_BMP_RESOURCE
;
1333 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1334 int desiredWidth
, int desiredHeight
);
1336 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
1340 void wxBitmap::CleanUpHandlers()
1342 wxNode
*node
= sm_handlers
.First();
1345 wxBitmapHandler
*handler
= (wxBitmapHandler
*)node
->Data();
1346 wxNode
*next
= node
->Next();
1353 void wxBitmap::InitStandardHandlers()
1355 AddHandler(new wxPICTResourceHandler
) ;
1356 AddHandler(new wxICONResourceHandler
) ;