Quartz image data can originate from three types of sources: a URL that specifies a location, a CFData object, which is a simple allocated buffer, and raw data, for which you provide a pointer and a set of callbacks that take care of memory management for the data.
To obtain image data from a data source, you use either a data provider (prior to Mac OS X v10.4) or an image source (starting in Mac OS X v.10.4). You can think of data providers and image sources as “data managers.” Quartz uses a data manager to obtain the source image data. The data manager handles the messy details of supplying bytes in their correct sequence—for example, a JPEG data provider might handle the task of decompressing the image data.
Here is the general procedure for getting image data from a data source and creating an image from it:
Create the data manager. If your application runs only in Mac OS X v10.4, use one of the CGImageSource creation functions. If your image data is in a common format (JPEG, PNG, and so forth) you can use the function CGImageSourceCreateWithURL. If your image data is in a nonstandard or proprietary format, you’ll need to set up a data provider along with callbacks for managing the data. For more information, see Data Management in Quartz 2D Programming Guide.
Supply the data manager to an image creation function. If you’ve created an image source, you supply the CGImageSource object to the function CGImageSourceCreateImageAtIndex. Image source indexes are zero based, so if your image file contains only one image, supply 0. If you’ve created a data provider, you supply it as a parameter to an image creation function (CGImageCreate, CGImageCreateWithJPEGDataProvider, or CGImageCreateWithPNGDataProvider). For a description of all the image creation functions in Quartz, see Bitmap Images and Image Masks in Quartz 2D Programming Guide.
To draw the newly created Quartz image in a graphics context, you call CGContextDrawImage and specify the destination rectangle for the image. This function does not have a parameter for a source rectangle; if you want to crop the image or extract a subimage, you’ll need to write some additional code—see “Drawing Subimages.”
When you move image data from QuickDraw to Quartz, you might notice that the pixels in an image drawn in a Quartz graphics context look different from the pixels in the same image in QuickDraw. Changes are due to factors such as:
Anti-aliasing around the edges of the image
Image interpolation due to scaling
Alpha-based blending of image pixels with background pixels
Color matching, if the image and context color spaces are different
Color adjustments to match the display hardware
Last updated: 2006-09-05