|
| 1 | +/* eslint-env node */ |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const { execSync } = require('child_process'); |
| 5 | + |
| 6 | +const inputPng = path.join(__dirname, '../assets/icon.png'); |
| 7 | +const outputIco = path.join(__dirname, '../assets/icon.ico'); |
| 8 | +const tempPng = path.join(__dirname, '../assets/icon-256.png'); |
| 9 | + |
| 10 | +try { |
| 11 | + console.log('Resizing icon to 256x256 using PowerShell...'); |
| 12 | + // Use PowerShell to resize the image because we don't want to add sharp/jimp dependencies just for this one-off |
| 13 | + const psCommand = ` |
| 14 | + Add-Type -AssemblyName System.Drawing; |
| 15 | + $img = [System.Drawing.Image]::FromFile('${inputPng}'); |
| 16 | + $newImg = new-object System.Drawing.Bitmap(256, 256); |
| 17 | + $graph = [System.Drawing.Graphics]::FromImage($newImg); |
| 18 | + $graph.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic; |
| 19 | + $graph.DrawImage($img, 0, 0, 256, 256); |
| 20 | + $newImg.Save('${tempPng}', [System.Drawing.Imaging.ImageFormat]::Png); |
| 21 | + $img.Dispose(); |
| 22 | + $newImg.Dispose(); |
| 23 | + $graph.Dispose(); |
| 24 | + `; |
| 25 | + |
| 26 | + execSync(`powershell -Command "${psCommand.replace(/\n/g, ' ')}"`, { stdio: 'inherit' }); |
| 27 | + |
| 28 | + if (!fs.existsSync(tempPng)) { |
| 29 | + throw new Error('Failed to create resized PNG'); |
| 30 | + } |
| 31 | + |
| 32 | + console.log('Generating ICO file...'); |
| 33 | + const pngBuffer = fs.readFileSync(tempPng); |
| 34 | + // size variable removed as it was unused |
| 35 | + |
| 36 | + // ICO Header |
| 37 | + // 0-1: Reserved (0) |
| 38 | + // 2-3: Type (1 for ICO) |
| 39 | + // 4-5: Number of images (1) |
| 40 | + const header = Buffer.alloc(6); |
| 41 | + header.writeUInt16LE(0, 0); |
| 42 | + header.writeUInt16LE(1, 2); |
| 43 | + header.writeUInt16LE(1, 4); |
| 44 | + |
| 45 | + // Icon Directory Entry |
| 46 | + // 0: Width (0 for 256) |
| 47 | + // 1: Height (0 for 256) |
| 48 | + // 2: Color count (0 for >= 256 colors) |
| 49 | + // 3: Reserved (0) |
| 50 | + // 4-5: Color planes (1) |
| 51 | + // 6-7: Bits per pixel (32) |
| 52 | + // 8-11: Size of image data |
| 53 | + // 12-15: Offset of image data |
| 54 | + const entry = Buffer.alloc(16); |
| 55 | + entry.writeUInt8(0, 0); // 256 width -> 0 |
| 56 | + entry.writeUInt8(0, 1); // 256 height -> 0 |
| 57 | + entry.writeUInt8(0, 2); |
| 58 | + entry.writeUInt8(0, 3); |
| 59 | + entry.writeUInt16LE(1, 4); |
| 60 | + entry.writeUInt16LE(32, 6); |
| 61 | + entry.writeUInt32LE(pngBuffer.length, 8); |
| 62 | + entry.writeUInt32LE(6 + 16, 12); // Header (6) + 1 Entry (16) |
| 63 | + |
| 64 | + const icoBuffer = Buffer.concat([header, entry, pngBuffer]); |
| 65 | + fs.writeFileSync(outputIco, icoBuffer); |
| 66 | + |
| 67 | + // Clean up |
| 68 | + fs.unlinkSync(tempPng); |
| 69 | + |
| 70 | + console.log(`Successfully created ${outputIco}`); |
| 71 | +} catch (error) { |
| 72 | + console.error('Error generating ICO:', error); |
| 73 | + process.exit(1); |
| 74 | +} |
0 commit comments