Sunday, August 2, 2009

Format of an icon file?

I have been programming an application in C++ that uses two bitmaps to create information for an icon. I have the user interface done but when I have the program create the new .ico file with the information created in the program it does not create the file correctly and I'm sure it is because I'm not using the correct format for the .ico file.

Format of an icon file?
The ICO File


An Icon file, which usually has the ICO extension, contains one icon resource. Given that an icon resource can contain multiple images, it is no surprise that the file begins with an icon directory:





Copy Code


typedef struct


{


WORD idReserved; // Reserved (must be 0)


WORD idType; // Resource Type (1 for icons)


WORD idCount; // How many images?


ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)


} ICONDIR, *LPICONDIR;


The idCount member indicates how many images are present in the icon resource. The size of the idEntries array is determined by idCount. There exists one ICONDIRENTRY for each icon image in the file, providing details about its location in the file, size and color depth. The ICONDIRENTRY structure is defined as:





Copy Code


typedef struct


{


BYTE bWidth; // Width, in pixels, of the image


BYTE bHeight; // Height, in pixels, of the image


BYTE bColorCount; // Number of colors in image (0 if %26gt;=8bpp)


BYTE bReserved; // Reserved ( must be 0)


WORD wPlanes; // Color Planes


WORD wBitCount; // Bits per pixel


DWORD dwBytesInRes; // How many bytes in this resource?


DWORD dwImageOffset; // Where in the file is this image?


} ICONDIRENTRY, *LPICONDIRENTRY;


For each ICONDIRENTRY, the file contains an icon image. The dwBytesInRes member indicates the size of the image data. This image data can be found dwImageOffset bytes from the beginning of the file, and is stored in the following format:





Copy Code


typdef struct


{


BITMAPINFOHEADER icHeader; // DIB header


RGBQUAD icColors[1]; // Color table


BYTE icXOR[1]; // DIB bits for XOR mask


BYTE icAND[1]; // DIB bits for AND mask


} ICONIMAGE, *LPICONIMAGE;


The icHeader member has the form of a DIB BITMAPINFOHEADER. Only the following members are used: biSize, biWidth, biHeight, biPlanes, biBitCount, biSizeImage. All other members must be 0. The biHeight member specifies the combined height of the XOR and AND masks. The members of icHeader define the contents and sizes of the other elements of the ICONIMAGE structure in the same way that the BITMAPINFOHEADER structure defines a CF_DIB format DIB.





The icColors member is an array of RGBQUADs. The number of elements in this array is determined by examining the icHeader member.





The icXOR member contains the DIB bits for the XOR mask of the image. The number of bytes in this array is determined by examining the icHeader member. The XOR mask is the color portion of the image and is applied to the destination using the XOR operation after the application of the AND mask.





The icAND member contains the bits for the monochrome AND mask. The number of bytes in this array is determined by examining the icHeader member, and assuming 1bpp. The dimensions of this bitmap must be the same as the dimensions of the XOR mask. The AND mask is applied to the destination using the AND operation, to preserve or remove destination pixels before applying the XOR mask.





Note





The biHeight member of the icHeader structure represents the combined height of the XOR and AND masks. Remember to divide this number by two before using it to perform calculations for either of the XOR or AND masks. Also remember that the AND mask is a monochrome DIB, with a color depth of 1 bpp.

plum

No comments:

Post a Comment