include/cinder/cocoa/CinderCocoa.h
Go to the documentation of this file.
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<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 
00111 CGContextRef getWindowContext();
00112 
00113 #if defined( CINDER_MAC )
00114 
00117 Surface8u convertNsBitmapDataRep( const NSBitmapImageRep *rep, bool assumeOwnership = false );
00118 #endif
00119 
00121 std::string convertCfString( CFStringRef str );
00123 CFStringRef createCfString( const std::string &str );
00125 SafeCfString createSafeCfString( const std::string &str );
00127 std::string convertNsString( NSString *str );
00129 CFURLRef createCfUrl( const cinder::Url &url );
00130 
00132 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color );
00134 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color, bool ligate );
00135 
00137 CGColorRef createCgColor( const Color &color );
00139 CGColorRef createCgColor( const ColorA &color );
00140 
00142 CGRect createCgRect( const Area &area );
00144 Area CgRectToArea( const CGRect &rect );
00145 
00147 inline CGSize createCgSize( const Vec2i &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; }
00149 inline CGSize createCgSize( const Vec2f &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; }
00150 
00152 void convertCgPath( CGPathRef cgPath, Shape2d *resultShape, bool flipVertical = true );
00153 
00154 #if defined( CINDER_MAC )
00155 
00157 int getCvPixelFormatTypeFromSurfaceChannelOrder( const SurfaceChannelOrder &sco );
00158 #endif
00159 
00161 CFDataRef createCfDataRef( const cinder::Buffer &buffer );
00162 
00163 
00164 typedef std::shared_ptr<class ImageSourceCgImage> ImageSourceCgImageRef;
00165 
00166 class ImageSourceCgImage : public ImageSource {
00167   public:
00169     static ImageSourceCgImageRef    createRef( ::CGImageRef imageRef, ImageSource::Options options = ImageSource::Options() );
00170     virtual ~ImageSourceCgImage() {}
00171 
00172     virtual void    load( ImageTargetRef target );
00173 
00174   protected:
00176     ImageSourceCgImage( ::CGImageRef imageRef, ImageSource::Options options );
00177 
00178     bool                        mIsIndexed, mIs16BitPacked;
00179     Color8u                     mColorTable[256];
00180     std::shared_ptr<CGImage>    mImageRef;
00181 
00182     uint16_t                    m16BitPackedRedOffset, m16BitPackedGreenOffset, m16BitPackedBlueOffset;
00183 };
00184 
00185 ImageSourceCgImageRef createImageSource( ::CGImageRef imageRef, ImageSource::Options = ImageSource::Options() );
00186 
00187 
00188 typedef std::shared_ptr<class ImageTargetCgImage> ImageTargetCgImageRef;
00189 
00190 class ImageTargetCgImage : public ImageTarget {
00191   public:
00192     static ImageTargetCgImageRef createRef( ImageSourceRef imageSource, ImageTarget::Options options );
00193     ~ImageTargetCgImage();
00194 
00195     virtual void*   getRowPointer( int32_t row );
00196     virtual void    finalize();
00197 
00198     ::CGImageRef    getCgImage() const { return mImageRef; }
00199 
00200   protected:
00201     ImageTargetCgImage( ImageSourceRef imageSource, ImageTarget::Options options );
00202 
00203     ::CGImageRef        mImageRef;
00204     size_t              mBitsPerComponent, mBitsPerPixel, mRowBytes;
00205     ::CGBitmapInfo      mBitmapInfo;
00206     ::CFMutableDataRef  mDataRef;
00207     uint8_t             *mDataPtr;
00208 };
00209 
00211 ::CGImageRef createCgImage( ImageSourceRef imageSource, ImageTarget::Options = ImageTarget::Options() );
00212 
00213 
00214 } } // namespace cinder::cocoa
00215 
00216 namespace cinder {
00217 
00219 class SurfaceConstraintsCgBitmapContext : public cinder::SurfaceConstraints {
00220  public:
00221     virtual SurfaceChannelOrder getChannelOrder( bool alpha ) const;
00222     virtual int32_t             getRowBytes( int requestedWidth, const SurfaceChannelOrder &sco, int elementSize ) const;
00223 };
00225 
00226 } // namespace cinder