00001 /* 00002 Copyright (c) 2010, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 #pragma once 00024 00025 #include "cinder/Cinder.h" 00026 #include "cinder/Surface.h" 00027 #include "cinder/Shape2d.h" 00028 #include "cinder/Color.h" 00029 #include "cinder/Buffer.h" 00030 #include "cinder/ImageIo.h" 00031 namespace cinder { 00032 class Url; 00033 class Font; 00034 } 00035 00036 #if defined( CINDER_COCOA_TOUCH ) 00037 #include <CoreGraphics/CoreGraphics.h> 00038 #else 00039 #include <ApplicationServices/ApplicationServices.h> 00040 #endif 00041 #include <CoreFoundation/CoreFoundation.h> 00042 #if defined( __OBJC__ ) 00043 @class NSBitmapImageRep; 00044 @class NSString; 00045 @class NSData; 00046 #else 00047 class NSBitmapImageRep; 00048 class NSString; 00049 class NSData; 00050 #endif 00051 00052 namespace cinder { namespace cocoa { 00053 00054 typedef std::shared_ptr<const struct __CFString> SafeCfString; 00055 00057 class SafeNsString { 00058 public: 00059 SafeNsString() {} 00061 SafeNsString( NSString *str ); 00063 SafeNsString( const std::string &str ); 00064 00065 operator NSString* const() { if( mPtr ) return mPtr.get(); else return 0; } 00066 operator std::string() const; 00067 00068 private: 00069 static void safeRelease( NSString *ptr ); 00070 00071 std::shared_ptr<NSString> mPtr; 00072 }; 00073 00075 class SafeNsData { 00076 public: 00077 SafeNsData() {} 00079 SafeNsData( const Buffer &buffer ); 00080 00081 operator NSData* const() { if( mPtr ) return mPtr.get(); else return 0; } 00082 00083 private: 00084 static void safeRelease( const NSData *ptr ); 00085 00086 std::shared_ptr<NSData> mPtr; 00087 const Buffer mBuffer; 00088 }; 00089 00091 class SafeNsAutoreleasePool { 00092 public: 00093 SafeNsAutoreleasePool(); 00094 ~SafeNsAutoreleasePool(); 00095 00096 private: 00097 void *mPool; 00098 }; 00099 00101 void safeCfRelease( const CFTypeRef cfRef ); 00102 00104 void safeCocoaRelease( void *nsObject ); 00105 00108 CGContextRef createCgBitmapContext( const Surface8u &surface ); 00109 00110 #if defined( CINDER_MAC ) 00111 00114 Surface8u convertNsBitmapDataRep( const NSBitmapImageRep *rep, bool assumeOwnership = false ); 00115 #endif 00116 00118 std::string convertCfString( CFStringRef str ); 00120 CFStringRef createCfString( const std::string &str ); 00122 SafeCfString createSafeCfString( const std::string &str ); 00124 std::string convertNsString( NSString *str ); 00126 CFURLRef createCfUrl( const cinder::Url &url ); 00127 00129 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color ); 00131 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color, bool ligate ); 00132 00134 CGColorRef createCgColor( const Color &color ); 00136 CGColorRef createCgColor( const ColorA &color ); 00137 00139 CGRect createCgRect( const Area &area ); 00141 Area CgRectToArea( const CGRect &rect ); 00142 00144 inline CGSize createCgSize( const Vec2i &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; } 00146 inline CGSize createCgSize( const Vec2f &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; } 00147 00149 void convertCgPath( CGPathRef cgPath, Shape2d *resultShape, bool flipVertical = true ); 00150 00151 #if defined( CINDER_MAC ) 00152 00154 int getCvPixelFormatTypeFromSurfaceChannelOrder( const SurfaceChannelOrder &sco ); 00155 #endif 00156 00158 CFDataRef createCfDataRef( const cinder::Buffer &buffer ); 00159 00160 00161 typedef std::shared_ptr<class ImageSourceCgImage> ImageSourceCgImageRef; 00162 00163 class ImageSourceCgImage : public ImageSource { 00164 public: 00166 static ImageSourceCgImageRef createRef( ::CGImageRef imageRef, ImageSource::Options options = ImageSource::Options() ); 00167 virtual ~ImageSourceCgImage() {} 00168 00169 virtual void load( ImageTargetRef target ); 00170 00171 protected: 00173 ImageSourceCgImage( ::CGImageRef imageRef, ImageSource::Options options ); 00174 00175 bool mIsIndexed, mIs16BitPacked; 00176 Color8u mColorTable[256]; 00177 std::shared_ptr<CGImage> mImageRef; 00178 00179 uint16_t m16BitPackedRedOffset, m16BitPackedGreenOffset, m16BitPackedBlueOffset; 00180 }; 00181 00182 ImageSourceCgImageRef createImageSource( ::CGImageRef imageRef, ImageSource::Options = ImageSource::Options() ); 00183 00184 00185 typedef std::shared_ptr<class ImageTargetCgImage> ImageTargetCgImageRef; 00186 00187 class ImageTargetCgImage : public ImageTarget { 00188 public: 00189 static ImageTargetCgImageRef createRef( ImageSourceRef imageSource, ImageTarget::Options options ); 00190 ~ImageTargetCgImage(); 00191 00192 virtual void* getRowPointer( int32_t row ); 00193 virtual void finalize(); 00194 00195 ::CGImageRef getCgImage() const { return mImageRef; } 00196 00197 protected: 00198 ImageTargetCgImage( ImageSourceRef imageSource, ImageTarget::Options options ); 00199 00200 ::CGImageRef mImageRef; 00201 size_t mBitsPerComponent, mBitsPerPixel, mRowBytes; 00202 ::CGBitmapInfo mBitmapInfo; 00203 ::CFMutableDataRef mDataRef; 00204 uint8_t *mDataPtr; 00205 }; 00206 00208 ::CGImageRef createCgImage( ImageSourceRef imageSource, ImageTarget::Options = ImageTarget::Options() ); 00209 00210 00211 } } // namespace cinder::cocoa 00212 00213 namespace cinder { 00214 00216 class SurfaceConstraintsCgBitmapContext : public cinder::SurfaceConstraints { 00217 public: 00218 virtual SurfaceChannelOrder getChannelOrder( bool alpha ) const; 00219 virtual int32_t getRowBytes( int requestedWidth, const SurfaceChannelOrder &sco, int elementSize ) const; 00220 }; 00222 00223 } // namespace cinder