X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a1b806b98241ab649c169aaa1f134df85e80fb8b..f86190702bb433f139dc2c335e2c551755def81f:/src/osx/core/bitmap.cpp diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index 5b0b5fa63a..da236465dd 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -98,13 +98,10 @@ public: // rescaled to 16 x 16 bool HasNativeSize(); +#ifndef __WXOSX_IPHONE__ // caller should increase ref count if needed longer // than the bitmap exists IconRef GetIconRef(); - -#ifndef __WXOSX_IPHONE__ - // returns a Pict from the bitmap content - PicHandle GetPictHandle(); #endif CGContextRef GetBitmapContext() const; @@ -125,10 +122,10 @@ public: bool m_ok; mutable CGImageRef m_cgImageRef; - IconRef m_iconRef; #ifndef __WXOSX_IPHONE__ - PicHandle m_pictHandle; + IconRef m_iconRef; #endif + CGContextRef m_hBitmap; }; @@ -189,13 +186,6 @@ void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bi info->contentType = kControlContentCGImageRef ; info->u.imageRef = (CGImageRef) bmap->CreateCGImage() ; } - else - { -#ifndef __LP64__ - info->contentType = kControlContentPictHandle ; - info->u.picture = bmap->GetPictHandle() ; -#endif - } } } @@ -247,7 +237,6 @@ void wxBitmapRefData::Init() #ifndef __WXOSX_IPHONE__ m_iconRef = NULL ; - m_pictHandle = NULL ; #endif m_hBitmap = NULL ; @@ -306,7 +295,7 @@ bool wxBitmapRefData::Create(CGImageRef image) memset( data , 0 , size ) ; m_memBuf.UngetWriteBuf( size ) ; CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image); - if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipLast || alpha == kCGImageAlphaNoneSkipLast ) + if ( alpha == kCGImageAlphaNone || alpha == kCGImageAlphaNoneSkipFirst || alpha == kCGImageAlphaNoneSkipLast ) { m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaNoneSkipFirst ); } @@ -315,7 +304,7 @@ bool wxBitmapRefData::Create(CGImageRef image) m_hasAlpha = true; m_hBitmap = CGBitmapContextCreate((char*) data, m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), kCGImageAlphaPremultipliedFirst ); } - CGRect rect = {{0,0},{m_width,m_height}}; + CGRect rect = CGRectMake(0,0,m_width,m_height); CGContextDrawImage(m_hBitmap, rect, image); wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ; @@ -379,8 +368,8 @@ void *wxBitmapRefData::BeginRawAccess() wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ) ; wxASSERT( m_rawAccessCount == 0 ) ; #ifndef __WXOSX_IPHONE__ - wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL , - wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; + wxASSERT_MSG( m_iconRef == NULL , + wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; #endif ++m_rawAccessCount ; @@ -428,62 +417,53 @@ IconRef wxBitmapRefData::GetIconRef() OSType dataType = 0 ; OSType maskType = 0 ; + // since we don't have PICT conversion available, use + // the next larger standard icon size + // TODO: Use NSImage + if (sz <= 16) + sz = 16; + else if ( sz <= 32) + sz = 32; + else if ( sz <= 48) + sz = 48; + else if ( sz <= 128) + sz = 128; + else if ( sz <= 256) + sz = 256; + else if ( sz <= 512) + sz = 512; + else if ( sz <= 1024) + sz = 1024; + switch (sz) { - case 128: -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 - if ( UMAGetSystemVersion() >= 0x1050 ) - { - dataType = kIconServices128PixelDataARGB ; - } - else +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 + case 1024: + dataType = kIconServices1024PixelDataARGB; + break; #endif - { - dataType = kThumbnail32BitData ; - maskType = kThumbnail8BitMask ; - } + case 512: + dataType = kIconServices512PixelDataARGB; + break; + + case 256: + dataType = kIconServices256PixelDataARGB; + break; + + case 128: + dataType = kIconServices128PixelDataARGB ; 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 ; - } + dataType = kIconServices48PixelDataARGB ; 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 ; - } + dataType = kIconServices32PixelDataARGB ; 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 ; - } + dataType = kIconServices16PixelDataARGB ; break; default: @@ -492,8 +472,7 @@ IconRef wxBitmapRefData::GetIconRef() if ( dataType != 0 ) { -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 - if ( maskType == 0 && UMAGetSystemVersion() >= 0x1050 ) + if ( maskType == 0 ) { size_t datasize = sz * sz * 4 ; Handle data = NewHandle( datasize ) ; @@ -555,7 +534,6 @@ IconRef wxBitmapRefData::GetIconRef() DisposeHandle( data ); } else -#endif { // setup the header properly @@ -623,11 +601,6 @@ IconRef wxBitmapRefData::GetIconRef() DisposeHandle( maskdata ) ; } } - else - { - PicHandle pic = GetPictHandle() ; - SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ; - } // transform into IconRef // cleaner version existing from 10.3 upwards @@ -642,41 +615,6 @@ IconRef wxBitmapRefData::GetIconRef() 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 ; -} #endif CGImageRef wxBitmapRefData::CreateCGImage() const @@ -816,14 +754,6 @@ void wxBitmapRefData::Free() ReleaseIconRef( m_iconRef ) ; m_iconRef = NULL ; } - -#ifndef __LP64__ - if ( m_pictHandle ) - { - KillPicture( m_pictHandle ) ; - m_pictHandle = NULL ; - } -#endif #endif if ( m_hBitmap ) { @@ -960,9 +890,9 @@ wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits { if ( no_bits == 1 ) { - int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; - if ( the_width % (sizeof(unsigned char) * 8) ) - linesize += sizeof(unsigned char); + int linesize = the_width / 8; + if ( the_width % 8 ) + linesize++; unsigned char* linestart = (unsigned char*) bits ; unsigned char* destptr = (unsigned char*) BeginRawAccess() ; @@ -1009,6 +939,11 @@ 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& WXUNUSED(dc)) +{ + (void) Create(width, height); +} + wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) { LoadFile(filename, type); @@ -1075,6 +1010,17 @@ IconRef wxBitmap::CreateIconRef() const #if wxOSX_USE_COCOA +wxBitmap::wxBitmap(WX_NSImage image) +{ + (void)Create(image); +} + +bool wxBitmap::Create(WX_NSImage image) +{ + wxCFRef cgimage(wxOSXCreateCGImageFromNSImage(image)); + return Create(cgimage); +} + WX_NSImage wxBitmap::GetNSImage() const { wxCFRef< CGImageRef > cgimage(CreateCGImage()); @@ -1109,20 +1055,23 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const { unsigned char *sourcedata = (unsigned char*) GetRawAccess() ; unsigned char *destdata = (unsigned char*) ret.BeginRawAccess() ; - wxASSERT( (sourcedata != NULL) && (destdata != NULL) ) ; - - int sourcelinesize = GetBitmapData()->GetBytesPerRow() ; - int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ; - unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ; - unsigned char *dest = destdata ; + wxASSERT((sourcedata != NULL) && (destdata != NULL)); - for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) + if ( (sourcedata != NULL) && (destdata != NULL) ) { - memcpy( dest , source , destlinesize ) ; + int sourcelinesize = GetBitmapData()->GetBytesPerRow() ; + int destlinesize = ret.GetBitmapData()->GetBytesPerRow() ; + unsigned char *source = sourcedata + rect.x * 4 + rect.y * sourcelinesize ; + unsigned char *dest = destdata ; + + for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) + { + memcpy( dest , source , destlinesize ) ; + } } + ret.EndRawAccess() ; } - ret.EndRawAccess() ; if ( M_BITMAPDATA->m_bitmapMask ) { @@ -1137,15 +1086,18 @@ wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const unsigned char *destdata = (unsigned char * ) maskbuf.GetWriteBuf( maskbufsize ) ; wxASSERT( (source != NULL) && (destdata != NULL) ) ; - source += rect.x * kMaskBytesPerPixel + rect.y * sourcelinesize ; - unsigned char *dest = destdata ; - - for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) + if ( (source != NULL) && (destdata != NULL) ) { - memcpy( dest , source , destlinesize ) ; - } + source += rect.x * kMaskBytesPerPixel + rect.y * sourcelinesize ; + unsigned char *dest = destdata ; - maskbuf.UngetWriteBuf( maskbufsize ) ; + for (int yy = 0; yy < destheight; ++yy, source += sourcelinesize , dest += destlinesize) + { + memcpy( dest , source , destlinesize ) ; + } + + maskbuf.UngetWriteBuf( maskbufsize ) ; + } ret.SetMask( new wxMask( maskbuf , destwidth , destheight , rowBytes ) ) ; } else if ( HasAlpha() ) @@ -1166,7 +1118,6 @@ bool wxBitmap::Create(int w, int h, int d) return M_BITMAPDATA->IsOk() ; } - bool wxBitmap::Create(CGImageRef image) { UnRef(); @@ -1638,26 +1589,29 @@ bool wxMask::Create(const wxBitmap& bitmap) size_t size = m_bytesPerRow * m_height ; unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; wxASSERT( destdatabase != NULL ) ; - - memset( destdatabase , 0 , size ) ; - unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; - - for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow ) + + if ( destdatabase ) { - unsigned char *destdata = destdatabase ; - unsigned char r, g, b; + memset( destdatabase , 0 , size ) ; + unsigned char * srcdata = (unsigned char*) bitmap.GetRawAccess() ; - for ( int x = 0 ; x < m_width ; ++x ) + for ( int y = 0 ; y < m_height ; ++y , destdatabase += m_bytesPerRow ) { - srcdata++ ; - r = *srcdata++ ; - g = *srcdata++ ; - b = *srcdata++ ; + unsigned char *destdata = destdatabase ; + unsigned char r, g, b; - if ( ( r + g + b ) > 0x10 ) - *destdata++ = 0xFF ; - else - *destdata++ = 0x00 ; + for ( int x = 0 ; x < m_width ; ++x ) + { + srcdata++ ; + r = *srcdata++ ; + g = *srcdata++ ; + b = *srcdata++ ; + + if ( ( r + g + b ) > 0x10 ) + *destdata++ = 0xFF ; + else + *destdata++ = 0x00 ; + } } } @@ -1678,37 +1632,60 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) size_t size = m_bytesPerRow * m_height ; unsigned char * destdatabase = (unsigned char*) m_memBuf.GetWriteBuf( size ) ; wxASSERT( destdatabase != NULL ) ; - - memset( destdatabase , 0 , size ) ; - unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ; - size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow(); - - for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow) + if ( destdatabase != NULL) { - unsigned char *srcdata = srcdatabase ; - unsigned char *destdata = destdatabase ; - unsigned char r, g, b; + memset( destdatabase , 0 , size ) ; + unsigned char * srcdatabase = (unsigned char*) bitmap.GetRawAccess() ; + size_t sourceBytesRow = bitmap.GetBitmapData()->GetBytesPerRow(); - for ( int x = 0 ; x < m_width ; ++x ) + for ( int y = 0 ; y < m_height ; ++y , srcdatabase+= sourceBytesRow, destdatabase += m_bytesPerRow) { - srcdata++ ; - r = *srcdata++ ; - g = *srcdata++ ; - b = *srcdata++ ; + unsigned char *srcdata = srcdatabase ; + unsigned char *destdata = destdatabase ; + unsigned char r, g, b; - if ( colour == wxColour( r , g , b ) ) - *destdata++ = 0xFF ; - else - *destdata++ = 0x00 ; + for ( int x = 0 ; x < m_width ; ++x ) + { + srcdata++ ; + r = *srcdata++ ; + g = *srcdata++ ; + b = *srcdata++ ; + + if ( colour == wxColour( r , g , b ) ) + *destdata++ = 0xFF ; + else + *destdata++ = 0x00 ; + } } } - m_memBuf.UngetWriteBuf( size ) ; RealizeNative() ; return true; } +wxBitmap wxMask::GetBitmap() const +{ + wxBitmap bitmap(m_width, m_height, 1); + unsigned char* dst = static_cast(bitmap.BeginRawAccess()); + const int dst_stride = bitmap.GetBitmapData()->GetBytesPerRow(); + const unsigned char* src = static_cast(GetRawAccess()); + for (int j = 0; j < m_height; j++, src += m_bytesPerRow, dst += dst_stride) + { + unsigned char* d = dst; + for (int i = 0; i < m_width; i++) + { + const unsigned char byte = src[i]; + *d++ = 0xff; + *d++ = byte; + *d++ = byte; + *d++ = byte; + } + } + bitmap.EndRawAccess(); + return bitmap; +} + WXHBITMAP wxMask::GetHBITMAP() const { return m_maskBitmap ; @@ -1720,7 +1697,7 @@ WXHBITMAP wxMask::GetHBITMAP() const class WXDLLEXPORT wxBundleResourceHandler: public wxBitmapHandler { - DECLARE_ABSTRACT_CLASS(wxPNGResourceHandler) + DECLARE_ABSTRACT_CLASS(wxBundleResourceHandler) public: inline wxBundleResourceHandler() @@ -1753,7 +1730,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxPNGResourceHandler, wxBundleResourceHandler) class WXDLLEXPORT wxJPEGResourceHandler: public wxBundleResourceHandler { - DECLARE_DYNAMIC_CLASS(wxPNGResourceHandler) + DECLARE_DYNAMIC_CLASS(wxJPEGResourceHandler) public: inline wxJPEGResourceHandler() @@ -1800,65 +1777,20 @@ bool wxBundleResourceHandler::LoadFile(wxBitmap *bitmap, return false ; } -#if !defined( __LP64__ ) && !defined(__WXOSX_IPHONE__) - -class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler +/* static */ +wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size) { - DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler) + wxCFRef + provider(CGDataProviderCreateWithData(NULL, data, size, NULL) ); + wxCFRef + image(CGImageCreateWithPNGDataProvider(provider, NULL, true, + kCGRenderingIntentDefault)); -public: - inline wxPICTResourceHandler() - { - SetName(wxT("Macintosh Pict resource")); - SetExtension(wxEmptyString); - SetType(wxBITMAP_TYPE_PICT_RESOURCE); - }; - - virtual bool LoadFile(wxBitmap *bitmap, - const wxString& name, - wxBitmapType type, - int desiredWidth, - int desiredHeight); -}; - -IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler) - - -bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, - const wxString& name, - wxBitmapType WXUNUSED(type), - int WXUNUSED(desiredWidth), - int WXUNUSED(desiredHeight)) -{ -#if wxUSE_METAFILE - Str255 theName ; - wxMacStringToPascal( name , theName ) ; - - PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ; - if ( thePict ) - { - wxMetafile mf ; - - mf.SetPICT( thePict ) ; - bitmap->Create( mf.GetWidth() , mf.GetHeight() ) ; - wxMemoryDC dc ; - dc.SelectObject( *bitmap ) ; - mf.Play( &dc ) ; - dc.SelectObject( wxNullBitmap ) ; - - return true ; - } -#endif - - return false ; + return wxBitmap(image); } -#endif void wxBitmap::InitStandardHandlers() { -#if !defined( __LP64__ ) && !defined(__WXOSX_IPHONE__) - AddHandler( new wxPICTResourceHandler ) ; -#endif #if wxOSX_USE_COCOA_OR_CARBON AddHandler( new wxICONResourceHandler ) ; #endif