< Previous PageNext Page > Hide TOC

Data Management

Managing data is a task every graphics application needs to perform. For Quartz, data management refers to supplying data to or receiving data from Quartz 2D routines. Some Quartz 2D routines move blocks of data into Quartz, such as those that get image or PDF data from a file or another part of your application. Other routines accept blocks of Quartz data, such as those that write image or PDF data to a file or provide the data to another part of your application.

Quartz provides a variety of functions for managing data. By reading this chapter, you should be able to determine which functions are best for your application. As you’ll see, some routines are available only in Mac OS X v10.4 and later while others have been with Quartz since its beginning.

Note: The preferred way to read and write image data in Mac OS X v10.4 and later is to use the Image I/O framework. See Image I/O Programming Guide. This guide describes how to use the CGImageSourceRef and CGImageDestinationRef opaque data types.

Quartz recognizes three broad categories of data sources and destinations:

The data itself, whether represented by a URL, a CFData object, or a data buffer, can be image data or PDF data. Image data can use any type of file format. Quartz understands most of the common image file formats. Some of the Quartz data management functions work specifically with image data, a few work only with PDF data, while others are more generic and can be used either for PDF or image data.

URL, CFData, and raw data sources and destinations refer to data outside the realm of Mac OS X graphics technologies, as shown in Figure 10-1. Your application can also move data between Quartz 2D and Core Image, which is another Mac OS X graphics technology. Although this is a fairly trivial operation, it’s an important one to keep in mind because Core Image can add impressive capabilities to your graphics application.


Figure 10-1  Moving data to and from Quartz 2D

Moving data to and from Quartz 2D

These sections provide detailed information on data management in Quartz 2D:

Moving Data Into Quartz 2D

The functions for getting data from a data source are listed in Table 10-1. All these functions, except for CGPDFDocumentCreateWithURL, either return an image source (CGImageSourceRef) or a data provider (CGDataProviderRef). Image sources and data providers abstract the data-access task and eliminate the need for applications to manage data through a raw memory buffer. Image sources are used exclusively for obtaining image data, but are available only in Mac OS X v10.4 or later. An image source represents a wide variety of image data. An image source can contain more than one image, thumbnail images, and properties for each image and the image file.

Data providers can be used to obtain image or PDF data, and all, except for CGDataProviderCreateWithCFData, are available in Mac OS Xv10.0 or later. The function CGPDFDocumentCreateWithURL is a convenience function that creates a PDF document from the file located at the specified URL.

You can supply a data provider to:

When you are working with image data and your application runs in Mac OS X v10.4 or later, image sources are the preferred way to move image data into Quartz. After you have a CGImageSourceRef, you can accomplish these tasks:

For more information on images, see “Bitmap Images and Image Masks.”

Table 10-1  Functions that move data into Quartz 2D

Function

Use this function

Availability

CGDataProviderCreate

To read image or PDF data in a stream. You supply callbacks to handle the data.

10.0

CGDataProviderCreateDirectAccess

To read image or PDF data in a block. You supply callbacks to handle the data.

10.0

CGDataProviderCreateWithData

To read a buffer of image or PDF data supplied by your application. You provide a callback to release the memory you allocated for the data.

10.0

CGDataProviderCreateWithURL

Whenever you can supply a URL that specifies the target for data access to image or PDF data.

10.0

CGPDFDocumentCreateWithURL

To create a PDF document from data that resides at the specified URL.

10.0

CGDataProviderCreateWithCFData

To read image or PDF data from a CFData object.

10.4

CGImageSourceCreateWithDataProvider

To create an image source from a data provider.

10.4

CGImageSourceCreateWithData

To create an image source from a CFData object.

10.4

CGImageSourceCreateWithURL

To create an image source from a URL that specifies the location of image data.

10.4

Moving Data Out Of Quartz 2D

The functions listed in Table 10-2 move data out of Quartz 2D. All these functions, except for CGPDFContextCreateWithURL, either return an image destination (CGImageDestinationRef) or a data consumer (CGDataConsumerRef). Image destination and data consumers abstract the data-writing task, letting Quartz take care of the details for you. Image destinations are used exclusively for writing image data, but are available only in Mac OS X v10.4 or later. Similar to image sources, an image destination can represent a variety of image data, from a single image to a destination that contains multiple images, thumbnail images, and properties for each image or for the image file.

Data consumers can be used to write image or PDF data, and all, except for CGDataConsumerCreateWithCFData, are available in Mac OS X v10.0 or later. The function CGPDFContextCreateWithURL is a convenience function that writes PDF data to the location specified by a URL.

You can supply a data consumer to:

When you are working with image data and your application runs in Mac OS X v10.4 or later, an image destination is the preferred way to move image data out of Quartz. After you have a CGImageDestinationRef, you can accomplish these tasks:

For more information on images, see “Bitmap Images and Image Masks.”

Table 10-2  Functions that move data out of Quartz 2D

Function

When to use this function

Availability

CGDataConsumerCreateWithURL

Whenever you can supply a URL that specifies where to write the image or PDF data.

10.0

CGDataConsumerCreate

To write image or PDF data using callbacks you supply.

10.0

CGPDFContextCreateWithURL

Whenever you can supply a URL that specifies where to write PDF data.

10.0

CGDataConsumerCreateWithCFData

To write image or PDF data to a CFData object.

10.4

CGImageDestinationCreateWithDataConsumer

To write image data to a data consumer.

10.4

CGImageDestinationCreateWithData

To write image data to a CFData object.

10.4

CGImageDestinationCreateWithURL

Whenever you can supply a URL that specifies where to write the image data.

10.4

Moving Data Between Quartz 2D and Core Image

The Core Image framework is an Objective-C API that supports image processing. Core Image lets you access built-in image filters for both video and still images and provides support for custom filters and near real-time processing. You can apply Core Image filters to Quartz 2D images. For example, you can use Core Image to correct color, distort the geometry of images, blur or sharpen images, and create a transition between images. Core Image also allows you to apply an iterative process to an image—one that feeds back the output of a filter operation to the input. To understand the capabilities of Core Image more fully, see Core Image Programming Guide.

Core Image methods operate on images that are packaged as Core Image images, or CIImage objects. Although “CIImage” looks similar to the “CGImage” on the printed page (or display screen), Core Image does not operate directly on Quartz images (CGImageRef data types). Quartz images must be converted to Core Image images before you apply a Core Image filter to the image.

The Quartz 2D API does not provide any functions that package Quartz images as Core Image images. But Core Image does. The following Core Image methods create a Core Image image from either a Quartz image or a Quartz layer (CGLayerRef). You can use them to move Quartz 2D data to Core Image.

The following Core Image methods return a Quartz image from a Core Image image. You can use them to move a processed image back into Quartz 2D:

For a complete description of Core Image methods, see Core Image Reference Collection.



< Previous PageNext Page > Hide TOC


© 2001, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-12-11)


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.