From aa2fcfe1a33ab68571ace7e49c32b32bb832efc6 Mon Sep 17 00:00:00 2001 From: Christopher Denter Date: Thu, 26 May 2011 10:25:42 +0200 Subject: [PATCH] osx core image: make it compile --- kivy/core/image/osxcoreimage.pyx | 121 +++++++++++++++++++++++++++++++ setup.py | 11 +++ 2 files changed, 132 insertions(+) create mode 100644 kivy/core/image/osxcoreimage.pyx diff --git a/kivy/core/image/osxcoreimage.pyx b/kivy/core/image/osxcoreimage.pyx new file mode 100644 index 000000000..992bd3db9 --- /dev/null +++ b/kivy/core/image/osxcoreimage.pyx @@ -0,0 +1,121 @@ +from libcpp cimport bool + +ctypedef unsigned long size_t +ctypedef signed long CFIndex + +cdef extern from "Python.h": + object PyString_FromStringAndSize(char *s, Py_ssize_t len) + + +cdef extern from "CoreFoundation/CFBase.h": + ctypedef void *CFAllocatorRef + + +cdef extern from "CoreFoundation/CFData.h": + ctypedef void *CFDataRef + # XXX + # char or int? + unsigned char * CFDataGetBytePtr(CFDataRef) + +#cdef extern from "CoreGraphics/CGDataProvider.h": +cdef extern from "QuartzCore/QuartzCore.h": + ctypedef void *CGDataProviderRef + CFDataRef CGDataProviderCopyData(CGDataProviderRef) + + +cdef extern from "CoreFoundation/CFURL.h": + ctypedef void *CFURLRef + # Not in the snippet, but I deem necessary: + # + # CFURLRef CFURLCreateFromFileSystemRepresentation ( + # CFAllocatorRef allocator, + # const UInt8 *buffer, + # CFIndex bufLen, + # Boolean isDirectory + # ); + CFURLRef CFURLCreateFromFileSystemRepresentation(CFAllocatorRef, + unsigned char *, + CFIndex, + # XXX CORRECT? + bool) + + +cdef extern from "CoreFoundation/CFDictionary.h": + ctypedef void *CFDictionaryRef + + +#cdef extern from "CoreGraphics/CGImage.h": +cdef extern from "QuartzCore/QuartzCore.h": + ctypedef void *CGImageRef + CGDataProviderRef CGImageGetDataProvider(CGImageRef) + # guessing these, because of no wifi: + int CGImageGetWidth(CGImageRef) + int CGImageGetHeight(CGImageRef) + int CGImageGetAlphaInfo(CGImageRef) + int kCGImageAlphaNone + + +#cdef extern from "ImageIO/CGImageSource.h": +cdef extern from "QuartzCore/QuartzCore.h": + ctypedef void *CGImageSourceRef + CGImageSourceRef CGImageSourceCreateWithURL(CFURLRef, + CFDictionaryRef) + CGImageRef CGImageSourceCreateImageAtIndex(CGImageSourceRef, + size_t, CFDictionaryRef) + + +def load_raw_image_data(bytes _url): + cdef CFURLRef url + url = CFURLCreateFromFileSystemRepresentation(NULL, _url, len(_url), 0) + + cdef CGImageSourceRef myImageSourceRef + # or maybe: UIImage* uiImage = [UIImage imageWithContentsOfFile:fullPath]; + # see iphone3d book + myImageSourceRef = CGImageSourceCreateWithURL(url, NULL) + if myImageSourceRef == NULL: + print 'myImageSourceRef is NULL' + return None + + cdef CGImageRef myImageRef + myImageRef = CGImageSourceCreateImageAtIndex (myImageSourceRef, 0, NULL) + if myImageRef == NULL: + print 'myImageRef is NULL' + return None + + cdef int width = CGImageGetWidth(myImageRef) + cdef int height = CGImageGetHeight(myImageRef) + cdef int hasAlpha = CGImageGetAlphaInfo(myImageRef) != kCGImageAlphaNone + + # correctly detect the image type !!! + imgtype = 'rgb' + typesize = 3 + if hasAlpha > 0: + imgtype = 'rgba' + typesize = 4 + + cdef CFDataRef data + data = CGDataProviderCopyData(CGImageGetDataProvider(myImageRef)) + + r_data = PyString_FromStringAndSize(CFDataGetBytePtr(data), + width * height * typesize) + + # XXX clean image object + + print 'Image:', _url, width, height, imgtype + return (width, height, imgtype, r_data) + +# +# bool hasAlpha = CGImageGetAlphaInfo(cgImage) != kCGImageAlphaNone; CGColorSpaceRef colorSpace = CGImageGetColorSpace(cgImage); switch (CGColorSpaceGetModel(colorSpace)) { +# case kCGColorSpaceModelMonochrome: description.Format = +# hasAlpha ? TextureFormatGrayAlpha : TextureFormatGray; break; +# case kCGColorSpaceModelRGB: description.Format = +# Texture Formats and Types +# | 189 +# } +# hasAlpha ? TextureFormatRgba : TextureFormatRgb; break; +# default: assert(!"Unsupported color space."); break; +# } description.BitsPerComponent = CGImageGetBitsPerComponent(cgImage); +# return description; +# +# + diff --git a/setup.py b/setup.py index 5b0d61160..0eff7d657 100644 --- a/setup.py +++ b/setup.py @@ -180,6 +180,7 @@ if True: ext_files = [pyx] ext_libraries = libraries[:] ext_include_dirs = include_dirs[:] + ext_extra_compile_args = [] ext_extra_link_args = extra_link_args[:] if pyx.endswith('sdl.pyx'): @@ -188,6 +189,15 @@ if True: ext_libraries += sdl_libraries ext_include_dirs += sdl_includes ext_extra_link_args += sdl_extra_link_args + + elif pyx.endswith('osxcoreimage.pyx'): + if platform != 'darwin': + continue + ext_extra_link_args += ['-framework', 'Cocoa'] + ext_extra_link_args += ['-framework', 'Foundation'] + ext_extra_link_args += ['-framework', 'QuartzCore'] + ext_extra_compile_args += ['-isysroot', '/Developer/SDKs/MacOSX10.6.sdk'] + elif 'graphics' in pyx: ext_files += pxd_graphics else: @@ -197,6 +207,7 @@ if True: module_name, ext_files, libraries=ext_libraries, + extra_compile_args=ext_extra_compile_args, include_dirs=ext_include_dirs, extra_link_args=ext_extra_link_args))