+wxBitmap::wxBitmap(const wxImage& image, int depth)
+{
+ wxCHECK_RET( image.Ok(), wxT("invalid image") )
+ wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") )
+
+ m_refData = new wxBitmapRefData();
+
+ // width and height of the device-dependent bitmap
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+
+ // Create picture
+
+ Create( width , height , 32 ) ;
+
+ CGrafPtr origPort ;
+ GDHandle origDevice ;
+
+ PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ;
+ LockPixels( pixMap );
+
+ GetGWorld( &origPort , &origDevice ) ;
+ SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;
+
+ // Render image
+ register unsigned char* data = image.GetData();
+ char* destinationBase = GetPixBaseAddr( pixMap );
+ register unsigned char* destination = (unsigned char*) destinationBase ;
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ *destination++ = 0 ;
+ *destination++ = *data++ ;
+ *destination++ = *data++ ;
+ *destination++ = *data++ ;
+ }
+ destinationBase += ((**pixMap).rowBytes & 0x7fff);
+ destination = (unsigned char*) destinationBase ;
+ }
+ if ( image.HasAlpha() )
+ {
+ unsigned char *alpha = image.GetAlpha();
+
+ wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
+ RGBColor color ;
+ wxBitmap maskBitmap ;
+
+ maskBitmap.Create( width, height, 24);
+ LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
+ SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
+
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ memset( &color , 255 - *alpha , sizeof( color ) );
+ SetCPixel(x,y, &color);
+
+ alpha += 1 ;
+ }
+ } // for height
+ SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
+ SetMask(new wxMask( maskBitmap ));
+ UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
+ }
+ else if ( image.HasMask() )
+ {
+ data = image.GetData();
+
+ wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
+ RGBColor white = { 0xffff, 0xffff, 0xffff };
+ RGBColor black = { 0 , 0 , 0 };
+ wxBitmap maskBitmap ;
+
+ maskBitmap.Create( width, height, 1);
+ LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
+ SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
+
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
+ {
+ SetCPixel(x,y, &white);
+ }
+ else {
+ SetCPixel(x,y, &black);
+ }
+ data += 3 ;
+ }
+ } // for height
+ SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
+ SetMask(new wxMask( maskBitmap ));
+ UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
+ }
+
+ UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) );
+ SetGWorld( origPort, origDevice );
+}
+
+wxImage wxBitmap::ConvertToImage() const
+{
+ wxImage image;
+
+ wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
+
+ // create an wxImage object
+ int width = GetWidth();
+ int height = GetHeight();
+ image.Create( width, height );
+
+ unsigned char *data = image.GetData();
+
+ wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
+
+ GWorldPtr origPort;
+ GDHandle origDevice;
+ RgnHandle maskRgn = NULL ;
+ GWorldPtr tempPort = NULL ;
+ int index;
+ RGBColor color;
+ // background color set to RGB(16,16,16) in consistent with wxGTK
+ unsigned char mask_r=16, mask_g=16, mask_b=16;
+ SInt16 r,g,b;
+ wxMask *mask = GetMask();
+
+ GetGWorld( &origPort, &origDevice );
+ if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
+ {
+ tempPort = wxMacCreateGWorld( width , height , -1) ;
+ }
+ else
+ {
+ tempPort = (GWorldPtr) GetHBITMAP() ;
+ }
+ LockPixels(GetGWorldPixMap(tempPort));
+ SetGWorld( tempPort, NULL);
+ if ( GetBitmapType() == kMacBitmapTypePict || GetBitmapType() == kMacBitmapTypeIcon )
+ {
+ Rect bitmaprect = { 0 , 0 , height, width };
+ if ( GetBitmapType() == kMacBitmapTypeIcon )
+ {
+ ::PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(GetHICON()) ) ;
+ maskRgn = NewRgn() ;
+ BitMapToRegion( maskRgn , &(**(MAC_WXHICON(GetHICON()))).iconMask ) ;
+ }
+ else
+ ::DrawPicture( (PicHandle) GetPict(), &bitmaprect ) ;
+ }
+ // Copy data into image
+ index = 0;
+ for (int yy = 0; yy < height; yy++)
+ {
+ for (int xx = 0; xx < width; xx++)
+ {
+ GetCPixel(xx,yy, &color);
+ r = ((color.red ) >> 8);
+ g = ((color.green ) >> 8);
+ b = ((color.blue ) >> 8);
+ data[index ] = r;
+ data[index + 1] = g;
+ data[index + 2] = b;
+ if ( maskRgn )
+ {
+ Point pt ;
+ pt.h = xx ;
+ pt.v = yy ;
+ if ( !PtInRgn( pt , maskRgn ) )
+ {
+ data[index ] = mask_r;
+ data[index + 1] = mask_g;
+ data[index + 2] = mask_b;
+ }
+ }
+ else
+ {
+ if (mask)
+ {
+ if (mask->PointMasked(xx,yy))
+ {
+ data[index ] = mask_r;
+ data[index + 1] = mask_g;
+ data[index + 2] = mask_b;
+ }
+ }
+ }
+ index += 3;
+ }
+ }
+ if (mask || maskRgn )
+ {
+ image.SetMaskColour( mask_r, mask_g, mask_b );
+ image.SetMask( true );
+ }
+
+ // Free resources
+ UnlockPixels(GetGWorldPixMap( tempPort ));
+ SetGWorld(origPort, origDevice);
+ if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
+ {
+ wxMacDestroyGWorld( tempPort ) ;
+ }
+ if ( maskRgn )
+ {
+ DisposeRgn( maskRgn ) ;
+ }
+
+ return image;
+}
+
+
+bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
+ const wxPalette *palette) const