// DIE's signature file
// Author: Jason Hood <jadoxa@yahoo.com.au>

init("image","Windows Icon");

includeScript("CurIcoBPP");

function detect(bShowType,bShowVersion,bShowOptions)
{
    if(Binary.getSize()>=40)
    {
        if(Binary.compare("00000100"))
        {
            // Find the biggest, assuming square.
            var nCount=Binary.readWord(4);
            var nWidth=0,nHeight=0,nBPP=0;
            for(var i=0;i<nCount;i++)
            {
                var w=Binary.readByte(6+i*16),h;
                var b=getCurIcoBPP(6+i*16);
                if(w==0)
                {
                    var nOffset=Binary.readDword(18+i*16);
                    if(Binary.compare("89'PNG\r\n'1A0A........'IHDR'",nOffset))
                    {
                        w=Binary.readBEDword(nOffset+16);
                        h=Binary.readBEDword(nOffset+20);
                    }
                }
                else
                {
                    h=Binary.readByte(7+i*16);
                }
                if(w>nWidth)
                {
                    nWidth=w;
                    nHeight=h;
                    nBPP=b;
                }
                else if(w==nWidth)
                {
                    if(b>nBPP)
                    {
                        nBPP=b;
                    }
                }
            }
            sOptions=sOptions.append(nWidth+"x"+nHeight,nBPP+"bpp");
            if(nCount>1)
            {
                sOptions=sOptions.append(nCount+" icons");
            }
            bDetected=1;
        }
    }

    return result(bShowType,bShowVersion,bShowOptions);
}
