-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): port buffer-to-typed-array and deps to package
- Loading branch information
Showing
21 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/core/typescript/itk-wasm/src/buffer-to-typed-array.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import IntTypes from './interface-types/int-types.js' | ||
import FloatTypes from './interface-types/float-types.js' | ||
import type TypedArray from './typed-array.js' | ||
|
||
function bufferToTypedArray (wasmType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] | 'null' | null, buffer: ArrayBuffer): null | TypedArray { | ||
let typedArray: null | TypedArray = null | ||
switch (wasmType) { | ||
case IntTypes.UInt8: { | ||
typedArray = new Uint8Array(buffer) | ||
break | ||
} | ||
case IntTypes.Int8: { | ||
typedArray = new Int8Array(buffer) | ||
break | ||
} | ||
case IntTypes.UInt16: { | ||
typedArray = new Uint16Array(buffer) | ||
break | ||
} | ||
case IntTypes.Int16: { | ||
typedArray = new Int16Array(buffer) | ||
break | ||
} | ||
case IntTypes.UInt32: { | ||
typedArray = new Uint32Array(buffer) | ||
break | ||
} | ||
case IntTypes.Int32: { | ||
typedArray = new Int32Array(buffer) | ||
break | ||
} | ||
case IntTypes.UInt64: { | ||
if (typeof globalThis.BigUint64Array === 'function') { | ||
typedArray = new BigUint64Array(buffer) | ||
} else { | ||
// Sub with reasonable default. Will get cast to Uint8Array when | ||
// transferred to WebAssembly. | ||
typedArray = new Uint8Array(buffer) | ||
} | ||
break | ||
} | ||
case IntTypes.Int64: { | ||
if (typeof globalThis.BigInt64Array === 'function') { | ||
typedArray = new BigInt64Array(buffer) | ||
} else { | ||
// Sub with reasonable default. Will get cast to Uint8Array when | ||
// transferred to WebAssembly. | ||
typedArray = new Uint8Array(buffer) | ||
} | ||
break | ||
} | ||
case FloatTypes.Float32: { | ||
typedArray = new Float32Array(buffer) | ||
break | ||
} | ||
case FloatTypes.Float64: { | ||
typedArray = new Float64Array(buffer) | ||
break | ||
} | ||
case 'null': { | ||
typedArray = null | ||
break | ||
} | ||
case null: { | ||
typedArray = null | ||
break | ||
} | ||
default: | ||
throw new Error('Type is not supported as a TypedArray') | ||
} | ||
|
||
return typedArray | ||
} | ||
|
||
export default bufferToTypedArray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Core API interfaces, data structures, and functions | ||
|
||
export { default as bufferToTypedArray } from './buffer-to-typed-array.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// itk-wasm Node API interfaces, data structures, and functions | ||
|
||
export * from './index-common.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// itk-wasm Browser API interfaces, data structures, and functions | ||
|
||
export * from './index-common.js' |
6 changes: 6 additions & 0 deletions
6
packages/core/typescript/itk-wasm/src/interface-types/binary-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface BinaryFile { | ||
data: Uint8Array | ||
path: string | ||
} | ||
|
||
export default BinaryFile |
5 changes: 5 additions & 0 deletions
5
packages/core/typescript/itk-wasm/src/interface-types/binary-stream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface BinaryStream { | ||
data: Uint8Array | ||
} | ||
|
||
export default BinaryStream |
8 changes: 8 additions & 0 deletions
8
packages/core/typescript/itk-wasm/src/interface-types/float-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const FloatTypes = { | ||
Float32: 'float32', | ||
Float64: 'float64', | ||
|
||
SpacePrecisionType: 'float64' | ||
} as const | ||
|
||
export default FloatTypes |
14 changes: 14 additions & 0 deletions
14
packages/core/typescript/itk-wasm/src/interface-types/image-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import IntTypes from './int-types.js' | ||
import FloatTypes from './float-types.js' | ||
import PixelTypes from './pixel-types.js' | ||
|
||
class ImageType { | ||
constructor ( | ||
public readonly dimension: number = 2, | ||
public readonly componentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = IntTypes.UInt8, | ||
public readonly pixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar, | ||
public readonly components: number = 1 | ||
) {} | ||
} | ||
|
||
export default ImageType |
44 changes: 44 additions & 0 deletions
44
packages/core/typescript/itk-wasm/src/interface-types/image.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import ImageType from './image-type.js' | ||
import type TypedArray from '../TypedArray.js' | ||
import setMatrixElement from '../setMatrixElement.js' | ||
import Metadata from './metadata.js' | ||
|
||
class Image { | ||
name: string = 'image' | ||
|
||
origin: number[] | ||
|
||
spacing: number[] | ||
|
||
direction: TypedArray | ||
|
||
size: number[] | ||
|
||
metadata: Metadata | ||
|
||
data: null | TypedArray | ||
|
||
constructor (public readonly imageType = new ImageType()) { | ||
const dimension = imageType.dimension | ||
this.origin = new Array(dimension) | ||
this.origin.fill(0.0) | ||
|
||
this.spacing = new Array(dimension) | ||
this.spacing.fill(1.0) | ||
|
||
this.direction = new Float64Array(dimension * dimension) | ||
this.direction.fill(0.0) | ||
for (let ii = 0; ii < dimension; ii++) { | ||
setMatrixElement(this.direction, dimension, ii, ii, 1.0) | ||
} | ||
|
||
this.size = new Array(dimension) | ||
this.size.fill(0) | ||
|
||
this.metadata = new Map() | ||
|
||
this.data = null | ||
} | ||
} | ||
|
||
export default Image |
17 changes: 17 additions & 0 deletions
17
packages/core/typescript/itk-wasm/src/interface-types/int-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const IntTypes = { | ||
Int8: 'int8', | ||
UInt8: 'uint8', | ||
Int16: 'int16', | ||
UInt16: 'uint16', | ||
Int32: 'int32', | ||
UInt32: 'uint32', | ||
Int64: 'int64', | ||
UInt64: 'uint64', | ||
|
||
SizeValueType: 'uint64', | ||
IdentifierType: 'uint64', | ||
IndexValueType: 'int64', | ||
OffsetValueType: 'int64' | ||
} as const | ||
|
||
export default IntTypes |
9 changes: 9 additions & 0 deletions
9
packages/core/typescript/itk-wasm/src/interface-types/json-compatible.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type JsonCompatible = | ||
| null | ||
| string | ||
| number | ||
| boolean | ||
| { [key: string]: JsonCompatible } | ||
| JsonCompatible[] | ||
|
||
export default JsonCompatible |
18 changes: 18 additions & 0 deletions
18
packages/core/typescript/itk-wasm/src/interface-types/mesh-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import IntTypes from './int-types.js' | ||
import FloatTypes from './float-types.js' | ||
import PixelTypes from './pixel-types.js' | ||
|
||
class MeshType { | ||
constructor ( | ||
public readonly dimension: number = 2, | ||
public readonly pointComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32, | ||
public readonly pointPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32, | ||
public readonly pointPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar, | ||
public readonly pointPixelComponents: number = 1, | ||
public readonly cellComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = IntTypes.Int32, | ||
public readonly cellPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32, | ||
public readonly cellPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar, | ||
public readonly cellPixelComponents: number = 1) {} | ||
} | ||
|
||
export default MeshType |
42 changes: 42 additions & 0 deletions
42
packages/core/typescript/itk-wasm/src/interface-types/mesh.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import MeshType from './mesh-type.js' | ||
import type TypedArray from '../TypedArray.js' | ||
|
||
class Mesh { | ||
meshType: MeshType | ||
|
||
name: string = 'mesh' | ||
|
||
numberOfPoints: number | ||
points: null | TypedArray | ||
|
||
numberOfPointPixels: number | ||
pointData: null | TypedArray | ||
|
||
numberOfCells: number | ||
cells: null | TypedArray | ||
cellBufferSize: number | ||
|
||
numberOfCellPixels: number | ||
cellData: null | TypedArray | ||
|
||
constructor (public readonly mt = new MeshType()) { | ||
this.meshType = mt | ||
|
||
this.name = 'mesh' | ||
|
||
this.numberOfPoints = 0 | ||
this.points = null | ||
|
||
this.numberOfPointPixels = 0 | ||
this.pointData = null | ||
|
||
this.numberOfCells = 0 | ||
this.cellBufferSize = 0 | ||
this.cells = null | ||
|
||
this.numberOfCellPixels = 0 | ||
this.cellData = null | ||
} | ||
} | ||
|
||
export default Mesh |
3 changes: 3 additions & 0 deletions
3
packages/core/typescript/itk-wasm/src/interface-types/metadata.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type Metadata = Map<string, string | string[] | number | number[] | number[][]> | ||
|
||
export default Metadata |
20 changes: 20 additions & 0 deletions
20
packages/core/typescript/itk-wasm/src/interface-types/pixel-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const PixelTypes = { | ||
Unknown: 'Unknown', | ||
Scalar: 'Scalar', | ||
RGB: 'RGB', | ||
RGBA: 'RGBA', | ||
Offset: 'Offset', | ||
Vector: 'Vector', | ||
Point: 'Point', | ||
CovariantVector: 'CovariantVector', | ||
SymmetricSecondRankTensor: 'SymmetricSecondRankTensor', | ||
DiffusionTensor3D: 'DiffusionTensor3D', | ||
Complex: 'Complex', | ||
FixedArray: 'FixedArray', | ||
Array: 'Array', | ||
Matrix: 'Matrix', | ||
VariableLengthVector: 'VariableLengthVector', | ||
VariableSizeMatrix: 'VariableSizeMatrix' | ||
} as const | ||
|
||
export default PixelTypes |
15 changes: 15 additions & 0 deletions
15
packages/core/typescript/itk-wasm/src/interface-types/poly-data-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import IntTypes from './int-types.js' | ||
import FloatTypes from './float-types.js' | ||
import PixelTypes from './pixel-types.js' | ||
|
||
class PolyDataType { | ||
constructor ( | ||
public readonly pointPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32, | ||
public readonly pointPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar, | ||
public readonly pointPixelComponents: number = 1, | ||
public readonly cellPixelComponentType: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float32, | ||
public readonly cellPixelType: typeof PixelTypes[keyof typeof PixelTypes] = PixelTypes.Scalar, | ||
public readonly cellPixelComponents: number = 1) {} | ||
} | ||
|
||
export default PolyDataType |
56 changes: 56 additions & 0 deletions
56
packages/core/typescript/itk-wasm/src/interface-types/poly-data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import PolyDataType from './poly-data-type.js' | ||
import TypedArray from '../TypedArray.js' | ||
|
||
class PolyData { | ||
name: string = 'PolyData' | ||
|
||
numberOfPoints: number | ||
points: Float32Array | ||
|
||
verticesBufferSize: number | ||
vertices: null | Uint32Array | ||
|
||
linesBufferSize: number | ||
lines: null | Uint32Array | ||
|
||
polygonsBufferSize: number | ||
polygons: null | Uint32Array | ||
|
||
triangleStripsBufferSize: number | ||
triangleStrips: null | Uint32Array | ||
|
||
numberOfPointPixels: number | ||
pointData: null | TypedArray | ||
|
||
numberOfCellPixels: number | ||
cellData: null | TypedArray | ||
|
||
constructor (public readonly polyDataType = new PolyDataType()) { | ||
this.polyDataType = polyDataType | ||
|
||
this.name = 'PolyData' | ||
|
||
this.numberOfPoints = 0 | ||
this.points = new Float32Array() | ||
|
||
this.verticesBufferSize = 0 | ||
this.vertices = null | ||
|
||
this.linesBufferSize = 0 | ||
this.lines = null | ||
|
||
this.polygonsBufferSize = 0 | ||
this.polygons = null | ||
|
||
this.triangleStripsBufferSize = 0 | ||
this.triangleStrips = null | ||
|
||
this.numberOfPointPixels = 0 | ||
this.pointData = null | ||
|
||
this.numberOfCellPixels = 0 | ||
this.cellData = null | ||
} | ||
} | ||
|
||
export default PolyData |
6 changes: 6 additions & 0 deletions
6
packages/core/typescript/itk-wasm/src/interface-types/text-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface TextFile { | ||
data: string | ||
path: string | ||
} | ||
|
||
export default TextFile |
5 changes: 5 additions & 0 deletions
5
packages/core/typescript/itk-wasm/src/interface-types/text-stream.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface TextStream { | ||
data: string | ||
} | ||
|
||
export default TextStream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type TypedArray = Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | BigInt64Array | BigUint64Array | ||
|
||
export default TypedArray |