Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add types for wasm codec modules #450

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dicomImageLoader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"@babel/plugin-proposal-object-rest-spread": "^7.14.7",
"@babel/runtime-corejs3": "^7.15.4",
"@types/emscripten": "^1.39.6",
"@types/jasmine": "^4.3.1",
"@types/karma": "^6.3.3",
"chai": "^4.3.4",
Expand Down
18 changes: 18 additions & 0 deletions packages/dicomImageLoader/src/shared/decoders/codec-charls.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference types="emscripten" />

declare module '@cornerstonejs/codec-charls/dist/charlswasm_decode' {
export class JpegLSDecoder {
decode: () => any;
getDecodedBuffer: () => any;
getEncodedBuffer: (length: number) => any;
getFrameInfo: () => any;
getInterleaveMode: () => any;
getNearLossless: () => any;
}
export interface CharlsModule extends EmscriptenModule {
JpegLSDecoder: typeof JpegLSDecoder;
getExceptionMessage: (exception: number) => any;
}
declare const Module: EmscriptenModuleFactory<CharlsModule>;
export default Module;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference types="emscripten" />

declare module '@cornerstonejs/codec-libjpeg-turbo-8bit/dist/libjpegturbowasm_decode' {
export class LibJpegTurbo8Bit {
decode: () => any;
getDecodedBuffer: () => any;
getEncodedBuffer: (length: number) => any;
getFrameInfo: () => any;
}
export interface OpenJpegModule extends EmscriptenModule {
JPEGDecoder: typeof LibJpegTurbo8Bit;
}
declare const Module: EmscriptenModuleFactory<OpenJpegModule>;
export default Module;
}
24 changes: 24 additions & 0 deletions packages/dicomImageLoader/src/shared/decoders/codec-openjpeg.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference types="emscripten" />

declare module '@cornerstonejs/codec-openjpeg/dist/openjpegwasm_decode' {
export class J2KDecoder {
decode: () => any;
getBlockDimensions: () => any;
getColorSpace: () => any;
getDecodedBuffer: () => any;
getEncodedBuffer: (length: number) => any;
getFrameInfo: () => any;
getImageOffset: () => any;
getIsReversible: () => any;
getNumDecompositions: () => any;
getNumLayers: () => any;
getProgressionOrder: () => number;
getTileOffset: () => any;
getTileSize: () => any;
}
export interface OpenJpegModule extends EmscriptenModule {
J2KDecoder: typeof J2KDecoder;
}
declare const Module: EmscriptenModuleFactory<OpenJpegModule>;
export default Module;
}
12 changes: 10 additions & 2 deletions packages/dicomImageLoader/src/shared/decoders/decodeJPEG2000.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// https://emscripten.org/docs/api_reference/module.html
import type {
J2KDecoder,
OpenJpegModule,
} from '@cornerstonejs/codec-openjpeg/dist/openjpegwasm_decode';
import openJpegFactory from '@cornerstonejs/codec-openjpeg/dist/openjpegwasm_decode';

const openjpegWasm = new URL(
Expand All @@ -8,7 +12,11 @@ const openjpegWasm = new URL(

import { ImageFrame, WebWorkerDecodeConfig } from '../../types';

const local = {
const local: {
codec: OpenJpegModule;
decoder: J2KDecoder;
decodeConfig: WebWorkerDecodeConfig;
} = {
codec: undefined,
decoder: undefined,
decodeConfig: {} as WebWorkerDecodeConfig,
Expand All @@ -26,7 +34,7 @@ export function initialize(
const openJpegModule = openJpegFactory({
locateFile: (f) => {
if (f.endsWith('.wasm')) {
return openjpegWasm;
return openjpegWasm.toString();
}

return f;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ByteArray } from 'dicom-parser';

import type {
LibJpegTurbo8Bit,
OpenJpegModule,
} from '@cornerstonejs/codec-libjpeg-turbo-8bit/dist/libjpegturbowasm_decode';
import libjpegTurboFactory from '@cornerstonejs/codec-libjpeg-turbo-8bit/dist/libjpegturbowasm_decode';
import { ByteArray } from 'dicom-parser';

const libjpegTurboWasm = new URL(
'@cornerstonejs/codec-libjpeg-turbo-8bit/dist/libjpegturbowasm_decode.wasm',
Expand All @@ -9,7 +12,10 @@ const libjpegTurboWasm = new URL(

import { ImageFrame } from '../../types';

const local = {
const local: {
codec: OpenJpegModule;
decoder: LibJpegTurbo8Bit;
} = {
codec: undefined,
decoder: undefined,
};
Expand All @@ -22,7 +28,7 @@ function initLibjpegTurbo(): Promise<void> {
const libjpegTurboModule = libjpegTurboFactory({
locateFile: (f) => {
if (f.endsWith('.wasm')) {
return libjpegTurboWasm;
return libjpegTurboWasm.toString();
}

return f;
Expand Down
12 changes: 10 additions & 2 deletions packages/dicomImageLoader/src/shared/decoders/decodeJPEGLS.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
CharlsModule,
JpegLSDecoder,
} from '@cornerstonejs/codec-charls/dist/charlswasm_decode';
import charlsFactory from '@cornerstonejs/codec-charls/dist/charlswasm_decode';

const charlsWasm = new URL(
Expand All @@ -8,7 +12,11 @@ const charlsWasm = new URL(
import { ByteArray } from 'dicom-parser';
import { ImageFrame, WebWorkerDecodeConfig } from '../../types';

const local = {
const local: {
codec: CharlsModule;
decoder: JpegLSDecoder;
decodeConfig: WebWorkerDecodeConfig;
} = {
codec: undefined,
decoder: undefined,
decodeConfig: {} as WebWorkerDecodeConfig,
Expand All @@ -32,7 +40,7 @@ export function initialize(
const charlsModule = charlsFactory({
locateFile: (f) => {
if (f.endsWith('.wasm')) {
return charlsWasm;
return charlsWasm.toString();
}

return f;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5520,6 +5520,11 @@
"@types/got" "^8"
"@types/node" "*"

"@types/emscripten@^1.39.6":
version "1.39.6"
resolved "https://registry.yarnpkg.com/@types/emscripten/-/emscripten-1.39.6.tgz#698b90fe60d44acf93c31064218fbea93fbfd85a"
integrity sha512-H90aoynNhhkQP6DRweEjJp5vfUVdIj7tdPLsu7pq89vODD/lcugKfZOsfgwpvM6XUewEp2N5dCg1Uf3Qe55Dcg==

"@types/eslint-scope@^3.7.0", "@types/eslint-scope@^3.7.3":
version "3.7.3"
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.3.tgz#125b88504b61e3c8bc6f870882003253005c3224"
Expand Down