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: void* CGBitmapContextGetData ( CGContextRef c ) ;
104 CONSTANT: kCGLRendererGenericFloatID HEX: 00020400
106 FUNCTION: CGLError CGLSetParameter ( CGLContextObj ctx, CGLContextParameter pname, GLint* params ) ;
110 : bitmap-flags ( -- flags )
111 { kCGImageAlphaPremultipliedFirst kCGBitmapByteOrder32Host } flags ;
113 : bitmap-size ( dim -- n )
114 product "uint" heap-size * ;
116 : malloc-bitmap-data ( dim -- alien )
117 bitmap-size 1 calloc &free ;
119 : bitmap-color-space ( -- color-space )
120 CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease ;
122 : <CGBitmapContext> ( data dim -- context )
123 [ first2 8 ] [ first 4 * ] bi
124 bitmap-color-space bitmap-flags CGBitmapContextCreate
125 [ "CGBitmapContextCreate failed" throw ] unless* ;
127 : bitmap-data ( bitmap dim -- data )
128 [ CGBitmapContextGetData ] [ bitmap-size ] bi*
131 : <bitmap-image> ( bitmap dim -- image )
135 little-endian? ARGB BGRA ? >>component-order ;
139 : dummy-context ( -- context )
141 [ 4 malloc { 1 1 } <CGBitmapContext> ] with-destructors
144 : make-bitmap-image ( dim quot -- image )
146 [ [ [ malloc-bitmap-data ] keep <CGBitmapContext> &CGContextRelease ] keep ] dip
147 [ nip call ] [ drop [ bitmap-data ] keep <bitmap-image> ] 3bi
148 ] with-destructors ; inline