Skip to content

Commit

Permalink
Merge pull request #31 from oolio-group/van/feat/add-upng-js
Browse files Browse the repository at this point in the history
feat: replace png-js with upng-js
  • Loading branch information
vankongv authored Oct 31, 2023
2 parents 810df31 + d0f731e commit 07b14e5
Show file tree
Hide file tree
Showing 4 changed files with 515 additions and 507 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"**/node_modules": true,
"**/coverage": true,
"**/lib": true,
"**/.vscode": true,
"**/yarn.lock": true,
"**/dist": true
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"xml",
"printer"
],
"version": "0.2.7",
"version": "0.2.8",
"files": [
"lib",
"LICENSE",
Expand Down Expand Up @@ -41,7 +41,7 @@
"mustache": "^4.1.0",
"mutable-buffer": "2.0.3",
"ndarray": "^1.0.19",
"pngjs": "^6.0.0",
"upng-js": "2.1.0",
"xml-parser": "^1.2.1"
},
"devDependencies": {
Expand Down
30 changes: 16 additions & 14 deletions src/nodes/image-node.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
import { XMLNode } from "../xml-node";
import { BufferBuilder, RASTER_MODE } from "../buffer-builder";
import ndarray from "ndarray";
import Image from "../image";
import pngjs from "pngjs";

const PNG = pngjs.PNG;
import { XMLNode } from '../xml-node';
import { BufferBuilder, RASTER_MODE } from '../buffer-builder';
import ndarray from 'ndarray';
import Image from '../image';
import upngjs from 'upng-js';

export default class ImageNode extends XMLNode {
constructor(node: any) {
super(node);
}

public open(bufferBuilder: BufferBuilder): BufferBuilder {
const img_data = PNG.sync.read(
Buffer.from(this.content.replace(/&#x2F/g, '/').slice("data:image/png;base64,".length), "base64")
const img_data = upngjs.decode(
Buffer.from(this.content.replace(/&#x2F/g, '/'), 'base64'),
);

const pixels = ndarray(
new Uint8Array(img_data.data),
[img_data.width | 0, img_data.height | 0, 4],
[4, (4 * img_data.width) | 0, 1],
0
0,
);

let mode;
switch (this.attributes.mode) {
case 'NORMAL':
mode = RASTER_MODE.NORMAL; break;
mode = RASTER_MODE.NORMAL;
break;
case 'DW':
mode = RASTER_MODE.DOUBLE_WIDTH; break;
mode = RASTER_MODE.DOUBLE_WIDTH;
break;
case 'DH':
mode = RASTER_MODE.DOUBLE_HEIGHT; break;
mode = RASTER_MODE.DOUBLE_HEIGHT;
break;
case 'DWH':
mode = RASTER_MODE.DOUBLE_WIDTH_HEIGHT; break;
mode = RASTER_MODE.DOUBLE_WIDTH_HEIGHT;
break;
default:
mode = RASTER_MODE.NORMAL;
}
Expand Down
Loading

0 comments on commit 07b14e5

Please sign in to comment.