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 shared_ptr<const struct __CFString> SafeCfString; 00055 00057 class SafeNsString { 00058 public: 00059 SafeNsString() {} 00061 SafeNsString( const 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( const NSString *ptr ); 00070 00071 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 shared_ptr<NSData> mPtr; 00087 const Buffer mBuffer; 00088 }; 00089 00091 void safeCfRelease( const CFTypeRef cfRef ); 00092 00094 void safeCocoaRelease( void *nsObject ); 00095 00098 CGContextRef createCgBitmapContext( const Surface8u &surface ); 00099 00100 #if defined( CINDER_MAC ) 00101 00104 Surface8u convertNsBitmapDataRep( const NSBitmapImageRep *rep, bool assumeOwnership = false ); 00105 #endif 00106 00108 std::string convertCfString( CFStringRef str ); 00110 CFStringRef createCfString( const std::string &str ); 00112 SafeCfString createSafeCfString( const std::string &str ); 00114 std::string convertNsString( NSString *str ); 00116 CFURLRef createCfUrl( const cinder::Url &url ); 00117 00118 #if defined( CINDER_MAC ) 00119 00120 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color ); 00121 #endif 00122 00124 CGColorRef createCgColor( const Color &color ); 00126 CGColorRef createCgColor( const ColorA &color ); 00127 00129 CGRect createCgRect( const Area &area ); 00131 Area CgRectToArea( const CGRect &rect ); 00132 00134 inline CGSize createCgSize( const Vec2i &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; } 00136 inline CGSize createCgSize( const Vec2f &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; } 00137 00139 void convertCgPath( CGPathRef cgPath, Shape2d *resultShape, bool flipVertical = true ); 00140 00141 #if defined( CINDER_MAC ) 00142 00144 int getCvPixelFormatTypeFromSurfaceChannelOrder( const SurfaceChannelOrder &sco ); 00145 #endif 00146 00148 CFDataRef createCfDataRef( const cinder::Buffer &buffer ); 00149 00150 00151 typedef shared_ptr<class ImageSourceCgImage> ImageSourceCgImageRef; 00152 00153 class ImageSourceCgImage : public ImageSource { 00154 public: 00155 static ImageSourceCgImageRef createRef( ::CGImageRef imageRef ); 00156 ~ImageSourceCgImage(); 00157 00158 virtual void load( ImageTargetRef target ); 00159 00160 protected: 00161 ImageSourceCgImage( ::CGImageRef imageRef ); 00162 00163 ::CGImageRef mImageRef; 00164 }; 00165 00166 ImageSourceCgImageRef createImageSource( ::CGImageRef imageRef ); 00167 00168 00169 typedef shared_ptr<class ImageTargetCgImage> ImageTargetCgImageRef; 00170 00171 class ImageTargetCgImage : public ImageTarget { 00172 public: 00173 static ImageTargetCgImageRef createRef( ImageSourceRef imageSource ); 00174 ~ImageTargetCgImage(); 00175 00176 virtual void* getRowPointer( int32_t row ); 00177 virtual void finalize(); 00178 00179 ::CGImageRef getCgImage() const { return mImageRef; } 00180 00181 protected: 00182 ImageTargetCgImage( ImageSourceRef imageSource ); 00183 00184 ::CGImageRef mImageRef; 00185 size_t mBitsPerComponent, mBitsPerPixel, mRowBytes; 00186 ::CGBitmapInfo mBitmapInfo; 00187 uint8_t *mData; 00188 }; 00189 00191 ::CGImageRef createCgImage( ImageSourceRef imageSource ); 00192 00193 00194 } } // namespace cinder::cocoa 00195 00196 namespace cinder { 00197 00199 class SurfaceConstraintsCgBitmapContext : public cinder::SurfaceConstraints { 00200 public: 00201 virtual SurfaceChannelOrder getChannelOrder( bool alpha ) const; 00202 virtual int32_t getRowBytes( int requestedWidth, const SurfaceChannelOrder &sco, int elementSize ) const; 00203 }; 00205 00206 } // namespace cinder