Skip to content

Commit

Permalink
feat: The UZIP dependency has been replaced with an internal UZIP imp…
Browse files Browse the repository at this point in the history
…lementation
  • Loading branch information
yegor-pelykh committed May 15, 2024
1 parent b6a0596 commit 0adf3d0
Show file tree
Hide file tree
Showing 9 changed files with 1,200 additions and 9 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
},
"devDependencies": {
"@types/node": "^20.12.11",
"@types/uzip": "^0.20201231.2",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
Expand All @@ -124,8 +123,5 @@
"release-it": "^17.2.1",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"dependencies": {
"uzip": "^0.20201231.0"
}
}
4 changes: 2 additions & 2 deletions src/formats/png-decoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

import { inflate } from 'uzip';
import { inflate } from '../uzip/uzip';
import { Crc32 } from '../common/crc32';
import { InputBuffer } from '../common/input-buffer';
import { ArrayUtils } from '../common/array-utils';
Expand Down Expand Up @@ -768,7 +768,7 @@ export class PngDecoder implements Decoder {

let uncompressed: Uint8Array | undefined = undefined;
try {
uncompressed = inflate(imageData);
uncompressed = inflate(imageData) as Uint8Array;
} catch (error) {
console.error(error);
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/formats/png-encoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

import { deflate } from 'uzip';
import { deflate } from '../uzip/uzip';
import { Crc32 } from '../common/crc32';
import { OutputBuffer } from '../common/output-buffer';
import { StringUtils } from '../common/string-utils';
Expand Down
2 changes: 1 addition & 1 deletion src/formats/tiff/tiff-image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

import { inflate } from 'uzip';
import { inflate } from '../../uzip/uzip';
import { ColorUtils } from '../../color/color-utils';
import { Format } from '../../color/format';
import { ArrayUtils } from '../../common/array-utils';
Expand Down
2 changes: 1 addition & 1 deletion src/image/icc-profile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

import { inflate, deflate } from 'uzip';
import { inflate, deflate } from '../uzip/uzip';
import { ArrayUtils } from '../common/array-utils';
import { IccProfileCompression } from './icc-profile-compression';

Expand Down
87 changes: 87 additions & 0 deletions src/uzip/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/** @format */

export function readUshort(buff, p) {
return buff[p] | (buff[p + 1] << 8);
}
export function writeUshort(buff, p, n) {
buff[p] = n & 255;
buff[p + 1] = (n >> 8) & 255;
}
export function readUint(buff, p) {
return (
buff[p + 3] * (256 * 256 * 256) +
((buff[p + 2] << 16) | (buff[p + 1] << 8) | buff[p])
);
}
export function writeUint(buff, p, n) {
buff[p] = n & 255;
buff[p + 1] = (n >> 8) & 255;
buff[p + 2] = (n >> 16) & 255;
buff[p + 3] = (n >> 24) & 255;
}
export function readASCII(buff, p, l) {
let s = '';
for (let i = 0; i < l; i++) s += String.fromCharCode(buff[p + i]);
return s;
}
export function writeASCII(data, p, s) {
for (let i = 0; i < s.length; i++) data[p + i] = s.charCodeAt(i);
}
export function pad(n) {
return n.length < 2 ? `0${n}` : n;
}
export function readUTF8(buff, p, l) {
let s = '';
let ns;
for (let i = 0; i < l; i++) s += `%${pad(buff[p + i].toString(16))}`;
try {
ns = decodeURIComponent(s);
} catch (e) {
return readASCII(buff, p, l);
}
return ns;
}
export function writeUTF8(buff, p, str) {
const strl = str.length;
let i = 0;
for (let ci = 0; ci < strl; ci++) {
const code = str.charCodeAt(ci);
if ((code & (0xffffffff - (1 << 7) + 1)) == 0) {
buff[p + i] = code;
i++;
} else if ((code & (0xffffffff - (1 << 11) + 1)) == 0) {
buff[p + i] = 192 | (code >> 6);
buff[p + i + 1] = 128 | ((code >> 0) & 63);
i += 2;
} else if ((code & (0xffffffff - (1 << 16) + 1)) == 0) {
buff[p + i] = 224 | (code >> 12);
buff[p + i + 1] = 128 | ((code >> 6) & 63);
buff[p + i + 2] = 128 | ((code >> 0) & 63);
i += 3;
} else if ((code & (0xffffffff - (1 << 21) + 1)) == 0) {
buff[p + i] = 240 | (code >> 18);
buff[p + i + 1] = 128 | ((code >> 12) & 63);
buff[p + i + 2] = 128 | ((code >> 6) & 63);
buff[p + i + 3] = 128 | ((code >> 0) & 63);
i += 4;
} else throw 'e';
}
return i;
}
export function sizeUTF8(str) {
const strl = str.length;
let i = 0;
for (let ci = 0; ci < strl; ci++) {
const code = str.charCodeAt(ci);
if ((code & (0xffffffff - (1 << 7) + 1)) == 0) {
i++;
} else if ((code & (0xffffffff - (1 << 11) + 1)) == 0) {
i += 2;
} else if ((code & (0xffffffff - (1 << 16) + 1)) == 0) {
i += 3;
} else if ((code & (0xffffffff - (1 << 21) + 1)) == 0) {
i += 4;
} else throw 'e';
}
return i;
}
Loading

0 comments on commit 0adf3d0

Please sign in to comment.