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:
GraphicsImportCreateCGImage imports
an image as a Core Graphics CGImage.
GraphicsExportSetInputCGImage specifies
a Core Graphics CGImage as
the source for a graphics export operation.
GraphicsExportGetInputCGImage determines
which Core Graphics CGImage is
the source for a graphics export operation.
GraphicsExportSetInputCGBitmapContext sets
the CGBitmapContext that
the graphics exporter will use as its input image.
GraphicsExportGetInputCGBitmapContext retrieves
the CGBitmapContext that
the graphics exporter is using as its input image.
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; |
} |
Last updated: 2003-09-01