]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/core/bitmap.cpp
Revert "using #ifdef wxABORT_ON_CONFIG_ERROR not just #if as elsewhere"
[wxWidgets.git] / src / osx / core / bitmap.cpp
index f1f1821a3859233f11b83cdfe4b456e6c5c5fe13..8ee6127b11a1fb931b4302bd39ecfd34421df8a1 100644 (file)
@@ -4,7 +4,6 @@
 // Author:      Stefan Csomor
 // Modified by:
 // Created:     1998-01-01
-// RCS-ID:      $Id$
 // Copyright:   (c) Stefan Csomor
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
@@ -25,6 +24,8 @@
 
 #include "wx/rawbmp.h"
 
+#include "wx/filename.h"
+
 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
 
@@ -58,7 +59,8 @@ class WXDLLEXPORT wxBitmapRefData: public wxGDIRefData
 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);
 
@@ -111,7 +113,8 @@ public:
     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;
@@ -281,14 +284,20 @@ wxBitmapRefData::wxBitmapRefData(int w , int h , int d, double logicalscale)
     Create( w , h , d, logicalscale ) ;
 }
 
-wxBitmapRefData::wxBitmapRefData(CGImageRef image)
+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 )
     {
@@ -296,6 +305,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 ;
@@ -319,7 +329,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 ) ;
@@ -328,6 +338,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);
@@ -965,9 +1010,9 @@ 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
@@ -1033,14 +1078,27 @@ wxBitmap::wxBitmap(WX_NSImage image)
 
 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
@@ -1154,11 +1212,11 @@ bool wxBitmap::CreateScaled(int w, int h, int d, double 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() ;
 }
@@ -1178,10 +1236,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;
         }
@@ -1213,7 +1290,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") );
 
@@ -1223,7 +1300,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())
     {
@@ -1794,9 +1871,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<CFURLRef> imageURL;
     
-    wxCFRef<CFURLRef> imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL));
+    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 )
     {
@@ -1812,7 +1903,7 @@ bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap,
                                                        kCGRenderingIntentDefault);
         if ( image != NULL )
         {
-            bitmap->Create(image);
+            bitmap->Create(image,scale);
             CGImageRelease(image);
         }
     }