2011-09-02 21:59:58 -04:00
|
|
|
! Copyright (C) 2010, 2011 Joe Groff, Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2013-09-20 14:58:39 -04:00
|
|
|
USING: accessors cocoa cocoa.classes core-foundation
|
|
|
|
core-foundation.data core-foundation.urls core-graphics
|
|
|
|
core-graphics.private core-graphics.types destructors
|
2015-08-12 09:49:02 -04:00
|
|
|
images.loader io kernel locals math sequences system ;
|
2011-11-02 12:38:03 -04:00
|
|
|
IN: images.loader.cocoa
|
2010-06-27 15:29:21 -04:00
|
|
|
|
|
|
|
SINGLETON: ns-image
|
2011-09-24 22:19:34 -04:00
|
|
|
|
|
|
|
os macosx? [
|
2014-03-14 12:17:13 -04:00
|
|
|
{ "png" "tif" "tiff" "gif" "jpg" "jpeg" "bmp" "ico" }
|
|
|
|
[ ns-image register-image-class ] each
|
2011-09-24 22:19:34 -04:00
|
|
|
] when
|
2010-06-27 15:29:21 -04:00
|
|
|
|
2011-09-02 21:59:58 -04:00
|
|
|
: <CGImage> ( byte-array -- image-rep )
|
|
|
|
[ NSBitmapImageRep ] dip
|
|
|
|
<CFData> -> autorelease
|
|
|
|
-> imageRepWithData:
|
|
|
|
-> CGImage ;
|
2010-06-27 15:29:21 -04:00
|
|
|
|
2011-09-02 21:59:58 -04:00
|
|
|
:: CGImage>image ( image -- image )
|
|
|
|
image CGImageGetWidth :> w
|
|
|
|
image CGImageGetHeight :> h
|
|
|
|
{ w h } [
|
|
|
|
0 0 w h <CGRect> image CGContextDrawImage
|
2011-09-09 02:08:16 -04:00
|
|
|
] make-bitmap-image ;
|
2010-06-27 15:29:21 -04:00
|
|
|
|
2013-09-20 14:58:39 -04:00
|
|
|
: image>CGImage ( image -- image )
|
|
|
|
[ bitmap>> ] [ dim>> first2 ] bi 8 pick 4 *
|
|
|
|
bitmap-color-space bitmap-flags
|
|
|
|
CGBitmapContextCreate -> autorelease
|
|
|
|
CGBitmapContextCreateImage ;
|
|
|
|
|
2012-08-25 14:44:40 -04:00
|
|
|
M: ns-image stream>image*
|
2011-09-02 21:59:58 -04:00
|
|
|
drop stream-contents <CGImage> CGImage>image ;
|
2013-09-20 14:58:39 -04:00
|
|
|
|
|
|
|
:: save-ns-image ( image path type -- )
|
|
|
|
[
|
|
|
|
path f <CFFileSystemURL> &CFRelease
|
|
|
|
type 1 f CGImageDestinationCreateWithURL &CFRelease
|
|
|
|
[
|
|
|
|
image image>CGImage &CFRelease
|
|
|
|
f CGImageDestinationAddImage
|
|
|
|
] [
|
|
|
|
CGImageDestinationFinalize drop
|
|
|
|
] bi
|
|
|
|
] with-destructors ;
|