1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.destructors alien.syntax accessors
4 destructors fry kernel math math.bitwise sequences libc colors
5 images core-graphics.types core-foundation.utilities ;
11 kCGImageAlphaPremultipliedLast
12 kCGImageAlphaPremultipliedFirst
15 kCGImageAlphaNoneSkipLast
16 kCGImageAlphaNoneSkipFirst ;
18 : kCGBitmapAlphaInfoMask ( -- n ) HEX: 1f ; inline
19 : kCGBitmapFloatComponents ( -- n ) 1 8 shift ; inline
21 : kCGBitmapByteOrderMask ( -- n ) HEX: 7000 ; inline
22 : kCGBitmapByteOrderDefault ( -- n ) 0 12 shift ; inline
23 : kCGBitmapByteOrder16Little ( -- n ) 1 12 shift ; inline
24 : kCGBitmapByteOrder32Little ( -- n ) 2 12 shift ; inline
25 : kCGBitmapByteOrder16Big ( -- n ) 3 12 shift ; inline
26 : kCGBitmapByteOrder32Big ( -- n ) 4 12 shift ; inline
28 : kCGBitmapByteOrder16Host ( -- n )
30 kCGBitmapByteOrder16Little
31 kCGBitmapByteOrder16Big ? ; foldable
33 : kCGBitmapByteOrder32Host ( -- n )
35 kCGBitmapByteOrder32Little
36 kCGBitmapByteOrder32Big ? ; foldable
38 FUNCTION: CGColorRef CGColorCreateGenericRGB (
45 : <CGColor> ( color -- CGColor )
46 >rgba-components CGColorCreateGenericRGB ;
48 M: color (>cf) <CGColor> ;
50 FUNCTION: CGColorSpaceRef CGColorSpaceCreateDeviceRGB ( ) ;
52 FUNCTION: CGContextRef CGBitmapContextCreate (
56 size_t bitsPerComponent,
58 CGColorSpaceRef colorspace,
59 CGBitmapInfo bitmapInfo
62 FUNCTION: void CGColorSpaceRelease ( CGColorSpaceRef ref ) ;
64 DESTRUCTOR: CGColorSpaceRelease
66 FUNCTION: void CGContextRelease ( CGContextRef ref ) ;
68 DESTRUCTOR: CGContextRelease
70 FUNCTION: void CGContextSetRGBStrokeColor (
78 FUNCTION: void CGContextSetRGBFillColor (
86 FUNCTION: void CGContextSetTextPosition (
92 FUNCTION: void CGContextFillRect (
97 FUNCTION: void CGContextSetShouldSmoothFonts (
99 bool shouldSmoothFonts
102 FUNCTION: CGLError CGLSetParameter ( CGLContextObj ctx, CGLContextParameter pname, GLint* params ) ;
104 FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
108 : bitmap-flags ( -- flags )
109 { kCGImageAlphaPremultipliedFirst kCGBitmapByteOrder32Host } flags ;
111 : bitmap-size ( dim -- n )
112 product "uint" heap-size * ;
114 : malloc-bitmap-data ( dim -- alien )
115 bitmap-size 1 calloc &free ;
117 : bitmap-color-space ( -- color-space )
118 CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease ;
120 : <CGBitmapContext> ( data dim -- context )
121 [ first2 8 ] [ first 4 * ] bi
122 bitmap-color-space bitmap-flags CGBitmapContextCreate
123 [ "CGBitmapContextCreate failed" throw ] unless* ;
125 : bitmap-data ( bitmap dim -- data )
126 [ CGBitmapContextGetData ] [ bitmap-size ] bi*
129 : <bitmap-image> ( bitmap dim -- image )
133 little-endian? ARGB BGRA ? >>component-order ;
137 : dummy-context ( -- context )
139 [ 4 malloc { 1 1 } <CGBitmapContext> ] with-destructors
142 : make-bitmap-image ( dim quot -- image )
144 [ [ [ malloc-bitmap-data ] keep <CGBitmapContext> &CGContextRelease ] keep ] dip
145 [ nip call ] [ drop [ bitmap-data ] keep <bitmap-image> ] 3bi
146 ] with-destructors ; inline