// Detect It Easy: detection rule file
// Author: LinXP

meta("image", "");

function detect() {
    if (Binary.getSize() >= 0x40) {
        if (Binary.compare("AB'KTX 11'BB0D0A1A0A")) {
            bDetected = true;
            sName = "Khronos Texture (.KTX)";
            if (Binary.isVerbose()) {
                sOption(Binary.read_uint32(0x24) + "x" + Binary.read_uint32(0x28));
                //TODO derive size ref https://registry.khronos.org/KTX/specs/1.0/ktxspec.v1.html
            }
        } else if (Binary.compare("AB'KTX 20'BB0D0A1A0A")) {
            bDetected = true;
            sName = "Khronos Texture (.KTX2)";
            if (Binary.isVerbose()) {
                sOption(Binary.read_uint32(0x14) + "x" + Binary.read_uint32(0x18));
                //ref https://registry.khronos.org/KTX/specs/2.0/ktxspec.v2.html
                p = 0x50; sz = 0; //max ofs among the levels' ends
                for(i = 0; i < Math.max(1,Binary.read_uint32(0x28)); i++, p += 0x18)
                    if((t = Binary.read_uint64(p)) > sz) { sz = t + Binary.read_uint64(p+8) }
                sOption('sz:'+outSz(sz))
            }
        }

        // https://github.com/BinomialLLC/basis_universal/wiki/.basis-File-Format-and-ETC1S-Texture-Video-Specification
        else if (Binary.compare("734213004D00")) {
            bDetected = true;
            sName = "Basis Universal GPU Texture (.BASIS)";
            if (Binary.isVerbose()) {
                //sOption(Binary.read_uint32(0x14)+"x"+Binary.read_uint32(0x18));
            }
        }



    }

    return result();
}