using Mac OS X CoreGraphics via ctypes

Daniel millerdev at gmail.com
Mon Jun 18 09:14:32 EDT 2007


On Jun 18, 6:07 am, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> Daniel wrote:
> >> > # the next line causes a segfault - what's the right way to do this?
> >> > #GCS_RGB = cglib.kCGColorSpaceGenericRGB()
>
> >> Usually, things in the OSX lib that start with k* are a constant - not a
> >> function. As is this.
>
> >> Diez
>
> > That's what I thought too. But when I try passing it directly as if it
> > were a constant:
>
> > GCS_RGB = cglib.kCGColorSpaceGenericRGB
> > cs = cglib.CGColorSpaceCreateWithName(GCS_RGB)
>
> > I get a segfault too. ctypes said kCGColorSpaceGenericRGB was a
> > function pointer, so I thought maybe I needed to call it to get the
> > value (not very good reasoning, I know).
>
> I'm not sure what that constant exported is - but how about looking the
> constant up in the ref docs and pass it as value? Usually these thingies
> are some 4-byte-string, or a normal zero-terminated one.
>

Thanks Diez. I'll try that if I decide to keep going with ctypes. I
got a bit further but had some problems with memory management (i.e.
retaining and releasing object references). It seemed like Python/
ctypes was accessing referenced objects after I had released them,
which caused segfaults. I may resort to writing an extension in C that
will handle the entire print process for me rather than fiddling
around with something that gets me half way there. For anyone
interested, here is the code I ended up with:


from ctypes import cdll, cast, c_void_p
from ctypes.util import find_library
cglib = cdll.LoadLibrary(find_library("ApplicationServices"))

CFStringRef = c_void_p

CGColorSpaceCreateWithName = cglib.CGColorSpaceCreateWithName
CGColorSpaceCreateWithName.restype = c_void_p
CGColorSpaceCreateWithName.argtypes = [CFStringRef]

CGPDFDocumentCreateWithProvider =
cglib.CGPDFDocumentCreateWithProvider
CGPDFDocumentCreateWithProvider.restype = c_void_p

provider = cglib.CGDataProviderCreateWithFilename("sample.pdf")
if provider:
    BMP_INFO = cglib.kCGBitmapByteOrderDefault
    CS_RGB = cast(cglib.kCGColorSpaceGenericRGB, CFStringRef)

    #cs = CGColorSpaceCreateWithName(CS_RGB) # segfault
    pdf = CGPDFDocumentCreateWithProvider(provider)
    npages = cglib.CGPDFDocumentGetNumberOfPages(pdf)

    for pnum in xrange(1, npages + 1):
        print "processing page", pnum
        page = cglib.CGPDFDocumentGetPage(pdf, pnum)
        rect = cglib.CGPDFPageGetBoxRect(page, 0) # kCGPDFMediaBox = 0
        page_w = cglib.CGRectGetWidth(rect)
        page_h = cglib.CGRectGetHeight(rect)

        # incorrect constructor for bitmap
        #bitmap = cglib.CGBitmapContextCreate(None, page_w, page_h,
cs, BMP_INFO)
        #cglib.CGContextDrawPDFDocument(bitmap, rect, pdf, pnum)

    #cglib.CGPDFDocumentRelease(pdf) # segfault
    #cglib.CGColorSpaceRelease(cs)
    cglib.CGDataProviderRelease(provider)

~ Daniel




More information about the Python-list mailing list