0

I've got an issue where I'm unable to get the pixel data (via CGDataProviderCopyData) from the Image that gets returned when I call CGImageCreate. Any other call and I'm able to get the data from images with no problem, so obviously I'm doing something incorrectly with the parameters that I pass in.

To make sure that I have "good" pixel data, I pull it from an image returned from CGWindowListCreateImage. Further, to verify that this pixel data is not the point of failure, I have tests which convert it to a PIL Image and save it to file so that I can verify that this are as they should be.

Getting the pixel data:

    display_monitor = get_rightmost_monitor()
        
    image = CG.CGWindowListCreateImage(
        CG.CGRectInfinite, 
        CG.kCGWindowListOptionOnScreenOnly, 
        display_monitor, 
        CG.kCGWindowImageDefault
    )
    new_image = CG.CGBitmapContextCreateImage(context)
    prov = CG.CGImageGetDataProvider(new_image)
    data = CG.CGDataProviderCopyData(prov)
    return data 

For testing purposes, I'm simply trying to recreate the image object from which the pixel data came. So, I feed all of the attributes of the first image into the parameters of the CGImageCreate function.

rebuilt_image = CG.CGImageCreate(
                CG.CGImageGetWidth(image),                          # width
                CG.CGImageGetHeight(image),                         # height
                CG.CGImageGetBitsPerComponent(image),       # bitsPerComponent
                CG.CGImageGetBitsPerPixel(image),               # bitsPerPixel
                CG.CGImageGetBytesPerRow(image),                # bytesPerRow
                CGImageGetColorSpace(image),                        # colorspace
                CG.CGImageGetBitmapInfo(image),                 # bitmapInfo
                data,                                               # pixel data from image
                None,                                                # decode
                False,                                                 # shouldInterpolate
                CG.kCGRenderingIntentDefault)  

Now, the Image gets created without throwing any errors. However, upon calling CGDataProviderCopyData to extract the pixeldata, everything crashes and spits out this error:

    <Error>: copy_read_only: vm_copy failed: status 1 

Despite my best googling, I'm unable to figure out what's causing this to fail. There are similar quesitons on SO, like this one. However, they're not quite in line with the issue I'm having, or perhaps due to my inexperience with the Core Graphics framework, I'm unable to translate the solutions offered in those questions to something applicable to my issue.

Could someone clear up where I've gone wrong?

Thanks!

Community
  • 1
  • 1
Zack Yoshyaro
  • 2,056
  • 6
  • 24
  • 46
  • First, you're not testing the return values of anything, even though some of these functions can fail by returning `None`/wrapped null pointers. What does `print(prov)` show? – abarnert Jan 16 '14 at 01:43
  • According to `/usr/include/mach/kern_return.h` on my Mac, status 1 is `KERN_INVALID_ADDRESS`. (This isn't an answer, just a comment so nobody else trying to help out has to dig up the kernel return status values…) – abarnert Jan 16 '14 at 01:46
  • @abarnert `print(prov)` gives a string of values. I didn't show all of the small tests for brevity, but I've verified that all of the calls (like `CGImageGetBitsPerComponent`) return values other than `Null/None`. – Zack Yoshyaro Jan 16 '14 at 16:06
  • Possible duplicate of [Crash CGDataProviderCreateWithCopyOfData: vm\_copy failed: status 1](https://stackoverflow.com/questions/13100078/crash-cgdataprovidercreatewithcopyofdata-vm-copy-failed-status-1) – kenorb May 29 '17 at 14:35

0 Answers0