]> gitweb.factorcode.org Git - factor.git/blob - basis/core-graphics/core-graphics.factor
Use flags{ instead of flags all over the place
[factor.git] / basis / core-graphics / core-graphics.factor
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 images.memory core-graphics.types core-foundation.utilities
6 opengl.gl literals ;
7 IN: core-graphics
8
9 ! CGImageAlphaInfo
10 C-ENUM:
11 kCGImageAlphaNone
12 kCGImageAlphaPremultipliedLast
13 kCGImageAlphaPremultipliedFirst
14 kCGImageAlphaLast
15 kCGImageAlphaFirst
16 kCGImageAlphaNoneSkipLast
17 kCGImageAlphaNoneSkipFirst ;
18
19 : kCGBitmapAlphaInfoMask ( -- n ) HEX: 1f ; inline
20 : kCGBitmapFloatComponents ( -- n ) 1 8 shift ; inline
21
22 : kCGBitmapByteOrderMask ( -- n ) HEX: 7000 ; inline
23 : kCGBitmapByteOrderDefault ( -- n ) 0 12 shift ; inline
24 : kCGBitmapByteOrder16Little ( -- n ) 1 12 shift ; inline
25 : kCGBitmapByteOrder32Little ( -- n ) 2 12 shift ; inline
26 : kCGBitmapByteOrder16Big ( -- n ) 3 12 shift ; inline
27 : kCGBitmapByteOrder32Big ( -- n ) 4 12 shift ; inline
28
29 : kCGBitmapByteOrder16Host ( -- n )
30     little-endian?
31     kCGBitmapByteOrder16Little
32     kCGBitmapByteOrder16Big ? ; foldable
33
34 : kCGBitmapByteOrder32Host ( -- n )
35     little-endian?
36     kCGBitmapByteOrder32Little
37     kCGBitmapByteOrder32Big ? ; foldable
38
39 FUNCTION: CGColorRef CGColorCreateGenericRGB (
40    CGFloat red,
41    CGFloat green,
42    CGFloat blue,
43    CGFloat alpha
44 ) ;
45
46 : <CGColor> ( color -- CGColor )
47     >rgba-components CGColorCreateGenericRGB ;
48
49 M: color (>cf) <CGColor> ;
50
51 FUNCTION: CGColorSpaceRef CGColorSpaceCreateDeviceRGB ( ) ;
52
53 FUNCTION: CGContextRef CGBitmapContextCreate (
54    void* data,
55    size_t width,
56    size_t height,
57    size_t bitsPerComponent,
58    size_t bytesPerRow,
59    CGColorSpaceRef colorspace,
60    CGBitmapInfo bitmapInfo
61 ) ;
62
63 FUNCTION: void CGColorSpaceRelease ( CGColorSpaceRef ref ) ;
64
65 DESTRUCTOR: CGColorSpaceRelease
66
67 FUNCTION: void CGContextRelease ( CGContextRef ref ) ;
68
69 DESTRUCTOR: CGContextRelease
70
71 FUNCTION: void CGContextSetRGBStrokeColor (
72    CGContextRef c,
73    CGFloat red,
74    CGFloat green,
75    CGFloat blue,
76    CGFloat alpha
77 ) ;
78   
79 FUNCTION: void CGContextSetRGBFillColor (
80    CGContextRef c,
81    CGFloat red,
82    CGFloat green,
83    CGFloat blue,
84    CGFloat alpha
85 ) ;
86
87 FUNCTION: void CGContextSetTextPosition (
88    CGContextRef c,
89    CGFloat x,
90    CGFloat y
91 ) ;
92
93 FUNCTION: void CGContextFillRect (
94    CGContextRef c,
95    CGRect rect
96 ) ;
97
98 FUNCTION: void CGContextSetShouldSmoothFonts (
99    CGContextRef c,
100    bool shouldSmoothFonts
101 ) ;
102
103 FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
104
105 CONSTANT: kCGLRendererGenericFloatID HEX: 00020400
106
107 FUNCTION: CGLError CGLSetParameter ( CGLContextObj ctx, CGLContextParameter pname, GLint* params ) ;
108
109 FUNCTION: CGDirectDisplayID CGMainDisplayID ( ) ;
110
111 FUNCTION: CGError CGDisplayHideCursor ( CGDirectDisplayID display ) ;
112 FUNCTION: CGError CGDisplayShowCursor ( CGDirectDisplayID display ) ;
113
114 FUNCTION: CGError CGDisplayMoveCursorToPoint ( CGDirectDisplayID display, CGPoint point ) ;
115
116 FUNCTION: CGError CGAssociateMouseAndMouseCursorPosition ( boolean_t connected ) ;
117
118 FUNCTION: CGError CGWarpMouseCursorPosition ( CGPoint newCursorPosition ) ;
119
120 FUNCTION: uint GetCurrentButtonState ( ) ;
121
122 <PRIVATE
123
124 CONSTANT: bitmap-flags flags{ kCGImageAlphaPremultipliedFirst kCGBitmapByteOrder32Host }
125
126 : bitmap-color-space ( -- color-space )
127     CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease ;
128
129 : <CGBitmapContext> ( data dim -- context )
130     [ first2 8 ] [ first 4 * ] bi
131     bitmap-color-space bitmap-flags CGBitmapContextCreate
132     [ "CGBitmapContextCreate failed" throw ] unless* ;
133
134 PRIVATE>
135
136 : dummy-context ( -- context )
137     \ dummy-context [
138         [ 4 malloc { 1 1 } <CGBitmapContext> ] with-destructors
139     ] initialize-alien ;
140
141 : make-bitmap-image ( dim quot -- image )
142     '[ <CGBitmapContext> &CGContextRelease @ ] make-memory-bitmap
143     ARGB >>component-order
144     ubyte-components >>component-type ; inline