News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ico file format

Started by petezl, January 12, 2005, 10:25:47 AM

Previous topic - Next topic

petezl

Does anyone know the format of ico files?
A search on the web has produced very little in real details.
I imagine it's a modified bmp file (being windows native picture format), it should be quite easy to write a small app to convert say a bitmap to ico.

Peter.
Cats and women do as they please
Dogs and men should realise it.

Ghirai

QuoteAn 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:

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:

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 >=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:

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.

The following is an incomplete code fragment for reading an .ICO file:

// We need an ICONDIR to hold the data
pIconDir = malloc( sizeof( ICONDIR ) );
// Read the Reserved word
ReadFile( hFile, &(pIconDir->idReserved), sizeof( WORD ), &dwBytesRead, NULL );
// Read the Type word - make sure it is 1 for icons
ReadFile( hFile, &(pIconDir->idType), sizeof( WORD ), &dwBytesRead, NULL );
// Read the count - how many images in this file?
ReadFile( hFile, &(pIconDir->idCount), sizeof( WORD ), &dwBytesRead, NULL );
// Reallocate IconDir so that idEntries has enough room for idCount elements
pIconDir = realloc( pIconDir, ( sizeof( WORD ) * 3 ) +
                              ( sizeof( ICONDIRENTRY ) * pIconDir->idCount ) );
// Read the ICONDIRENTRY elements
ReadFile( hFile, pIconDir->idEntries, pIconDir->idCount * sizeof(ICONDIRENTRY),
          &dwBytesRead, NULL );
// Loop through and read in each image
for(i=0;i<pIconDir->idCount;i++)
{
  // Allocate memory to hold the image
  pIconImage = malloc( pIconDir->idEntries.dwBytesInRes );
  // Seek to the location in the file that has the image
  SetFilePointer( hFile, pIconDir->idEntries.dwImageOffset,
                  NULL, FILE_BEGIN );
  // Read the image data
  ReadFile( hFile, pIconImage, pIconDir->idEntries.dwBytesInRes,
            &dwBytesRead, NULL );
  // Here, pIconImage is an ICONIMAGE structure. Party on it :)
  // Then, free the associated memory
  free( pIconImage );
}
// Clean up the ICONDIR memory
free( pIconDir );

This is what i've found, dunno if you found it already, or if it provides any real help.
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

Vortex


petezl

Thanks all,
I'll delve into it and see what I come up with...
Peter
Cats and women do as they please
Dogs and men should realise it.

sluggy


petezl

Thanks Sluggy,  Actually, that looks an informative site! Have added it to my bookmarks.

Peter.
Cats and women do as they please
Dogs and men should realise it.


petezl

Is nothing safe anymore!

Some of the icons used in commercial soft are tiny masterpieces. Are they using an alternative way of producing them or is it just the eye being tricked into thinking there is more detail there than there really is?
Peter.
Cats and women do as they please
Dogs and men should realise it.

donkey

Hi Petezl,

If you would like an example of saving a bitmap as an icon, I wrote the SaveAsIcon plugin for TBPaint,the source is available on my website...

TbxSaveAsIcon.zip

There is also one to save an imagelist as an ANI icon.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

petezl

Thanks Donkey, Your a Star!
Very much appreciated.

Peter.
Cats and women do as they please
Dogs and men should realise it.

jimh

Quote from: petezl on January 12, 2005, 12:08:27 PM
Some of the icons used in commercial soft are tiny masterpieces. Are they using an alternative way of producing them or is it just the eye being tricked into thinking there is more detail there than there really is?

The short answer is that no, the people who make the icons for say, Apple or Microsoft do not use any special format for making their icons, they are simply artists who make good use of color and form...and they know when to (and when NOT to) use anti-aliasing.  There's more to making an icon than simply using an app such as Photoshop to resize a large image down to a small one.  Much of the time if you really look at the icons in XP, you'll see that the outer portion of the icon is a dark color (not anti-aliased) and the interior is shaded and all dark interior lines are anti-aliased to that shading to create the illusion of depth and detail.  Just glance at the XP notepad.exe large icon and you'll see that they even gave the impression of transparency, although there isn't any... just nicely done shading.

donkey

Yup, I agree with jimh on this one, it really takes an artist to create good icons. You see alot in the web collections that are just horrible. I was lucky that Lim Chee Aun drew my icons for TBPaint, he is the creator of Phoenity icons and is truly an artist, at my request he also did a set for RadASM that are very nice. XP can also use Alpha Blended icons that allow the high order 8 bits  of 32 bit icons to be used for an Alpha channel. The Alpha channel specifies the amount of transparency (0-FFh) of the pixel, this allows you to have partially transparent icons and in effect anti-aliasing of the edges. Many new apps have this type of icon, I am consdering adding the capability to TBPaint version 2 but I am so far behind in the project right now that I just want to get the project into beta first.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable