Q: I'm trying to use a Graphics Importer to rotate
images in my application. I call GraphicsImportSetMatrix
then GraphicsImportSetBoundsRect to set up the draw
rectangle, but when I call GraphicsImportDraw my image isn't
rotated. What am I doing wrong?
A: GraphicsImportSetBoundsRect creates a transformation matrix
to map the image's natural bounds to the bounds specified in the call,
then sets the matrix. This results in the matrix set with your initial
call to GraphicsImportSetMatrix being reset. A single call to GraphicsImportSetMatrix
is all that is required.
Listing 1 demonstrates a 90 degrees clockwise rotation.
OSErr Rotate90(GraphicsImporterComponent inImporter)
{
Rect theNaturalBounds;
MatrixRecord theMatrix;
OSErr err;
err = GraphicsImportGetNaturalBounds(inImporter, &theNaturalBounds);
if (err) goto bail;
// Blue says "Rotating around the origin and then translating is much
// easier than working out where the appropriate centre of rotation is."
SetIdentityMatrix(&theMatrix);
RotateMatrix(&theMatrix, Long2Fix(90), 0, 0);
TranslateMatrix(&theMatrix, Long2Fix(theNaturalBounds.bottom), 0);
err = GraphicsImportSetMatrix(inImporter, &theMatrix);
bail:
return err;
}
|
Listing 1. Rotate CW 90 degrees
|
Listing 2 demonstrates adding scaling to the rotation.
OSErr RotateAndScale(GraphicsImporterComponent inImporter)
{
Rect theNaturalBounds;
MatrixRecord theMatrix;
OSErr err;
err = GraphicsImportGetNaturalBounds(inImporter, &theNaturalBounds);
if (err) goto bail;
SetIdentityMatrix(&theMatrix);
RotateMatrix(&theMatrix, Long2Fix(90), 0, 0);
TranslateMatrix(&theMatrix, Long2Fix(theNaturalBounds.bottom), 0);
// scale down by 50 percent
ScaleMatrix(&theMatrix, fixed1 / 2, fixed1 / 2, 0, 0);
err = GraphicsImportSetMatrix(inImporter, &theMatrix);
bail:
return err;
}
|
Listing 2. Rotate CW 90 degrees and scale
down by 50%
|
[May 25 2002]
|