Technical Note TN2057

Fill in the size field before calling ICMGetPixelFormatInfo

This technote discusses why you should always fill in the size field of the ICMPixelFormatInfo structure before calling ICMGetPixelFormatInfo.

Introduction
Why should I care?
References
Document Revision History

Introduction

The Image Compression Manager routine ICMGetPixelFormatInfo takes a PixelFormat code and a pointer to a ICMPixelFormatInfo structure you provide, and fills in this structure with information about a given pixel format.

 OSErr ICMGetPixelFormatInfo( OSType PixelFormat,
                                  ICMPixelFormatInfoPtr theInfo );

It is very important for code calling ICMGetPixelFormatInfo to fill in the size field of the ICMPixelFormatInfo structure first. Initialize this field with sizeof(ICMPixelFormatInfo). In C, the following syntax does this and also sets the rest of the structure to zero:

ICMPixelFormatInfo myICMPixelFormatInfo = {sizeof(ICMPixelFormatInfo), 0};

If you call ICMGetPixelFormatInfo multiple times, reset the size field to sizeof(ICMPixelFormatInfo) before each call.

Why should I care?

From time to time, new fields are added to the end of the ICMPixelFormatInfo structure -- in QuickTime 4.1, defaultGammaLevel was added, and for QuickTime 6.0 horizontalSubsampling and verticalSubsampling have been added.

The Image Compression Manager is careful to not write more bytes than the size field indicates and on return, the size field will contain the number of valid bytes in the data structure.

By filling in the size field you guarantee that the Image Compression Manager won't write past the end of the structure (and corrupt the stack) if your application and the Image Compression Manager were compiled with different versions of the structure.

// QuickTime 6.0
struct ICMPixelFormatInfo {
    long          size;
    unsigned long formatFlags;
    short         bitsPerPixel[14]; /* list each plane's bits per
                                       pixel separately if planar */
                                    /* new field for QuickTime 4.1 */
    Fixed         defaultGammaLevel;
                                    /* new fields for QuickTime 6.0 */
    short         horizontalSubsampling[14]; /* per plane; use
                                                1 if plane is
                                                not subsampled */
    short         verticalSubsampling[14]; /* per plane; use
                                                1 if plane is
                                                not subsampled */
};
typedef struct ICMPixelFormatInfo       ICMPixelFormatInfo;
typedef ICMPixelFormatInfo *            ICMPixelFormatInfoPtr;
formatFlags Constants

Here's an example routine that calls ICMGetPixelFormatInfo to determine if a given pixel format is planar.

Boolean IsPixelFormatPlanar(OSType inPixelFormat)
{
     OSErr err;
     ICMPixelFormatInfo outInfo = {sizeof(ICMPixelFormatInfo), 0};
 
     err = ICMGetPixelFormatInfo(inPixelFormat, &outInfo);
 
     if (noErr != err)
         return false; // unknown pixel formats are not planar
 
     if ((outInfo.formatFlags & kICMPixelFormatIsPlanarMask) < 2)
         return false; // zero means pixel format is chunky; one plane is silly
 
     return true; // pixel format is planar
}

References



Document Revision History


DateNotes
2002-07-17

New document that discusses why you should always fill in the size field of the ICMPixelFormatInfo structure before calling ICMGetPixelFormatInfo.