Search code examples
cocoacore-graphicscgimagequickdraw

PicHandle to CGImageRef


This project is a generic C++ plugin with core-graphics support. I am unable to convert a PicHandle to CGImageRef successfully. Saving off image from CFDataRef results in a bad image.

    if(pic)
{
    Handle thePictureFileHandle;
    thePictureFileHandle = NewHandleClear(512); 
    HandAndHand((Handle)pic,thePictureFileHandle);

    dataProvider= CGDataProviderCreateWithData(NULL,(void*)*thePictureFileHandle,GetHandleSize( (Handle)thePictureFileHandle ),NULL );
    CFDataRef data = CGDataProviderCopyData(dataProvider);
    CGImageSourceRef source = CGImageSourceCreateWithDataProvider(dataProvider, NULL);
    if ( source )
    {
        cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
    }
    CGDataProviderRelease(dataProvider);
    CFRelease( source );
    CFRelease( data );
}

Solution

  • In CGImageSourceCreateImageAtIndex, you need to pass the dictionary saying that your data is a PICT. See the Apple documentation here. You need to do something like

    CFDictionaryRef   myOptions = NULL;
    CFStringRef       myKeys[1];
    CFTypeRef         myValues[1];
    
    myKeys[0] = kCGImageSourceTypeIdentifierHint;
    myValues[0] = (CFTypeRef)kUTTypePICT;
    myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
                   (const void **) myValues, 1,
                   &kCFTypeDictionaryKeyCallBacks,
                   & kCFTypeDictionaryValueCallBacks);
    
    cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, myOptions);
    
    CFRelease(myOptions);