+wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
+{
+ Init() ;
+ Create( w , h , d ) ;
+}
+
+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 ) ;
+ if ( data != NULL )
+ {
+ memset( data , 0 , size ) ;
+ m_memBuf.UngetWriteBuf( size ) ;
+
+ 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 ;
+}
+
+void wxBitmapRefData::UseAlpha( bool use )
+{
+ if ( m_hasAlpha == use )
+ return ;
+
+ m_hasAlpha = use ;
+
+ 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 );
+}
+
+void *wxBitmapRefData::GetRawAccess() const
+{
+ wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ;
+ return m_memBuf.GetData() ;
+}
+
+void *wxBitmapRefData::BeginRawAccess()
+{
+ 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") ) ;
+
+ ++m_rawAccessCount ;
+
+ // we must destroy an existing cached image, as
+ // the bitmap data may change now
+ if ( m_cgImageRef )
+ {
+ CGImageRelease( m_cgImageRef ) ;
+ m_cgImageRef = NULL ;
+ }
+
+ return m_memBuf.GetData() ;
+}
+
+void wxBitmapRefData::EndRawAccess()
+{
+ wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
+ wxASSERT( m_rawAccessCount == 1 ) ;
+
+ --m_rawAccessCount ;
+}
+
+bool wxBitmapRefData::HasNativeSize()
+{
+ int w = GetWidth() ;
+ int h = GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ return ( sz == 128 || sz == 48 || sz == 32 || sz == 16 );
+}
+
+IconRef wxBitmapRefData::GetIconRef()
+{
+ if ( m_iconRef == NULL )
+ {
+ // Create Icon Family Handle
+
+ IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle( 0 );
+
+ int w = GetWidth() ;
+ int h = GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ OSType dataType = 0 ;
+ OSType maskType = 0 ;
+
+ switch (sz)
+ {
+ case 128:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices128PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kThumbnail32BitData ;
+ maskType = kThumbnail8BitMask ;
+ }
+ break;
+
+ case 48:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices48PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kHuge32BitData ;
+ maskType = kHuge8BitMask ;
+ }
+ break;
+
+ case 32:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices32PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kLarge32BitData ;
+ maskType = kLarge8BitMask ;
+ }
+ break;
+
+ case 16:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices16PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kSmall32BitData ;
+ maskType = kSmall8BitMask ;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ if ( dataType != 0 )
+ {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( maskType == 0 && UMAGetSystemVersion() >= 0x1050 )
+ {
+ size_t datasize = sz * sz * 4 ;
+ Handle data = NewHandle( datasize ) ;
+ HLock( data ) ;
+ unsigned char* ptr = (unsigned char*) *data ;
+ memset( ptr, 0, datasize );
+ 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;
+ unsigned char * masksource = masksourcePtr;
+ unsigned char * dest = ptr + y * sz * 4 ;
+ unsigned char a, r, g, b;
+
+ for ( int x = 0 ; x < w ; ++x )
+ {
+ a = *source ++ ;
+ r = *source ++ ;
+ g = *source ++ ;
+ b = *source ++ ;
+
+ if ( mask )
+ {
+ a = 0xFF - *masksource++ ;
+ }
+ else if ( !hasAlpha )
+ a = 0xFF ;
+ else
+ {
+#if wxMAC_USE_PREMULTIPLIED_ALPHA
+ // this must be non-premultiplied data
+ if ( a != 0xFF && a!= 0 )
+ {
+ r = r * 255 / a;
+ g = g * 255 / a;
+ b = b * 255 / a;
+ }
+#endif
+ }
+ *dest++ = a ;
+ *dest++ = r ;
+ *dest++ = g ;
+ *dest++ = b ;
+
+ }
+ }
+ HUnlock( data );
+ OSStatus err = SetIconFamilyData( iconFamily, dataType , data );
+ wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") );
+ DisposeHandle( data );
+ }
+ else
+#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;
+ unsigned char * masksource = masksourcePtr;
+ 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++ ;
+ 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 ) ;
+ DisposeHandle( maskdata ) ;
+ }
+ }
+ else
+ {
+ PicHandle pic = GetPictHandle() ;
+ SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
+ }
+ // transform into IconRef
+
+ // cleaner version existing from 10.3 upwards
+ HLock((Handle) iconFamily);
+ OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &m_iconRef );
+ HUnlock((Handle) iconFamily);
+ wxASSERT_MSG( err == noErr , wxT("Error when constructing icon ref") );
+ DisposeHandle( (Handle) iconFamily ) ;
+ }
+
+ return m_iconRef ;
+}
+
+PicHandle wxBitmapRefData::GetPictHandle()
+{
+ if ( m_pictHandle == NULL )
+ {
+#ifndef __LP64__
+ GraphicsExportComponent exporter = 0;
+ OSStatus err = OpenADefaultComponent(GraphicsExporterComponentType, kQTFileTypePicture, &exporter);
+ if (noErr == err)
+ {
+ m_pictHandle = (PicHandle) NewHandle(0);
+ if ( m_pictHandle )
+ {
+ // 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 = CreateCGImage();
+ err = GraphicsExportSetInputCGImage( exporter, imageRef );
+ err = GraphicsExportSetOutputHandle(exporter, (Handle)m_pictHandle);
+ err = GraphicsExportDoExport(exporter, NULL);
+ CGImageRelease( imageRef );
+
+ size_t handleSize = GetHandleSize( (Handle) m_pictHandle );
+ // the 512 bytes header is only needed for pict files, but not in memory
+ if ( handleSize >= 512 )
+ {
+ memmove( *m_pictHandle , (char*)(*m_pictHandle)+512, handleSize - 512 );
+ SetHandleSize( (Handle) m_pictHandle, handleSize - 512 );
+ }
+ }
+ CloseComponent( exporter );
+ }
+#endif
+ }
+
+ return m_pictHandle ;
+}
+
+CGImageRef wxBitmapRefData::CreateCGImage() const
+{
+ wxASSERT( m_ok ) ;
+ wxASSERT( m_rawAccessCount >= 0 ) ;
+ CGImageRef image ;
+ if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
+ {
+ if ( m_depth != 1 && m_bitmapMask == NULL )
+ {
+ if ( m_bitmapMask )
+ {
+ CGImageRef tempImage = CGBitmapContextCreateImage( m_hBitmap );
+ CGImageRef tempMask = CGBitmapContextCreateImage((CGContextRef) m_bitmapMask->GetHBITMAP() );
+ image = CGImageCreateWithMask( tempImage, tempMask );
+ CGImageRelease(tempMask);
+ CGImageRelease(tempImage);
+ }
+ else
+ image = CGBitmapContextCreateImage( m_hBitmap );
+ }
+ else
+ {
+ 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;
+
+ if ( m_bitmapMask )
+ {
+ alphaInfo = kCGImageAlphaFirst ;
+ 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() ;
+ for ( int y = 0 ; y < h ; ++y , destalphastart += m_bytesPerRow, sourcemaskstart += maskrowbytes)
+ {
+ unsigned char *sourcemask = sourcemaskstart ;
+ unsigned char *destalpha = destalphastart ;
+ for ( int x = 0 ; x < w ; ++x , sourcemask += kMaskBytesPerPixel , destalpha += 4 )
+ {
+ *destalpha = 0xFF - *sourcemask ;
+ }
+ }
+ membuf.UngetWriteBuf( imageSize );
+ }
+ else
+ {
+ if ( m_hasAlpha )
+ {
+#if wxMAC_USE_PREMULTIPLIED_ALPHA
+ alphaInfo = kCGImageAlphaPremultipliedFirst ;
+#else
+ alphaInfo = kCGImageAlphaFirst ;
+#endif
+ }
+
+ membuf = m_memBuf;
+ }
+
+ CGDataProviderRef dataProvider = NULL ;
+ if ( m_depth == 1 )
+ {
+ // 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
+ 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 =
+ wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf );
+
+ image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false );
+ }
+ else
+ {
+ CGColorSpaceRef colorSpace = wxMacGetGenericRGBColorSpace();
+ dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( membuf );
+ image =
+ ::CGImageCreate(
+ w, h, 8 , 32 , m_bytesPerRow , colorSpace, alphaInfo ,
+ dataProvider, NULL , false , kCGRenderingIntentDefault );
+ }
+ CGDataProviderRelease( dataProvider);
+ }
+ }
+ else
+ {
+ image = m_cgImageRef ;
+ CGImageRetain( image ) ;
+ }
+
+ if ( m_rawAccessCount == 0 && m_cgImageRef == NULL)
+ {
+ // we keep it for later use
+ m_cgImageRef = image ;
+ CGImageRetain( image ) ;
+ }
+
+ return image ;
+}
+
+CGContextRef wxBitmapRefData::GetBitmapContext() const