Skip to content

Commit

Permalink
Custom optimized packbits encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Sep 24, 2023
1 parent 01c3deb commit 59a2d07
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"copyright": "Copyright (c) 2019-2023 JrMasterModelBuilder",
"license": "MPL-2.0",
"dependencies": {
"@fiahfy/packbits": "0.0.6",
"upng-js": "^2.1.0"
},
"devDependencies": {
Expand Down
12 changes: 2 additions & 10 deletions src/icon/icns.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {encode as packbitsEncode} from '@fiahfy/packbits';

import {IImageData} from '../types';
import {Icon} from '../icon';
import {concatUint8Arrays} from '../util';
import {concatUint8Arrays, packBitsIcns} from '../util';

const typeArgb = ['ic04', 'ic05'];

Expand Down Expand Up @@ -343,12 +341,6 @@ export class IconIcns extends Icon {
* @returns Compressed data.
*/
protected _encodePackBitsIcns(data: Readonly<Uint8Array>) {
const b = packbitsEncode(
Buffer.from(data.buffer, data.byteOffset, data.byteLength),
{
format: 'icns'
}
);
return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
return packBitsIcns(data);
}
}
44 changes: 44 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,47 @@ export function pngIhdr(data: Readonly<Uint8Array>): IPngIhdr {

throw new Error('Missing PNG IHDR tag');
}

/**
* Encode data using PackBits ICNS compression.
*
* @param data Data to be compressed.
* @returns Compressed data.
*/
export function packBitsIcns(data: Readonly<Uint8Array>) {
const chunks = [];
const l = data.length;
for (let i = 0; i < l; ) {
const b = data[i];
if (i + 2 >= l) {
// Not enough left for anything but literal data.
chunks.push(new Uint8Array([l - i - 1]), data.subarray(i, l));
break;
}
if (b === data[i + 1] && b === data[i + 2]) {
// 3+ bytes repeat RLE.
i += 2;
let c = 3;
for (; ++i < l && b === data[i] && c < 130; c++);
chunks.push(new Uint8Array([c + 125, b]));
} else {
// Literal until next 3 bytes repeat.
let e = i + 2;
let c = 3;
for (let r = 1, p = data[e]; ++e < l && c < 128; c++) {
const b = data[e];
if (p !== b) {
p = b;
r = 1;
} else if (++r > 2) {
e -= 2;
c -= 2;
break;
}
}
chunks.push(new Uint8Array([c - 1]), data.subarray(i, e));
i = e;
}
}
return concatUint8Arrays(chunks);
}

0 comments on commit 59a2d07

Please sign in to comment.