Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "itk-wasm-workspace",
"version": "1.0.0",
"private": true,
"packageManager": "pnpm@10.8.0",
"packageManager": "pnpm@10.12.1",
"description": "High-performance spatial analysis in a web browser, Node.js, and reproducible execution across programming languages and hardware architectures.",
"type": "module",
"directories": {
Expand Down Expand Up @@ -44,8 +44,9 @@
"esbuild": "^0.25.1",
"start-server-and-test": "^2.0.12",
"ava": "^6.1.3",
"cypress": "^14.4.1",
"cypress": "^14.5.0",
"shx": "^0.4.0",
"typescript": "^5.8.3",
"vite": "^6.2.3"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export async function imageToJsonNode(
compressionLevel: level,
stringify: true,
});
encoded.direction = decoder.decode(direction.output.buffer);
// Convert ArrayBufferLike to a compatible type for TextDecoder
const directionBuffer =
direction.output.buffer instanceof ArrayBuffer
? direction.output.buffer
: direction.output;
encoded.direction = decoder.decode(directionBuffer);

if (image.data === null) {
encoded.data = null;
Expand All @@ -40,7 +45,12 @@ export async function imageToJsonNode(
compressionLevel: level,
stringify: true,
});
encoded.data = decoder.decode(encodedData.output.buffer);
// Convert ArrayBufferLike to a compatible type for TextDecoder
const dataBuffer =
encodedData.output.buffer instanceof ArrayBuffer
? encodedData.output.buffer
: encodedData.output;
encoded.data = decoder.decode(dataBuffer);
}

return { encoded };
Expand Down Expand Up @@ -109,8 +119,13 @@ export async function meshToJsonNode(
compressionLevel: level,
stringify: true,
});
// Convert ArrayBufferLike to a compatible type for TextDecoder
const propBuffer =
encodedProp.output.buffer instanceof ArrayBuffer
? encodedProp.output.buffer
: encodedProp.output;
// @ts-ignore
encoded[prop] = decoder.decode(encodedProp.output.buffer);
encoded[prop] = decoder.decode(propBuffer);
}
}

Expand Down Expand Up @@ -201,8 +216,13 @@ export async function polyDataToJsonNode(
compressionLevel: level,
stringify: true,
});
// Convert ArrayBufferLike to a compatible type for TextDecoder
const propBuffer =
encodedProp.output.buffer instanceof ArrayBuffer
? encodedProp.output.buffer
: encodedProp.output;
// @ts-ignore
encoded[prop] = decoder.decode(encodedProp.output.buffer);
encoded[prop] = decoder.decode(propBuffer);
}
}

Expand Down
14 changes: 12 additions & 2 deletions packages/compress-stringify/typescript/src/interface-type-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export async function imageToJson(
noCopy: options.noCopy,
});
const usedWebWorker = direction.webWorker;
encoded.direction = decoder.decode(direction.output.buffer);
// Convert ArrayBufferLike to a compatible type for TextDecoder
const directionBuffer =
direction.output.buffer instanceof ArrayBuffer
? direction.output.buffer
: direction.output;
encoded.direction = decoder.decode(directionBuffer);

if (image.data === null) {
encoded.data = null;
Expand All @@ -55,7 +60,12 @@ export async function imageToJson(
webWorker: usedWebWorker,
noCopy: options.noCopy,
});
encoded.data = decoder.decode(encodedData.output.buffer);
// Convert ArrayBufferLike to a compatible type for TextDecoder
const dataBuffer =
encodedData.output.buffer instanceof ArrayBuffer
? encodedData.output.buffer
: encodedData.output;
encoded.data = decoder.decode(dataBuffer);
}

