Skip to content

Commit ab2f4f0

Browse files
committed
port latest changes to deno
1 parent 73a595d commit ab2f4f0

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

‎ImageScript.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as gif from './utils/gif.js';
33
import * as fontlib from './utils/wasm/font.js';
44
import * as svglib from './utils/wasm/svg.js';
55
import * as jpeglib from './utils/wasm/jpeg.js';
6-
const jpeglib = require('./utils/wasm/jpeg');
76

87
/**
98
* Represents an image; provides utility functions

‎tests/jpeg.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const fs = require('fs').promises;
2-
const {Image} = require('../ImageScript');
1+
import {Image} from '../ImageScript.js';
2+
import {equal} from "https://deno.land/std/bytes/mod.ts";
33

44
const panic = message => {
55
console.error(message);
66
process.exit(1);
77
};
88

99
(async () => {
10-
const binary = await fs.readFile('./tests/targets/external.jpg');
10+
const binary = await Deno.readFile('./tests/targets/external.jpg');
1111
const image = await Image.decode(binary);
1212

1313
if ([image.width, image.height].some(v => v !== 638))
1414
panic('dimensions don\'t match');
15-
if (!Buffer.from(image.bitmap.slice(0, 4)).equals(Buffer.from([70, 65, 61, 255])))
15+
if (!equal(image.bitmap.slice(0, 4), [70, 65, 61, 255]))
1616
panic('pixel doesn\'t match');
1717

1818
await image.encodeJPEG(100);

‎utils/wasm/jpeg.js‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ let wasm;
88
}
99

1010
let cachegetUint8Memory0 = null;
11-
1211
function getUint8Memory0() {
1312
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
1413
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
@@ -34,6 +33,10 @@ function getInt32Memory0() {
3433
return cachegetInt32Memory0;
3534
}
3635

36+
function getArrayU8FromWasm0(ptr, len) {
37+
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
38+
}
39+
3740
let cachegetUint16Memory0 = null;
3841

3942
function getUint16Memory0() {
@@ -47,21 +50,39 @@ function getArrayU16FromWasm0(ptr, len) {
4750
return getUint16Memory0().subarray(ptr / 2, ptr / 2 + len);
4851
}
4952

50-
function getArrayU8FromWasm0(ptr, len) {
51-
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
53+
/**
54+
* @param {number} width
55+
* @param {number} height
56+
* @param {number} quality
57+
* @param {Uint8Array|Uint8ClampedArray} buffer
58+
* @returns {Uint8Array}
59+
*/
60+
export function encode(width, height, quality, buffer) {
61+
try {
62+
const retptr = wasm.__wbindgen_export_0.value - 16;
63+
wasm.__wbindgen_export_0.value = retptr;
64+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
65+
wasm.encode(retptr, width, height, quality, ptr0, WASM_VECTOR_LEN);
66+
const r0 = getInt32Memory0()[retptr / 4];
67+
const r1 = getInt32Memory0()[retptr / 4 + 1];
68+
const v1 = getArrayU8FromWasm0(r0, r1).slice();
69+
wasm.__wbindgen_free(r0, r1 * 1);
70+
return v1;
71+
} finally {
72+
wasm.__wbindgen_export_0.value += 16;
73+
}
5274
}
5375

5476
/**
5577
* @param {number} ptr
5678
* @param {Uint8Array} buffer
57-
* @param {number} _width
58-
* @param {number} _height
79+
* @param {number} width
80+
* @param {number} height
5981
* @returns {number}
6082
*/
61-
export async function decode(ptr, buffer, _width, _height) {
62-
wasm = await wasm;
83+
export function decode(ptr, buffer, width, height) {
6384
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
64-
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN, _width, _height);
85+
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN, width, height);
6586
}
6687

6788
/**
@@ -70,16 +91,16 @@ export async function decode(ptr, buffer, _width, _height) {
7091
*/
7192
export function meta(id) {
7293
try {
73-
const retptr = wasm.__wbindgen_export_1.value - 16;
74-
wasm.__wbindgen_export_1.value = retptr;
94+
const retptr = wasm.__wbindgen_export_0.value - 16;
95+
wasm.__wbindgen_export_0.value = retptr;
7596
wasm.meta(retptr, id);
7697
const r0 = getInt32Memory0()[retptr / 4];
7798
const r1 = getInt32Memory0()[retptr / 4 + 1];
7899
const v0 = getArrayU16FromWasm0(r0, r1).slice();
79100
wasm.__wbindgen_free(r0, r1 * 2);
80101
return v0;
81102
} finally {
82-
wasm.__wbindgen_export_1.value += 16;
103+
wasm.__wbindgen_export_0.value += 16;
83104
}
84105
}
85106

@@ -89,16 +110,16 @@ export function meta(id) {
89110
*/
90111
export function buffer(id) {
91112
try {
92-
const retptr = wasm.__wbindgen_export_1.value - 16;
93-
wasm.__wbindgen_export_1.value = retptr;
113+
const retptr = wasm.__wbindgen_export_0.value - 16;
114+
wasm.__wbindgen_export_0.value = retptr;
94115
wasm.buffer(retptr, id);
95116
const r0 = getInt32Memory0()[retptr / 4];
96117
const r1 = getInt32Memory0()[retptr / 4 + 1];
97118
const v0 = getArrayU8FromWasm0(r0, r1).slice();
98119
wasm.__wbindgen_free(r0, r1 * 1);
99120
return v0;
100121
} finally {
101-
wasm.__wbindgen_export_1.value += 16;
122+
wasm.__wbindgen_export_0.value += 16;
102123
}
103124
}
104125

0 commit comments

Comments
 (0)