Skip to content

Commit

Permalink
jpeg encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
matmen committed Dec 6, 2020
1 parent 5263fd9 commit fce1f77
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
13 changes: 13 additions & 0 deletions ImageScript.js
Expand Up @@ -1079,6 +1079,19 @@ class Image {
return await png.encode(this.bitmap, {width: this.width, height: this.height, level: compression, channels: 4});
}

/**
* Encodes the image into a JPEG
* @param {number} [quality=90] The JPEG quality to use
* @return {Promise<Uint8Array>}
*/
async encodeJPEG(quality = 90) {
quality = Math.max(1, Math.min(100, quality));
const jpegCanvas = new this.constructor(this.width, this.height);
jpegCanvas.fill(0xff);
jpegCanvas.composite(this);
return jpeglib.encode(this.width, this.height, quality, jpegCanvas.bitmap);
}

/**
* Decodes an image (PNG or JPEG)
* @param {Buffer|Uint8Array} data The binary data to decode
Expand Down
59 changes: 41 additions & 18 deletions utils/wasm/jpeg.js
@@ -1,5 +1,5 @@
const {readFile} = require('fs').promises;
const {join} = require('path');
const {promises: {readFile}} = require('fs');

let wasm = new Promise(async resolve => {
const module = new WebAssembly.Module(await readFile(join(__dirname, './jpeg.wasm')));
Expand All @@ -10,7 +10,6 @@ let wasm = new Promise(async resolve => {
});

let cachegetUint8Memory0 = null;

function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
Expand All @@ -36,6 +35,10 @@ function getInt32Memory0() {
return cachegetInt32Memory0;
}

function getArrayU8FromWasm0(ptr, len) {
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}

let cachegetUint16Memory0 = null;

function getUint16Memory0() {
Expand All @@ -49,39 +52,60 @@ function getArrayU16FromWasm0(ptr, len) {
return getUint16Memory0().subarray(ptr / 2, ptr / 2 + len);
}

function getArrayU8FromWasm0(ptr, len) {
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}

module.exports = {
/**
* @param {number} width
* @param {number} height
* @param {number} quality
* @param {Uint8Array|Uint8ClampedArray} buffer
* @returns {Uint8Array}
*/
async encode(width, height, quality, buffer) {
wasm = await wasm;

try {
const retptr = wasm.__wbindgen_export_0.value - 16;
wasm.__wbindgen_export_0.value = retptr;
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
wasm.encode(retptr, width, height, quality, ptr0, WASM_VECTOR_LEN);
const r0 = getInt32Memory0()[retptr / 4];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const v1 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
} finally {
wasm.__wbindgen_export_0.value += 16;
}
},
/**
* @param {number} ptr
* @param {Uint8Array} buffer
* @param {number} _width
* @param {number} _height
* @param {number} width
* @param {number} height
* @returns {number}
*/
async decode(ptr, buffer, _width, _height) {
async decode(ptr, buffer, width, height) {
wasm = await wasm;

const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN, _width, _height);
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN, width, height);
},
/**
* @param {number} id
* @returns {Uint16Array}
*/
meta(id) {
try {
const retptr = wasm.__wbindgen_export_1.value - 16;
wasm.__wbindgen_export_1.value = retptr;
const retptr = wasm.__wbindgen_export_0.value - 16;
wasm.__wbindgen_export_0.value = retptr;
wasm.meta(retptr, id);
const r0 = getInt32Memory0()[retptr / 4];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const v0 = getArrayU16FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 2);
return v0;
} finally {
wasm.__wbindgen_export_1.value += 16;
wasm.__wbindgen_export_0.value += 16;
}
},
/**
Expand All @@ -90,16 +114,16 @@ module.exports = {
*/
buffer(id) {
try {
const retptr = wasm.__wbindgen_export_1.value - 16;
wasm.__wbindgen_export_1.value = retptr;
const retptr = wasm.__wbindgen_export_0.value - 16;
wasm.__wbindgen_export_0.value = retptr;
wasm.buffer(retptr, id);
const r0 = getInt32Memory0()[retptr / 4];
const r1 = getInt32Memory0()[retptr / 4 + 1];
const v0 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v0;
} finally {
wasm.__wbindgen_export_1.value += 16;
wasm.__wbindgen_export_0.value += 16;
}
},
/**
Expand All @@ -108,5 +132,4 @@ module.exports = {
free(id) {
wasm.free(id);
}
};

}
Binary file modified utils/wasm/jpeg.wasm
Binary file not shown.

0 comments on commit fce1f77

Please sign in to comment.