Technical: QuickTime
Advanced Search
Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

Using QuickTime for Fast and Flexible Drawing

Dispatch 8

Many applications draw graphics into an offscreen buffer and then copy the offscreen buffer to the screen. This allows for flicker free drawing of complex graphics. Most applications use QuickDraw's CopyBits function to copy the offscreen buffer to the screen. QuickTime contains its own set of drawing routines which provide higher performance drawing than CopyBits in many cases, as well as capabilities not provided by CopyBits such as alpha channel transfer modes and rotation. QuickTime's drawing routines can be hardware accelerated as well. This acceleration is performed through the addition of hardware based Image Decompressors. On Windows, this acceleration is also achieved through the use of Direct Draw.

The following code snippets assume that there is an offscreen image buffer (GWorldPtr) called "gw" and a window (WindowPtr) called "w".

The easiest way to use QuickTime to copy an offscreen buffer is with the Image Compression Manager's DecompressImage function.

ImageDescriptionHandle imageDesc;
   
SetGWorld((CGrafPtr)w, nil);
   
MakeImageDescriptionForPixMap(gw->portPixMap, &imageDesc);
   
DecompressImage(GetPixBaseAddr(gw->portPixMap), imageDesc,
 ((CGrafPtr)w)->portPixMap, nil, &w->portRect, ditherCopy, nil);
   
DisposeHandle((Handle)imageDesc);

DecompressImage can be used to draw in any QuickDraw transfer mode and in any QuickTime graphics mode, including alpha channel modes such as graphicsModeStraightAlpha. DecompressImage doesn't provide a way to rotate an image. To do that, it is necessary to use the "fancy" version of DecompressImage, FDecompressImage. This example shows how to use FDecompressImage to draw an image rotated by 45 degrees.

Rect srcRect;
MatrixRecord matrix;
   
SetGWorld((CGrafPtr)w, nil);
   
MacSetRect(&srcRect, 0, 0, (**imageDesc).width, (**imageDesc).height);
RectMatrix(&matrix, &srcRect, &w->portRect);
   
RotateMatrix(&matrix, FixRatio(45, 1),
  FixRatio((**imageDesc).width, 2),
  FixRatio((**imageDesc).height, 2));
   
FDecompressImage(GetPixBaseAddr(gw->portPixMap), imageDesc,
  ((CGrafPtr)w)->portPixMap,nil, &matrix, ditherCopy, nil, nil, nil, codecNormalQuality, 0
  (**imageDesc).dataSize, nil, nil);

Because FDecompressImage uses a three by three matrix to describe how the image should be transformed when it is drawn, it is possible to use FDecompressImage to perform more complex transformations. The Image Compression Manager provides functions to make this convenient including RotateMatrix, ScaleMatrix, TranslateMatrix, and SkewMatrix.

Both DecompressImage and FDecompressImage were designed to be used to draw a single image. If an application is going to repeatedly draw the same offscreen buffer to the same destination, it is more efficient to create an Image Sequence. Using an Image Sequence allows QuickTime to pre-calculate information about the transfer operation once for a sequence of images, rather than calculating it for each individual copy operation. Use the DecompressSequenceBegin function to create an Image Sequence.

Rect srcRect;
MatrixRecord matrix;
   
ImageSequence imageSeq;
   
MacSetRect(&srcRect, 0, 0, (**imageDesc).width, (**imageDesc).height);
   
RectMatrix(&matrix, &srcRect, &w->portRect);
   
DecompressSequenceBegin(&imageSeq, imageDesc, (CGrafPtr)w, nil, nil, &matrix, ditherCopy,
  nil, 0, codecNormalQuality, nil);

After creating the Image Sequence, parameters of the Image Sequence such as the graphics mode, transformation matrix, and clip can be changed using SetDSequenceGraphicsMode, SetDSequenceMatrix, and SetDSequenceMask. To draw the image using the Image Sequence, use the DecompressSequenceFrameS function.

DecompressSequenceFrameS(imageSeq, GetPixBaseAddr(gw->portPixMap),
  (**imageDesc).dataSize, 0, nil, nil);

DecompressSequenceFrameS can be called repeatedly. When the Image Sequence is no longer needed, it should be disposed using CDSequenceEnd.

CDSequenceEnd(imageSeq);

See Also

QuickTime 3 Reference - Image Compression Manager

Change History

4/16/98 - jph - First published
Topics
Previous | Next