s2-tools
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
35 lines • 1.26 kB
JavaScript
/**
* Image decoder
* @param buffer - the input buffer
* @param options - user defined options
* @returns - the decoded buffer
*/
export async function imageDecoder(buffer, options) {
const blob = new Blob([buffer]); // e.g. { type: 'image/png' }
const imageBitmap = await createImageBitmap(blob);
let { x = 0, y = 0, width = imageBitmap.width, height = imageBitmap.height } = options ?? {};
const { modulo = 1 } = options ?? {};
// If modulo exists, shift x and y by modulo's diff
const diff = imageBitmap.width % modulo;
if (diff !== 0) {
x = y = Math.min(diff / 2);
width = height = imageBitmap.width - diff;
}
// Create OffscreenCanvas and draw
const canvas = new OffscreenCanvas(imageBitmap.width, imageBitmap.height);
const ctx = canvas.getContext('2d');
if (ctx === null)
throw new Error('Could not get 2d context');
ctx.drawImage(imageBitmap, 0, 0);
return ctx.getImageData(x, y, width, height);
}
/**
* Image decoder
* @param buffer - the input buffer
* @returns - the decoded buffer
*/
export async function imageDecoderBuffer(buffer) {
const imageData = await imageDecoder(buffer);
return imageData.data.buffer;
}
//# sourceMappingURL=decoder.js.map