Skip navigation.
 
mlImage Capture: getting an NSImage from a data Ptr
FROM : Andrew Zamler-Carhart
DATE : Mon Oct 18 19:11:22 2004

Dear List,

I have a question about using the Image Capture framework with Cocoa.

I would like to be able to display thumbnails of photos before 
downloading them, the way that Image Capture.app does if you click 
"Download Some..."

Image Capture has several methods that return a pointer to some image 
data, from which I would like to create an NSImage. Similar mechanisms 
are used for loading the camera icon, thumbnails, and complete images.

But I'm not sure what to do with this data pointer. I've tried making 
an NSData object with the bytes returned by the Image Capture functions 
(see code below), and then making an NSImage from the NSData, but I 
never get a valid NSImage.

For example, here's the code that I'm using to get an the camera icon. 
This is adapted from GetImageData() in the file ICAUtils.c, whose 
source code is available at:

   http://developer.apple.com/samplecode/ImagesToQTMovie/listing3.html

NSImage *getCameraIcon(ICAObject object) {
        ICAGetPropertyByTypePB getPropertyByTypePB;
        ICAGetPropertyDataPB  getPropertyDataPB;
        OSErr                  err;
        Ptr                    dataPtr = nil;
        UInt32                dataSize;

        memset(&getPropertyByTypePB, 0, sizeof(ICAGetPropertyByTypePB));
        getPropertyByTypePB.object = object;
        getPropertyByTypePB.propertyType = kICAPropertyCameraIcon;
        err = ICAGetPropertyByType (&getPropertyByTypePB, nil);
        if (noErr == err)    {
            dataSize = getPropertyByTypePB.propertyInfo.dataSize;
            // this gets set to something reasonable, like 9 KB

            dataPtr = malloc(dataSize);
            if (dataPtr)        {
                memset(&getPropertyDataPB, 0, 
sizeof(ICAGetPropertyDataPB));
                getPropertyDataPB.property = 
getPropertyByTypePB.property;
                getPropertyDataPB.startByte    = 0;
                getPropertyDataPB.dataPtr      = dataPtr;
                getPropertyDataPB.requestedSize = dataSize;
                getPropertyDataPB.dataType      = 
getPropertyByTypePB.propertyInfo.dataType;
                err = ICAGetPropertyData(&getPropertyDataPB, nil);
                if (noErr != err) {
                    free(dataPtr);
                    dataPtr = nil;
                } else {
                    NSData *data = [NSData dataWithBytes: dataPtr 
length: dataSize];
                    NSImage *image = [[[NSImage alloc] initWithData: 
data] autorelease];
                    free(dataPtr);
                    return image;
                }
            }
        }

        return nil;
    }

I've been able to confirm that dataSize gets set to the right amount of 
data (the same number of bytes as reported for the image in the Image 
Capture Browser application), so I know that I'm on the right track.

I've tried saving the bytes returned by this function to disk. I get 
some data in a file, but nothing that any image editor can open. So I 
really don't know what format it's in.

I've tried experimenting with NSBitmapImageRep 
-initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:
samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bytesPerRow:
bitsPerPixel: (is that Cocoa's longest designated initializer or what?) 
but I just get colored static.

I've also tried using this code, adapted from the Image Capture 
Architecture PDF, to get the thumbnail for an image. In this case, 
ICACopyObjectThumbnail() returns noErr, but the data object is always 
nil. So less progress here than with the other method.

    NSImage *getThumbnail(ICAObject object) {
        ICACopyObjectThumbnailPB pb = {};
        NSData *data;

        pb.header.refcon = nil;
        pb.object = object;
        pb.thumbnailData = (CFDataRef*)&data;
        pb.thumbnailFormat = kICAThumbnailFormatICA;

        if (ICACopyObjectThumbnail(&pb, NULL) == noErr) {
            NSImage *image = [[[NSImage alloc] initWithData: data] 
autorelease];
            return image;
        }

        return nil;
    }

Any pointers, so to speak? Does anybody have any experience loading 
images with Image Capture at all?

Thanks for your help.

Best regards,

Andrew
KavaSoft

Related mailsAuthorDate
No related mails found.