osximage.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 277 行
CPP
277 行
//OSXImage.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/GraphicsKit/GraphicsKit.h"#include "vcf/GraphicsKit/GraphicsKitPrivate.h"using namespace VCF;OSXImage::OSXImage(): AbstractImage(true),//note: we allocate the memory for the ImageBits pixels grafPort_(0), imageRef_(0){ init();}OSXImage::OSXImage( const unsigned long& width, const unsigned long& height ): AbstractImage(true),//note: we allocate the memory for the ImageBits pixels grafPort_(0), imageRef_(0){ init(); setSize( width, height );}OSXImage::OSXImage( GraphicsContext* context, Rect* rect ): AbstractImage(true),//note: we allocate the memory for the ImageBits pixels grafPort_(0), imageRef_(0){ init(); setSize( (ulong32)rect->getWidth(), (ulong32)rect->getHeight() ); context_->copyContext( Rect(0,0,getWidth(),getHeight()), *rect, context );}OSXImage::OSXImage( CFURLRef url, const String& ext ): AbstractImage(true),//note: we allocate the memory for the ImageBits pixels grafPort_(0), imageRef_(0){ loadFromURL( url, ext );}OSXImage::~OSXImage(){ if ( NULL != grafPort_ ) { DisposeGWorld( grafPort_ ); grafPort_ = NULL; } if ( NULL != imageRef_ ) { CGImageRelease( imageRef_ ); imageRef_ = NULL; }}void OSXImage::init(){ ImageBits::Traits::setChannelType( flags_, ImageBits::Traits::getTraitsChannelType() ); ImageBits::Traits::setChannelSize( flags_, ImageBits::Traits::getTraitsChannelSize() ); ImageBits::Traits::setImageType( flags_, ImageBits::Traits::getTraitsImageType() ); ImageBits::Traits::setPixelLayoutOrder( flags_, Image::ploRGBA ); context_ = new GraphicsContext(0);}void OSXImage::setAlpha( float val ){ uchar alphaVal = (uchar) (val * 255.0); SysPixelType* bits = imageBits_->pixels_; uint32 size = getWidth() * getHeight(); while ( size > 0 ) { bits[size-1].a = alphaVal; size --; } }void OSXImage::createBMP(){ if ( NULL != grafPort_ ) { DisposeGWorld( grafPort_ ); grafPort_ = NULL; } if ( NULL != imageRef_ ) { CGImageRelease( imageRef_ ); imageRef_ = NULL; } GWorldPtr newGWorld = 0; int componentCount = getType(); int bitsPerPix = getChannelSize() * componentCount; int bytesPerRow = (getWidth() * (bitsPerPix/componentCount) * componentCount) / 8; ulong32 width = getWidth(); ulong32 height = getHeight(); ::Rect boundsRect; boundsRect.left = boundsRect.top = 0; boundsRect.right = width; boundsRect.bottom = height; OSStatus err = 0; err = NewGWorldFromPtr( &newGWorld, k32RGBAPixelFormat, &boundsRect, NULL, NULL, 0, (char*)imageBits_->pixels_, bytesPerRow ); //setAlpha( 0.67 ); if ( noErr == err ) { grafPort_ = newGWorld; ulong32 imgSize = width * height * componentCount; CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, imageBits_->pixels_, imgSize, NULL ); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); imageRef_ = CGImageCreate( width, height, getChannelSize(), bitsPerPix, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst, provider, NULL, FALSE, kCGRenderingIntentDefault ); CGColorSpaceRelease(colorSpace); CGDataProviderRelease(provider); //context_->getPeer()->setContextID( (ulong32)grafPort_ ); OSXContext* peerCtx = (OSXContext*)context_->getPeer(); peerCtx->setPortFromImage( grafPort_, width, height ); //CGContextScaleCTM(contextID_, 1, -1); } else { throw RuntimeException( MAKE_ERROR_MSG_2("OSXImage failed to create a new GWorld!") ); }}void OSXImage::setSize( const unsigned long & width, const unsigned long & height ){ AbstractImage::setSize( width, height ); createBMP();}void OSXImage::beginDrawing(){ }void OSXImage::finishedDrawing(){}void OSXImage::loadFromURL( CFURLRef url, const String& ext ){ CFRefObject<CFURLRef> urlRef(url); CFRefObject<CGDataProviderRef> provider = CGDataProviderCreateWithURL( url ); String tmp = StringUtils::lowerCase(ext); CFRefObject<CGImageRef> urlImage; if ( tmp == "png" ) { urlImage = CGImageCreateWithPNGDataProvider( provider, NULL, false, kCGRenderingIntentDefault ); } else if ( tmp == "jpg" || tmp == "jpeg" ) { urlImage = CGImageCreateWithJPEGDataProvider( provider, NULL, false, kCGRenderingIntentDefault ); } if ( NULL != urlImage ) { setSize( CGImageGetWidth(urlImage), CGImageGetHeight(urlImage) ); int componentCount = getType(); int bitsPerPix = getChannelSize() * componentCount; int bytesPerRow = (getWidth() * (bitsPerPix/componentCount) * componentCount) / 8; CFRefObject<CGColorSpaceRef> colorSpace = CGColorSpaceCreateDeviceRGB(); CFRefObject<CGContextRef> imgCtx = CGBitmapContextCreate( imageBits_->pixels_, getWidth(), getHeight(), bitsPerPix, bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst ); CGRect r; r.origin.x = 0; r.origin.x = 0; r.size.width = getWidth(); r.size.height = getHeight(); CGContextDrawImage( imgCtx, r, urlImage ); }}/***CVS Log info*$Log$*Revision 1.4 2006/04/07 02:35:41 ddiego*initial checkin of merge from 0.6.9 dev branch.**Revision 1.3.2.1 2005/11/10 02:02:39 ddiego*updated the osx build so that it*compiles again on xcode 1.5. this applies to the foundationkit and graphicskit.**Revision 1.3 2005/07/09 23:06:01 ddiego*added missing gtk files**Revision 1.2.4.1 2005/06/23 01:26:57 ddiego*build updates**Revision 1.2 2004/08/07 02:49:18 ddiego*merged in the devmain-0-6-5 branch to stable**Revision 1.1.2.6 2004/07/27 04:26:05 ddiego*updated devmain-0-6-5 branch with osx changes**Revision 1.1.2.5.2.1 2004/06/20 00:36:11 ddiego*finished the new theme API updates**Revision 1.1.2.5 2004/06/06 07:05:34 marcelloptr*changed macros, text reformatting, copyright sections**Revision 1.1.2.4 2004/05/31 13:20:59 ddiego*more osx updates**Revision 1.1.2.3 2004/04/30 05:44:34 ddiego*added OSX changes for unicode migration**Revision 1.2 2004/04/03 15:48:47 ddiego*Merged over code from the 0-6-3 branch.**Revision 1.1.2.2 2004/02/23 05:51:31 ddiego*basic GraphicsKit implementation done for OSX**Revision 1.1.2.1 2004/02/21 03:27:09 ddiego*updates for OSX porting**/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?