return { encoded, webWorker: usedWebWorker };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ 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 {
function bufferToTypedArray(
wasmType:
| (typeof IntTypes)[keyof typeof IntTypes]
| (typeof FloatTypes)[keyof typeof FloatTypes]
| 'null'
| null,
buffer: ArrayBuffer | SharedArrayBuffer
): null | TypedArray {
let typedArray: null | TypedArray = null
switch (wasmType) {
case IntTypes.UInt8: {
Expand Down
168 changes: 149 additions & 19 deletions packages/core/typescript/itk-wasm/src/cast-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ import FloatTypes from './interface-types/float-types.js'
* @param {Image} image - The input image
* @param {CastImageOptions} options - specify the componentType and/or pixelType of the output
*/
function castImage (inputImage: Image, options?: CastImageOptions): Image {
function castImage(inputImage: Image, options?: CastImageOptions): Image {
const outputImageType = { ...inputImage.imageType }

if (typeof options !== 'undefined' && typeof options.pixelType !== 'undefined') {
if (
typeof options !== 'undefined' &&
typeof options.pixelType !== 'undefined'
) {
outputImageType.pixelType = options.pixelType
if (options.pixelType === PixelTypes.Scalar && outputImageType.components !== 1) {
if (
options.pixelType === PixelTypes.Scalar &&
outputImageType.components !== 1
) {
throw new Error('Cannot cast multi-component image to a scalar image')
}
}
if (typeof options !== 'undefined' && typeof options.componentType !== 'undefined' && options.componentType !== inputImage.imageType.componentType) {
if (
typeof options !== 'undefined' &&
typeof options.componentType !== 'undefined' &&
options.componentType !== inputImage.imageType.componentType
) {
outputImageType.componentType = options.componentType
}

Expand All @@ -31,10 +41,16 @@ function castImage (inputImage: Image, options?: CastImageOptions): Image {
outputImage.direction = inputImage.direction.slice()
outputImage.size = Array.from(inputImage.size)
// Deep copy the map
outputImage.metadata = new Map(JSON.parse(JSON.stringify(Array.from(inputImage.metadata))))
outputImage.metadata = new Map(
JSON.parse(JSON.stringify(Array.from(inputImage.metadata)))
)

if (inputImage.data !== null) {
if (typeof options !== 'undefined' && typeof options.componentType !== 'undefined' && options.componentType !== inputImage.imageType.componentType) {
if (
typeof options !== 'undefined' &&
typeof options.componentType !== 'undefined' &&
options.componentType !== inputImage.imageType.componentType
) {
switch (inputImage.imageType.componentType) {
case IntTypes.UInt8:
case IntTypes.Int8:
Expand All @@ -46,39 +62,125 @@ function castImage (inputImage: Image, options?: CastImageOptions): Image {
case FloatTypes.Float64:
switch (outputImage.imageType.componentType) {
case IntTypes.UInt8:
outputImage.data = new Uint8Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Uint8Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Uint8Array(inputImage.data)
}
break
case IntTypes.Int8:
outputImage.data = new Int8Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Int8Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Int8Array(inputImage.data)
}
break
case IntTypes.UInt16:
outputImage.data = new Uint16Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Uint16Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Uint16Array(inputImage.data)
}
break
case IntTypes.Int16:
outputImage.data = new Int16Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Int16Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Int16Array(inputImage.data)
}
break
case IntTypes.UInt32:
outputImage.data = new Uint32Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Uint32Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Uint32Array(inputImage.data)
}
break
case IntTypes.Int32:
outputImage.data = new Int32Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Int32Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Int32Array(inputImage.data)
}
break
case FloatTypes.Float32:
outputImage.data = new Float32Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Float32Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Float32Array(inputImage.data)
}
break
case FloatTypes.Float64:
outputImage.data = new Float64Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new Float64Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = Number(inputImage.data[idx])
}
} else {
outputImage.data = new Float64Array(inputImage.data)
}
break
case IntTypes.UInt64:
outputImage.data = new BigUint64Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = BigInt.asIntN(64, BigInt(inputImage.data[idx]))
outputImage.data[idx] = BigInt.asIntN(
64,
BigInt(inputImage.data[idx])
)
}
break
case IntTypes.Int64:
outputImage.data = new BigInt64Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = BigInt.asUintN(64, BigInt(inputImage.data[idx]))
outputImage.data[idx] = BigInt.asUintN(
64,
BigInt(inputImage.data[idx])
)
}
break
}
Expand Down Expand Up @@ -135,17 +237,45 @@ function castImage (inputImage: Image, options?: CastImageOptions): Image {
}
break
case IntTypes.UInt64:
outputImage.data = new BigUint64Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new BigUint64Array(inputImage.data)
} else {
outputImage.data = new BigUint64Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = BigInt.asUintN(
64,
BigInt(inputImage.data[idx])
)
}
}
break
case IntTypes.Int64:
outputImage.data = new BigInt64Array(inputImage.data)
if (
inputImage.data instanceof BigInt64Array ||
inputImage.data instanceof BigUint64Array
) {
outputImage.data = new BigInt64Array(inputImage.data)
} else {
outputImage.data = new BigInt64Array(inputImage.data.length)
for (let idx = 0; idx < outputImage.data.length; idx++) {
outputImage.data[idx] = BigInt.asIntN(
64,
BigInt(inputImage.data[idx])
)
}
}
break
}
break
}
} else {
// copy
const CTor = inputImage.data.constructor as new(length: number) => typeof inputImage.data
const CTor = inputImage.data.constructor as new (
length: number
) => typeof inputImage.data
outputImage.data = new CTor(inputImage.data.length)
if (outputImage.data != null) {
// @ts-expect-error: error TS2345: Argument of type 'TypedArray' is not assignable to parameter of type 'ArrayLike<number> & ArrayLike<bigint>'
Expand Down
Loading
Loading