X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/31c1cbc7bc2cff4db5f1bf1c7765ceb23e2b3a59..6cef0db28018fd2644ee4e38af715872e5242459:/src/mac/carbon/bitmap.cpp?ds=sidebyside diff --git a/src/mac/carbon/bitmap.cpp b/src/mac/carbon/bitmap.cpp index 4f879b9dd2..7fd7536644 100644 --- a/src/mac/carbon/bitmap.cpp +++ b/src/mac/carbon/bitmap.cpp @@ -43,22 +43,95 @@ IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) // under Quartz then content is transformed into a CGImageRef representing the same data // which can be transferred to the GPU by the OS for fast rendering -#if wxMAC_USE_CORE_GRAPHICS - #define wxMAC_USE_PREMULTIPLIED_ALPHA 1 - static const int kBestByteAlignement = 16; - static const int kMaskBytesPerPixel = 1; -#else -#define wxMAC_USE_PREMULTIPLIED_ALPHA 0 - static const int kBestByteAlignement = 4; - static const int kMaskBytesPerPixel = 4; -#endif +class WXDLLEXPORT wxBitmapRefData: public wxGDIRefData +{ + friend class WXDLLIMPEXP_FWD_CORE wxIcon; + friend class WXDLLIMPEXP_FWD_CORE wxCursor; +public: + wxBitmapRefData(int width , int height , int depth); + wxBitmapRefData(); + wxBitmapRefData(const wxBitmapRefData &tocopy); + + virtual ~wxBitmapRefData(); + + virtual bool IsOk() const { return m_ok; } + + void Free(); + void SetOk( bool isOk) { m_ok = isOk; } + + void SetWidth( int width ) { m_width = width; } + void SetHeight( int height ) { m_height = height; } + void SetDepth( int depth ) { m_depth = depth; } + + int GetWidth() const { return m_width; } + int GetHeight() const { return m_height; } + int GetDepth() const { return m_depth; } + + void *GetRawAccess() const; + void *BeginRawAccess(); + void EndRawAccess(); + + bool HasAlpha() const { return m_hasAlpha; } + void UseAlpha( bool useAlpha ); + +public: +#if wxUSE_PALETTE + wxPalette m_bitmapPalette; +#endif // wxUSE_PALETTE + + wxMask * m_bitmapMask; // Optional mask + CGImageRef CreateCGImage() const; + + // returns true if the bitmap has a size that + // can be natively transferred into a true icon + // if no is returned GetIconRef will still produce + // an icon but it will be generated via a PICT and + // rescaled to 16 x 16 + bool HasNativeSize(); + + // caller should increase ref count if needed longer + // than the bitmap exists + IconRef GetIconRef(); + + // returns a Pict from the bitmap content + PicHandle GetPictHandle(); + + CGContextRef GetBitmapContext() const; + + int GetBytesPerRow() const { return m_bytesPerRow; } + private : + bool Create(int width , int height , int depth); + void Init(); + + int m_width; + int m_height; + int m_bytesPerRow; + int m_depth; + bool m_hasAlpha; + wxMemoryBuffer m_memBuf; + int m_rawAccessCount; + bool m_ok; + mutable CGImageRef m_cgImageRef; + + IconRef m_iconRef; + PicHandle m_pictHandle; + + CGContextRef m_hBitmap; +}; + + +#define wxMAC_USE_PREMULTIPLIED_ALPHA 1 +static const int kBestByteAlignement = 16; +static const int kMaskBytesPerPixel = 1; static int GetBestBytesPerRow( int rawBytes ) { return (((rawBytes)+kBestByteAlignement-1) & ~(kBestByteAlignement-1) ); } -#if wxUSE_BMPBUTTON +#if wxUSE_GUI + +// this is used for more controls than just the wxBitmap button, also for notebooks etc void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType ) { @@ -71,17 +144,7 @@ void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bi if ( forceType == 0 ) { - // NOTE : For testing Panther behaviour under higher - // Systems make this always be false - if ( UMAGetSystemVersion() >= 0x1040 ) - { - // as soon as it is supported, it's a better default - forceType = kControlContentCGImageRef; - } - else if ( bmap->HasNativeSize() ) - { - forceType = kControlContentIconRef; - } + forceType = kControlContentCGImageRef; } if ( forceType == kControlContentIconRef ) @@ -111,7 +174,7 @@ void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bi else if ( forceType == kControlContentCGImageRef ) { info->contentType = kControlContentCGImageRef ; - info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ; + info->u.imageRef = (CGImageRef) bmap->CreateCGImage() ; } else { @@ -128,7 +191,7 @@ CGImageRef wxMacCreateCGImageFromBitmap( const wxBitmap& bitmap ) wxBitmapRefData * bmap = bitmap.GetBitmapData() ; if ( bmap == NULL ) return NULL ; - return (CGImageRef) bmap->CGImageCreate(); + return (CGImageRef) bmap->CreateCGImage(); } void wxMacReleaseBitmapButton( ControlButtonContentInfo*info ) @@ -172,10 +235,6 @@ void wxBitmapRefData::Init() m_iconRef = NULL ; m_pictHandle = NULL ; m_hBitmap = NULL ; -#if! wxMAC_USE_CORE_GRAPHICS - m_hMaskBitmap = NULL; - m_maskBytesPerRow = 0 ; -#endif m_rawAccessCount = 0 ; m_hasAlpha = false; @@ -184,8 +243,8 @@ void wxBitmapRefData::Init() wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData &tocopy) { Init(); - Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth); - + Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth); + if (tocopy.m_bitmapMask) m_bitmapMask = new wxMask(*tocopy.m_bitmapMask); else if (tocopy.m_hasAlpha) @@ -213,25 +272,21 @@ bool wxBitmapRefData::Create( int w , int h , int d ) m_width = wxMax(1, w); m_height = wxMax(1, h); m_depth = d ; + m_hBitmap = NULL ; m_bytesPerRow = GetBestBytesPerRow( w * 4 ) ; size_t size = m_bytesPerRow * h ; void* data = m_memBuf.GetWriteBuf( size ) ; - memset( data , 0 , size ) ; - m_memBuf.UngetWriteBuf( size ) ; + if ( data != NULL ) + { + memset( data , 0 , size ) ; + m_memBuf.UngetWriteBuf( size ) ; - m_hBitmap = NULL ; -#if !wxMAC_USE_CORE_GRAPHICS - Rect rect = { 0 , 0 , m_height , m_width } ; - verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 , - (char*) data , m_bytesPerRow ) ) ; - wxASSERT_MSG( m_hBitmap , wxT("Unable to create GWorld context") ) ; -#else - m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); - wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; - CGContextTranslateCTM( m_hBitmap, 0, m_height ); - CGContextScaleCTM( m_hBitmap, 1, -1 ); -#endif + m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); + wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; + CGContextTranslateCTM( m_hBitmap, 0, m_height ); + CGContextScaleCTM( m_hBitmap, 1, -1 ); + } /* data != NULL */ m_ok = ( m_hBitmap != NULL ) ; return m_ok ; @@ -243,54 +298,23 @@ void wxBitmapRefData::UseAlpha( bool use ) return ; m_hasAlpha = use ; -#if wxMAC_USE_CORE_GRAPHICS + CGContextRelease( m_hBitmap ); m_hBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), m_hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst ); wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; CGContextTranslateCTM( m_hBitmap, 0, m_height ); CGContextScaleCTM( m_hBitmap, 1, -1 ); -#else - if ( m_hasAlpha ) - { - wxASSERT( m_hMaskBitmap == NULL ) ; - - int width = GetWidth() ; - int height = GetHeight() ; - m_maskBytesPerRow = GetBestBytesPerRow( width * kMaskBytesPerPixel ); - size_t size = height * m_maskBytesPerRow ; - unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ; - wxASSERT( data != NULL ) ; - - memset( data , 0 , size ) ; - Rect rect = { 0 , 0 , height , width } ; - - verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 , - (char*) data , m_maskBytesPerRow ) ) ; - wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ; - - m_maskMemBuf.UngetWriteBuf(size) ; - - - UpdateAlphaMask() ; - } - else - { - DisposeGWorld( m_hMaskBitmap ) ; - m_hMaskBitmap = NULL ; - m_maskBytesPerRow = 0 ; - } -#endif } void *wxBitmapRefData::GetRawAccess() const { - wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; + wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; return m_memBuf.GetData() ; } void *wxBitmapRefData::BeginRawAccess() { - wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ; + wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ) ; wxASSERT( m_rawAccessCount == 0 ) ; wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL , wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; @@ -310,14 +334,10 @@ void *wxBitmapRefData::BeginRawAccess() void wxBitmapRefData::EndRawAccess() { - wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ; + wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; wxASSERT( m_rawAccessCount == 1 ) ; --m_rawAccessCount ; - -#if !wxMAC_USE_CORE_GRAPHICS - UpdateAlphaMask() ; -#endif } bool wxBitmapRefData::HasNativeSize() @@ -335,18 +355,7 @@ IconRef wxBitmapRefData::GetIconRef() { // Create Icon Family Handle - IconFamilyHandle iconFamily = NULL ; - - if ( UMAGetSystemVersion() < 0x1040 ) - { - iconFamily = (IconFamilyHandle) NewHandle( 8 ) ; - (**iconFamily).resourceType = kIconFamilyType ; - (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size); - } - else - { - iconFamily = (IconFamilyHandle) NewHandle( 0 ) ; - } + IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle( 0 ); int w = GetWidth() ; int h = GetHeight() ; @@ -358,7 +367,7 @@ IconRef wxBitmapRefData::GetIconRef() switch (sz) { case 128: -#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if ( UMAGetSystemVersion() >= 0x1050 ) { dataType = kIconServices128PixelDataARGB ; @@ -372,7 +381,7 @@ IconRef wxBitmapRefData::GetIconRef() break; case 48: -#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if ( UMAGetSystemVersion() >= 0x1050 ) { dataType = kIconServices48PixelDataARGB ; @@ -386,7 +395,7 @@ IconRef wxBitmapRefData::GetIconRef() break; case 32: -#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if ( UMAGetSystemVersion() >= 0x1050 ) { dataType = kIconServices32PixelDataARGB ; @@ -400,7 +409,7 @@ IconRef wxBitmapRefData::GetIconRef() break; case 16: -#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if ( UMAGetSystemVersion() >= 0x1050 ) { dataType = kIconServices16PixelDataARGB ; @@ -419,7 +428,7 @@ IconRef wxBitmapRefData::GetIconRef() if ( dataType != 0 ) { -#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 if ( maskType == 0 && UMAGetSystemVersion() >= 0x1050 ) { size_t datasize = sz * sz * 4 ; @@ -480,30 +489,30 @@ IconRef wxBitmapRefData::GetIconRef() #endif { // setup the header properly - + Handle data = NULL ; Handle maskdata = NULL ; unsigned char * maskptr = NULL ; unsigned char * ptr = NULL ; size_t datasize, masksize ; - + datasize = sz * sz * 4 ; data = NewHandle( datasize ) ; HLock( data ) ; ptr = (unsigned char*) *data ; memset( ptr , 0, datasize ) ; - + masksize = sz * sz ; maskdata = NewHandle( masksize ) ; HLock( maskdata ) ; maskptr = (unsigned char*) *maskdata ; memset( maskptr , 0 , masksize ) ; - + bool hasAlpha = HasAlpha() ; wxMask *mask = m_bitmapMask ; unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ; unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; - + for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) { unsigned char * source = sourcePtr; @@ -511,41 +520,34 @@ IconRef wxBitmapRefData::GetIconRef() unsigned char * dest = ptr + y * sz * 4 ; unsigned char * maskdest = maskptr + y * sz ; unsigned char a, r, g, b; - + for ( int x = 0 ; x < w ; ++x ) { a = *source ++ ; r = *source ++ ; g = *source ++ ; b = *source ++ ; - + *dest++ = 0 ; *dest++ = r ; *dest++ = g ; *dest++ = b ; - + if ( mask ) - { *maskdest++ = 0xFF - *masksource++ ; -#if !wxMAC_USE_CORE_GRAPHICS - masksource++ ; - masksource++ ; - masksource++ ; -#endif - } else if ( hasAlpha ) *maskdest++ = a ; else *maskdest++ = 0xFF ; } } - + OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ; wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ; - + err = SetIconFamilyData( iconFamily, maskType , maskdata ) ; wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ; - + HUnlock( data ) ; HUnlock( maskdata ) ; DisposeHandle( data ) ; @@ -574,77 +576,6 @@ PicHandle wxBitmapRefData::GetPictHandle() { if ( m_pictHandle == NULL ) { -#if !wxMAC_USE_CORE_GRAPHICS - CGrafPtr origPort = NULL ; - GDHandle origDev = NULL ; - GWorldPtr wp = NULL ; - GWorldPtr mask = NULL ; - int height = GetHeight() ; - int width = GetWidth() ; - - Rect rect = { 0 , 0 , height , width } ; - RgnHandle clipRgn = NULL ; - - GetGWorld( &origPort , &origDev ) ; - wp = GetHBITMAP( &mask ) ; - - if ( mask ) - { - GWorldPtr monoworld ; - clipRgn = NewRgn() ; - OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ; - verify_noerr(err) ; - LockPixels( GetGWorldPixMap( monoworld ) ) ; - LockPixels( GetGWorldPixMap( mask ) ) ; - SetGWorld( monoworld , NULL ) ; - - RGBColor white = { 0xffff , 0xffff , 0xffff } ; - RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ; - RGBForeColor( &black ) ; - RGBBackColor( &white ) ; - - CopyBits(GetPortBitMapForCopyBits(mask), - GetPortBitMapForCopyBits(monoworld), - &rect, - &rect, - srcCopy, NULL); - BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ; - - UnlockPixels( GetGWorldPixMap( monoworld ) ) ; - UnlockPixels( GetGWorldPixMap( mask ) ) ; - DisposeGWorld( monoworld ) ; - } - - SetGWorld( wp , NULL ) ; - Rect portRect ; - GetPortBounds( wp , &portRect ) ; - m_pictHandle = OpenPicture(&portRect); - - if (m_pictHandle) - { - RGBColor white = { 0xffff , 0xffff , 0xffff } ; - RGBColor black = { 0x0000 , 0x0000 , 0x0000 } ; - - RGBForeColor( &black ) ; - RGBBackColor( &white ) ; - - if ( clipRgn ) - SetClip( clipRgn ) ; - - LockPixels( GetGWorldPixMap( wp ) ) ; - CopyBits(GetPortBitMapForCopyBits(wp), - GetPortBitMapForCopyBits(wp), - &portRect, - &portRect, - srcCopy,clipRgn); - UnlockPixels( GetGWorldPixMap( wp ) ) ; - ClosePicture(); - } - - SetGWorld( origPort , origDev ) ; - if ( clipRgn ) - DisposeRgn( clipRgn ) ; -#else #ifndef __LP64__ GraphicsExportComponent exporter = 0; OSStatus err = OpenADefaultComponent(GraphicsExporterComponentType, kQTFileTypePicture, &exporter); @@ -655,7 +586,7 @@ PicHandle wxBitmapRefData::GetPictHandle() { // QT does not correctly export the mask // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands - CGImageRef imageRef = CGImageCreate(); + CGImageRef imageRef = CreateCGImage(); err = GraphicsExportSetInputCGImage( exporter, imageRef ); err = GraphicsExportSetOutputHandle(exporter, (Handle)m_pictHandle); err = GraphicsExportDoExport(exporter, NULL); @@ -671,31 +602,20 @@ PicHandle wxBitmapRefData::GetPictHandle() } CloseComponent( exporter ); } -#endif #endif } return m_pictHandle ; } -void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t WXUNUSED(size)) -{ - wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ; - - wxASSERT( data == membuf->GetData() ) ; - - delete membuf ; -} - -CGImageRef wxBitmapRefData::CGImageCreate() const +CGImageRef wxBitmapRefData::CreateCGImage() const { wxASSERT( m_ok ) ; wxASSERT( m_rawAccessCount >= 0 ) ; CGImageRef image ; if ( m_rawAccessCount > 0 || m_cgImageRef == NULL ) { -#if wxMAC_USE_CORE_GRAPHICS - if ( UMAGetSystemVersion() >= 0x1040 && m_depth != 1 && m_bitmapMask == NULL ) + if ( m_depth != 1 && m_bitmapMask == NULL ) { if ( m_bitmapMask ) { @@ -709,23 +629,21 @@ CGImageRef wxBitmapRefData::CGImageCreate() const image = CGBitmapContextCreateImage( m_hBitmap ); } else -#endif { size_t imageSize = m_height * m_bytesPerRow ; void * dataBuffer = m_memBuf.GetData() ; int w = m_width ; int h = m_height ; CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; - wxMemoryBuffer* membuf = NULL ; - + wxMemoryBuffer membuf; + if ( m_bitmapMask ) { alphaInfo = kCGImageAlphaFirst ; - membuf = new wxMemoryBuffer( imageSize ) ; - memcpy( membuf->GetData() , dataBuffer , imageSize ) ; + unsigned char *destalphastart = (unsigned char*) membuf.GetWriteBuf( imageSize ) ; + memcpy( destalphastart , dataBuffer , imageSize ) ; unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ; int maskrowbytes = m_bitmapMask->GetBytesPerRow() ; - unsigned char *destalphastart = (unsigned char *) membuf->GetData() ; for ( int y = 0 ; y < h ; ++y , destalphastart += m_bytesPerRow, sourcemaskstart += maskrowbytes) { unsigned char *sourcemask = sourcemaskstart ; @@ -735,6 +653,7 @@ CGImageRef wxBitmapRefData::CGImageCreate() const *destalpha = 0xFF - *sourcemask ; } } + membuf.UngetWriteBuf( imageSize ); } else { @@ -746,36 +665,38 @@ CGImageRef wxBitmapRefData::CGImageCreate() const alphaInfo = kCGImageAlphaFirst ; #endif } - - membuf = new wxMemoryBuffer( m_memBuf ) ; + + membuf = m_memBuf; } - + CGDataProviderRef dataProvider = NULL ; if ( m_depth == 1 ) { - wxMemoryBuffer* maskBuf = new wxMemoryBuffer( m_width * m_height ); - unsigned char * maskBufData = (unsigned char *) maskBuf->GetData(); - unsigned char * bufData = (unsigned char *) membuf->GetData() ; + // TODO CHECK ALIGNMENT + wxMemoryBuffer maskBuf; + unsigned char * maskBufData = (unsigned char*) maskBuf.GetWriteBuf( m_width * m_height ); + unsigned char * bufData = (unsigned char *) membuf.GetData() ; // copy one color component - for( int i = 0 ; i < m_width * m_height ; ++i ) - maskBufData[i] = bufData[i*4+3]; + size_t i = 0; + for( int y = 0 ; y < m_height ; bufData+= m_bytesPerRow, ++y ) + { + unsigned char *bufDataIter = bufData+3; + for ( int x = 0 ; x < m_width ; bufDataIter += 4, ++x, ++i ) + { + maskBufData[i] = *bufDataIter; + } + } + maskBuf.UngetWriteBuf( m_width * m_height ); + dataProvider = - CGDataProviderCreateWithData( - maskBuf , (const void *) maskBufData , m_width * m_height, - wxMacMemoryBufferReleaseProc ); - // as we are now passing the mask buffer to the data provider, we have - // to release the membuf ourselves - delete membuf ; - + wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf ); + image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false ); } else { CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace(); - dataProvider = - CGDataProviderCreateWithData( - membuf , (const void *)membuf->GetData() , imageSize, - wxMacMemoryBufferReleaseProc ); + dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf ); image = ::CGImageCreate( w, h, 8 , 32 , m_bytesPerRow , colorSpace, alphaInfo , @@ -800,61 +721,10 @@ CGImageRef wxBitmapRefData::CGImageCreate() const return image ; } -#if wxMAC_USE_CORE_GRAPHICS CGContextRef wxBitmapRefData::GetBitmapContext() const { return m_hBitmap; } -#else -GWorldPtr wxBitmapRefData::GetHBITMAP(GWorldPtr* mask) const -{ - wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); - if ( mask ) - { - *mask = NULL ; - if ( m_bitmapMask ) - { - *mask = (GWorldPtr) m_bitmapMask->GetHBITMAP() ; - } - else if ( m_hasAlpha ) - { - if ( m_rawAccessCount > 0 ) - UpdateAlphaMask() ; - *mask = m_hMaskBitmap ; - } - } - - return m_hBitmap ; -} -#endif - -#if !wxMAC_USE_CORE_GRAPHICS -void wxBitmapRefData::UpdateAlphaMask() const -{ - if ( m_hasAlpha ) - { - unsigned char *sourcemask = (unsigned char *) GetRawAccess() ; - unsigned char *destalphabase = (unsigned char *) m_maskMemBuf.GetData() ; - - int h = GetHeight() ; - int w = GetWidth() ; - - for ( int y = 0 ; y < h ; ++y , destalphabase += m_maskBytesPerRow ) - { - unsigned char* destalpha = destalphabase ; - - for ( int x = 0 ; x < w ; ++x , sourcemask += 4 ) - { - // we must have 24 bit depth for non quartz smooth alpha - *destalpha++ = 255 ; - *destalpha++ = 255 - *sourcemask ; - *destalpha++ = 255 - *sourcemask ; - *destalpha++ = 255 - *sourcemask ; - } - } - } -} -#endif void wxBitmapRefData::Free() { @@ -882,21 +752,10 @@ void wxBitmapRefData::Free() if ( m_hBitmap ) { -#if !wxMAC_USE_CORE_GRAPHICS - DisposeGWorld( MAC_WXHBITMAP(m_hBitmap) ) ; -#else CGContextRelease(m_hBitmap); -#endif m_hBitmap = NULL ; } -#if !wxMAC_USE_CORE_GRAPHICS - if ( m_hMaskBitmap ) - { - DisposeGWorld( MAC_WXHBITMAP(m_hMaskBitmap) ) ; - m_hMaskBitmap = NULL ; - } -#endif if (m_bitmapMask) { delete m_bitmapMask; @@ -986,7 +845,7 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) *destination++ = ( (*source++) * a + 127 ) / 255; *destination++ = ( (*source++) * a + 127 ) / 255; *destination++ = ( (*source++) * a + 127 ) / 255; -#else +#else *destination++ = *source++ ; *destination++ = *source++ ; *destination++ = *source++ ; @@ -1024,51 +883,56 @@ wxBitmap::~wxBitmap() wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) { - m_refData = new wxBitmapRefData( the_width , the_height , no_bits ) ; + wxBitmapRefData* bitmapRefData; - if ( no_bits == 1 ) - { - int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; - if ( the_width % (sizeof(unsigned char) * 8) ) - linesize += sizeof(unsigned char); + m_refData = bitmapRefData = new wxBitmapRefData( the_width , the_height , no_bits ) ; - unsigned char* linestart = (unsigned char*) bits ; - unsigned char* destptr = (unsigned char*) BeginRawAccess() ; - - for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) + if (bitmapRefData->IsOk()) + { + if ( no_bits == 1 ) { - unsigned char* destination = destptr; - int index, bit, mask; + int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; + if ( the_width % (sizeof(unsigned char) * 8) ) + linesize += sizeof(unsigned char); - for ( int x = 0 ; x < the_width ; ++x ) + unsigned char* linestart = (unsigned char*) bits ; + unsigned char* destptr = (unsigned char*) BeginRawAccess() ; + + for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) { - index = x / 8 ; - bit = x % 8 ; - mask = 1 << bit ; + unsigned char* destination = destptr; + int index, bit, mask; - if ( linestart[index] & mask ) - { - *destination++ = 0xFF ; - *destination++ = 0 ; - *destination++ = 0 ; - *destination++ = 0 ; - } - else + for ( int x = 0 ; x < the_width ; ++x ) { - *destination++ = 0xFF ; - *destination++ = 0xFF ; - *destination++ = 0xFF ; - *destination++ = 0xFF ; + index = x / 8 ; + bit = x % 8 ; + mask = 1 << bit ; + + if ( linestart[index] & mask ) + { + *destination++ = 0xFF ; + *destination++ = 0 ; + *destination++ = 0 ; + *destination++ = 0 ; + } + else + { + *destination++ = 0xFF ; + *destination++ = 0xFF ; + *destination++ = 0xFF ; + *destination++ = 0xFF ; + } } } - } - EndRawAccess() ; - } - else - { - wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); - } + EndRawAccess() ; + } + else + { + wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); + } + } /* bitmapRefData->IsOk() */ } wxBitmap::wxBitmap(int w, int h, int d) @@ -1086,12 +950,12 @@ wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) LoadFile(filename, type); } -wxObjectRefData* wxBitmap::CreateRefData() const +wxGDIRefData* wxBitmap::CreateGDIRefData() const { return new wxBitmapRefData; } -wxObjectRefData* wxBitmap::CloneRefData(const wxObjectRefData* data) const +wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const { return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData *, data)); } @@ -1117,11 +981,25 @@ void wxBitmap::EndRawAccess() M_BITMAPDATA->EndRawAccess() ; } -WXCGIMAGEREF wxBitmap::CGImageCreate() const +CGImageRef wxBitmap::CreateCGImage() const +{ + wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; + + return M_BITMAPDATA->CreateCGImage() ; +} + +IconRef wxBitmap::GetIconRef() const { wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; - return M_BITMAPDATA->CGImageCreate() ; + return M_BITMAPDATA->GetIconRef() ; +} + +IconRef wxBitmap::CreateIconRef() const +{ + IconRef icon = GetIconRef(); + verify_noerr( AcquireIconRef(icon) ); + return icon; } wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const @@ -1195,7 +1073,7 @@ bool wxBitmap::Create(int w, int h, int d) m_refData = new wxBitmapRefData( w , h , d ); - return M_BITMAPDATA->Ok() ; + return M_BITMAPDATA->IsOk() ; } bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) @@ -1251,68 +1129,73 @@ bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height wxBitmap::wxBitmap(const wxImage& image, int depth) { wxCHECK_RET( image.Ok(), wxT("invalid image") ); - + // width and height of the device-dependent bitmap int width = image.GetWidth(); int height = image.GetHeight(); + + wxBitmapRefData* bitmapRefData; - m_refData = new wxBitmapRefData( width , height , depth ) ; - - // Create picture - - bool hasAlpha = false ; - - if ( image.HasMask() ) - { - // takes precedence, don't mix with alpha info - } - else - { - hasAlpha = image.HasAlpha() ; - } - - if ( hasAlpha ) - UseAlpha() ; - - unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; - register unsigned char* data = image.GetData(); - if ( destinationstart != NULL && data != NULL ) + m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ; + + if ( bitmapRefData->IsOk()) { - const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; - for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) + // Create picture + + bool hasAlpha = false ; + + if ( image.HasMask() ) { - unsigned char * destination = destinationstart; - for (int x = 0; x < width; x++) + // takes precedence, don't mix with alpha info + } + else + { + hasAlpha = image.HasAlpha() ; + } + + if ( hasAlpha ) + UseAlpha() ; + + unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; + register unsigned char* data = image.GetData(); + if ( destinationstart != NULL && data != NULL ) + { + const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; + for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) { - if ( hasAlpha ) - { - const unsigned char a = *alpha++; - *destination++ = a ; - -#if wxMAC_USE_PREMULTIPLIED_ALPHA - *destination++ = ((*data++) * a + 127) / 255 ; - *destination++ = ((*data++) * a + 127) / 255 ; - *destination++ = ((*data++) * a + 127) / 255 ; -#else - *destination++ = *data++ ; - *destination++ = *data++ ; - *destination++ = *data++ ; -#endif - } - else + unsigned char * destination = destinationstart; + for (int x = 0; x < width; x++) { - *destination++ = 0xFF ; - *destination++ = *data++ ; - *destination++ = *data++ ; - *destination++ = *data++ ; + if ( hasAlpha ) + { + const unsigned char a = *alpha++; + *destination++ = a ; + + #if wxMAC_USE_PREMULTIPLIED_ALPHA + *destination++ = ((*data++) * a + 127) / 255 ; + *destination++ = ((*data++) * a + 127) / 255 ; + *destination++ = ((*data++) * a + 127) / 255 ; + #else + *destination++ = *data++ ; + *destination++ = *data++ ; + *destination++ = *data++ ; + #endif + } + else + { + *destination++ = 0xFF ; + *destination++ = *data++ ; + *destination++ = *data++ ; + *destination++ = *data++ ; + } } } + + EndRawAccess() ; } - - EndRawAccess() ; - } - if ( image.HasMask() ) - SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; + if ( image.HasMask() ) + SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; + } /* bitmapRefData->IsOk() */ } wxImage wxBitmap::ConvertToImage() const @@ -1394,11 +1277,6 @@ wxImage wxBitmap::ConvertToImage() const } else if ( r == MASK_RED && g == MASK_GREEN && b == MASK_BLUE ) b = MASK_BLUE_REPLACEMENT ; -#if !wxMAC_USE_CORE_GRAPHICS - maskp++ ; - maskp++ ; - maskp++ ; -#endif } else if ( hasAlpha ) { @@ -1454,11 +1332,6 @@ bool wxBitmap::SaveFile( const wxString& filename, return success; } -bool wxBitmap::IsOk() const -{ - return (M_BITMAPDATA && M_BITMAPDATA->Ok()); -} - int wxBitmap::GetHeight() const { wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); @@ -1544,13 +1417,9 @@ void wxBitmap::SetMask(wxMask *mask) WXHBITMAP wxBitmap::GetHBITMAP(WXHBITMAP* mask) const { -#if !wxMAC_USE_CORE_GRAPHICS - return WXHBITMAP(M_BITMAPDATA->GetHBITMAP((GWorldPtr*)mask)); -#else wxUnusedVar(mask); return WXHBITMAP(M_BITMAPDATA->GetBitmapContext()); -#endif } // ---------------------------------------------------------------------------- @@ -1605,11 +1474,7 @@ wxMask::~wxMask() { if ( m_maskBitmap ) { -#if wxMAC_USE_CORE_GRAPHICS CGContextRelease( (CGContextRef) m_maskBitmap ); -#else - DisposeGWorld( (GWorldPtr)m_maskBitmap ) ; -#endif m_maskBitmap = NULL ; } } @@ -1632,32 +1497,19 @@ void wxMask::RealizeNative() { if ( m_maskBitmap ) { -#if wxMAC_USE_CORE_GRAPHICS CGContextRelease( (CGContextRef) m_maskBitmap ); -#else - DisposeGWorld( (GWorldPtr)m_maskBitmap ) ; -#endif m_maskBitmap = NULL ; } -#if wxMAC_USE_CORE_GRAPHICS CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); // from MouseTracking sample : - // Ironically, due to a bug in CGImageCreateWithMask, you cannot use + // Ironically, due to a bug in CGImageCreateWithMask, you cannot use // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point! - - m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace, + + m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace, kCGImageAlphaNone ); CGColorSpaceRelease( colorspace ); wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ; -#else - Rect rect = { 0 , 0 , m_height , m_width } ; - - OSStatus err = NewGWorldFromPtr( - (GWorldPtr*) &m_maskBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 , - (char*) m_memBuf.GetData() , m_bytesPerRow ) ; - verify_noerr( err ) ; -#endif } // Create a mask from a mono bitmap (copies the bitmap). @@ -1703,23 +1555,9 @@ bool wxMask::Create(const wxBitmap& bitmap) b = *srcdata++ ; if ( ( r + g + b ) > 0x10 ) - { *destdata++ = 0xFF ; -#if !wxMAC_USE_CORE_GRAPHICS - *destdata++ = 0xFF ; - *destdata++ = 0xFF ; - *destdata++ = 0xFF ; -#endif - } else - { - *destdata++ = 0x00 ; -#if !wxMAC_USE_CORE_GRAPHICS - *destdata++ = 0x00 ; *destdata++ = 0x00 ; - *destdata++ = 0x00 ; -#endif - } } } @@ -1759,23 +1597,9 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) b = *srcdata++ ; if ( colour == wxColour( r , g , b ) ) - { *destdata++ = 0xFF ; -#if !wxMAC_USE_CORE_GRAPHICS - *destdata++ = 0xFF ; - *destdata++ = 0xFF ; - *destdata++ = 0xFF ; -#endif - } else - { - *destdata++ = 0x00 ; -#if !wxMAC_USE_CORE_GRAPHICS - *destdata++ = 0x00 ; *destdata++ = 0x00 ; - *destdata++ = 0x00 ; -#endif - } } } @@ -1800,6 +1624,8 @@ IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase) // Standard Handlers // ---------------------------------------------------------------------------- +#ifndef __LP64__ + class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler { DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) @@ -1847,10 +1673,13 @@ bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, return false ; } +#endif void wxBitmap::InitStandardHandlers() { +#ifndef __LP64__ AddHandler( new wxPICTResourceHandler ) ; +#endif AddHandler( new wxICONResourceHandler ) ; }