Skip to content

Commit

Permalink
converted @cwasm/webp to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin IGARASHI committed Dec 29, 2020
1 parent 14a3c3b commit 3e93631
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 14 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ module.exports = {
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
rules: {
"@typescript-eslint/ban-ts-comment": "off"
},
};
44 changes: 35 additions & 9 deletions package-lock.json

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

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@watergis/terrain-rgb",
"version": "1.1.0",
"version": "1.1.1",
"description": "This module is to get elevation from terrain RGB tilesets by longitude and latitude.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -44,8 +44,10 @@
"typescript": "^3.9.6"
},
"dependencies": {
"@cwasm/webp": "^0.1.5",
"@canvas/image-data": "^1.0.0",
"axios": "^0.21.0",
"fs": "0.0.1-security",
"path": "^0.12.7",
"png-ts": "0.0.3"
},
"jest": {
Expand All @@ -58,7 +60,7 @@
},
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.json"
"tsconfig": "tsconfig.json"
}
},
"testMatch": [
Expand Down
4 changes: 2 additions & 2 deletions src/terrainrgb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as tilebelt from './tilebelt';
import axios from 'axios';
import PNG from 'png-ts';
import webp from '@cwasm/webp';
import { decode } from './webp';


class TerrainRGB {
Expand Down Expand Up @@ -85,7 +85,7 @@ class TerrainRGB {
}

getElevationFromWEBP(binary: Uint8Array, tile: number[], lng: number, lat: number, tileSize: number): number{
const image = webp.decode(binary);
const image = decode(binary);
const pixels = image.data;

const data = [];
Expand Down
80 changes: 80 additions & 0 deletions src/webp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* global WebAssembly */

/**
* This module was inpired from the below repository.
* https://github.com/LinusU/cwasm-webp
* I converted the module to typescript.
* Thanks a lot!
*/

import fs from 'fs'
import path from 'path'

import ImageData from '@canvas/image-data'

const stubs = {
fd_close () { throw new Error('Syscall fd_close not implemented') },
fd_seek () { throw new Error('Syscall fd_seek not implemented') },
fd_write () { throw new Error('Syscall fd_write not implemented') }
}

const code = fs.readFileSync(path.join(__dirname, 'webp.wasm'))
const wasmModule = new WebAssembly.Module(code)
const instance = new WebAssembly.Instance(wasmModule, { wasi_snapshot_preview1: stubs })

export const decode = (input: Uint8Array): ImageData => {
// Allocate memory to hand over the input data to WASM
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const inputPointer = instance.exports.malloc(input.byteLength)
// @ts-ignore
const targetView = new Uint8Array(instance.exports.memory.buffer, inputPointer, input.byteLength)

// Copy input data into WASM readable memory
targetView.set(input)

// Allocate metadata (width & height)
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const metadataPointer : number = instance.exports.malloc(8)

// Decode input data
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const outputPointer = instance.exports.WebPDecodeRGBA(inputPointer, input.byteLength, metadataPointer, metadataPointer + 4)

// Free the input data in WASM land
// @ts-ignore
instance.exports.free(inputPointer)

// Guard return value for NULL pointer
if (outputPointer === 0) {
// @ts-ignore
instance.exports.free(metadataPointer)
throw new Error('Failed to decode WebP image')
}

// Read returned metadata
// @ts-ignore
const metadata = new Uint32Array(instance.exports.memory.buffer, metadataPointer, 2)
const [width, height] = metadata

// Free the metadata in WASM land
// @ts-ignore
instance.exports.free(metadataPointer)

// Create an empty buffer for the resulting data
const outputSize = (width * height * 4)
const output = new Uint8ClampedArray(outputSize)

// Copy decoded data from WASM memory to JS
// @ts-ignore
output.set(new Uint8Array(instance.exports.memory.buffer, outputPointer, outputSize))

// Free WASM copy of decoded data
// @ts-ignore
instance.exports.free(outputPointer)

// Return decoded image as raw data
return new ImageData(output, width, height)
}
Binary file added src/webp.wasm
Binary file not shown.

0 comments on commit 3e93631

Please sign in to comment.