/////////////////////////////////////////////////////////////////////////////// // ImageSourceFileQuartz ImageSourceFileQuartzRef ImageSourceFileQuartz::createFileQuartzRef( DataSourceRef dataSourceRef, ImageSource::Options options ) { std::shared_ptr<CGImageSource> sourceRef; std::shared_ptr<CGImage> imageRef; ::CFStringRef keys[1] = { kCGImageSourceShouldAllowFloat }; ::CFBooleanRef values[1] = { kCFBooleanTrue }; const std::shared_ptr<__CFDictionary> optionsDict( (__CFDictionary*)CFDictionaryCreate( kCFAllocatorDefault, (const void **)&keys, (const void **)&values, 1, NULL, NULL ), cocoa::safeCfRelease ); if( dataSourceRef->isFilePath() ) { ::CFStringRef pathString = cocoa::createCfString( dataSourceRef->getFilePath().string() ); ::CFURLRef urlRef = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, pathString, kCFURLPOSIXPathStyle, false ); sourceRef = std::shared_ptr<CGImageSource>( ::CGImageSourceCreateWithURL( urlRef, optionsDict.get() ), cocoa::safeCfRelease ); ::CFRelease( pathString ); ::CFRelease( urlRef ); } else if( dataSourceRef->isUrl() ) { ::CFURLRef urlRef = cocoa::createCfUrl( dataSourceRef->getUrl() ); if( ! urlRef ) throw ImageIoExceptionFailedLoad(); sourceRef = std::shared_ptr<CGImageSource>( ::CGImageSourceCreateWithURL( urlRef, optionsDict.get() ), cocoa::safeCfRelease ); ::CFRelease( urlRef ); } else { // last ditch, we'll use a dataref from the buffer ::CFDataRef dataRef = cocoa::createCfDataRef( dataSourceRef->getBuffer() ); if( ! dataRef ) throw ImageIoExceptionFailedLoad(); sourceRef = std::shared_ptr<CGImageSource>( ::CGImageSourceCreateWithData( dataRef, optionsDict.get() ), cocoa::safeCfRelease ); ::CFRelease( dataRef ); } if( sourceRef ) { imageRef = std::shared_ptr<CGImage>( ::CGImageSourceCreateImageAtIndex( sourceRef.get(), options.getIndex(), optionsDict.get() ), CGImageRelease ); if( ! imageRef ) throw ImageIoExceptionFailedLoad(); } else throw ImageIoExceptionFailedLoad(); const std::shared_ptr<__CFDictionary> imageProperties( (__CFDictionary*)::CGImageSourceCopyProperties( sourceRef.get(), NULL ), ::CFRelease ); const std::shared_ptr<__CFDictionary> imageIndexProperties( (__CFDictionary*)::CGImageSourceCopyPropertiesAtIndex( sourceRef.get(), options.getIndex(), NULL ), ::CFRelease ); return ImageSourceFileQuartzRef( new ImageSourceFileQuartz( imageRef.get(), options, imageProperties, imageIndexProperties ) ); }
/////////////////////////////////////////////////////////////////////////////// // ImageSourceFileQuartz ImageSourceFileQuartzRef ImageSourceFileQuartz::createFileQuartzRef( DataSourceRef dataSourceRef ) { ::CGImageSourceRef sourceRef = NULL; ::CGImageRef imageRef = NULL; ::CFStringRef keys[1] = { kCGImageSourceShouldAllowFloat }; ::CFBooleanRef values[1] = { kCFBooleanTrue }; ::CFDictionaryRef optionsDict = ::CFDictionaryCreate( kCFAllocatorDefault, (const void **)&keys, (const void **)&values, 1, NULL, NULL ); if( dataSourceRef->isFilePath() ) { ::CFStringRef pathString = cocoa::createCfString( dataSourceRef->getFilePath() ); ::CFURLRef urlRef = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, pathString, kCFURLPOSIXPathStyle, false ); sourceRef = ::CGImageSourceCreateWithURL( urlRef, optionsDict ); ::CFRelease( pathString ); ::CFRelease( urlRef ); } else if( dataSourceRef->isUrl() ) { ::CFURLRef urlRef = cocoa::createCfUrl( dataSourceRef->getUrl() ); if( ! urlRef ) throw ImageIoExceptionFailedLoad(); sourceRef = ::CGImageSourceCreateWithURL( urlRef, optionsDict ); ::CFRelease( urlRef ); } else { // last ditch, we'll use a dataref from the buffer ::CFDataRef dataRef = cocoa::createCfDataRef( dataSourceRef->getBuffer() ); if( ! dataRef ) throw ImageIoExceptionFailedLoad(); sourceRef = ::CGImageSourceCreateWithData( dataRef, optionsDict ); ::CFRelease( dataRef ); } if( sourceRef ) { imageRef = ::CGImageSourceCreateImageAtIndex( sourceRef, 0, optionsDict ); ::CFRelease( sourceRef ); if( ! imageRef ) throw ImageIoExceptionFailedLoad(); } else throw ImageIoExceptionFailedLoad(); return ImageSourceFileQuartzRef( new ImageSourceFileQuartz( imageRef ) ); }