]> gitweb.factorcode.org Git - factor.git/commitdiff
new vocab images.cocoa: load image objects using native cocoa api
authorJoe Groff <arcata@gmail.com>
Sun, 27 Jun 2010 19:29:21 +0000 (12:29 -0700)
committerJoe Groff <arcata@gmail.com>
Sun, 27 Jun 2010 19:29:21 +0000 (12:29 -0700)
basis/cocoa/cocoa.factor
basis/images/cocoa/authors.txt [new file with mode: 0644]
basis/images/cocoa/cocoa.factor [new file with mode: 0644]
basis/images/cocoa/platforms.txt [new file with mode: 0644]
basis/images/cocoa/summary.txt [new file with mode: 0644]

index fb21843c0f6f4e396dd6723e19073dc7779eb8dd..53f22addcb2aa9ada3c9b5812b1c8fb43731ecaf 100644 (file)
@@ -43,7 +43,9 @@ SYNTAX: IMPORT: scan [ ] import-objc-class ;
         "NSApplication"
         "NSArray"
         "NSAutoreleasePool"
+        "NSBitmapImageRep"
         "NSBundle"
+        "NSColorSpace"
         "NSData"
         "NSDictionary"
         "NSError"
diff --git a/basis/images/cocoa/authors.txt b/basis/images/cocoa/authors.txt
new file mode 100644 (file)
index 0000000..f13c9c1
--- /dev/null
@@ -0,0 +1 @@
+Joe Groff
diff --git a/basis/images/cocoa/cocoa.factor b/basis/images/cocoa/cocoa.factor
new file mode 100644 (file)
index 0000000..2150747
--- /dev/null
@@ -0,0 +1,64 @@
+! (c)2010 Joe Groff bsd license
+USING: accessors alien.data cocoa cocoa.classes cocoa.messages
+combinators core-foundation.data core-graphics.types fry images
+images.loader io kernel literals ;
+IN: images.cocoa
+
+SINGLETON: ns-image
+! "png" ns-image register-image-class
+! "tif" ns-image register-image-class
+! "tiff" ns-image register-image-class
+! "gif" ns-image register-image-class
+! "jpg" ns-image register-image-class
+! "jpeg" ns-image register-image-class
+! "bmp" ns-image register-image-class
+! "ico" ns-image register-image-class
+
+CONSTANT: NSImageRepLoadStatusUnknownType     -1
+CONSTANT: NSImageRepLoadStatusReadingHeader   -2
+CONSTANT: NSImageRepLoadStatusWillNeedAllData -3
+CONSTANT: NSImageRepLoadStatusInvalidData     -4
+CONSTANT: NSImageRepLoadStatusUnexpectedEOF   -5
+CONSTANT: NSImageRepLoadStatusCompleted       -6
+
+CONSTANT: NSColorRenderingIntentDefault                 0
+CONSTANT: NSColorRenderingIntentAbsoluteColorimetric    1
+CONSTANT: NSColorRenderingIntentRelativeColorimetric    2
+CONSTANT: NSColorRenderingIntentPerceptual              3
+CONSTANT: NSColorRenderingIntentSaturation              4
+
+ERROR: ns-image-unknown-type ;
+ERROR: ns-image-invalid-data ;
+ERROR: ns-image-unexpected-eof ;
+ERROR: ns-image-planar-images-not-supported ;
+
+<PRIVATE
+
+: check-return ( n -- )
+    {
+        { $ NSImageRepLoadStatusUnknownType   [ ns-image-unknown-type   ] }
+        { $ NSImageRepLoadStatusInvalidData   [ ns-image-invalid-data   ] }
+        { $ NSImageRepLoadStatusUnexpectedEOF [ ns-image-unexpected-eof ] }
+        [ drop ]
+    } case ;
+
+PRIVATE>
+
+: load-image-rep ( -- image-rep )
+    NSBitmapImageRep contents <CFData> -> autorelease -> imageRepWithData:
+    NSColorSpace -> genericRGBColorSpace
+    NSColorRenderingIntentDefault
+    -> bitmapImageRepByConvertingToColorSpace:renderingIntent: ;
+
+: image-rep>image ( image-rep -- image )
+    image new swap {
+        [ -> size CGSize>dim >>dim ]
+        [ -> bitmapData ]
+        [ -> bytesPerPlane memory>byte-array >>bitmap ]
+    } cleave
+        RGBA >>component-order
+        ubyte-components >>component-type
+        f >>upside-down? ;
+
+M: ns-image stream>image
+    drop [ load-image-rep ] with-input-stream image-rep>image ;
diff --git a/basis/images/cocoa/platforms.txt b/basis/images/cocoa/platforms.txt
new file mode 100644 (file)
index 0000000..6e806f4
--- /dev/null
@@ -0,0 +1 @@
+macosx
diff --git a/basis/images/cocoa/summary.txt b/basis/images/cocoa/summary.txt
new file mode 100644 (file)
index 0000000..628cce9
--- /dev/null
@@ -0,0 +1 @@
+Image loading using MacOS X's native Cocoa APIs