+wxBitmapRefData::wxBitmapRefData(int w , int h , int d, double logicalscale)
+{
+ Init() ;
+ Create( w , h , d, logicalscale ) ;
+}
+
+wxBitmapRefData::wxBitmapRefData(CGContextRef context)
+{
+ Init();
+ Create( context );
+}
+
+wxBitmapRefData::wxBitmapRefData(CGImageRef image, double scale)
+{
+ Init();
+ Create( image, scale );
+}
+// code from Technical Q&A QA1509
+
+bool wxBitmapRefData::Create(CGImageRef image, double scale)
+{
+ if ( image != NULL )
+ {
+ m_width = CGImageGetWidth(image);
+ m_height = CGImageGetHeight(image);
+ m_depth = 32;
+ m_hBitmap = NULL;
+ m_scaleFactor = scale;
+
+ m_bytesPerRow = GetBestBytesPerRow( m_width * 4 ) ;
+ size_t size = m_bytesPerRow * m_height ;
+ void* data = m_memBuf.GetWriteBuf( size ) ;
+ if ( data != NULL )
+ {
+ memset( data , 0 , size ) ;
+ m_memBuf.UngetWriteBuf( size ) ;
+ CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image);
+ if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast )
+ {
+ m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst );
+ }
+ else
+ {
+ m_hasAlpha = true;
+ m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst );
+ }
+ CGRect rect = CGRectMake(0,0,m_width,m_height);
+ CGContextDrawImage(m_hBitmap, rect, image);
+
+ wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ;
+ CGContextTranslateCTM( m_hBitmap, 0, m_height );
+ CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor );
+ } /* data != NULL */
+ }
+ m_ok = ( m_hBitmap != NULL ) ;
+
+ return m_ok ;
+
+}
+
+bool wxBitmapRefData::Create(CGContextRef context)
+{
+ if ( context != NULL && CGBitmapContextGetData(context) )
+ {
+ m_hBitmap = context;
+ m_bytesPerRow = CGBitmapContextGetBytesPerRow(context);
+ m_width = CGBitmapContextGetWidth(context);
+ m_height = CGBitmapContextGetHeight(context);
+ m_depth = CGBitmapContextGetBitsPerPixel(context) ;
+
+ // our own contexts conform to this, always.
+ wxASSERT( m_depth == 32 );
+
+ // determine content scale
+ CGRect userrect = CGRectMake(0, 0, 10, 10);
+ CGRect devicerect;
+ devicerect = CGContextConvertRectToDeviceSpace(context, userrect);
+ m_scaleFactor = devicerect.size.height / userrect.size.height;
+
+ CGImageAlphaInfo alpha = CGBitmapContextGetAlphaInfo(context);
+
+ if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast )
+ {
+ // no alpha
+ }
+ else
+ {
+ m_hasAlpha = true;
+ }
+ }
+ m_ok = ( m_hBitmap != NULL ) ;
+
+ return m_ok ;
+}
+