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 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
26 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
27 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
30 #include <ApplicationServices/ApplicationServices.h>
32 #include <PictUtils.h>
35 #include "wx/mac/uma.h"
37 CTabHandle
wxMacCreateColorTable( int numColors
)
39 CTabHandle newColors
; /* Handle to the new color table */
41 /* Allocate memory for the color table */
42 newColors
= (CTabHandle
)NewHandleClear( sizeof (ColorTable
) +
43 sizeof (ColorSpec
) * (numColors
- 1) );
46 /* Initialize the fields */
47 (**newColors
).ctSeed
= GetCTSeed();
48 (**newColors
).ctFlags
= 0;
49 (**newColors
).ctSize
= numColors
- 1;
50 /* Initialize the table of colors */
55 void wxMacDestroyColorTable( CTabHandle colors
)
57 DisposeHandle( (Handle
) colors
) ;
60 void wxMacSetColorTableEntry( CTabHandle newColors
, int index
, int red
, int green
, int blue
)
62 (**newColors
).ctTable
[index
].value
= index
;
63 (**newColors
).ctTable
[index
].rgb
.red
= 0 ;// someRedValue;
64 (**newColors
).ctTable
[index
].rgb
.green
= 0 ; // someGreenValue;
65 (**newColors
).ctTable
[index
].rgb
.blue
= 0 ; // someBlueValue;
68 GWorldPtr
wxMacCreateGWorld( int width
, int height
, int depth
)
72 Rect rect
= { 0 , 0 , height
, width
} ;
76 depth
= wxDisplayDepth() ;
79 err
= NewGWorld( &port
, depth
, &rect
, NULL
, NULL
, 0 ) ;
87 void wxMacDestroyGWorld( GWorldPtr gw
)
93 #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
95 OSErr
SetupCIconHandlePixMap( CIconHandle icon
, short depth
, Rect
*bounds
, CTabHandle colors
)
97 CTabHandle newColors
; /* Color table used for the off-screen PixMap */
98 Ptr offBaseAddr
; /* Pointer to the off-screen pixel image */
99 OSErr error
; /* Returns error code */
100 short bytesPerRow
; /* Number of bytes per row in the PixMap */
107 bytesPerRow
= ((depth
* (bounds
->right
- bounds
->left
) + 31) / 32) * 4;
109 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
113 error
= HandToHand((Handle
*) &newColors
);
117 newColors
= (CTabHandle
) NewHandle(sizeof(ColorTable
) -
123 /* Allocate pixel image; long integer multiplication avoids overflow */
124 (**icon
).iconData
= NewHandle((unsigned long) bytesPerRow
* (bounds
->bottom
-
126 if ((**icon
).iconData
!= nil
)
128 /* Initialize fields common to indexed and direct PixMaps */
129 (**icon
).iconPMap
.baseAddr
= 0; /* Point to image */
130 (**icon
).iconPMap
.rowBytes
= bytesPerRow
| /* MSB set for PixMap */
132 (**icon
).iconPMap
.bounds
= *bounds
; /* Use given bounds */
133 (**icon
).iconPMap
.pmVersion
= 0; /* No special stuff */
134 (**icon
).iconPMap
.packType
= 0; /* Default PICT pack */
135 (**icon
).iconPMap
.packSize
= 0; /* Always zero in mem */
136 (**icon
).iconPMap
.hRes
= kDefaultRes
; /* 72 DPI default res */
137 (**icon
).iconPMap
.vRes
= kDefaultRes
; /* 72 DPI default res */
138 (**icon
).iconPMap
.pixelSize
= depth
; /* Set # bits/pixel */
140 /* Initialize fields specific to indexed and direct PixMaps */
143 /* PixMap is indexed */
144 (**icon
).iconPMap
.pixelType
= 0; /* Indicates indexed */
145 (**icon
).iconPMap
.cmpCount
= 1; /* Have 1 component */
146 (**icon
).iconPMap
.cmpSize
= depth
; /* Component size=depth */
147 (**icon
).iconPMap
.pmTable
= newColors
; /* Handle to CLUT */
151 /* PixMap is direct */
152 (**icon
).iconPMap
.pixelType
= RGBDirect
; /* Indicates direct */
153 (**icon
).iconPMap
.cmpCount
= 3; /* Have 3 components */
155 (**icon
).iconPMap
.cmpSize
= 5; /* 5 bits/component */
157 (**icon
).iconPMap
.cmpSize
= 8; /* 8 bits/component */
158 (**newColors
).ctSeed
= 3 * (**icon
).iconPMap
.cmpSize
;
159 (**newColors
).ctFlags
= 0;
160 (**newColors
).ctSize
= 0;
161 (**icon
).iconPMap
.pmTable
= newColors
;
170 /* If no errors occured, return a handle to the new off-screen PixMap */
173 if (newColors
!= nil
)
174 DisposeCTable(newColors
);
177 /* Return the error code */
181 CIconHandle
wxMacCreateCIcon(GWorldPtr image
, GWorldPtr mask
, short dstDepth
, short iconSize
)
186 GetGWorld(&saveWorld
,&saveHandle
); // save Graphics env state
187 SetGWorld(image
,nil
);
189 Rect frame
= { 0 , 0 , iconSize
, iconSize
} ;
190 Rect imageBounds
= frame
;
191 GetPortBounds( image
, &imageBounds
) ;
193 int bwSize
= iconSize
/ 8 * iconSize
;
194 CIconHandle icon
= (CIconHandle
) NewHandleClear( sizeof ( CIcon
) + 2 * bwSize
) ;
195 HLock((Handle
)icon
) ;
196 SetupCIconHandlePixMap( icon
, dstDepth
, &frame
,GetCTable(dstDepth
)) ;
197 HLock( (**icon
).iconData
) ;
198 (**icon
).iconPMap
.baseAddr
= *(**icon
).iconData
;
200 LockPixels(GetGWorldPixMap(image
));
202 CopyBits(GetPortBitMapForCopyBits(image
),
203 (BitMapPtr
)&((**icon
).iconPMap
),
206 srcCopy
| ditherCopy
, nil
);
209 UnlockPixels(GetGWorldPixMap(image
));
210 HUnlock( (**icon
).iconData
) ;
212 (**icon
).iconMask
.rowBytes
= iconSize
/ 8 ;
213 (**icon
).iconMask
.bounds
= frame
;
215 (**icon
).iconBMap
.rowBytes
= iconSize
/ 8 ;
216 (**icon
).iconBMap
.bounds
= frame
;
217 (**icon
).iconMask
.baseAddr
= (char*) &(**icon
).iconMaskData
;
218 (**icon
).iconBMap
.baseAddr
= (char*) &(**icon
).iconMaskData
+ bwSize
;
222 LockPixels(GetGWorldPixMap(mask
) ) ;
223 CopyBits(GetPortBitMapForCopyBits(mask
) ,
224 &(**icon
).iconBMap
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
225 CopyBits(GetPortBitMapForCopyBits(mask
) ,
226 &(**icon
).iconMask
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
227 UnlockPixels(GetGWorldPixMap( mask
) ) ;
231 LockPixels(GetGWorldPixMap(image
));
232 CopyBits(GetPortBitMapForCopyBits(image
) ,
233 &(**icon
).iconBMap
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
234 CopyBits(GetPortBitMapForCopyBits(image
) ,
235 &(**icon
).iconMask
, &imageBounds
, &imageBounds
, srcCopy
, nil
) ;
236 UnlockPixels(GetGWorldPixMap(image
));
239 (**icon
).iconMask
.baseAddr
= NULL
;
240 (**icon
).iconBMap
.baseAddr
= NULL
;
241 (**icon
).iconPMap
.baseAddr
= NULL
;
242 HUnlock((Handle
)icon
) ;
243 SetGWorld(saveWorld
,saveHandle
);
248 PicHandle
wxMacCreatePict(GWorldPtr wp
, GWorldPtr mask
)
255 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
256 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
258 GetGWorld( &origPort
, &origDev
) ;
260 RgnHandle clipRgn
= NULL
;
265 LockPixels( GetGWorldPixMap( mask
) ) ;
266 BitMapToRegion( clipRgn
, (BitMap
*) *GetGWorldPixMap( mask
) ) ;
267 UnlockPixels( GetGWorldPixMap( mask
) ) ;
270 SetGWorld( wp
, NULL
) ;
272 GetPortBounds( wp
, &portRect
) ;
274 pict
= OpenPicture(&portRect
);
277 RGBForeColor( &black
) ;
278 RGBBackColor( &white
) ;
280 LockPixels( GetGWorldPixMap( wp
) ) ;
281 CopyBits(GetPortBitMapForCopyBits(wp
),
282 GetPortBitMapForCopyBits(wp
),
286 UnlockPixels( GetGWorldPixMap( wp
) ) ;
289 SetGWorld( origPort
, origDev
) ;
293 wxBitmapRefData::wxBitmapRefData()
305 m_bitmapType
= kMacBitmapTypeUnknownType
;
308 // TODO move this do a public function of Bitmap Ref
309 static void DisposeBitmapRefData(wxBitmapRefData
*data
)
311 switch (data
->m_bitmapType
)
313 case kMacBitmapTypePict
:
317 KillPicture( data
->m_hPict
) ;
318 data
->m_hPict
= NULL
;
322 case kMacBitmapTypeGrafWorld
:
324 if ( data
->m_hBitmap
)
326 wxMacDestroyGWorld( data
->m_hBitmap
) ;
327 data
->m_hBitmap
= NULL
;
331 case kMacBitmapTypeIcon
:
334 DisposeCIcon( data
->m_hIcon
) ;
335 data
->m_hIcon
= NULL
;
343 if (data
->m_bitmapMask
)
345 delete data
->m_bitmapMask
;
346 data
->m_bitmapMask
= NULL
;
350 wxBitmapRefData::~wxBitmapRefData()
352 DisposeBitmapRefData( this ) ;
355 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
366 wxBitmap::~wxBitmap()
370 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
372 m_refData
= new wxBitmapRefData
;
374 M_BITMAPDATA
->m_width
= the_width
;
375 M_BITMAPDATA
->m_height
= the_height
;
376 M_BITMAPDATA
->m_depth
= no_bits
;
377 M_BITMAPDATA
->m_numColors
= 0;
380 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
381 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( the_width
, the_height
, no_bits
) ;
382 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
385 GDHandle origDevice
;
387 GetGWorld( &origPort
, &origDevice
) ;
388 SetGWorld( M_BITMAPDATA
->m_hBitmap
, NULL
) ;
389 LockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
391 // bits is a char array
393 unsigned char* linestart
= (unsigned char*) bits
;
394 int linesize
= ( the_width
/ (sizeof(unsigned char) * 8)) ;
395 if ( the_width
% (sizeof(unsigned char) * 8) ) {
396 linesize
+= sizeof(unsigned char);
399 RGBColor colors
[2] = {
400 { 0xFFFF , 0xFFFF , 0xFFFF } ,
404 for ( int y
= 0 ; y
< the_height
; ++y
, linestart
+= linesize
)
406 for ( int x
= 0 ; x
< the_width
; ++x
)
410 int mask
= 1 << bit
;
411 if ( linestart
[index
] & mask
)
413 SetCPixel( x
, y
, &colors
[1] ) ;
417 SetCPixel( x
, y
, &colors
[0] ) ;
421 UnlockPixels( GetGWorldPixMap( M_BITMAPDATA
->m_hBitmap
) ) ;
423 SetGWorld( origPort
, origDevice
) ;
427 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
431 wxBitmap::wxBitmap(int w
, int h
, int d
)
433 (void)Create(w
, h
, d
);
436 wxBitmap::wxBitmap(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
438 (void) Create(data
, type
, width
, height
, depth
);
441 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
443 LoadFile(filename
, type
);
446 bool wxBitmap::CreateFromXpm(const char **bits
)
448 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
449 wxXPMDecoder decoder
;
450 wxImage img
= decoder
.ReadData(bits
);
451 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
452 *this = wxBitmap(img
);
456 wxBitmap::wxBitmap(const char **bits
)
458 (void) CreateFromXpm(bits
);
461 wxBitmap::wxBitmap(char **bits
)
463 (void) CreateFromXpm((const char **)bits
);
466 wxBitmap
wxBitmap::GetSubBitmap(const wxRect
&rect
) const
469 (rect
.x
>= 0) && (rect
.y
>= 0) &&
470 (rect
.x
+rect
.width
<= GetWidth()) &&
471 (rect
.y
+rect
.height
<= GetHeight()),
472 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
475 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
476 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
481 GetGWorld( &origPort
, &origDevice
);
483 // Update the subbitmaps reference data
484 wxBitmapRefData
*ref
= (wxBitmapRefData
*)ret
.GetRefData();
486 ref
->m_numColors
= M_BITMAPDATA
->m_numColors
;
487 ref
->m_bitmapPalette
= M_BITMAPDATA
->m_bitmapPalette
;
488 ref
->m_bitmapType
= M_BITMAPDATA
->m_bitmapType
;
490 // Copy sub region of this bitmap
491 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
)
493 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
495 else if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
500 WXHBITMAP submask
, mask
;
503 mask
= GetMask()->GetMaskBitmap();
504 submask
= wxMacCreateGWorld(rect
.width
, rect
.height
, 1);
505 LockPixels(GetGWorldPixMap(mask
));
506 LockPixels(GetGWorldPixMap(submask
));
508 for(int yy
= 0; yy
< rect
.height
; yy
++)
510 for(int xx
= 0; xx
< rect
.width
; xx
++)
512 SetGWorld(mask
, NULL
);
513 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
514 SetGWorld(submask
, NULL
);
515 SetCPixel(xx
,yy
, &color
);
518 UnlockPixels(GetGWorldPixMap(mask
));
519 UnlockPixels(GetGWorldPixMap(submask
));
520 ref
->m_bitmapMask
= new wxMask
;
521 ref
->m_bitmapMask
->SetMaskBitmap(submask
);
527 WXHBITMAP subbitmap
, bitmap
;
530 bitmap
= GetHBITMAP();
531 subbitmap
= ref
->m_hBitmap
;
532 LockPixels(GetGWorldPixMap(bitmap
));
533 LockPixels(GetGWorldPixMap(subbitmap
));
535 for(int yy
= 0; yy
< rect
.height
; yy
++)
537 for(int xx
= 0; xx
< rect
.width
; xx
++)
539 SetGWorld(bitmap
, NULL
);
540 GetCPixel(rect
.x
+ xx
, rect
.y
+ yy
, &color
);
541 SetGWorld(subbitmap
, NULL
);
542 SetCPixel(xx
, yy
, &color
);
545 UnlockPixels(GetGWorldPixMap(bitmap
));
546 UnlockPixels(GetGWorldPixMap(subbitmap
));
549 SetGWorld( origPort
, origDevice
);
554 bool wxBitmap::Create(int w
, int h
, int d
)
558 m_refData
= new wxBitmapRefData
;
560 M_BITMAPDATA
->m_width
= w
;
561 M_BITMAPDATA
->m_height
= h
;
562 M_BITMAPDATA
->m_depth
= d
;
564 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
565 M_BITMAPDATA
->m_hBitmap
= wxMacCreateGWorld( w
, h
, d
) ;
566 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
567 return M_BITMAPDATA
->m_ok
;
570 int wxBitmap::GetBitmapType() const
572 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType
, wxT("invalid bitmap") );
574 return M_BITMAPDATA
->m_bitmapType
;
577 void wxBitmap::SetHBITMAP(WXHBITMAP bmp
)
579 DisposeBitmapRefData( M_BITMAPDATA
) ;
581 M_BITMAPDATA
->m_bitmapType
= kMacBitmapTypeGrafWorld
;
582 M_BITMAPDATA
->m_hBitmap
= bmp
;
583 M_BITMAPDATA
->m_ok
= (M_BITMAPDATA
->m_hBitmap
!= NULL
) ;
586 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
590 wxBitmapHandler
*handler
= FindHandler(type
);
594 m_refData
= new wxBitmapRefData
;
596 return handler
->LoadFile(this, filename
, type
, -1, -1);
600 wxImage
loadimage(filename
, type
);
601 if (loadimage
.Ok()) {
606 wxLogWarning("no bitmap handler for type %d defined.", type
);
610 bool wxBitmap::Create(void *data
, wxBitmapType type
, int width
, int height
, int depth
)
614 m_refData
= new wxBitmapRefData
;
616 wxBitmapHandler
*handler
= FindHandler(type
);
618 if ( handler
== NULL
) {
619 wxLogWarning("no bitmap handler for type %d defined.", type
);
624 return handler
->Create(this, data
, type
, width
, height
, depth
);
627 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
629 wxCHECK_RET( image
.Ok(), wxT("invalid image") )
630 wxCHECK_RET( depth
== -1, wxT("invalid bitmap depth") )
632 m_refData
= new wxBitmapRefData();
634 // width and height of the device-dependent bitmap
635 int width
= image
.GetWidth();
636 int height
= image
.GetHeight();
640 Create( width
, height
, 32 ) ;
643 GDHandle origDevice
;
645 PixMapHandle pixMap
= GetGWorldPixMap(GetHBITMAP()) ;
646 LockPixels( pixMap
);
648 GetGWorld( &origPort
, &origDevice
) ;
649 SetGWorld( GetHBITMAP() , NULL
) ;
654 register unsigned char* data
= image
.GetData();
655 char* destinationBase
= GetPixBaseAddr( pixMap
);
656 register unsigned char* destination
= (unsigned char*) destinationBase
;
657 for (int y
= 0; y
< height
; y
++)
659 for (int x
= 0; x
< width
; x
++)
662 *destination
++ = *data
++ ;
663 *destination
++ = *data
++ ;
664 *destination
++ = *data
++ ;
666 destinationBase
+= ((**pixMap
).rowBytes
& 0x7fff);
667 destination
= (unsigned char*) destinationBase
;
669 if ( image
.HasMask() )
671 data
= image
.GetData();
673 wxColour
maskcolor(image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
674 RGBColor white
= { 0xffff, 0xffff, 0xffff };
675 RGBColor black
= { 0 , 0 , 0 };
676 wxBitmap maskBitmap
;
678 maskBitmap
.Create( width
, height
, 1);
679 LockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
680 SetGWorld(maskBitmap
.GetHBITMAP(), NULL
);
682 for (int y
= 0; y
< height
; y
++)
684 for (int x
= 0; x
< width
; x
++)
686 if ( data
[0] == image
.GetMaskRed() && data
[1] == image
.GetMaskGreen() && data
[2] == image
.GetMaskBlue() )
688 SetCPixel(x
,y
, &white
);
691 SetCPixel(x
,y
, &black
);
696 SetGWorld(GetHBITMAP(), NULL
);
697 SetMask(new wxMask( maskBitmap
));
698 UnlockPixels( GetGWorldPixMap(maskBitmap
.GetHBITMAP()) );
701 UnlockPixels( GetGWorldPixMap(GetHBITMAP()) );
702 SetGWorld( origPort
, origDevice
);
705 wxImage
wxBitmap::ConvertToImage() const
709 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
711 // create an wxImage object
712 int width
= GetWidth();
713 int height
= GetHeight();
714 image
.Create( width
, height
);
716 unsigned char *data
= image
.GetData();
718 wxCHECK_MSG( data
, wxNullImage
, wxT("Could not allocate data for image") );
724 // background color set to RGB(16,16,16) in consistent with wxGTK
725 unsigned char mask_r
=16, mask_g
=16, mask_b
=16;
727 wxMask
*mask
= GetMask();
729 GetGWorld( &origPort
, &origDevice
);
730 LockPixels(GetGWorldPixMap(GetHBITMAP()));
731 SetGWorld( GetHBITMAP(), NULL
);
733 // Copy data into image
735 for (int yy
= 0; yy
< height
; yy
++)
737 for (int xx
= 0; xx
< width
; xx
++)
739 GetCPixel(xx
,yy
, &color
);
740 r
= ((color
.red
) >> 8);
741 g
= ((color
.green
) >> 8);
742 b
= ((color
.blue
) >> 8);
748 if (mask
->PointMasked(xx
,yy
))
750 data
[index
] = mask_r
;
751 data
[index
+ 1] = mask_g
;
752 data
[index
+ 2] = mask_b
;
760 image
.SetMaskColour( mask_r
, mask_g
, mask_b
);
761 image
.SetMask( true );
765 UnlockPixels(GetGWorldPixMap(GetHBITMAP()));
766 SetGWorld(origPort
, origDevice
);
772 bool wxBitmap::SaveFile(const wxString
& filename
, wxBitmapType type
,
773 const wxPalette
*palette
) const
775 wxBitmapHandler
*handler
= FindHandler(type
);
779 return handler
->SaveFile(this, filename
, type
, palette
);
783 wxImage image
= ConvertToImage();
785 return image
.SaveFile(filename
, type
);
788 wxLogWarning("no bitmap handler for type %d defined.", type
);
792 bool wxBitmap::Ok() const
794 return (M_BITMAPDATA
&& M_BITMAPDATA
->m_ok
);
797 int wxBitmap::GetHeight() const
799 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
801 return M_BITMAPDATA
->m_height
;
804 int wxBitmap::GetWidth() const
806 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
808 return M_BITMAPDATA
->m_width
;
811 int wxBitmap::GetDepth() const
813 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
815 return M_BITMAPDATA
->m_depth
;
818 int wxBitmap::GetQuality() const
820 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
822 return M_BITMAPDATA
->m_quality
;
825 wxMask
*wxBitmap::GetMask() const
827 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
829 return M_BITMAPDATA
->m_bitmapMask
;
832 void wxBitmap::SetWidth(int w
)
835 m_refData
= new wxBitmapRefData
;
837 M_BITMAPDATA
->m_width
= w
;
840 void wxBitmap::SetHeight(int h
)
843 m_refData
= new wxBitmapRefData
;
845 M_BITMAPDATA
->m_height
= h
;
848 void wxBitmap::SetDepth(int d
)
851 m_refData
= new wxBitmapRefData
;
853 M_BITMAPDATA
->m_depth
= d
;
856 void wxBitmap::SetQuality(int q
)
859 m_refData
= new wxBitmapRefData
;
861 M_BITMAPDATA
->m_quality
= q
;
864 void wxBitmap::SetOk(bool isOk
)
867 m_refData
= new wxBitmapRefData
;
869 M_BITMAPDATA
->m_ok
= isOk
;
872 wxPalette
*wxBitmap::GetPalette() const
874 wxCHECK_MSG( Ok(), NULL
, wxT("Invalid bitmap GetPalette()") );
876 return &M_BITMAPDATA
->m_bitmapPalette
;
879 void wxBitmap::SetPalette(const wxPalette
& palette
)
882 m_refData
= new wxBitmapRefData
;
884 M_BITMAPDATA
->m_bitmapPalette
= palette
;
887 void wxBitmap::SetMask(wxMask
*mask
)
890 m_refData
= new wxBitmapRefData
;
892 // Remove existing mask if there is one.
893 if (M_BITMAPDATA
->m_bitmapMask
)
894 delete M_BITMAPDATA
->m_bitmapMask
;
896 M_BITMAPDATA
->m_bitmapMask
= mask
;
899 WXHBITMAP
wxBitmap::GetHBITMAP() const
901 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
903 return M_BITMAPDATA
->m_hBitmap
;
906 PicHandle
wxBitmap::GetPict() const
908 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
910 PicHandle picture
; // This is the returned picture
912 // If bitmap already in Pict format return pointer
913 if(M_BITMAPDATA
->m_bitmapType
== kMacBitmapTypePict
) {
914 return M_BITMAPDATA
->m_hPict
;
916 else if(M_BITMAPDATA
->m_bitmapType
!= kMacBitmapTypeGrafWorld
) {
921 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
922 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
923 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
929 GetPortBounds( GetHBITMAP() , &portRect
) ;
930 int width
= portRect
.right
- portRect
.left
;
931 int height
= portRect
.bottom
- portRect
.top
;
933 LockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
934 GetGWorld( &origPort
, &origDev
) ;
938 SetGWorld( GetHBITMAP() , NULL
) ;
940 picture
= OpenPicture(&portRect
); // open a picture, this disables drawing
948 RGBColor trans
= white
;
950 RGBBackColor( &gray
);
951 EraseRect( &portRect
);
952 RGBColor trans
= gray
;
954 RGBForeColor( &black
) ;
955 RGBBackColor( &white
) ;
956 PenMode(transparent
);
958 for ( int y
= 0 ; y
< height
; ++y
)
960 for( int x
= 0 ; x
< width
; ++x
)
962 if ( !mask
->PointMasked(x
,y
) )
966 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
967 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
970 // With transparency this sets a blank pixel
971 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &trans
);
978 RGBBackColor( &gray
) ;
979 EraseRect(&portRect
);
980 RGBForeColor( &black
) ;
981 RGBBackColor( &white
) ;
983 CopyBits(GetPortBitMapForCopyBits(GetHBITMAP()),
984 // src PixMap - we copy image over itself -
985 GetPortBitMapForCopyBits(GetHBITMAP()),
986 // dst PixMap - no drawing occurs
987 &portRect
, // srcRect - it will be recorded and compressed -
988 &portRect
, // dstRect - into the picture that is open -
989 srcCopy
,NULL
); // copyMode and no clip region
991 ClosePicture(); // We are done recording the picture
992 UnlockPixels( GetGWorldPixMap( GetHBITMAP() ) ) ;
993 SetGWorld( origPort
, origDev
) ;
995 return picture
; // return our groovy pict handle
1007 // Construct a mask from a bitmap and a colour indicating
1008 // the transparent area
1009 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1012 Create(bitmap
, colour
);
1015 // Construct a mask from a bitmap and a palette index indicating
1016 // the transparent area
1017 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1020 Create(bitmap
, paletteIndex
);
1023 // Construct a mask from a mono bitmap (copies the bitmap).
1024 wxMask::wxMask(const wxBitmap
& bitmap
)
1034 wxMacDestroyGWorld( m_maskBitmap
) ;
1035 m_maskBitmap
= NULL
;
1039 // Create a mask from a mono bitmap (copies the bitmap).
1040 bool wxMask::Create(const wxBitmap
& bitmap
)
1044 wxMacDestroyGWorld( m_maskBitmap
) ;
1045 m_maskBitmap
= NULL
;
1047 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1048 wxT("Cannot create mask from this bitmap type (TODO)"));
1049 // other types would require a temporary bitmap. not yet implemented
1051 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Invalid bitmap"));
1053 wxCHECK_MSG(bitmap
.GetDepth() == 1, false,
1054 wxT("Cannot create mask from colour bitmap"));
1056 m_maskBitmap
= wxMacCreateGWorld(bitmap
.GetWidth(), bitmap
.GetHeight(), 1);
1057 Rect rect
= { 0,0, bitmap
.GetHeight(), bitmap
.GetWidth() };
1059 LockPixels( GetGWorldPixMap(m_maskBitmap
) );
1060 LockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1061 CopyBits(GetPortBitMapForCopyBits(bitmap
.GetHBITMAP()),
1062 GetPortBitMapForCopyBits(m_maskBitmap
),
1063 &rect
, &rect
, srcCopy
, 0);
1064 UnlockPixels( GetGWorldPixMap(m_maskBitmap
) );
1065 UnlockPixels( GetGWorldPixMap(bitmap
.GetHBITMAP()) );
1070 // Create a mask from a bitmap and a palette index indicating
1071 // the transparent area
1072 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1075 wxCHECK_MSG( 0, false, wxT("Not implemented"));
1079 // Create a mask from a bitmap and a colour indicating
1080 // the transparent area
1081 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1085 wxMacDestroyGWorld( m_maskBitmap
) ;
1086 m_maskBitmap
= NULL
;
1088 wxCHECK_MSG( bitmap
.GetBitmapType() == kMacBitmapTypeGrafWorld
, false,
1089 wxT("Cannot create mask from this bitmap type (TODO)"));
1090 // other types would require a temporary bitmap. not yet implemented
1092 wxCHECK_MSG( bitmap
.Ok(), false, wxT("Illigal bitmap"));
1094 m_maskBitmap
= wxMacCreateGWorld( bitmap
.GetWidth() , bitmap
.GetHeight() , 1 );
1095 LockPixels( GetGWorldPixMap( m_maskBitmap
) );
1096 LockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) );
1097 RGBColor maskColor
= colour
.GetPixel();
1099 // this is not very efficient, but I can't think
1100 // of a better way of doing it
1102 GDHandle origDevice
;
1104 RGBColor colors
[2] = {
1105 { 0xFFFF, 0xFFFF, 0xFFFF },
1108 GetGWorld( &origPort
, &origDevice
) ;
1109 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
1111 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
1113 SetGWorld( bitmap
.GetHBITMAP(), NULL
) ;
1114 GetCPixel( w
, h
, &col
) ;
1115 SetGWorld( m_maskBitmap
, NULL
) ;
1116 if (col
.red
== maskColor
.red
&& col
.green
== maskColor
.green
&& col
.blue
== maskColor
.blue
)
1118 SetCPixel( w
, h
, &colors
[0] ) ;
1122 SetCPixel( w
, h
, &colors
[1] ) ;
1126 UnlockPixels( GetGWorldPixMap( (CGrafPtr
) m_maskBitmap
) ) ;
1127 UnlockPixels( GetGWorldPixMap( bitmap
.GetHBITMAP() ) ) ;
1128 SetGWorld( origPort
, origDevice
) ;
1133 bool wxMask::PointMasked(int x
, int y
)
1136 GDHandle origDevice
;
1140 GetGWorld( &origPort
, &origDevice
);
1142 //Set port to mask and see if it masked (1) or not ( 0 )
1143 SetGWorld(m_maskBitmap
, NULL
);
1144 LockPixels(GetGWorldPixMap(m_maskBitmap
));
1145 GetCPixel(x
,y
, &color
);
1146 masked
= !(color
.red
== 0 && color
.green
== 0 && color
.blue
== 0);
1147 UnlockPixels(GetGWorldPixMap(m_maskBitmap
));
1149 SetGWorld( origPort
, origDevice
);
1158 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1163 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1164 int desiredWidth
, int desiredHeight
)
1169 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1178 class WXDLLEXPORT wxPICTResourceHandler
: public wxBitmapHandler
1180 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler
)
1182 inline wxPICTResourceHandler()
1184 m_name
= "Macintosh Pict resource";
1186 m_type
= wxBITMAP_TYPE_PICT_RESOURCE
;
1189 virtual bool LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1190 int desiredWidth
, int desiredHeight
);
1192 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler
, wxBitmapHandler
)
1194 bool wxPICTResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1195 int desiredWidth
, int desiredHeight
)
1200 c2pstrcpy( (StringPtr
) theName
, name
) ;
1202 strcpy( (char *) theName
, name
) ;
1203 c2pstr( (char *)theName
) ;
1206 PicHandle thePict
= (PicHandle
) GetNamedResource( 'PICT' , theName
) ;
1211 GetPictInfo( thePict
, &theInfo
, 0 , 0 , systemMethod
, 0 ) ;
1212 DetachResource( (Handle
) thePict
) ;
1213 M_BITMAPHANDLERDATA
->m_bitmapType
= kMacBitmapTypePict
;
1214 M_BITMAPHANDLERDATA
->m_hPict
= thePict
;
1215 M_BITMAPHANDLERDATA
->m_width
= theInfo
.sourceRect
.right
- theInfo
.sourceRect
.left
;
1216 M_BITMAPHANDLERDATA
->m_height
= theInfo
.sourceRect
.bottom
- theInfo
.sourceRect
.top
;
1218 M_BITMAPHANDLERDATA
->m_depth
= theInfo
.depth
;
1219 M_BITMAPHANDLERDATA
->m_ok
= true ;
1220 M_BITMAPHANDLERDATA
->m_numColors
= theInfo
.uniqueColors
;
1221 // M_BITMAPHANDLERDATA->m_bitmapPalette;
1222 // M_BITMAPHANDLERDATA->m_quality;
1228 void wxBitmap::InitStandardHandlers()
1230 AddHandler(new wxPICTResourceHandler
) ;
1231 AddHandler(new wxICONResourceHandler
) ;