]> gitweb.factorcode.org Git - factor.git/blob - basis/core-graphics/core-graphics.factor
use radix literals
[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.data alien.destructors
4 alien.syntax accessors destructors fry kernel math math.bitwise
5 sequences libc colors images images.memory core-graphics.types
6 core-foundation.utilities 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 0x1f
19 CONSTANT: kCGBitmapFloatComponents 256
20
21 CONSTANT: kCGBitmapByteOrderMask 0x7000
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 CGContextDrawImage (
103    CGContextRef c,
104    CGRect rect,
105    CGImageRef image
106 ) ;
107
108 FUNCTION: size_t CGImageGetWidth (
109    CGImageRef image
110 ) ;
111
112 FUNCTION: size_t CGImageGetHeight (
113    CGImageRef image
114 ) ;
115
116 FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
117
118 CONSTANT: kCGLRendererGenericFloatID 0x00020400
119
120 FUNCTION: CGLError CGLSetParameter ( CGLContextObj ctx, CGLContextParameter pname, GLint* params ) ;
121
122 FUNCTION: CGDirectDisplayID CGMainDisplayID ( ) ;
123
124 FUNCTION: CGError CGDisplayHideCursor ( CGDirectDisplayID display ) ;
125 FUNCTION: CGError CGDisplayShowCursor ( CGDirectDisplayID display ) ;
126
127 FUNCTION: CGError CGDisplayMoveCursorToPoint ( CGDirectDisplayID display, CGPoint point ) ;
128
129 FUNCTION: CGError CGAssociateMouseAndMouseCursorPosition ( boolean_t connected ) ;
130
131 FUNCTION: CGError CGWarpMouseCursorPosition ( CGPoint newCursorPosition ) ;
132
133 FUNCTION: uint GetCurrentButtonState ( ) ;
134
135 <PRIVATE
136
137 : bitmap-flags ( -- n )
138     kCGImageAlphaPremultipliedFirst kCGBitmapByteOrder32Host bitor ;
139
140 : bitmap-color-space ( -- color-space )
141     CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease ;
142
143 : <CGBitmapContext> ( data dim -- context )
144     [ first2 8 ] [ first 4 * ] bi
145     bitmap-color-space bitmap-flags CGBitmapContextCreate
146     [ "CGBitmapContextCreate failed" throw ] unless* ;
147
148 PRIVATE>
149
150 : dummy-context ( -- context )
151     \ dummy-context [
152         [ 4 malloc { 1 1 } <CGBitmapContext> ] with-destructors
153     ] initialize-alien ;
154
155 : make-bitmap-image ( dim quot -- image )
156     '[ <CGBitmapContext> &CGContextRelease @ ] make-memory-bitmap
157     ARGB >>component-order
158     ubyte-components >>component-type
159     t >>premultiplied-alpha? ; inline