Skip to content

Commit

Permalink
Replaced upng-js with pngjs
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Sep 24, 2023
1 parent 651344f commit a244a18
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 47 deletions.
40 changes: 19 additions & 21 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
"copyright": "Copyright (c) 2019-2023 JrMasterModelBuilder",
"license": "MPL-2.0",
"dependencies": {
"upng-js": "^2.1.0"
"pngjs": "^7.0.0"
},
"devDependencies": {
"@babel/cli": "^7.22.15",
"@babel/core": "^7.22.20",
"@babel/preset-env": "^7.22.20",
"@babel/preset-typescript": "^7.22.15",
"@types/node": "^20.6.4",
"@types/upng-js": "^2.1.3",
"@types/pngjs": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"babel-plugin-esm-resolver": "^2.3.1",
Expand Down
44 changes: 20 additions & 24 deletions src/icon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import UPNG from 'upng-js';
import {PNG} from 'pngjs';

import {IImageData} from './types';

Expand All @@ -18,11 +18,17 @@ export abstract class Icon {
* @returns Image data.
*/
protected _decodePngToRgba(data: Readonly<Uint8Array>) {
const image = UPNG.decode(data);
const {
width,
height,
data: d
} = PNG.sync.read(
Buffer.from(data.buffer, data.byteOffset, data.byteLength)
);
return {
width: image.width,
height: image.height,
data: new Uint8Array(UPNG.toRGBA8(image)[0])
width,
height,
data: new Uint8Array(d.buffer, d.byteOffset, d.byteLength)
} as IImageData;
}

Expand All @@ -33,25 +39,15 @@ export abstract class Icon {
* @returns PNG data.
*/
protected _encodeRgbaToPng(imageData: Readonly<IImageData>) {
return new Uint8Array(
(
UPNG.encode as (
imgs: ArrayBuffer[],
w: number,
h: number,
cnum: number,
dels?: number[],
forbidPlte?: boolean
) => ArrayBuffer
)(
[imageData.data.buffer],
imageData.width,
imageData.height,
0,
[],
true
)
);
const {width, height, data} = imageData;
const png = new PNG({width, height});
png.data = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
const d = PNG.sync.write(png, {
deflateLevel: 9,
deflateStrategy: 1,
deflateChunkSize: 32 * 1024
});
return new Uint8Array(d.buffer, d.byteOffset, d.byteLength);
}

/**
Expand Down

0 comments on commit a244a18

Please sign in to comment.