142 lines
3.5 KiB
Factor
142 lines
3.5 KiB
Factor
! Copyright (C) 2009 Slava Pestov.
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
USING: alien.c-types alien.destructors alien.syntax accessors
|
|
destructors fry kernel math math.bitwise sequences libc colors
|
|
images core-graphics.types core-foundation.utilities ;
|
|
IN: core-graphics
|
|
|
|
! CGImageAlphaInfo
|
|
C-ENUM:
|
|
kCGImageAlphaNone
|
|
kCGImageAlphaPremultipliedLast
|
|
kCGImageAlphaPremultipliedFirst
|
|
kCGImageAlphaLast
|
|
kCGImageAlphaFirst
|
|
kCGImageAlphaNoneSkipLast
|
|
kCGImageAlphaNoneSkipFirst ;
|
|
|
|
: kCGBitmapAlphaInfoMask ( -- n ) HEX: 1f ; inline
|
|
: kCGBitmapFloatComponents ( -- n ) 1 8 shift ; inline
|
|
|
|
: kCGBitmapByteOrderMask ( -- n ) HEX: 7000 ; inline
|
|
: kCGBitmapByteOrderDefault ( -- n ) 0 12 shift ; inline
|
|
: kCGBitmapByteOrder16Little ( -- n ) 1 12 shift ; inline
|
|
: kCGBitmapByteOrder32Little ( -- n ) 2 12 shift ; inline
|
|
: kCGBitmapByteOrder16Big ( -- n ) 3 12 shift ; inline
|
|
: kCGBitmapByteOrder32Big ( -- n ) 4 12 shift ; inline
|
|
|
|
: kCGBitmapByteOrder16Host ( -- n )
|
|
little-endian?
|
|
kCGBitmapByteOrder16Little
|
|
kCGBitmapByteOrder16Big ? ; foldable
|
|
|
|
: kCGBitmapByteOrder32Host ( -- n )
|
|
little-endian?
|
|
kCGBitmapByteOrder32Little
|
|
kCGBitmapByteOrder32Big ? ; foldable
|
|
|
|
FUNCTION: CGColorRef CGColorCreateGenericRGB (
|
|
CGFloat red,
|
|
CGFloat green,
|
|
CGFloat blue,
|
|
CGFloat alpha
|
|
) ;
|
|
|
|
: <CGColor> ( color -- CGColor )
|
|
>rgba-components CGColorCreateGenericRGB ;
|
|
|
|
M: color (>cf) <CGColor> ;
|
|
|
|
FUNCTION: CGColorSpaceRef CGColorSpaceCreateDeviceRGB ( ) ;
|
|
|
|
FUNCTION: CGContextRef CGBitmapContextCreate (
|
|
void* data,
|
|
size_t width,
|
|
size_t height,
|
|
size_t bitsPerComponent,
|
|
size_t bytesPerRow,
|
|
CGColorSpaceRef colorspace,
|
|
CGBitmapInfo bitmapInfo
|
|
) ;
|
|
|
|
FUNCTION: void CGColorSpaceRelease ( CGColorSpaceRef ref ) ;
|
|
|
|
DESTRUCTOR: CGColorSpaceRelease
|
|
|
|
FUNCTION: void CGContextRelease ( CGContextRef ref ) ;
|
|
|
|
DESTRUCTOR: CGContextRelease
|
|
|
|
FUNCTION: void CGContextSetRGBStrokeColor (
|
|
CGContextRef c,
|
|
CGFloat red,
|
|
CGFloat green,
|
|
CGFloat blue,
|
|
CGFloat alpha
|
|
) ;
|
|
|
|
FUNCTION: void CGContextSetRGBFillColor (
|
|
CGContextRef c,
|
|
CGFloat red,
|
|
CGFloat green,
|
|
CGFloat blue,
|
|
CGFloat alpha
|
|
) ;
|
|
|
|
FUNCTION: void CGContextSetTextPosition (
|
|
CGContextRef c,
|
|
CGFloat x,
|
|
CGFloat y
|
|
) ;
|
|
|
|
FUNCTION: void CGContextFillRect (
|
|
CGContextRef c,
|
|
CGRect rect
|
|
) ;
|
|
|
|
FUNCTION: void CGContextSetShouldSmoothFonts (
|
|
CGContextRef c,
|
|
bool shouldSmoothFonts
|
|
) ;
|
|
|
|
FUNCTION: CGLError CGLSetParameter ( CGLContextObj ctx, CGLContextParameter pname, GLint* params ) ;
|
|
|
|
FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ;
|
|
|
|
<PRIVATE
|
|
|
|
: bitmap-flags ( -- flags )
|
|
{ kCGImageAlphaPremultipliedFirst kCGBitmapByteOrder32Host } flags ;
|
|
|
|
: bitmap-size ( dim -- n )
|
|
product "uint" heap-size * ;
|
|
|
|
: malloc-bitmap-data ( dim -- alien )
|
|
bitmap-size 1 calloc &free ;
|
|
|
|
: bitmap-color-space ( -- color-space )
|
|
CGColorSpaceCreateDeviceRGB &CGColorSpaceRelease ;
|
|
|
|
: <CGBitmapContext> ( dim -- context )
|
|
[ malloc-bitmap-data ] [ first2 8 ] [ first 4 * ] tri
|
|
bitmap-color-space bitmap-flags CGBitmapContextCreate
|
|
[ "CGBitmapContextCreate failed" throw ] unless* ;
|
|
|
|
: bitmap-data ( bitmap dim -- data )
|
|
[ CGBitmapContextGetData ] [ bitmap-size ] bi*
|
|
memory>byte-array ;
|
|
|
|
: <bitmap-image> ( bitmap dim -- image )
|
|
<image>
|
|
swap >>dim
|
|
swap >>bitmap
|
|
little-endian? ARGB BGRA ? >>component-order ;
|
|
|
|
PRIVATE>
|
|
|
|
: make-bitmap-image ( dim quot -- image )
|
|
[
|
|
[ [ <CGBitmapContext> &CGContextRelease ] keep ] dip
|
|
[ nip call ] [ drop [ bitmap-data ] keep <bitmap-image> ] 3bi
|
|
] with-destructors ; inline
|