Coder Social home page Coder Social logo

ico-endec-js's Introduction

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.

Accessor Type Description
width Number width of the image, maximum of 256
height Number height of the image, maximum of 256
colors Number number of colors
colorPlanes Number color planes of an ICO image
bitsPerPixel Number bits per pixel of an ICO image
horizontalHotspot Number horizontal hotspot of a CUR image
verticalHotspot Number vertical hotspot of a CUR image
imageSize Number (interal) size of imageData's buffer
imageOffset Number (interal) offset start of the image data
imageType String 'png' or 'bmp'
imageData Buffer image data of the icon

ico-endec-js's People

Contributors

kettek avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

vheemstra

ico-endec-js's Issues

Palette chunk check starts at wrong index

if (imageData[29] !== 'P' && imageData[30] !== 'L' && imageData[31] !== 'T' && imageData[32] !== 'E') {

The indexes for the PLTE-chunk check are wrong. They should be further, depending on the length of the IHDR-chunk + the 4 byte CRC of that chunk (see specification).

In this case, it should skip 4 bytes (CRC of the IHDR-chunk) and another 4 bytes (the size indicator of the PLTE-chunk).

Check out my visual helper for PNG.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.