X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5ca21fe7bfda18e2da4e17eb529c4dfa0c824db2..54889dae199cb509b525b4c911185d16bef3ea2c:/src/osx/core/bitmap.cpp diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 1161256016..14e725b071 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -25,6 +25,8 @@ #include "wx/rawbmp.h" +#include "wx/filename.h" + IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject) IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) @@ -56,8 +58,10 @@ 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, 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); @@ -75,7 +79,7 @@ public: int GetWidth() const { return m_width; } int GetHeight() const { return m_height; } int GetDepth() const { return m_depth; } - + double GetScaleFactor() const { return m_scaleFactor; } void *GetRawAccess() const; void *BeginRawAccess(); void EndRawAccess(); @@ -109,7 +113,9 @@ public: int GetBytesPerRow() const { return m_bytesPerRow; } private : bool Create(int width , int height , int depth); - bool Create( CGImageRef image ); + bool Create(int width , int height , int depth, double logicalScale); + bool Create( CGImageRef image, double scale ); + bool Create( CGContextRef bitmapcontext); void Init(); int m_width; @@ -127,6 +133,7 @@ public: #endif CGContextRef m_hBitmap; + double m_scaleFactor; }; @@ -242,12 +249,13 @@ void wxBitmapRefData::Init() m_rawAccessCount = 0 ; m_hasAlpha = false; + m_scaleFactor = 1.0; } wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData &tocopy) : wxGDIRefData() { Init(); - Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth); + Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth, tocopy.m_scaleFactor); if (tocopy.m_bitmapMask) m_bitmapMask = new wxMask(*tocopy.m_bitmapMask); @@ -271,14 +279,26 @@ wxBitmapRefData::wxBitmapRefData( int w , int h , int d ) Create( w , h , d ) ; } -wxBitmapRefData::wxBitmapRefData(CGImageRef image) +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 ); + Create( image, scale ); } // code from Technical Q&A QA1509 -bool wxBitmapRefData::Create(CGImageRef image) +bool wxBitmapRefData::Create(CGImageRef image, double scale) { if ( image != NULL ) { @@ -286,6 +306,7 @@ bool wxBitmapRefData::Create(CGImageRef 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 ; @@ -309,7 +330,7 @@ bool wxBitmapRefData::Create(CGImageRef image) 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 ) ; @@ -318,6 +339,41 @@ bool wxBitmapRefData::Create(CGImageRef image) } +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); @@ -336,13 +392,19 @@ bool wxBitmapRefData::Create( int w , int h , int d ) 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 ); + CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor ); } /* data != NULL */ m_ok = ( m_hBitmap != NULL ) ; return m_ok ; } +bool wxBitmapRefData::Create( int w , int h , int d, double logicalScale ) +{ + m_scaleFactor = logicalScale; + return Create(w*logicalScale,h*logicalScale,d); +} + void wxBitmapRefData::UseAlpha( bool use ) { if ( m_hasAlpha == use ) @@ -354,7 +416,7 @@ void wxBitmapRefData::UseAlpha( bool use ) 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 ); + CGContextScaleCTM( m_hBitmap, 1*m_scaleFactor, -1*m_scaleFactor ); } void *wxBitmapRefData::GetRawAccess() const @@ -939,14 +1001,19 @@ wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, i (void) Create(data, type, width, height, depth); } +wxBitmap::wxBitmap(int width, int height, const wxDC& dc) +{ + (void)Create(width, height, dc); +} + wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) { LoadFile(filename, type); } -wxBitmap::wxBitmap(CGImageRef image) +wxBitmap::wxBitmap(CGImageRef image, double scale) { - (void) Create(image); + (void) Create(image,scale); } wxGDIRefData* wxBitmap::CreateGDIRefData() const @@ -1005,10 +1072,34 @@ IconRef wxBitmap::CreateIconRef() const #if wxOSX_USE_COCOA +wxBitmap::wxBitmap(WX_NSImage image) +{ + (void)Create(image); +} + +bool wxBitmap::Create(WX_NSImage image) +{ + 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 @@ -1030,11 +1121,13 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const (rect.y+rect.height <= GetHeight()), wxNullBitmap, wxT("invalid bitmap or bitmap region") ); - wxBitmap ret( rect.width, rect.height, GetDepth() ); + wxBitmap ret; + double scale = GetScaleFactor(); + ret.CreateScaled( rect.width, rect.height, GetDepth(), scale ); wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") ); - int destwidth = rect.width ; - int destheight = rect.height ; + int destwidth = rect.width*scale ; + int destheight = rect.height*scale ; { unsigned char *sourcedata = (unsigned char*) GetRawAccess() ; @@ -1045,7 +1138,7 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const { int sourcelinesize = GetBitmapData()->GetBytesPerRow() ; int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ; - unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ; + unsigned char *source = sourcedata + int(rect.x * scale * 4 + rect.y *scale * sourcelinesize) ; unsigned char *dest = destdata ; for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) @@ -1102,12 +1195,29 @@ bool wxBitmap::Create(int w, int h, int d) return M_BITMAPDATA->IsOk() ; } +bool wxBitmap::Create(int w, int h, const wxDC& dc) +{ + double factor = dc.GetContentScaleFactor(); + return CreateScaled(w,h,wxBITMAP_SCREEN_DEPTH, factor); +} + +bool wxBitmap::CreateScaled(int w, int h, int d, double logicalScale) +{ + UnRef(); + + if ( d < 0 ) + d = wxDisplayDepth() ; + + m_refData = new wxBitmapRefData( w , h , d, logicalScale ); + + 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() ; } @@ -1127,10 +1237,29 @@ bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) else { #if wxUSE_IMAGE - wxImage loadimage(filename, type); + double scale = 1.0; + wxString fname = filename; + + if ( type == wxBITMAP_TYPE_PNG ) + { + if ( wxOSXGetMainScreenContentScaleFactor() > 1.9 ) + { + wxFileName fn(filename); + fn.MakeAbsolute(); + fn.SetName(fn.GetName()+"@2x"); + + if ( fn.Exists() ) + { + fname = fn.GetFullPath(); + scale = 2.0; + } + } + } + + wxImage loadimage(fname, type); if (loadimage.IsOk()) { - *this = loadimage; + *this = wxBitmap(loadimage,-1,scale); return true; } @@ -1162,7 +1291,7 @@ bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height #if wxUSE_IMAGE -wxBitmap::wxBitmap(const wxImage& image, int depth) +wxBitmap::wxBitmap(const wxImage& image, int depth, double scale) { wxCHECK_RET( image.IsOk(), wxT("invalid image") ); @@ -1172,7 +1301,7 @@ wxBitmap::wxBitmap(const wxImage& image, int depth) wxBitmapRefData* bitmapRefData; - m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ; + m_refData = bitmapRefData = new wxBitmapRefData( width/scale, height/scale, depth, scale) ; if ( bitmapRefData->IsOk()) { @@ -1382,6 +1511,13 @@ int wxBitmap::GetWidth() const return M_BITMAPDATA->GetWidth() ; } +double wxBitmap::GetScaleFactor() const +{ + wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); + + return M_BITMAPDATA->GetScaleFactor() ; +} + int wxBitmap::GetDepth() const { wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") ); @@ -1736,9 +1872,23 @@ bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap, { wxString ext = GetExtension().Lower(); wxCFStringRef resname(name); + wxCFStringRef resname2x(name+"@2x"); wxCFStringRef restype(ext); + double scale = 1.0; - wxCFRef imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL)); + wxCFRef imageURL; + + if ( wxOSXGetMainScreenContentScaleFactor() > 1.9 ) + { + imageURL.reset(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname2x, restype, NULL)); + scale = 2.0; + } + + if ( imageURL.get() == NULL ) + { + imageURL.reset(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL)); + scale = 1.0; + } if ( imageURL.get() != NULL ) { @@ -1754,7 +1904,7 @@ bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap, kCGRenderingIntentDefault); if ( image != NULL ) { - bitmap->Create(image); + bitmap->Create(image,scale); CGImageRelease(image); } }