Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CinderCocoa.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Surface.h"
27 #include "cinder/Shape2d.h"
28 #include "cinder/Color.h"
29 #include "cinder/Buffer.h"
30 #include "cinder/ImageIo.h"
31 namespace cinder {
32  class Url;
33  class Font;
34 }
35 
36 #if defined( CINDER_COCOA_TOUCH )
37  #include <CoreGraphics/CoreGraphics.h>
38 #else
39  #include <ApplicationServices/ApplicationServices.h>
40 #endif
41 #include <CoreFoundation/CoreFoundation.h>
42 #if defined( __OBJC__ )
43  @class NSBitmapImageRep;
44  @class NSString;
45  @class NSData;
46 #else
47  class NSBitmapImageRep;
48  class NSString;
49  class NSData;
50 #endif
51 
52 namespace cinder { namespace cocoa {
53 
54 typedef std::shared_ptr<struct __CFString> SafeCfString;
55 
57 class SafeNsString {
58  public:
61  SafeNsString( NSString *str );
63  SafeNsString( const std::string &str );
64 
65  operator NSString* const() { if( mPtr ) return mPtr.get(); else return 0; }
66  operator std::string() const;
67 
68  private:
69  static void safeRelease( NSString *ptr );
70 
71  std::shared_ptr<NSString> mPtr;
72 };
73 
75 class SafeNsData {
76  public:
79  SafeNsData( const Buffer &buffer );
80 
81  operator NSData* const() { if( mPtr ) return mPtr.get(); else return 0; }
82 
83  private:
84  static void safeRelease( const NSData *ptr );
85 
86  std::shared_ptr<NSData> mPtr;
87  const Buffer mBuffer;
88 };
89 
92  public:
95 
96  private:
97  void *mPool;
98 };
99 
101 void safeCfRelease( const CFTypeRef cfRef );
102 
104 void safeCocoaRelease( void *nsObject );
105 
108 CGContextRef createCgBitmapContext( const Surface8u &surface );
109 
111 CGContextRef getWindowContext();
112 
113 #if defined( CINDER_MAC )
114 
117 Surface8u convertNsBitmapDataRep( const NSBitmapImageRep *rep, bool assumeOwnership = false );
118 #endif
119 
121 std::string convertCfString( CFStringRef str );
123 CFStringRef createCfString( const std::string &str );
127 std::string convertNsString( NSString *str );
129 CFURLRef createCfUrl( const cinder::Url &url );
130 
132 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color );
134 CFAttributedStringRef createCfAttributedString( const std::string &str, const cinder::Font &font, const ColorA &color, bool ligate );
135 
137 CGColorRef createCgColor( const Color &color );
139 CGColorRef createCgColor( const ColorA &color );
140 
142 CGRect createCgRect( const Area &area );
144 Area CgRectToArea( const CGRect &rect );
145 
147 inline CGSize createCgSize( const Vec2i &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; }
149 inline CGSize createCgSize( const Vec2f &s ) { CGSize result; result.width = s.x; result.height = s.y; return result; }
150 
152 void convertCgPath( CGPathRef cgPath, Shape2d *resultShape, bool flipVertical = true );
153 
154 #if defined( CINDER_MAC )
155 
157 int getCvPixelFormatTypeFromSurfaceChannelOrder( const SurfaceChannelOrder &sco );
158 #endif
159 
161 CFDataRef createCfDataRef( const cinder::Buffer &buffer );
162 
163 
164 typedef std::shared_ptr<class ImageSourceCgImage> ImageSourceCgImageRef;
165 
167  public:
170  virtual ~ImageSourceCgImage() {}
171 
172  virtual void load( ImageTargetRef target );
173 
174  protected:
176  ImageSourceCgImage( ::CGImageRef imageRef, ImageSource::Options options );
177 
180  std::shared_ptr<CGImage> mImageRef;
181 
183 };
184 
186 
187 
188 typedef std::shared_ptr<class ImageTargetCgImage> ImageTargetCgImageRef;
189 
191  public:
194 
195  virtual void* getRowPointer( int32_t row );
196  virtual void finalize();
197 
198  ::CGImageRef getCgImage() const { return mImageRef; }
199 
200  protected:
202 
206  ::CFMutableDataRef mDataRef;
207  uint8_t *mDataPtr;
208 };
209 
212 
213 
214 } } // namespace cinder::cocoa
215 
216 namespace cinder {
217 
219 class SurfaceConstraintsCgBitmapContext : public cinder::SurfaceConstraints {
220  public:
221  virtual SurfaceChannelOrder getChannelOrder( bool alpha ) const;
222  virtual int32_t getRowBytes( int requestedWidth, const SurfaceChannelOrder &sco, int elementSize ) const;
223 };
225 
226 } // namespace cinder
void safeCocoaRelease(void *nsObject)
Safely release a Cocoa object, testing for null before calling [nsObject release]. Designed to be used as the deleter of a shared_ptr.
Definition: CinderCocoa.mm:103
Base class for defining the properties of a Surface necessary to be interoperable with different APIs...
Definition: Surface.h:76
CGContextRef getWindowContext()
Returns the current CoreGraphics context for the active window. Requires the current Renderer to be a...
Definition: CinderCocoa.mm:141
CFDataRef createCfDataRef(const cinder::Buffer &buffer)
Creates a CFDataRef from a cinder::Buffer buffer. The result does not assume ownership of the data an...
Definition: CinderCocoa.mm:398
Color8u mColorTable[256]
Definition: CinderCocoa.h:179
uint16_t m16BitPackedBlueOffset
Definition: CinderCocoa.h:182
virtual void load(ImageTargetRef target)
Definition: CinderCocoa.mm:503
std::shared_ptr< class ImageTargetCgImage > ImageTargetCgImageRef
Definition: CinderCocoa.h:188
::CGImageRef createCgImage(ImageSourceRef imageSource, ImageTarget::Options=ImageTarget::Options())
Loads an ImageSource into a new CGImageRef. Release the result with ::CGImageRelease.
Definition: CinderCocoa.mm:626
ImageSourceCgImage(::CGImageRef imageRef, ImageSource::Options options)
Retains (and later releases) imageRef.
Definition: CinderCocoa.mm:410
An in-memory representation of an image. Implicitly shared object.
Definition: Surface.h:92
Definition: Area.h:37
uint8_t * mDataPtr
Definition: CinderCocoa.h:207
GLsizei const GLchar ** string
Definition: GLee.h:2427
std::string convertCfString(CFStringRef str)
Converts a CFStringRef into std::string with UTF8 encoding.
Definition: CinderCocoa.mm:174
Area CgRectToArea(const CGRect &rect)
Converts a CGRect to a cinder::Area.
Definition: CinderCocoa.mm:308
SafeCfString createSafeCfString(const std::string &str)
Converts a std::string into an exception-safe CFString pointer. Assumes UTF8 encoding. The deleter is set to free the string when appropriate.
Definition: CinderCocoa.mm:192
CGSize createCgSize(const Vec2i &s)
Creates a Cocoa CGSize from a cinder::Vec2i.
Definition: CinderCocoa.h:147
virtual void * getRowPointer(int32_t row)
Definition: CinderCocoa.mm:612
ImageSourceCgImageRef createImageSource(::CGImageRef imageRef, ImageSource::Options=ImageSource::Options())
Definition: CinderCocoa.mm:543
T x
Definition: Vector.h:71
Represents an exception-safe Cocoa NSData which behaves like a shared_ptr but can implicitly cast its...
Definition: CinderCocoa.h:75
GLenum target
Definition: GLee.h:13607
Definition: ImageIo.h:151
CFURLRef createCfUrl(const cinder::Url &url)
Converts a cinder::URL into a CFURLRef. User must call CFRelease() to free the result.
Definition: CinderCocoa.mm:206
std::shared_ptr< struct __CFString > SafeCfString
Definition: CinderCocoa.h:54
void flipVertical(SurfaceT< T > *surface)
Definition: Flip.cpp:30
std::shared_ptr< class ImageTarget > ImageTargetRef
Definition: ImageIo.h:42
CGContextRef createCgBitmapContext(const Surface8u &surface)
Creates a CGBitmapContext that represents a cinder::Surface8u. Users must call CGContextRelease() to ...
Definition: CinderCocoa.mm:109
Specifies the in-memory ordering of the channels of a Surface.
Definition: Surface.h:42
Represents an exception-safe NSAutoreleasePool. Replaces the global NSAutoreleasePool for its lifetim...
Definition: CinderCocoa.h:91
virtual void finalize()
Definition: CinderCocoa.mm:617
CFAttributedStringRef createCfAttributedString(const std::string &str, const cinder::Font &font, const ColorA &color)
Converts a std::string to a CFAttributedStringRef with attributes set for font and color...
Definition: CinderCocoa.mm:214
GLenum GLenum GLvoid * row
Definition: GLee.h:1089
Definition: Buffer.h:31
::CGBitmapInfo mBitmapInfo
Definition: CinderCocoa.h:205
::CFMutableDataRef mDataRef
Definition: CinderCocoa.h:206
Definition: ImageIo.h:159
static ImageTargetCgImageRef createRef(ImageSourceRef imageSource, ImageTarget::Options options)
Definition: CinderCocoa.mm:551
GLclampf GLclampf GLclampf alpha
Definition: GLee.h:951
Represents an instance of a font at a point size. Implicitly shared object.
Definition: Font.h:63
void safeCfRelease(const CFTypeRef cfRef)
Safely release a CoreFoundation object, testing for null before calling CFRelease. Designed to be used as the deleter of a shared_ptr.
Definition: CinderCocoa.mm:97
SafeNsString()
Definition: CinderCocoa.h:59
GLuint buffer
Definition: GLee.h:2065
::CGImageRef getCgImage() const
Definition: CinderCocoa.h:198
Definition: CinderCocoa.h:166
~ImageTargetCgImage()
Definition: CinderCocoa.mm:605
::CGImageRef mImageRef
Definition: CinderCocoa.h:203
bool mIs16BitPacked
Definition: CinderCocoa.h:178
size_t mBitsPerComponent
Definition: CinderCocoa.h:204
struct CGImage * CGImageRef
Definition: ImageSourceFileQuartz.h:30
CGColorRef createCgColor(const Color &color)
Converts a cinder::Color to CGColor. User must call CGColorRelease() to free the result.
Definition: CinderCocoa.mm:284
Optional parameters passed when creating an Image.
Definition: ImageIo.h:92
T y
Definition: Vector.h:71
SafeNsData()
Definition: CinderCocoa.h:77
size_t mRowBytes
Definition: CinderCocoa.h:204
static ImageSourceCgImageRef createRef(::CGImageRef imageRef, ImageSource::Options options=ImageSource::Options())
Retains (and later releases) imageRef.
Definition: CinderCocoa.mm:405
uint16_t m16BitPackedRedOffset
Definition: CinderCocoa.h:182
virtual ~ImageSourceCgImage()
Definition: CinderCocoa.h:170
Represents an exception-safe Cocoa NSString which behaves like a shared_ptr but can implicitly cast i...
Definition: CinderCocoa.h:57
std::shared_ptr< CGImage > mImageRef
Definition: CinderCocoa.h:180
~SafeNsAutoreleasePool()
Definition: CinderCocoa.mm:92
std::shared_ptr< class ImageSource > ImageSourceRef
Definition: Channel.h:33
size_t mBitsPerPixel
Definition: CinderCocoa.h:204
uint16_t m16BitPackedGreenOffset
Definition: CinderCocoa.h:182
bool mIsIndexed
Definition: CinderCocoa.h:178
void convertCgPath(CGPathRef cgPath, Shape2d *resultShape, bool flipVertical=true)
Converts a CGPathRef to a cinder::Shape2d. If flipVertical then the path will be flipped vertically...
Definition: CinderCocoa.mm:361
GLdouble s
Definition: GLee.h:1378
std::string convertNsString(NSString *str)
Converts a NSString into a std::string with UTF8 encoding.
Definition: CinderCocoa.mm:201
CFStringRef createCfString(const std::string &str)
Converts a std::string into a CFStringRef. Assumes UTF8 encoding. User must call CFRelease() to free ...
Definition: CinderCocoa.mm:186
std::shared_ptr< class ImageSourceCgImage > ImageSourceCgImageRef
Definition: CinderCocoa.h:164
Definition: Shape2d.h:34
ImageTargetCgImage(ImageSourceRef imageSource, ImageTarget::Options options)
Definition: CinderCocoa.mm:556
SafeNsAutoreleasePool()
Definition: CinderCocoa.mm:83
Definition: ImageIo.h:86
uint32_t CGBitmapInfo
Definition: ImageTargetFileQuartz.h:31
CGRect createCgRect(const Area &area)
Converts a cinder::Area to a CGRect.
Definition: CinderCocoa.mm:298
Definition: CinderCocoa.h:190
Definition: Url.h:31
GLuint color
Definition: GLee.h:3198