Last active
June 23, 2024 11:39
-
-
Save twolfson/7656254 to your computer and use it in GitHub Desktop.
Proof of concept for creating .ico files in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference for encoding | |
// http://en.wikipedia.org/wiki/ICO_%28file_format%29#Outline | |
var fs = require('fs'); | |
var file = fs.createWriteStream('out.ico'); | |
// Write out the .ico header | |
// Reserved space | |
var buff = new Buffer(2); | |
buff.writeUInt8(0, 0); | |
file.write(buff, 'binary'); | |
// Indiciate ico file | |
buff = new Buffer(2); | |
buff.writeUInt8(1, 0); | |
file.write(buff, 'binary'); | |
// Indiciate 1 image | |
buff = new Buffer(2); | |
buff.writeUInt8(1, 0); | |
file.write(buff, 'binary'); | |
// Create the image | |
var pngparse = require('pngparse'); | |
var image = fs.readFileSync('sprite1.png'); | |
pngparse.parse(image, function (err, data) { | |
// If there was an error, throw it | |
if (err) { throw err; } | |
// Image is 50 px wide | |
buff = new Buffer(1); | |
buff.writeUInt8(50, 0); | |
file.write(buff, 'binary'); | |
// Image is 50 px tall | |
buff = new Buffer(1); | |
buff.writeUInt8(50, 0); | |
file.write(buff, 'binary'); | |
// Specify no color palette | |
// TODO: Not sure if this is appropriate | |
buff = new Buffer(1); | |
buff.writeUInt8(0, 0); | |
file.write(buff, 'binary'); | |
// Reserved space | |
// TODO: Not sure if this is appropriate | |
buff = new Buffer(1); | |
buff.writeUInt8(0, 0); | |
file.write(buff, 'binary'); | |
// Specify no color planes | |
// TODO: Not sure if this is appropriate | |
buff = new Buffer(1); | |
buff.writeUInt8(1, 0); | |
file.write(buff, 'binary'); | |
// Specify 8 bits per pixel (bit depth) | |
// TODO: Quite confident in this one | |
buff = new Buffer(2); | |
buff.writeUInt8(8, 0); | |
file.write(buff, 'binary'); | |
// Specify image size in bytes | |
// DEV: Assuming LE means little endian | |
// TODO: Semi-confident in this one | |
buff = new Buffer(4); | |
buff.writeUInt32LE(image.length, 0); | |
file.write(buff, 'binary'); | |
// Specify image size in bytes | |
// TODO: Not that confident in this one | |
buff = new Buffer(4); | |
buff.writeUInt32LE(file._writableState.length, 0); | |
file.write(buff, 'binary'); | |
// Dump the .png | |
file.write(image, 'binary'); | |
// Close the image | |
file.end(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gist-ico-poc", | |
"version": "0.1.0", | |
"description": "Proof of concept to encode a PNG to an ICO", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git@gist.github.com:7656254.git" | |
}, | |
"keywords": [ | |
"png", | |
"ico" | |
], | |
"author": "Todd Wolfson <todd@twolfson.com>", | |
"license": "UNLICENSE", | |
"bugs": { | |
"url": "https://gist.github.com/7656254" | |
}, | |
"dependencies": { | |
"pngparse": "~2.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment