X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fb728ebb014744759434c2b570be0a9e899442ea..0bbe61b8c18a1795189f0cf73cc61c14a0fb846d:/src/mac/carbon/bitmap.cpp diff --git a/src/mac/carbon/bitmap.cpp b/src/mac/carbon/bitmap.cpp index 3304fdacec..19334a0f7c 100644 --- a/src/mac/carbon/bitmap.cpp +++ b/src/mac/carbon/bitmap.cpp @@ -51,58 +51,58 @@ public: wxBitmapRefData(int width , int height , int depth); wxBitmapRefData(); wxBitmapRefData(const wxBitmapRefData &tocopy); - + virtual ~wxBitmapRefData(); - + + virtual bool IsOk() const { return m_ok; } + void Free(); - bool Ok() const { return IsOk(); } - bool IsOk() const { return m_ok; } void SetOk( bool isOk) { m_ok = isOk; } - + void SetWidth( int width ) { m_width = width; } void SetHeight( int height ) { m_height = height; } void SetDepth( int depth ) { m_depth = depth; } - + int GetWidth() const { return m_width; } int GetHeight() const { return m_height; } int GetDepth() const { return m_depth; } - + void *GetRawAccess() const; void *BeginRawAccess(); void EndRawAccess(); - + bool HasAlpha() const { return m_hasAlpha; } void UseAlpha( bool useAlpha ); - + public: #if wxUSE_PALETTE wxPalette m_bitmapPalette; #endif // wxUSE_PALETTE - + wxMask * m_bitmapMask; // Optional mask CGImageRef CreateCGImage() const; - + // returns true if the bitmap has a size that // can be natively transferred into a true icon // if no is returned GetIconRef will still produce // an icon but it will be generated via a PICT and // rescaled to 16 x 16 bool HasNativeSize(); - + // caller should increase ref count if needed longer // than the bitmap exists IconRef GetIconRef(); - + // returns a Pict from the bitmap content PicHandle GetPictHandle(); - + CGContextRef GetBitmapContext() const; - + int GetBytesPerRow() const { return m_bytesPerRow; } private : bool Create(int width , int height , int depth); void Init(); - + int m_width; int m_height; int m_bytesPerRow; @@ -112,10 +112,10 @@ public: int m_rawAccessCount; bool m_ok; mutable CGImageRef m_cgImageRef; - + IconRef m_iconRef; PicHandle m_pictHandle; - + CGContextRef m_hBitmap; }; @@ -243,8 +243,8 @@ void wxBitmapRefData::Init() wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData &tocopy) { Init(); - Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth); - + Create(tocopy.m_width, tocopy.m_height, tocopy.m_depth); + if (tocopy.m_bitmapMask) m_bitmapMask = new wxMask(*tocopy.m_bitmapMask); else if (tocopy.m_hasAlpha) @@ -272,19 +272,21 @@ bool wxBitmapRefData::Create( int w , int h , int d ) m_width = wxMax(1, w); m_height = wxMax(1, h); m_depth = d ; + m_hBitmap = NULL ; m_bytesPerRow = GetBestBytesPerRow( w * 4 ) ; size_t size = m_bytesPerRow * h ; void* data = m_memBuf.GetWriteBuf( size ) ; - memset( data , 0 , size ) ; - m_memBuf.UngetWriteBuf( size ) ; - - m_hBitmap = NULL ; - 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 ); - + if ( data != NULL ) + { + memset( data , 0 , size ) ; + m_memBuf.UngetWriteBuf( size ) ; + + 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 ); + } /* data != NULL */ m_ok = ( m_hBitmap != NULL ) ; return m_ok ; @@ -306,13 +308,13 @@ void wxBitmapRefData::UseAlpha( bool use ) void *wxBitmapRefData::GetRawAccess() const { - wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; + wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ; return m_memBuf.GetData() ; } void *wxBitmapRefData::BeginRawAccess() { - wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ; + wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ) ; wxASSERT( m_rawAccessCount == 0 ) ; wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL , wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ; @@ -332,7 +334,7 @@ void *wxBitmapRefData::BeginRawAccess() void wxBitmapRefData::EndRawAccess() { - wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ; + wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ; wxASSERT( m_rawAccessCount == 1 ) ; --m_rawAccessCount ; @@ -487,30 +489,30 @@ IconRef wxBitmapRefData::GetIconRef() #endif { // setup the header properly - + Handle data = NULL ; Handle maskdata = NULL ; unsigned char * maskptr = NULL ; unsigned char * ptr = NULL ; size_t datasize, masksize ; - + datasize = sz * sz * 4 ; data = NewHandle( datasize ) ; HLock( data ) ; ptr = (unsigned char*) *data ; memset( ptr , 0, datasize ) ; - + masksize = sz * sz ; maskdata = NewHandle( masksize ) ; HLock( maskdata ) ; maskptr = (unsigned char*) *maskdata ; memset( maskptr , 0 , masksize ) ; - + bool hasAlpha = HasAlpha() ; wxMask *mask = m_bitmapMask ; unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ; unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ; - + for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 ) { unsigned char * source = sourcePtr; @@ -518,19 +520,19 @@ IconRef wxBitmapRefData::GetIconRef() unsigned char * dest = ptr + y * sz * 4 ; unsigned char * maskdest = maskptr + y * sz ; unsigned char a, r, g, b; - + for ( int x = 0 ; x < w ; ++x ) { a = *source ++ ; r = *source ++ ; g = *source ++ ; b = *source ++ ; - + *dest++ = 0 ; *dest++ = r ; *dest++ = g ; *dest++ = b ; - + if ( mask ) *maskdest++ = 0xFF - *masksource++ ; else if ( hasAlpha ) @@ -539,13 +541,13 @@ IconRef wxBitmapRefData::GetIconRef() *maskdest++ = 0xFF ; } } - + OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ; wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ; - + err = SetIconFamilyData( iconFamily, maskType , maskdata ) ; wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ; - + HUnlock( data ) ; HUnlock( maskdata ) ; DisposeHandle( data ) ; @@ -563,8 +565,9 @@ IconRef wxBitmapRefData::GetIconRef() HLock((Handle) iconFamily); OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &m_iconRef ); HUnlock((Handle) iconFamily); - wxASSERT_MSG( err == noErr , wxT("Error when constructing icon ref") ); DisposeHandle( (Handle) iconFamily ) ; + + wxCHECK_MSG( err == noErr, NULL, wxT("Error when constructing icon ref") ); } return m_iconRef ; @@ -634,7 +637,7 @@ CGImageRef wxBitmapRefData::CreateCGImage() const int h = m_height ; CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ; wxMemoryBuffer membuf; - + if ( m_bitmapMask ) { alphaInfo = kCGImageAlphaFirst ; @@ -663,10 +666,10 @@ CGImageRef wxBitmapRefData::CreateCGImage() const alphaInfo = kCGImageAlphaFirst ; #endif } - + membuf = m_memBuf; } - + CGDataProviderRef dataProvider = NULL ; if ( m_depth == 1 ) { @@ -685,10 +688,10 @@ CGImageRef wxBitmapRefData::CreateCGImage() const } } maskBuf.UngetWriteBuf( m_width * m_height ); - + dataProvider = wxMacCGDataProviderCreateWithMemoryBuffer( maskBuf ); - + image = ::CGImageMaskCreate( w, h, 8, 8, m_width , dataProvider, NULL, false ); } else @@ -843,7 +846,7 @@ bool wxBitmap::CopyFromIcon(const wxIcon& icon) *destination++ = ( (*source++) * a + 127 ) / 255; *destination++ = ( (*source++) * a + 127 ) / 255; *destination++ = ( (*source++) * a + 127 ) / 255; -#else +#else *destination++ = *source++ ; *destination++ = *source++ ; *destination++ = *source++ ; @@ -881,51 +884,56 @@ wxBitmap::~wxBitmap() wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) { - m_refData = new wxBitmapRefData( the_width , the_height , 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); + wxBitmapRefData* bitmapRefData; - unsigned char* linestart = (unsigned char*) bits ; - unsigned char* destptr = (unsigned char*) BeginRawAccess() ; + m_refData = bitmapRefData = new wxBitmapRefData( the_width , the_height , no_bits ) ; - for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) + if (bitmapRefData->IsOk()) + { + if ( no_bits == 1 ) { - unsigned char* destination = destptr; - int index, bit, mask; + int linesize = ( the_width / (sizeof(unsigned char) * 8)) ; + if ( the_width % (sizeof(unsigned char) * 8) ) + linesize += sizeof(unsigned char); + + unsigned char* linestart = (unsigned char*) bits ; + unsigned char* destptr = (unsigned char*) BeginRawAccess() ; - for ( int x = 0 ; x < the_width ; ++x ) + for ( int y = 0 ; y < the_height ; ++y , linestart += linesize, destptr += M_BITMAPDATA->GetBytesPerRow() ) { - index = x / 8 ; - bit = x % 8 ; - mask = 1 << bit ; + unsigned char* destination = destptr; + int index, bit, mask; - if ( linestart[index] & mask ) - { - *destination++ = 0xFF ; - *destination++ = 0 ; - *destination++ = 0 ; - *destination++ = 0 ; - } - else + for ( int x = 0 ; x < the_width ; ++x ) { - *destination++ = 0xFF ; - *destination++ = 0xFF ; - *destination++ = 0xFF ; - *destination++ = 0xFF ; + index = x / 8 ; + bit = x % 8 ; + mask = 1 << bit ; + + if ( linestart[index] & mask ) + { + *destination++ = 0xFF ; + *destination++ = 0 ; + *destination++ = 0 ; + *destination++ = 0 ; + } + else + { + *destination++ = 0xFF ; + *destination++ = 0xFF ; + *destination++ = 0xFF ; + *destination++ = 0xFF ; + } } } - } - EndRawAccess() ; - } - else - { - wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); - } + EndRawAccess() ; + } + else + { + wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented")); + } + } /* bitmapRefData->IsOk() */ } wxBitmap::wxBitmap(int w, int h, int d) @@ -943,12 +951,12 @@ wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type) LoadFile(filename, type); } -wxObjectRefData* wxBitmap::CreateRefData() const +wxGDIRefData* wxBitmap::CreateGDIRefData() const { return new wxBitmapRefData; } -wxObjectRefData* wxBitmap::CloneRefData(const wxObjectRefData* data) const +wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const { return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData *, data)); } @@ -984,7 +992,7 @@ CGImageRef wxBitmap::CreateCGImage() const IconRef wxBitmap::GetIconRef() const { wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ; - + return M_BITMAPDATA->GetIconRef() ; } @@ -1066,7 +1074,7 @@ bool wxBitmap::Create(int w, int h, int d) m_refData = new wxBitmapRefData( w , h , d ); - return M_BITMAPDATA->Ok() ; + return M_BITMAPDATA->IsOk() ; } bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) @@ -1122,68 +1130,73 @@ bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height wxBitmap::wxBitmap(const wxImage& image, int depth) { wxCHECK_RET( image.Ok(), wxT("invalid image") ); - + // width and height of the device-dependent bitmap int width = image.GetWidth(); int height = image.GetHeight(); + + wxBitmapRefData* bitmapRefData; - m_refData = new wxBitmapRefData( width , height , depth ) ; - - // Create picture - - bool hasAlpha = false ; - - if ( image.HasMask() ) - { - // takes precedence, don't mix with alpha info - } - else - { - hasAlpha = image.HasAlpha() ; - } - - if ( hasAlpha ) - UseAlpha() ; - - unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; - register unsigned char* data = image.GetData(); - if ( destinationstart != NULL && data != NULL ) + m_refData = bitmapRefData = new wxBitmapRefData( width , height , depth ) ; + + if ( bitmapRefData->IsOk()) { - const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; - for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) + // Create picture + + bool hasAlpha = false ; + + if ( image.HasMask() ) { - unsigned char * destination = destinationstart; - for (int x = 0; x < width; x++) + // takes precedence, don't mix with alpha info + } + else + { + hasAlpha = image.HasAlpha() ; + } + + if ( hasAlpha ) + UseAlpha() ; + + unsigned char* destinationstart = (unsigned char*) BeginRawAccess() ; + register unsigned char* data = image.GetData(); + if ( destinationstart != NULL && data != NULL ) + { + const unsigned char *alpha = hasAlpha ? image.GetAlpha() : NULL ; + for (int y = 0; y < height; destinationstart += M_BITMAPDATA->GetBytesPerRow(), y++) { - if ( hasAlpha ) + unsigned char * destination = destinationstart; + for (int x = 0; x < width; x++) { - const unsigned char a = *alpha++; - *destination++ = a ; - -#if wxMAC_USE_PREMULTIPLIED_ALPHA - *destination++ = ((*data++) * a + 127) / 255 ; - *destination++ = ((*data++) * a + 127) / 255 ; - *destination++ = ((*data++) * a + 127) / 255 ; -#else - *destination++ = *data++ ; - *destination++ = *data++ ; - *destination++ = *data++ ; -#endif - } - else - { - *destination++ = 0xFF ; - *destination++ = *data++ ; - *destination++ = *data++ ; - *destination++ = *data++ ; + if ( hasAlpha ) + { + const unsigned char a = *alpha++; + *destination++ = a ; + + #if wxMAC_USE_PREMULTIPLIED_ALPHA + *destination++ = ((*data++) * a + 127) / 255 ; + *destination++ = ((*data++) * a + 127) / 255 ; + *destination++ = ((*data++) * a + 127) / 255 ; + #else + *destination++ = *data++ ; + *destination++ = *data++ ; + *destination++ = *data++ ; + #endif + } + else + { + *destination++ = 0xFF ; + *destination++ = *data++ ; + *destination++ = *data++ ; + *destination++ = *data++ ; + } } } + + EndRawAccess() ; } - - EndRawAccess() ; - } - if ( image.HasMask() ) - SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; + if ( image.HasMask() ) + SetMask( new wxMask( *this , wxColour( image.GetMaskRed() , image.GetMaskGreen() , image.GetMaskBlue() ) ) ) ; + } /* bitmapRefData->IsOk() */ } wxImage wxBitmap::ConvertToImage() const @@ -1320,11 +1333,6 @@ bool wxBitmap::SaveFile( const wxString& filename, return success; } -bool wxBitmap::IsOk() const -{ - return (M_BITMAPDATA && M_BITMAPDATA->Ok()); -} - int wxBitmap::GetHeight() const { wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); @@ -1496,10 +1504,10 @@ void wxMask::RealizeNative() CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); // from MouseTracking sample : - // Ironically, due to a bug in CGImageCreateWithMask, you cannot use + // Ironically, due to a bug in CGImageCreateWithMask, you cannot use // CGColorSpaceCreateWithName(kCGColorSpaceGenericGray) at this point! - - m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace, + + m_maskBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, colorspace, kCGImageAlphaNone ); CGColorSpaceRelease( colorspace ); wxASSERT_MSG( m_maskBitmap , wxT("Unable to create CGBitmapContext context") ) ;