]> gitweb.factorcode.org Git - factor.git/blob - basis/core-graphics/core-graphics.factor
Remove ENUM: f and replace uses with CONSTANTs.
[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 TYPEDEF: int CGImageAlphaInfo
10 CONSTANT: kCGImageAlphaNone 0
11 CONSTANT: kCGImageAlphaPremultipliedLast 1
12 CONSTANT: kCGImageAlphaPremultipliedFirst 2
13 CONSTANT: kCGImageAlphaLast 3
14 CONSTANT: kCGImageAlphaFirst 4
15 CONSTANT: kCGImageAlphaNoneSkipLast 5
16 CONSTANT: kCGImageAlphaNoneSkipFirst 6
17
18 CONSTANT: kCGBitmapAlphaInfoMask HEX: 1f
19 CONSTANT: kCGBitmapFloatComponents 256
20
21 CONSTANT: kCGBitmapByteOrderMask HEX: 7000
22 CONSTANT: kCGBitmapByteOrderDefault 0
23 CONSTANT: kCGBitmapByteOrder16Little 4096
24 CONSTANT: kCGBitmapByteOrder32Little 8192
25 CONSTANT: kCGBitmapByteOrder16Big 12288
26 CONSTANT: kCGBitmapByteOrder32Big 16384
27
28 : kCGBitmapByteOrder16Host ( -- n )
29     little-endian?
30     kCGBitmapByteOrder16Little
31     kCGBitmapByteOrder16Big ? ; foldable
32
33 : kCGBitmapByteOrder32Host ( -- n )
34     little-endian?
35     kCGBitmapByteOrder32Little
36     kCGBitmapByteOrder32Big ? ; foldable
37
38 FUNCTION: CGColorRef CGColorCreateGenericRGB (
39    CGFloat red,
40    CGFloat green,
41    CGFloat blue,
42    CGFloat alpha
43 ) ;
44
45 : <CGColor> ( color -- CGColor )
46     >rgba-components CGColorCreateGenericRGB ;
47
48 M: color (>cf) <CGColor> ;
49
50 FUNCTION: CGColorSpaceRef CGColorSpaceCreateDeviceRGB ( ) ;
51
52 FUNCTION: CGContextRef CGBitmapContextCreate (
53    void* data,
54    size_t width,
55    size_t height,
56    size_t bitsPerComponent,
57    size_t bytesPerRow,
58    CGColorSpaceRef colorspace,
59    CGBitmapInfo bitmapInfo
60 ) ;
61
62 FUNCTION: void CGColorSpaceRelease ( CGColorSpaceRef ref ) ;
63
64 DESTRUCTOR: CGColorSpaceRelease
65
66 FUNCTION: void CGContextRelease ( CGContextRef ref ) ;
67
68 DESTRUCTOR: CGContextRelease
69
70 FUNCTION: void CGContextSetRGBStrokeColor (
71    CGContextRef c,
72    CGFloat red,
73    CGFloat green,
74    CGFloat blue,
75    CGFloat alpha
76 ) ;
77   
78 FUNCTION: void CGContextSetRGBFillColor (
79    CGContextRef c,
80    CGFloat red,
81    CGFloat green,
82    CGFloat blue,
83    CGFloat alpha
84 ) ;
85
86 FUNCTION: void CGContextSetTextPosition (
87    CGContextRef c,
88    CGFloat x,
89    CGFloat y
90 ) ;
91
92 FUNCTION: void CGContextFillRect (
93    CGContextRef c,
94    CGRect rect
95 ) ;
96
97 FUNCTION: void CGContextSetShouldSmoothFonts (
98    CGContextRef c,
99    bool shouldSmoothFonts
100 ) ;
101
102 FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
103
104 CONSTANT: kCGLRendererGenericFloatID HEX: 00020400
105
106 FUNCTION: CGLError CGLSetParameter ( CGLContextObj ctx, CGLContextParameter pname, GLint* params ) ;
107
108 FUNCTION: CGDirectDisplayID CGMainDisplayID ( ) ;
109
110 FUNCTION: CGError CGDisplayHideCursor ( CGDirectDisplayID display ) ;
111 FUNCTION: CGError CGDisplayShowCursor ( CGDirectDisplayID display ) ;
112
113 FUNCTION: CGError CGDisplayMoveCursorToPoint ( CGDirectDisplayID display, CGPoint point ) ;
114
115 FUNCTION: CGError CGAssociateMouseAndMouseCursorPosition ( boolean_t connected ) ;
116
117 FUNCTION: CGError CGWarpMouseCursorPosition ( CGPoint newCursorPosition ) ;
118
119 FUNCTION: uint GetCurrentButtonState ( ) ;
120
121 <PRIVATE
122
123 : bitmap-flags ( -- n )
124     kCGImageAlphaPremultipliedFirst kCGBitmapByteOrder32Host bitor ;
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