// DIE's signature file

init("image","TIFF");

function detect(bShowType,bShowVersion,bShowOptions)
{
    if(Binary.getSize()>=8)
    {
        var nOffset=Binary.readDword(4);
        var bBE=0;
        if(Binary.compare("'II'2A00"))
        {
            sOptions="little endian";
            bDetected=1;
        }
        else if(Binary.compare("'MM'002A"))
        {
            sOptions="big endian";
            nOffset=Binary.swapBytes(nOffset);
            bBE=1;
            bDetected=1;
        }
        if(bDetected&&bShowOptions)
        {
            var nCount=Binary.readEWord(nOffset,bBE);
            var nWidth=0,nHeight=0,nBPS=1,nCol=0,nComp=1;
            nOffset+=2;
            while(nCount--)
            {
                var nTag=Binary.readEWord(nOffset,bBE);
                var nType=Binary.readEWord(nOffset+2,bBE);
                switch(nTag)
                {
                case 0x100: nWidth=(nType==3)?Binary.readEWord(nOffset+8)
                                             :Binary.readEDword(nOffset+8);
                            break;
                case 0x101: nHeight=(nType==3)?Binary.readEWord(nOffset+8)
                                              :Binary.readEDword(nOffset+8);
                            break;
                case 0x102: nBPS=Binary.readEDword(nOffset+4)==1
                                 ?Binary.readEWord(nOffset+8)
                                 :Binary.readEWord(Binary.readEDword(nOffset+8));
                            // assume all samples are the same size
                            break;
                case 0x103: nComp=Binary.readEWord(nOffset+8);
                            break;
                case 0x106: nCol=Binary.readEWord(nOffset+8);
                            break;
                }
                nOffset+=12;
            }
            switch(nComp)
            {
            case     1: sOptions=sOptions.append("Uncompressed"); break;
            case     2: sOptions=sOptions.append("Huffman");      break;
            case     3: sOptions=sOptions.append("Group 3");      break;
            case     4: sOptions=sOptions.append("Group 4");      break;
            case     5: sOptions=sOptions.append("LZW");          break;
            case     6: sOptions=sOptions.append("embedded JPEG (602Photo software)"); break;
            case     7: sOptions=sOptions.append("JPEG");         break;
            case 32771: sOptions=sOptions.append("Alchemy software type 6 unknown compression"); break;
            case 32773: sOptions=sOptions.append("PackBits");     break;
            case 32946: sOptions=sOptions.append("Deflate");      break;
            case 34690: sOptions=sOptions.append("LDF bitonal");  break;
            }
            if(nWidth!=0&&nHeight!=0)
            {
                sOptions=sOptions.append(nWidth+"x"+nHeight);
            }
            sOptions=sOptions.append(nBPS+"-bit ");
            switch(nCol)
            {
            case 0: sOptions+="W/B";   break;
            case 1: sOptions+="B/W";   break;
            case 2: sOptions+="RGB";   break;
            case 3: sOptions+="index"; break;
            }
        }
    }

    return result(bShowType,bShowVersion,bShowOptions);
}
