// Detect It Easy: detection rule file
// Author: LinXP
// doc-ref: https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/

meta("image", "Adobe Photoshop");

function detect() {
    if (Binary.getSize() >= 0x20) {
        if (Binary.compare("'8BPS'")) {
            bDetected = true;
            switch (File.read_uint16(0x04, _BE)) { // Version check
                case 1:
                    sName += " (.PSD)";
                    sVersion = "v1.0";
                    break;
                case 2:
                    sName += " BIG (.PSB)";
                    sVersion = "v2.0";
                    break;
                default:
                    bDetected = false;
            }

            const
                channelCount = Binary.read_uint16(0x0C, _BE),
                imageHeight = Binary.read_uint32(0x0E, _BE),
                imageWidth = Binary.read_uint32(0x12, _BE),
                bitsPerChannel = Binary.read_uint16(0x16, _BE),
                colorMode = Binary.read_uint16(0x18, _BE);

            sOption(imageWidth + "x" + imageHeight);
            switch (colorMode) {
                case 0: sOption("Bitmap"); break;
                case 1: sOption("Gray-scale"); break;
                case 2: sOption("Indexed"); break;
                case 3: sOption("RGB"); break;
                case 4: sOption("CMYK"); break;
                case 7: sOption("Multichannel"); break;
                case 8: sOption("Duotone"); break;
                case 9: sOption("Lab"); break;
                default:
                    bDetected = false;
            }
            sOption((channelCount * bitsPerChannel) + "-bit");
        }
    }

    return result();
}