aboutsummaryrefslogtreecommitdiff
path: root/build/js/io/encoders/ico.js
blob: e6e8e902b207f98bdc33496c94de8a227e6d277b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// ICO container with PNG-compressed entries (supported since Windows Vista and by all browsers).

/** images: [{ width, height, png: Uint8Array }] */
export function encodeICO(images) {
  const headerSize = 6 + images.length * 16;
  const total = headerSize + images.reduce((s, im) => s + im.png.length, 0);
  const buf = new ArrayBuffer(total);
  const v = new DataView(buf);
  const u8 = new Uint8Array(buf);
  v.setUint16(0, 0, true);
  v.setUint16(2, 1, true); // icon
  v.setUint16(4, images.length, true);
  let offset = headerSize;
  images.forEach((im, i) => {
    const o = 6 + i * 16;
    v.setUint8(o, im.width >= 256 ? 0 : im.width);
    v.setUint8(o + 1, im.height >= 256 ? 0 : im.height);
    v.setUint8(o + 2, 0);
    v.setUint8(o + 3, 0);
    v.setUint16(o + 4, 1, true);
    v.setUint16(o + 6, 32, true);
    v.setUint32(o + 8, im.png.length, true);
    v.setUint32(o + 12, offset, true);
    u8.set(im.png, offset);
    offset += im.png.length;
  });
  return new Blob([buf], { type: 'image/x-icon' });
}