Skip to content

Commit

Permalink
feat(Image): use Map for metadata member
Browse files Browse the repository at this point in the history
Better interface with `has` that is more appropriate for key/value
pairs of the itk::MetaDataDictionary.

BREAKING_CHANGE: Image.metadata is now a Map instead of an Object
  • Loading branch information
thewtex committed Nov 14, 2022
1 parent 8a7cfa7 commit f9ac56e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/content/api/Image.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ An `Image` is the N-dimensional image data structure for *itk-wasm*. `Image` is
**direction**: A *dimension* by *dimension* Float64Array in column-major order, that describes the orientation of the image at its *origin*. The orientation of each axis are the orthonormal columns.
**size**: An Array with length *dimension* that contains the number of pixels along dimension.
**data**: A [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) containing the pixel buffer data.
**metadata**: A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) of string keys to values that could be strings, string arrays, numbers, number arrays, or arrays of number arrays providing additional metadata for the image.

For more information, see [the description](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch4.html#x38-490004.1) of an `itk::Image` in the ITK Software Guide.

Expand Down
5 changes: 3 additions & 2 deletions src/core/Image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ImageType from './ImageType.js'
import type TypedArray from './TypedArray.js'
import setMatrixElement from './setMatrixElement.js'
import Metadata from './Metadata.js'

class Image {
name: string = 'image'
Expand All @@ -13,7 +14,7 @@ class Image {

size: number[]

metadata: Record<string, string | string[] | number | number[] | number[][]>
metadata: Metadata

data: null | TypedArray

Expand All @@ -34,7 +35,7 @@ class Image {
this.size = new Array(dimension)
this.size.fill(0)

this.metadata = {}
this.metadata = new Map()

this.data = null
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/Metadata.ts
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
3 changes: 2 additions & 1 deletion src/core/castImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function castImage (inputImage: Image, options?: CastImageOptions): Image {
outputImage.spacing = Array.from(inputImage.spacing)
outputImage.direction = inputImage.direction.slice()
outputImage.size = Array.from(inputImage.size)
outputImage.metadata = { ...inputImage.metadata }
// Deep copy the map
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) {
Expand Down
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as JsonObject } from './JsonObject.js'
export { default as TypedArray } from './TypedArray.js'
export { default as IntTypes } from './IntTypes.js'
export { default as FloatTypes } from './FloatTypes.js'
export { default as Metadata } from './Metadata.js'

export { default as IOTypes } from './IOTypes.js'

Expand Down
8 changes: 4 additions & 4 deletions src/io/readImageDICOMArrayBufferSeries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createWebWorkerPromise from '../core/internal/createWebWorkerPromise.js'
import WorkerPool from '../core/WorkerPool.js'
import Metadata from '../core/Metadata.js'
import stackImages from '../core/stackImages.js'
import BinaryFile from '../core/BinaryFile.js'
import InterfaceTypes from '../core/InterfaceTypes.js'
Expand Down Expand Up @@ -63,12 +64,11 @@ const workerFunction = async (
filenames?.pop()

if (image.metadata === undefined) {
const metadata:
Record<string, string | string[] | number | number[] | number[][]> = {}
metadata.orderedFileNames = filenames
const metadata: Metadata = new Map()
metadata.set('OrderedFileNames', filenames)
image.metadata = metadata
} else {
image.metadata.orderedFileNames = filenames
image.metadata.set('OrderedFileNames', filenames)
}

return { image: result.outputs[0].data as Image, webWorker: worker }
Expand Down
4 changes: 3 additions & 1 deletion src/pipeline/internal/runPipelineEmscripten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args:
spacing: image.spacing,
direction: `data:application/vnd.itk.address,0:${directionPtr}`,
size: image.size,
data: `data:application/vnd.itk.address,0:${dataPtr}`
data: `data:application/vnd.itk.address,0:${dataPtr}`,
metadata: JSON.stringify(Array.from(image.metadata.entries()))
}
setPipelineModuleInputJSON(pipelineModule, imageJSON, index)
break
Expand Down Expand Up @@ -353,6 +354,7 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args:
const image = getPipelineModuleOutputJSON(pipelineModule, index) as Image
image.data = getPipelineModuleOutputArray(pipelineModule, index, 0, image.imageType.componentType)
image.direction = getPipelineModuleOutputArray(pipelineModule, index, 1, FloatTypes.Float64) as Float64Array
image.metadata = new Map(image.metadata)
outputData = image
break
}
Expand Down

0 comments on commit f9ac56e

Please sign in to comment.