ico-utils
Version:
A javascript library to create ICO file from PNG images and extract PNG images from ICO file.
32 lines (31 loc) • 1.23 kB
JavaScript
import { joinBytesToNumber } from './helpers/bytesConverter.js';
const CHANNELS_PER_PIXEL = {
0: 1,
2: 3,
3: 1,
4: 2,
6: 4,
};
export function parsePng(buffer) {
const uint8 = new Uint8Array(buffer.slice(0, 33));
if (!isPngSignatureValid(uint8.slice(0, 8))) {
throw new Error('INVALID_PNG_IMAGE');
}
if (!isIhdrChunkValid(uint8.slice(8, 16))) {
throw new Error('INVALID_PNG_IMAGE');
}
const width = joinBytesToNumber(Array.from(uint8.slice(16, 20).reverse()));
const height = joinBytesToNumber(Array.from(uint8.slice(20, 24).reverse()));
const bitsPerChannel = uint8[24];
const colourType = uint8[25];
const bitsPerPixel = bitsPerChannel * CHANNELS_PER_PIXEL[colourType];
return { width, height, bpp: bitsPerPixel };
}
export function isPngSignatureValid(imageSignatureBytes) {
const pngSignatureBytes = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
return pngSignatureBytes.toString() === imageSignatureBytes.toString();
}
export function isIhdrChunkValid(chunkBeginBytes) {
const ihdrChunkBeginBytes = [0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52];
return ihdrChunkBeginBytes.toString() === chunkBeginBytes.toString();
}