Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

New Graphics Functions

QuickTime’s graphics import components now have the ability to provide Mac OS X Core Graphics CGImage images. As a result, you can now specify a CGImage as the source for an image export operation. With QuickTime 6.4, you can combine the use of QuickTime to read and write image files with the use of Core Graphics to draw or manage images.

QuickTime 6.4 includes five new functions for working with Core Graphics:

The following sample code illustrates how to use existing graphics importer functions with the new Core Graphics functions and data reference utilities:

// Open a still image from a file
OpenGraphicsImportComponentimageFromPath(CFStringRef path)
{
    ComponentResult            result;
    GraphicsImportComponent    grip = NULL;
 
    result = QTNewDataReferenceFromFullPathCFString(path,
                    kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
    if (NULL != dataRef) {
        GetGraphicsImporterForDataRefWithFlags(dataRef, dataRefType,
                                             &grip, 0);
        DisposeHandle(dataRef);
    }
    return grip;
}
 
// Export a still image to a file
 
OSStatus exportImageToPNGFile(GraphicsImportComponent imageGrip,
                                CFStringRef path)
{
    Handle                     dataRef = NULL;
    OSType                     dataRefType;
    GraphicsExportComponent    graphicsExporter;
    unsigned long              sizeWritten;
    ComponentResult            result;
 
    result = QTNewDataReferenceFromFullPathCFString(path,
                     kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
    result = OpenADefaultComponent(GraphicsExporterComponentType,
                                    kQTFileTypePNG, &graphicsExporter);
    result = GraphicsExportSetInputGraphicsImporter(graphicsExporter,
                                                    imageGrip);
    result = GraphicsExportSetOutputDataReference(graphicsExporter,
                                                   dataRef, dataRefType);
    result = GraphicsExportDoExport(graphicsExporter, &sizeWritten);
    CloseComponent(graphicsExporter);
    DisposeHandle(dataRef);
    return result;
}
// Drawing movies and images in windows and offscreen buffers
 
void playMovieToWindow(Movie qtMovie, Rect *movieBounds, WindowRef window)
{
    Rect        qdRect;
    CGrafPtr    windowPort;
    OSErr       err;
 
    SetMovieBox(qtMovie, movieBounds);
    windowPort = GetWindowPort(window);
    SetMovieGWorld(qtMovie, windowPort, NULL);
 
    // set the movie's rate to start it playing
}
 
void setMovieToRenderToOffscreenBuffer(Movie qtMovie, unsigned long
         pixelFormat,CGSize outputSize, void *buffer, size_t bytesPerRow)
{
    Rect          qdRect;
    GWorldPtr     gWorld = NULL;
    OSErr         err;
 
    SetRect(&qdRect, 0, 0, outputSize.width, outputSize.height);
    err = NewGWorldFromPtr(&gWorld, pixelFormat, &qdRect, NULL, NULL, 0,
                              buffer, bytesPerRow);
    SetMovieGWorld(qtMovie, gWorld, NULL);
 
    // set the movie's rate to start it playing
}
 
void drawImageToOffscreenBuffer(GraphicsImportComponent grip, unsigned long
          pixelFormat, CGSize outputSize, void *buffer, size_t bytesPerRow)
{
    Rect         qdRect;
    GWorldPtr    gWorld = NULL;
    OSErr        err;
 
    SetRect(&qdRect, 0, 0, outputSize.width, outputSize.height);
    err = NewGWorldFromPtr(&gWorld, pixelFormat, &qdRect, NULL, NULL, 0,
                             buffer, bytesPerRow);
    GraphicsImportSetGWorld(grip, gWorld, NULL);
    GraphicsImportDraw(grip);
}
 
OSStatus drawMovieImageToCGContext(Movie qtMovie, CGContextRef ctx,
                                                       CGRect drawRect)
{
    TimeValue             movieTime;
    PicHandle             picHndl;
    CGDataProviderRef     dataProvider;
    QDPictRef             pictDataRef;
    OSStatus              result;
 
    movieTime = GetMovieTime(qtMovie, NULL);
    picHndl = GetMoviePict(qtMovie, movieTime);
    HLock((Handle)picHndl);
    dataProvider = CGDataProviderCreateWithData ( NULL, *picHndl,
                                  GetHandleSize((Handle)picHndl), NULL );
    pictDataRef = QDPictCreateWithProvider(dataProvider);
    result = QDPictDrawToCGContext(ctx, drawRect, pictDataRef);
    QDPictRelease(pictDataRef);
    CGDataProviderRelease(dataProvider);
    KillPicture(picHndl);
    return result;
}
 
// Combining Graphics importers/exporters with Core Graphics
 
CGImageRef getCGImageFromPath(CFStringRef path)
{
    CGImageRef                  imageRef = NULL;
    Handle                      dataRef = NULL;
    OSType                      dataRefType;
    GraphicsImportComponent     gi;
    ComponentResult             result;
 
    result = QTNewDataReferenceFromFullPathCFString(path,
                    kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
    if (NULL != dataRef) {
       GetGraphicsImporterForDataRefWithFlags(dataRef, dataRefType, &gi, 0);
       result = GraphicsImportCreateCGImage(gi, &imageRef, 0);
       DisposeHandle(dataRef);
       CloseComponent(gi);
    }
    return CGImageRef;
}
 
OSStatus exportCGImageToPNGFile(CGImageRef imageRef, CFStringRef path)
{
    Handle                     dataRef = NULL;
    OSType                     dataRefType;
    GraphicsExportComponent    graphicsExporter;
    unsigned long              sizeWritten;
    ComponentResult            result;
 
    result = QTNewDataReferenceFromFullPathCFString(path,
                   kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
    result = OpenADefaultComponent(GraphicsExporterComponentType,
                   kQTFileTypePNG, &graphicsExporter);
    result = GraphicsExportSetInputCGImage(graphicsExporter, imageRef);
    result = GraphicsExportSetOutputDataReference(graphicsExporter,
                                                   dataRef, dataRefType);
    result = GraphicsExportDoExport(graphicsExporter, &sizeWritten);
    CloseComponent(graphicsExporter);
    DisposeHandle(dataRef);
    return result;
}


< Previous PageNext Page > Hide TOC


Last updated: 2003-09-01




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice