]> gitweb.factorcode.org Git - factor.git/commitdiff
images.loader.cocoa: adding ability to save cocoa images to files.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 20 Sep 2013 18:58:39 +0000 (11:58 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 20 Sep 2013 18:58:39 +0000 (11:58 -0700)
basis/images/loader/cocoa/cocoa.factor

index b3d05999f939b2007189c256e9ad010be8d6f669..31c2574acc4c9ec544f102a6112a786ca7752678 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2010, 2011 Joe Groff, Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors alien.data cocoa cocoa.classes cocoa.messages
-combinators core-foundation.data core-graphics
-core-graphics.types fry locals images images.loader io kernel
-math sequences ;
+USING: accessors cocoa cocoa.classes core-foundation
+core-foundation.data core-foundation.urls core-graphics
+core-graphics.private core-graphics.types destructors
+images.loader io kernel locals math sequences ;
 FROM: system => os macosx? ;
 IN: images.loader.cocoa
 
@@ -33,5 +33,23 @@ os macosx? [
         0 0 w h <CGRect> image CGContextDrawImage
     ] make-bitmap-image ;
 
+: image>CGImage ( image -- image )
+    [ bitmap>> ] [ dim>> first2 ] bi 8 pick 4 *
+    bitmap-color-space bitmap-flags
+    CGBitmapContextCreate -> autorelease
+    CGBitmapContextCreateImage ;
+
 M: ns-image stream>image*
     drop stream-contents <CGImage> CGImage>image ;
+
+:: 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 ;