@dcmjs-org/imaging 0.0.0-stage → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/License.txt ADDED
@@ -0,0 +1,22 @@
1
+
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2017 Steve Pieper
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,253 @@
1
- # Temporary Holding Version
1
+ # @dcmjs-org/imaging
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The codec-free image builder of dcmjs. This package is the image counterpart
4
+ of `@dcmjs-org/video`. It puts decoded pixels into a conformant DICOM image
5
+ instance. It also reads the headers of compressed streams. It does not decode
6
+ pixels, and it does not read MP4 containers. A consumer that builds only
7
+ image instances uses this package, and does not need the larger video
8
+ package.
9
+
10
+ - `buildImageDataset(decodedImage, options)` — wrap decoded pixels into a
11
+ naturalized image instance. The default is Secondary Capture (`OT`).
12
+ `SECONDARY_CAPTURE_SOP_CLASS_UID` and `imageBase` are exported with it.
13
+ - `derivedImageBase(derivedFrom, { lossy })` — a base block that marks the
14
+ image as derived. `buildImageDataset` adds it for `derivedFrom`, and a
15
+ caller can add it to a custom `baseDicom`.
16
+ - `derivedImageAttributes(derivedFrom, { lossy })` — the plain function
17
+ under `derivedImageBase`: `ImageType`, `SourceImageSequence`, and the
18
+ Lossy Image Compression attributes.
19
+ - `parseJpegInfo(bytes)` — gets the geometry, the photometric
20
+ interpretation, and the matching DICOM transfer syntax from the frame
21
+ header of a JPEG (IMG-USR-11). It does not decode the pixels.
22
+ - `h264TransferSyntaxUID(profileIdc, levelIdc)` — gives the DICOM transfer
23
+ syntax that can carry an AVC profile and level (Supplement 225), or
24
+ `null`.
25
+
26
+ `@dcmjs-org/video` puts compressed streams into DICOM. It also reads MP4
27
+ headers, and encapsulates and streams video. That package uses the shared
28
+ `h264TransferSyntaxUID` map of this package.
29
+
30
+ ## Options of `buildImageDataset`
31
+
32
+ The options follow CORE-USR-1 in `packages/core/README.md`: the
33
+ `createInstance` controls, `overrides`, and these control keys. All of
34
+ them are options. `overrides` is a naturalized DICOM provider with the
35
+ forms of `baseDicom`, and holds only DICOM attributes. A control key never
36
+ goes in `overrides`. An extra option key has no effect (CORE-USR-7).
37
+
38
+ - `baseDicom` (default `imageBase`) — base blocks. `imageBase` has no
39
+ `predecessorBase`, so `predecessor: true` needs a custom base
40
+ (IMG-USR-10).
41
+ - `derivedFrom` — a naturalized instance, or an array of them, that the
42
+ image is derived from. Each needs `SOPClassUID` and `SOPInstanceUID`. An
43
+ empty array is the same as no `derivedFrom`.
44
+ - `lossy` (default `true`) — `false`, or `{ method, ratio }`. It has an
45
+ effect only with `derivedFrom` (IMG-USR-8).
46
+ - `encapsulated` — `{ transferSyntaxUID, frames }`: pre-encoded frames, in
47
+ place of `decodedImage.pixels`.
48
+
49
+ The geometry fields of `decodedImage` (`rows`, `columns`, `samplesPerPixel`,
50
+ and the others of IMG-USR-2) have no default. A decoded value replaces the
51
+ value from the base, from `instance`, or from `overrides`. A field that the
52
+ decoded image does not give writes nothing, and that value stays. Give the
53
+ photometric attributes in `overrides` when the decoder does not give them.
54
+
55
+ ```js
56
+ const image = buildImageDataset(decodedImage, {
57
+ instance: priorInstanceInSeries,
58
+ dataLevel: "series",
59
+ derivedFrom: [sourceA, sourceB],
60
+ overrides: { BodyPartExamined: "CHEST" }
61
+ });
62
+
63
+ // The same derivation in a custom base:
64
+ const base = combineBases(imageBase, { Modality: "MR" });
65
+ const derived = buildImageDataset(decodedImage, {
66
+ baseDicom: combineBases(base, derivedImageBase(sourceA, { lossy: false }))
67
+ });
68
+ ```
69
+
70
+ ## Scope and change rule
71
+
72
+ The shared requirements of `createInstance` (CORE-USR-n, CORE-IMP-n in
73
+ `packages/core/README.md`) apply to `buildImageDataset`. This README lists
74
+ only the requirements of this package.
75
+
76
+ The change rule of the core README applies. Change a user requirement
77
+ (IMG-USR-n) only when it describes the behaviour wrongly, or when the
78
+ intended user-facing behaviour changes, and name the change in the pull
79
+ request. The implementation requirements (IMG-IMP-n) describe the current
80
+ approach, and can change freely. Do not renumber an identifier.
81
+
82
+ ## § User requirements
83
+
84
+ - **IMG-USR-1** `instance` is where the new image goes (CORE-USR-3). It is
85
+ not the source of a derivation. `derivedFrom` is the source of a
86
+ derivation, and it is a separate option.
87
+ - **IMG-USR-2** The geometry attributes are `Rows`, `Columns`,
88
+ `SamplesPerPixel`, `PhotometricInterpretation`, `BitsAllocated`,
89
+ `BitsStored`, `HighBit`, `PixelRepresentation`, `PlanarConfiguration`,
90
+ and `NumberOfFrames`. The builder sets the geometry that the decoded
91
+ input gives, and `PixelData`, after `createInstance`.
92
+ - The decoded input gives a value in these cases:
93
+ - A field of `decodedImage` (`rows`, `columns`, `samplesPerPixel`,
94
+ `photometricInterpretation`, `bitsAllocated`, `bitsStored`,
95
+ `highBit`, `pixelRepresentation`, `planarConfiguration`,
96
+ `numberOfFrames`) that is not `undefined`, `null`, or `""`.
97
+ - `BitsAllocated` always comes from the typed-array width of
98
+ `pixels`.
99
+ - `PixelRepresentation` is 1 for an `Int8Array`, an `Int16Array`,
100
+ or an `Int32Array` when `decodedImage` has no
101
+ `pixelRepresentation`.
102
+ - `NumberOfFrames` always comes from `encapsulated.frames.length`.
103
+ - `HighBit` is `bitsStored` - 1 when `decodedImage` has
104
+ `bitsStored` and has no `highBit`.
105
+ - The builder calculates no other value. For example, it does not
106
+ calculate `HighBit` from `BitsAllocated`, or from a `BitsStored`
107
+ of the base, of `instance`, or of `overrides`. It does not
108
+ calculate `BitsStored` from `BitsAllocated`.
109
+ - A decoded value replaces the base value, the inherited value, and the
110
+ override, with no warning. This rule applies to each form of
111
+ `overrides`, also to a function and to an array.
112
+ - The builder does not write a geometry attribute that the decoded
113
+ input does not give, and does not remove it. The value from the
114
+ base, from `instance`, or from `overrides` stays. Without one of
115
+ these, the result does not have the attribute. The builder writes
116
+ no default.
117
+ - The builder writes `PlanarConfiguration` only when the effective
118
+ `SamplesPerPixel` is more than 1. The builder writes `NumberOfFrames`
119
+ in three cases: the decoded count is more than 1,
120
+ `decodedImage.numberOfFrames` gives the count, or the dataset
121
+ already has a `NumberOfFrames`. Thus a count of 1 from
122
+ `encapsulated.frames.length` alone does not add `NumberOfFrames`.
123
+ - The builder checks the effective geometry before it writes the
124
+ geometry (IMG-USR-9).
125
+ - `_vrMap.PixelData` is `OW` for 16-bit native pixels, else `OB`.
126
+ Encapsulated frames always use `OB`.
127
+ - The `_meta` rule of CORE-USR-13 applies. For `encapsulated`, the
128
+ builder sets `_meta.TransferSyntaxUID` to the transfer syntax of the
129
+ frames. Without `encapsulated`, the builder sets Explicit VR Little
130
+ Endian only when the merged `_meta` has no `TransferSyntaxUID`. Thus
131
+ a `TransferSyntaxUID` override in `_meta` stays.
132
+ - **IMG-USR-3** With a non-empty `derivedFrom`, the result is a derived
133
+ image:
134
+ - `ImageType` is `DERIVED\SECONDARY`.
135
+ - `SourceImageSequence` has one item for each source, in order.
136
+ - `LossyImageCompression` is `"01"`. For `lossy: false` it is `"00"`,
137
+ but when a source has `"01"`, the result keeps `"01"`, because a lossy
138
+ image stays lossy (PS3.3 C.7.6.1.1.5).
139
+ - `LossyImageCompressionMethod` and `LossyImageCompressionRatio` keep
140
+ the history of the lossy steps (PS3.3 C.7.6.1.1.5). The values of each
141
+ lossy source come first, in `derivedFrom` order. Then come `method`
142
+ and `ratio` of a `lossy` object. One value is a plain value, and more
143
+ values are an array.
144
+ - PS3.3 C.7.6.1.1.5 pairs each ratio with the method at the same
145
+ position. When the result has ratios, and the number of ratios is not
146
+ the number of methods, the result has no `LossyImageCompressionRatio`.
147
+ The builder then logs one warning.
148
+ - `SOPClassUID` stays the value of the base (Secondary Capture for
149
+ `imageBase`).
150
+ - These values are base values (IMG-IMP-1). An inherited value or an
151
+ override replaces them (CORE-USR-6). For example, a custom `levels`
152
+ that inherits `ImageType` or `LossyImageCompression` gives the value
153
+ of `instance`, and not the derived value.
154
+ - An empty `derivedFrom` array is the same as no `derivedFrom`.
155
+ - **IMG-USR-4** Each `derivedFrom` item is a naturalized instance, full or
156
+ partial. The builder reads the DICOM keywords `SOPClassUID`,
157
+ `SOPInstanceUID`, and `LossyImageCompression`. An item without
158
+ `SOPClassUID` or `SOPInstanceUID` throws an error that names the absent
159
+ keyword.
160
+ - **IMG-USR-5** The default `ImageType` `ORIGINAL\PRIMARY` is a base value.
161
+ A custom base, the derivation, an inherited value, and an override each
162
+ replace it, in the order of CORE-USR-6. The default `ConversionType`
163
+ `WSD` is also a base value of `secondaryCaptureImageBase`. An override
164
+ replaces it, and an `undefined` override removes it (CORE-USR-8). A
165
+ custom base without `secondaryCaptureImageBase` gives no
166
+ `ConversionType`.
167
+ - **IMG-USR-6** The result never gets the `SOPInstanceUID` of
168
+ `derivedFrom`. With the default levels, the result does not get the
169
+ `SOPInstanceUID` of `instance` (CORE-USR-4). `overrides.SOPInstanceUID`
170
+ always wins.
171
+ - **IMG-USR-7** A caller can add `derivedImageBase(derivedFrom, { lossy })`
172
+ to a custom `baseDicom` (CORE-USR-11). The result is the same as with the
173
+ `derivedFrom` option. `derivedImageBase` and `derivedImageAttributes`
174
+ need at least one instance: an empty `derivedFrom` throws an error.
175
+ - **IMG-USR-8** `lossy` without `derivedFrom`, or with an empty
176
+ `derivedFrom`, has no effect.
177
+ - **IMG-USR-9** The functions throw an error for these input values
178
+ (CORE-IMP-11). `buildImageDataset` uses the effective values: the
179
+ decoded value, else the value of the merged dataset.
180
+
181
+ - `decodedImage.pixels` and `options.encapsulated` together.
182
+ - An `encapsulated` without `transferSyntaxUID`, or without frames.
183
+ - `Rows` or `Columns` that is not a positive integer. The decoded
184
+ input or the merged dataset must give them.
185
+ - `MONOCHROME1` or `MONOCHROME2` with a `SamplesPerPixel` that is not
186
+ 1.
187
+ - `RGB` or `YBR_*` with a `SamplesPerPixel` that is not 3.
188
+ - A `pixels` that is a `Float32Array` or a `Float64Array`. DICOM
189
+ integer pixel data needs an integer typed array.
190
+ - A `bitsAllocated` that is not the typed-array width of `pixels`.
191
+ - A `numberOfFrames` that is not `encapsulated.frames.length`.
192
+ - A byte length of `pixels` that is not `Rows` x `Columns` x
193
+ `SamplesPerPixel` x `NumberOfFrames` x `BitsAllocated` / 8.
194
+ - `parseJpegInfo`: bytes without the JPEG SOI marker, a JPEG without
195
+ a frame header (SOF), or a truncated SOF segment.
196
+
197
+ When a value that a check needs is absent from the decoded input and
198
+ from the merged dataset, the check uses a default. An absent
199
+ `SamplesPerPixel` is 1, and an absent `NumberOfFrames` is 1. The builder
200
+ does not write these defaults. Without a `PhotometricInterpretation`,
201
+ the builder skips the photometric check. IMG-USR-4 and IMG-USR-7 give
202
+ the errors of `derivedFrom`. The input type of `pixels` is a typed array
203
+ (CORE-USR-7).
204
+
205
+ - **IMG-USR-10** `imageBase` does not include `predecessorBase`
206
+ (CORE-USR-9). With the default base, `predecessor: true` has no effect:
207
+ the result has no `PredecessorDocumentsSequence`, and the builder throws
208
+ no predecessor error. For a predecessor, give
209
+ `baseDicom: combineBases(imageBase, predecessorBase)`, with
210
+ `predecessorBase` from `@dcmjs-org/core`.
211
+ - **IMG-USR-11** `parseJpegInfo` gives DICOM values that go directly into
212
+ the `decodedImage` of `buildImageDataset`:
213
+ - `bitsAllocated` is 8 for a sample precision of 8 or less, else 16.
214
+ `bitsStored` is the sample precision. (Before this rule,
215
+ `bitsAllocated` was the sample precision.)
216
+ - `photometricInterpretation` follows PS3.5 8.2.1: `MONOCHROME2` for
217
+ 1 component; for 3 components `RGB` when the data has no colour
218
+ transform (Adobe APP14 transform 0, the component IDs R, G, B, or a
219
+ lossless process), else `YBR_FULL` without chroma subsampling and
220
+ `YBR_FULL_422` with it. Another component count gives `null`.
221
+ - `transferSyntaxUID` is the baseline (SOF0), extended (SOF1), or
222
+ lossless (SOF3) transfer syntax, else `null`.
223
+
224
+ ## § Implementation requirements
225
+
226
+ - **IMG-IMP-1** `buildImageDataset` calls `createInstance`. For a non-empty
227
+ `derivedFrom`, the base is
228
+ `combineBases(baseDicom, derivedImageBase(derivedFrom, { lossy }))`, so
229
+ the merge stays base < inherited < overrides (CORE-IMP-4).
230
+ `buildImageDataset` removes its control keys `derivedFrom`, `lossy`, and
231
+ `encapsulated` before it calls `createInstance` (CORE-IMP-9), and passes
232
+ `overrides` and `postProcess` through without a change. Then
233
+ `buildImageDataset` checks the effective geometry, writes only the
234
+ decoded geometry of IMG-USR-2, and sets the pixel encoding. It removes
235
+ no geometry attribute.
236
+ - **IMG-IMP-2** `derivedImageAttributes` is the plain function under
237
+ `derivedImageBase`. The block calls it for each context, so each result
238
+ gets its own `SourceImageSequence`.
239
+ - **IMG-IMP-3** `imageBase` is `defaultInstanceBase`,
240
+ `secondaryCaptureImageBase` (SOP class, Modality `OT`, `ConversionType`
241
+ `WSD`, `ImageType`, `ContentDate`, `ContentTime`). `imageBase` has no
242
+ `predecessorBase` (IMG-USR-10). The builder writes no default after
243
+ `createInstance`.
244
+ - **IMG-IMP-4** NumberOfFrames: a single-frame IOD, for example Secondary
245
+ Capture, has no `NumberOfFrames`. For this reason a count of 1 from
246
+ `encapsulated.frames.length` alone is not written. A count that
247
+ `decodedImage` states is written, because the caller asks for it.
248
+
249
+ ## Dependencies
250
+
251
+ Builds only on `@dcmjs-org/core` (`createInstance`). The datasets it
252
+ produces are naturalized and serialize through the event-stream writers
253
+ (`@dcmjs-org/generator`) like any other.