From b3cbdb04269708c04ab259b6bc583aaee0faf77e Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 2 Sep 2011 18:59:58 -0700 Subject: [PATCH] images.cocoa: fix it to work on OS X 10.5 by drawing the image into a CGContext instead of getting image data directly --- basis/core-graphics/core-graphics.factor | 14 +++++++++ basis/core-graphics/types/types.factor | 2 ++ basis/images/cocoa/cocoa.factor | 37 +++++++++++------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/basis/core-graphics/core-graphics.factor b/basis/core-graphics/core-graphics.factor index 8463bf145f..45a6ddc72b 100644 --- a/basis/core-graphics/core-graphics.factor +++ b/basis/core-graphics/core-graphics.factor @@ -99,6 +99,20 @@ FUNCTION: void CGContextSetShouldSmoothFonts ( bool shouldSmoothFonts ) ; +FUNCTION: void CGContextDrawImage ( + CGContextRef c, + CGRect rect, + CGImageRef image +) ; + +FUNCTION: size_t CGImageGetWidth ( + CGImageRef image +) ; + +FUNCTION: size_t CGImageGetHeight ( + CGImageRef image +) ; + FUNCTION: void* CGBitmapContextGetData ( CGContextRef c ) ; CONSTANT: kCGLRendererGenericFloatID HEX: 00020400 diff --git a/basis/core-graphics/types/types.factor b/basis/core-graphics/types/types.factor index ac0ba31270..d6df20de90 100644 --- a/basis/core-graphics/types/types.factor +++ b/basis/core-graphics/types/types.factor @@ -76,6 +76,8 @@ STRUCT: CGAffineTransform TYPEDEF: void* CGColorRef TYPEDEF: void* CGColorSpaceRef TYPEDEF: void* CGContextRef +TYPEDEF: void* CGImageRef + TYPEDEF: uint CGBitmapInfo TYPEDEF: int CGLError diff --git a/basis/images/cocoa/cocoa.factor b/basis/images/cocoa/cocoa.factor index 6d4b97da1f..a308d3f475 100644 --- a/basis/images/cocoa/cocoa.factor +++ b/basis/images/cocoa/cocoa.factor @@ -1,7 +1,9 @@ -! (c)2010 Joe Groff bsd license +! 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.types fry images -images.loader io kernel math sequences ; +combinators core-foundation.data core-graphics +core-graphics.types fry locals images images.loader io kernel +math sequences ; IN: images.cocoa SINGLETON: ns-image @@ -44,23 +46,18 @@ ERROR: ns-image-planar-images-not-supported ; PRIVATE> -: load-image-rep ( -- image-rep ) - NSBitmapImageRep contents -> autorelease -> imageRepWithData: - NSColorSpace -> genericRGBColorSpace - NSColorRenderingIntentDefault - -> bitmapImageRepByConvertingToColorSpace:renderingIntent: ; +: ( byte-array -- image-rep ) + [ NSBitmapImageRep ] dip + -> autorelease + -> imageRepWithData: + -> CGImage ; -: image-rep>image ( image-rep -- image ) - image new swap { - [ -> size CGSize>dim [ >integer ] map >>dim ] - [ -> bitmapData ] - [ -> bytesPerPlane memory>byte-array >>bitmap ] - } cleave - RGBA >>component-order - ubyte-components >>component-type - t >>premultiplied-alpha? - f >>upside-down? ; +:: CGImage>image ( image -- image ) + image CGImageGetWidth :> w + image CGImageGetHeight :> h + { w h } [ + 0 0 w h image CGContextDrawImage + ] make-bitmap-image ; M: ns-image stream>image - drop - [ load-image-rep ] with-input-stream image-rep>image ; + drop stream-contents CGImage>image ;