wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth = 1);
// creates an bitmap from the native image format
- wxBitmap(CGImageRef image);
+ wxBitmap(CGImageRef image, double scale = 1.0);
wxBitmap(WX_NSImage image);
+ wxBitmap(CGContextRef bitmapcontext);
// Create a bitmap compatible with the given DC
wxBitmap(int width, int height, const wxDC& dc);
{ return Create(sz.GetWidth(), sz.GetHeight(), depth); }
virtual bool Create(const void* data, wxBitmapType type, int width, int height, int depth = 1);
- bool Create( CGImageRef image );
+ bool Create( CGImageRef image, double scale = 1.0 );
bool Create( WX_NSImage image );
+ bool Create( CGContextRef bitmapcontext);
// Create a bitmap compatible with the given DC, inheriting its magnification factor
bool Create(int width, int height, const wxDC& dc);
CGContextRef inContext,
const CGRect * inBounds,
CGImageRef inImage) ;
-WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image );
-CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage );
+WX_NSImage WXDLLIMPEXP_CORE wxOSXGetNSImageFromCGImage( CGImageRef image, double scale = 1.0 );
+CGImageRef WXDLLIMPEXP_CORE wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scale = NULL );
+CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage);
+
wxBitmap WXDLLIMPEXP_CORE wxOSXCreateSystemBitmap(const wxString& id, const wxString &client, const wxSize& size);
WXWindow WXDLLIMPEXP_CORE wxOSXGetMainWindow();
wxBitmap wxOSXCreateSystemBitmap(const wxString& name, const wxString &WXUNUSED(client), const wxSize& WXUNUSED(size))
{
wxCFStringRef cfname(name);
- wxCFRef<CGImageRef> image( wxOSXCreateCGImageFromNSImage([NSImage imageNamed:cfname.AsNSString()]) );
- return wxBitmap( image );
+ return wxBitmap( [NSImage imageNamed:cfname.AsNSString()] );
}
// From "Cocoa Drawing Guide:Working with Images"
-WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image )
+WX_NSImage wxOSXGetNSImageFromCGImage( CGImageRef image, double scaleFactor )
{
NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
// Get the image dimensions.
- imageRect.size.height = CGImageGetHeight(image);
- imageRect.size.width = CGImageGetWidth(image);
+ imageRect.size.height = CGImageGetHeight(image)/scaleFactor;
+ imageRect.size.width = CGImageGetWidth(image)/scaleFactor;
// Create a new image to receive the Quartz image data.
NSImage *newImage = [[NSImage alloc] initWithSize:imageRect.size];
return( newImage );
}
-CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage )
+CGContextRef WXDLLIMPEXP_CORE wxOSXCreateBitmapContextFromNSImage( WX_NSImage nsimage)
{
// based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
-
- CGImageRef image = NULL;
+
+ CGContextRef hbitmap = NULL;
if (nsimage != nil)
{
+ CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
+
NSSize imageSize = [nsimage size];
- CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst);
- NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
+
+ hbitmap = CGBitmapContextCreate(NULL, imageSize.width*scale, imageSize.height*scale, 8, 0, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst);
+ CGContextScaleCTM( hbitmap, scale, scale );
+
+ NSGraphicsContext *nsGraphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:hbitmap flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:nsGraphicsContext];
[[NSColor whiteColor] setFill];
NSRectFill(NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height));
[nsimage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext setCurrentContext:nsGraphicsContext];
+ }
+ return hbitmap;
+}
+
+CGImageRef wxOSXCreateCGImageFromNSImage( WX_NSImage nsimage, double *scaleptr )
+{
+ // based on http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg18065.html
+
+ CGImageRef image = NULL;
+ if (nsimage != nil)
+ {
+ CGContextRef context = wxOSXCreateBitmapContextFromNSImage(nsimage);
+ if ( scaleptr )
+ {
+ // determine content scale
+ CGRect userrect = CGRectMake(0, 0, 10, 10);
+ CGRect devicerect;
+ devicerect = CGContextConvertRectToDeviceSpace(context, userrect);
+ *scaleptr = devicerect.size.height / userrect.size.height;
+ }
image = CGBitmapContextCreateImage(context);
CFRelease(context);
}
public:
wxBitmapRefData(int width , int height , int depth, double logicalscale);
wxBitmapRefData(int width , int height , int depth);
- wxBitmapRefData(CGImageRef image);
+ wxBitmapRefData(CGContextRef context);
+ wxBitmapRefData(CGImageRef image, double scale);
wxBitmapRefData();
wxBitmapRefData(const wxBitmapRefData &tocopy);
private :
bool Create(int width , int height , int depth);
bool Create(int width , int height , int depth, double logicalScale);
- bool Create( CGImageRef image );
+ bool Create( CGImageRef image, double scale );
+ bool Create( CGContextRef bitmapcontext);
void Init();
int m_width;
Create( w , h , d, logicalscale ) ;
}
-wxBitmapRefData::wxBitmapRefData(CGImageRef image)
+wxBitmapRefData::wxBitmapRefData(CGContextRef context)
{
Init();
- Create( image );
+ Create( context );
+}
+
+wxBitmapRefData::wxBitmapRefData(CGImageRef image, double scale)
+{
+ Init();
+ Create( image, scale );
}
// code from Technical Q&A QA1509
-bool wxBitmapRefData::Create(CGImageRef image)
+bool wxBitmapRefData::Create(CGImageRef image, double scale)
{
if ( image != NULL )
{
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 ;
wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ;
CGContextTranslateCTM( m_hBitmap, 0, m_height );
- CGContextScaleCTM( m_hBitmap, 1, -1 );
+ CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor );
} /* data != NULL */
}
m_ok = ( m_hBitmap != NULL ) ;
}
+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 ;
+}
+
bool wxBitmapRefData::Create( int w , int h , int d )
{
m_width = wxMax(1, w);
LoadFile(filename, type);
}
-wxBitmap::wxBitmap(CGImageRef image)
+wxBitmap::wxBitmap(CGImageRef image, double scale)
{
- (void) Create(image);
+ (void) Create(image,scale);
}
wxGDIRefData* wxBitmap::CreateGDIRefData() const
bool wxBitmap::Create(WX_NSImage image)
{
- wxCFRef<CGImageRef> cgimage(wxOSXCreateCGImageFromNSImage(image));
- return Create(cgimage);
+ return Create(wxOSXCreateBitmapContextFromNSImage(image));
+}
+
+wxBitmap::wxBitmap(CGContextRef bitmapcontext)
+{
+ (void)Create(bitmapcontext);
+}
+
+bool wxBitmap::Create(CGContextRef bitmapcontext)
+{
+ UnRef();
+
+ m_refData = new wxBitmapRefData( bitmapcontext );
+
+ return M_BITMAPDATA->IsOk() ;
}
WX_NSImage wxBitmap::GetNSImage() const
{
wxCFRef< CGImageRef > cgimage(CreateCGImage());
- return wxOSXGetNSImageFromCGImage( cgimage );
+ return wxOSXGetNSImageFromCGImage( cgimage, GetScaleFactor() );
}
#endif
return M_BITMAPDATA->IsOk() ;
}
-bool wxBitmap::Create(CGImageRef image)
+bool wxBitmap::Create(CGImageRef image, double scale)
{
UnRef();
- m_refData = new wxBitmapRefData( image );
+ m_refData = new wxBitmapRefData( image, scale );
return M_BITMAPDATA->IsOk() ;
}