0.1.6 • Published 4 years ago

ico-endec v0.1.6

Weekly downloads
2
License
MPL-2.0
Repository
github
Last release
4 years ago

ICO encoder & decoder

This JavaScript library provides an encoder and decoder for ICO and CUR files. Although this library can encode and decode both BMP and PNG images, BMP endec does not provide bitmasking support, and as such, will not work with some icons. However, PNG support is widespread and has become a more defacto standard for application icons, so this problem is largely moot -- but it still would be nice to have.

Encoding

icoEndec.encode(Buffer||ArrayBuffer)

The encode function takes an array of ArrayBuffers or Buffers that contain BMP or PNG data. It returns a Buffer containing the binary data of the ICO file.

Example

const icoEndec   = require('ico-endec')
const fsPromises = require('fs').Promises

(async () => {
  let icoBuffer = icoEndec.encode([
    await fsPromises.readFile('myIcon-16x16.png'),
    await fsPromises.readFile('myIcon-32x32.png'),
    await fsPromises.readFile('myIcon-64x64.png')
  ])
  
  await fsPromises.writeFile('myIcon.ico', icoBuffer)
})()

Decoding

icoEndec.decode(Buffer)

The decode function takes a Buffer or an ArrayBuffer that holds the binary data of an ICO file. It returns an array of IconEntries.

Example

const icoEndec   = require('ico-endec')
const fsPromises = require('fs').Promises

(async () => {
  let icons = icoEndec.decode(await fsPromises.readFile('myIcon.ico'))
  
  icons.forEach((icon, index) => {
    fsPromises.writeFile(`myIcon-${icon.width}x${icon.height}.${icon.imageType}`, icon.imageData)
  })
})()

IconEntry

The IconEntry class stores various information about the given icon entry.

AccessorTypeDescription
widthNumberwidth of the image, maximum of 256
heightNumberheight of the image, maximum of 256
colorsNumbernumber of colors
colorPlanesNumbercolor planes of an ICO image
bitsPerPixelNumberbits per pixel of an ICO image
horizontalHotspotNumberhorizontal hotspot of a CUR image
verticalHotspotNumbervertical hotspot of a CUR image
imageSizeNumber(interal) size of imageData's buffer
imageOffsetNumber(interal) offset start of the image data
imageTypeString'png' or 'bmp'
imageDataBufferimage data of the icon