"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sizeOfDDS = exports.getDDSSize = void 0;
function getDDSSize(buffer) {
    if (validatePNG(buffer)) {
        return sizeOfPNG(buffer);
    }
    else if (validateDDS(buffer)) {
        return sizeOfDDS(buffer);
    }
    return null;
}
exports.getDDSSize = getDDSSize;
const pngSignature = "PNG\r\n\x1a\n";
const pngImageHeaderChunkName = "IHDR";
// Used to detect "fried" png's: http://www.jongware.com/pngdefry.html
const pngFriedChunkName = "CgBI";
function validatePNG(buffer) {
    if (pngSignature === buffer.toString("ascii", 1, 8)) {
        let chunkName = buffer.toString("ascii", 12, 16);
        if (chunkName === pngFriedChunkName) {
            chunkName = buffer.toString("ascii", 28, 32);
        }
        if (chunkName !== pngImageHeaderChunkName) {
            throw new TypeError("Invalid PNG");
        }
        return true;
    }
    return false;
}
function sizeOfPNG(buffer) {
    if (buffer.toString("ascii", 12, 16) === pngFriedChunkName) {
        return {
            height: buffer.readUInt32BE(36),
            width: buffer.readUInt32BE(32)
        };
    }
    return {
        height: buffer.readUInt32BE(20),
        width: buffer.readUInt32BE(16)
    };
}
function validateDDS(buffer) {
    return buffer.readUInt32LE(0) === 0x20534444;
}
function sizeOfDDS(buffer) {
    return {
        height: buffer.readUInt32LE(12),
        width: buffer.readUInt32LE(16)
    };
}
exports.sizeOfDDS = sizeOfDDS;
//# sourceMappingURL=image-size.js.map