const jimp = require('jimp');
const path = require('path');

const inputPath = path.resolve(__dirname, '../assets/logo.jpeg');
const outputPath = path.resolve(__dirname, '../assets/logo.png');

async function fixLogo() {
    try {
        const image = await jimp.read(inputPath);
        const width = image.bitmap.width;
        const height = image.bitmap.height;

        console.log(`Original Size: ${width}x${height}`);

        // Loop through all pixels. If it's black (rgb < 15), make transparent
        image.scan(0, 0, width, height, function (x, y, idx) {
            const red = this.bitmap.data[idx + 0];
            const green = this.bitmap.data[idx + 1];
            const blue = this.bitmap.data[idx + 2];

            // If it's very dark (close to black), turn alpha to 0 and pre-multiply it essentially
            if (red < 20 && green < 20 && blue < 20) {
                this.bitmap.data[idx + 3] = 0; // Alpha
            }
        });

        // We want only the ZV. Assuming it's in the top half, 
        // we can crop the top 65% of the image to remove 'ZENVYRO LABS' 
        // Let's be safe and crop the bottom 35%
        const cropHeight = Math.floor(height * 0.65);
        image.crop(0, 0, width, cropHeight);

        // Autocrop again to tightly fit the ZV and remove any remaining transparent borders
        image.autocrop();

        await image.writeAsync(outputPath);
        console.log(`Saved transparent logo to ${outputPath}`);
    } catch (e) {
        console.error('Error processing image:', e);
    }
}

fixLogo();
