@dcmjs-org/dcmjs 0.0.0-stage → 1.0.0-beta.4
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 +22 -0
- package/README.md +263 -2
- package/build/dcmjs.es.js +3 -0
- package/build/dcmjs.es.js.map +1 -0
- package/build/dcmjs.js +30107 -0
- package/build/dcmjs.js.map +1 -0
- package/build/dcmjs.min.js +3 -0
- package/build/dcmjs.min.js.map +1 -0
- package/build/dcmjs.shared-C5jsZ5KD.js +15800 -0
- package/build/dcmjs.shared-C5jsZ5KD.js.map +1 -0
- package/build/encapsulated.es.js +21 -0
- package/build/encapsulated.es.js.map +1 -0
- package/build/eventStream.es.js +1075 -0
- package/build/eventStream.es.js.map +1 -0
- package/build/image.es.js +15 -0
- package/build/image.es.js.map +1 -0
- package/build/media.es.js +11 -0
- package/build/media.es.js.map +1 -0
- package/generate/dictionary.mjs +56029 -0
- package/package.json +131 -3
|
@@ -0,0 +1,1075 @@
|
|
|
1
|
+
import { _ as _inherits, a as _createClass, b as _objectSpread2, c as _classCallCheck, d as _callSuper, e as _createForOfIteratorHelper, f as datasetToDict, g as _typeof, h as _asyncToGenerator, i as _regeneratorRuntime } from './dcmjs.shared-C5jsZ5KD.js';
|
|
2
|
+
import { CollectorListener, EventStreamListener, createEventAsyncIterable, fromPart10, fromPart10Stream, fromDicomWebJson, fromDataSet, fromNaturalized, NaturalizedListener, CONTRACT_VERSION, EVENT_STREAM_VOCABULARY } from '@dcmjs-org/parser';
|
|
3
|
+
export { CONTRACT_VERSION, CollectorListener, EVENT_STREAM_VOCABULARY, EventStreamListener, NaturalizedListener, createEventAsyncIterable, fromDataSet, fromDicomWebJson, fromNaturalized, fromPart10, fromPart10Stream } from '@dcmjs-org/parser';
|
|
4
|
+
import { writePart10, encodeBase64, EXPLICIT_BIG_ENDIAN, IMPLICIT_LITTLE_ENDIAN, WriteBufferStream, EXPLICIT_LITTLE_ENDIAN, DEFLATED_EXPLICIT_LITTLE_ENDIAN, writeDataSet, writeTagObject } from '@dcmjs-org/core';
|
|
5
|
+
import { encapsulatePdf, extractEncapsulatedPdf } from '@dcmjs-org/encapsulated';
|
|
6
|
+
import { buildImageDataset } from '@dcmjs-org/imaging';
|
|
7
|
+
import { createVideoEventSource, extractEncapsulatedVideo } from '@dcmjs-org/video';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Part10Writer — slice E2: an event-stream sink that produces DICOM Part 10
|
|
11
|
+
* bytes.
|
|
12
|
+
*
|
|
13
|
+
* Architecture: this is a thin LAYER over the canonical encoder, not a second
|
|
14
|
+
* encoder. It collects the event stream into a tag-keyed `{ meta, dict }` tree
|
|
15
|
+
* (reusing CollectorListener) and delegates serialization to core's
|
|
16
|
+
* `writePart10` (the proven Part 10 envelope, relocated from legacy
|
|
17
|
+
* `DicomDict.write()`), which handles every VR, undefined-length sequences,
|
|
18
|
+
* deflate, padding, Big16, and FileMetaInformationGroupLength recomputation.
|
|
19
|
+
* This closed the one temporary `@dcmjs-org/legacy` edge this package had.
|
|
20
|
+
*
|
|
21
|
+
* Byte-IDENTICAL Part 10 round-tripping (incl. re-emitting compressed pixel data
|
|
22
|
+
* verbatim) is intentionally NOT this path's job — that is a non-goal of the
|
|
23
|
+
* naturalized/event layer (spec §4.5) and remains served by the lazy-read +
|
|
24
|
+
* R4 passthrough-write path. This writer produces correct, semantically faithful
|
|
25
|
+
* Part 10 output from any event source.
|
|
26
|
+
*/
|
|
27
|
+
var Part10Writer = /*#__PURE__*/function (_CollectorListener) {
|
|
28
|
+
function Part10Writer() {
|
|
29
|
+
_classCallCheck(this, Part10Writer);
|
|
30
|
+
return _callSuper(this, Part10Writer, arguments);
|
|
31
|
+
}
|
|
32
|
+
_inherits(Part10Writer, _CollectorListener);
|
|
33
|
+
return _createClass(Part10Writer, [{
|
|
34
|
+
key: "write",
|
|
35
|
+
value:
|
|
36
|
+
/**
|
|
37
|
+
* Serialize the collected dataset to a Part 10 ArrayBuffer.
|
|
38
|
+
* @param {Object} [writeOptions] forwarded to writePart10
|
|
39
|
+
* @returns {ArrayBuffer}
|
|
40
|
+
*/
|
|
41
|
+
function write(writeOptions) {
|
|
42
|
+
var meta = _objectSpread2({}, this.result.meta);
|
|
43
|
+
// writePart10 recomputes the group length; drop any collected one so
|
|
44
|
+
// it is not double-counted.
|
|
45
|
+
delete meta["00020000"];
|
|
46
|
+
var dataSet = {
|
|
47
|
+
meta: meta,
|
|
48
|
+
dict: this.result.dict
|
|
49
|
+
};
|
|
50
|
+
return writeOptions ? writePart10(dataSet, writeOptions) : writePart10(dataSet);
|
|
51
|
+
}
|
|
52
|
+
}]);
|
|
53
|
+
}(CollectorListener);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* DicomWebJsonWriter — slice E1: an event-stream consumer that writes the
|
|
57
|
+
* DICOM JSON model (DICOMweb metadata representation).
|
|
58
|
+
*
|
|
59
|
+
* The faithful inverse of `fromDicomWebJson`: it makes the contract a SINK as
|
|
60
|
+
* well as a source, so the architecture round-trips
|
|
61
|
+
* (DICOMweb JSON -> events -> DICOMweb JSON is identity) and any source can be
|
|
62
|
+
* serialized to DICOMweb JSON (Part 10 -> events -> JSON).
|
|
63
|
+
*
|
|
64
|
+
* Output shape per tag: `{ vr, Value: [...] }`, or `{ vr, BulkDataURI }`, or
|
|
65
|
+
* `{ vr, InlineBinary: base64 }`. Person Names pass through as the DICOMweb
|
|
66
|
+
* `{ Alphabetic }` objects the contract carries. Binary source form is preserved
|
|
67
|
+
* (§25): a `bulkDataReference` stays a `BulkDataURI`; assembled binary fragments
|
|
68
|
+
* become base64 `InlineBinary`. Meta (group 0002) elements, when present, are
|
|
69
|
+
* written flat alongside the dataset.
|
|
70
|
+
*/
|
|
71
|
+
var DicomWebJsonWriter = /*#__PURE__*/function (_EventStreamListener) {
|
|
72
|
+
function DicomWebJsonWriter() {
|
|
73
|
+
var _this;
|
|
74
|
+
_classCallCheck(this, DicomWebJsonWriter);
|
|
75
|
+
for (var _len = arguments.length, filters = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
76
|
+
filters[_key] = arguments[_key];
|
|
77
|
+
}
|
|
78
|
+
_this = _callSuper(this, DicomWebJsonWriter, [].concat(filters));
|
|
79
|
+
_this.result = {};
|
|
80
|
+
// Stack of containers (the root object or a sequence item object) plus
|
|
81
|
+
// the element/sequence currently being built.
|
|
82
|
+
_this._containers = [_this.result];
|
|
83
|
+
_this._pending = null; // { tag, attr, fragments? }
|
|
84
|
+
return _this;
|
|
85
|
+
}
|
|
86
|
+
_inherits(DicomWebJsonWriter, _EventStreamListener);
|
|
87
|
+
return _createClass(DicomWebJsonWriter, [{
|
|
88
|
+
key: "_container",
|
|
89
|
+
value: function _container() {
|
|
90
|
+
return this._containers[this._containers.length - 1];
|
|
91
|
+
}
|
|
92
|
+
}, {
|
|
93
|
+
key: "_baseStartDataSet",
|
|
94
|
+
value: function _baseStartDataSet() {
|
|
95
|
+
this.result = {};
|
|
96
|
+
this._containers = [this.result];
|
|
97
|
+
this._pending = null;
|
|
98
|
+
}
|
|
99
|
+
}, {
|
|
100
|
+
key: "_baseStartElement",
|
|
101
|
+
value: function _baseStartElement(tag) {
|
|
102
|
+
var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
103
|
+
this._pending = {
|
|
104
|
+
tag: tag,
|
|
105
|
+
attr: {
|
|
106
|
+
vr: info.vr
|
|
107
|
+
},
|
|
108
|
+
values: []
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}, {
|
|
112
|
+
key: "_baseValue",
|
|
113
|
+
value: function _baseValue(v, info) {
|
|
114
|
+
this._pending.values.push(this._jsonModelValue(v, info));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* PS3.18 F.2.3.1: DS/IS may be encoded as JSON numbers OR strings.
|
|
119
|
+
* Emit a number when the conversion is lossless (dcm4chee parity, #53);
|
|
120
|
+
* fall back to the raw source string when Number() would silently lose
|
|
121
|
+
* integer precision — e.g. DS/IS integers beyond 2^53 (#398). The raw
|
|
122
|
+
* string is the standard's escape hatch; `_rawValue` itself never
|
|
123
|
+
* appears in the JSON model (#404).
|
|
124
|
+
*/
|
|
125
|
+
}, {
|
|
126
|
+
key: "_jsonModelValue",
|
|
127
|
+
value: function _jsonModelValue(v, info) {
|
|
128
|
+
var vr = this._pending.attr.vr;
|
|
129
|
+
if ((vr === "DS" || vr === "IS") && info && typeof info.rawValue === "string") {
|
|
130
|
+
var raw = info.rawValue.trim();
|
|
131
|
+
var parsed = Number(raw);
|
|
132
|
+
if (/^[+-]?\d+$/.test(raw) && !Number.isSafeInteger(parsed) && String(parsed) !== raw) {
|
|
133
|
+
return raw;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return v;
|
|
137
|
+
}
|
|
138
|
+
}, {
|
|
139
|
+
key: "_baseBulkDataReference",
|
|
140
|
+
value: function _baseBulkDataReference() {
|
|
141
|
+
var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
142
|
+
this._pending.attr.BulkDataURI = ref.uri;
|
|
143
|
+
this._pending.values = null; // bulk reference carries no Value
|
|
144
|
+
}
|
|
145
|
+
}, {
|
|
146
|
+
key: "_baseStartBinary",
|
|
147
|
+
value: function _baseStartBinary() {
|
|
148
|
+
this._pending.fragments = [];
|
|
149
|
+
this._pending.values = null;
|
|
150
|
+
}
|
|
151
|
+
}, {
|
|
152
|
+
key: "_baseBinaryFragment",
|
|
153
|
+
value: function _baseBinaryFragment(chunk) {
|
|
154
|
+
this._pending.fragments.push(chunk);
|
|
155
|
+
}
|
|
156
|
+
}, {
|
|
157
|
+
key: "_baseEndBinary",
|
|
158
|
+
value: function _baseEndBinary() {
|
|
159
|
+
this._pending.attr.InlineBinary = encodeBase64(concatFragments(this._pending.fragments));
|
|
160
|
+
}
|
|
161
|
+
}, {
|
|
162
|
+
key: "_baseEndElement",
|
|
163
|
+
value: function _baseEndElement() {
|
|
164
|
+
var _this$_pending = this._pending,
|
|
165
|
+
tag = _this$_pending.tag,
|
|
166
|
+
attr = _this$_pending.attr,
|
|
167
|
+
values = _this$_pending.values;
|
|
168
|
+
if (values && values.length) {
|
|
169
|
+
attr.Value = values;
|
|
170
|
+
}
|
|
171
|
+
this._container()[tag] = attr;
|
|
172
|
+
this._pending = null;
|
|
173
|
+
}
|
|
174
|
+
}, {
|
|
175
|
+
key: "_baseStartSequence",
|
|
176
|
+
value: function _baseStartSequence(tag) {
|
|
177
|
+
var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
178
|
+
var attr = {
|
|
179
|
+
vr: info.vr,
|
|
180
|
+
Value: []
|
|
181
|
+
};
|
|
182
|
+
this._container()[tag] = attr;
|
|
183
|
+
this._pending = null;
|
|
184
|
+
// Items are pushed into attr.Value via a sequence marker on the stack.
|
|
185
|
+
this._containers.push({
|
|
186
|
+
__seqValue: attr.Value
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}, {
|
|
190
|
+
key: "_baseStartItem",
|
|
191
|
+
value: function _baseStartItem() {
|
|
192
|
+
var item = {};
|
|
193
|
+
this._container().__seqValue.push(item);
|
|
194
|
+
this._containers.push(item);
|
|
195
|
+
}
|
|
196
|
+
}, {
|
|
197
|
+
key: "_baseEndItem",
|
|
198
|
+
value: function _baseEndItem() {
|
|
199
|
+
this._containers.pop();
|
|
200
|
+
}
|
|
201
|
+
}, {
|
|
202
|
+
key: "_baseEndSequence",
|
|
203
|
+
value: function _baseEndSequence() {
|
|
204
|
+
this._containers.pop();
|
|
205
|
+
}
|
|
206
|
+
}]);
|
|
207
|
+
}(EventStreamListener);
|
|
208
|
+
function concatFragments(fragments) {
|
|
209
|
+
// a view, or a buffer (ArrayBuffer or SharedArrayBuffer)
|
|
210
|
+
var parts = fragments.map(function (f) {
|
|
211
|
+
return ArrayBuffer.isView(f) ? new Uint8Array(f.buffer, f.byteOffset, f.byteLength) : new Uint8Array(f);
|
|
212
|
+
});
|
|
213
|
+
var total = parts.reduce(function (n, p) {
|
|
214
|
+
return n + p.length;
|
|
215
|
+
}, 0);
|
|
216
|
+
var out = new Uint8Array(total);
|
|
217
|
+
var off = 0;
|
|
218
|
+
var _iterator = _createForOfIteratorHelper(parts),
|
|
219
|
+
_step;
|
|
220
|
+
try {
|
|
221
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
222
|
+
var p = _step.value;
|
|
223
|
+
out.set(p, off);
|
|
224
|
+
off += p.length;
|
|
225
|
+
}
|
|
226
|
+
} catch (err) {
|
|
227
|
+
_iterator.e(err);
|
|
228
|
+
} finally {
|
|
229
|
+
_iterator.f();
|
|
230
|
+
}
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
var FMI_GROUP_LENGTH = "00020000";
|
|
235
|
+
var TRANSFER_SYNTAX_UID = "00020010";
|
|
236
|
+
var UNDEFINED_LENGTH = 0xffffffff;
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* StreamingPart10Writer — plan-R2: an event-stream sink that emits DICOM
|
|
240
|
+
* Part 10 bytes incrementally, as events arrive, instead of collecting the
|
|
241
|
+
* whole dataset first like {@link Part10Writer}.
|
|
242
|
+
*
|
|
243
|
+
* Purpose: writing datasets whose Pixel Data does not fit in memory (the
|
|
244
|
+
* R2 trigger — e.g. a Supplement 225 fragmentable video instance measured in
|
|
245
|
+
* tens of GB). Peak memory is bounded by the largest single non-encapsulated
|
|
246
|
+
* element plus the file meta group; encapsulated pixel-data fragments are
|
|
247
|
+
* forwarded chunk-for-chunk and never accumulated.
|
|
248
|
+
*
|
|
249
|
+
* Output contract: `options.onChunk(Uint8Array)` is called zero or more times
|
|
250
|
+
* per event, in file order. Absent `onChunk`, chunks accumulate on
|
|
251
|
+
* `this.chunks` (testing convenience; that mode is NOT streaming). The caller
|
|
252
|
+
* owns backpressure: wire the generator's `setDrain` to the sink that
|
|
253
|
+
* consumes the chunks (e.g. a Node fs write stream's own drain).
|
|
254
|
+
*
|
|
255
|
+
* Encoding decisions (all legal Part 10, all chosen to avoid backpatching):
|
|
256
|
+
* - the file meta group is buffered — it is small and its group length
|
|
257
|
+
* (0002,0000) must be computed over the encoded elements, exactly as
|
|
258
|
+
* DicomDict.write does;
|
|
259
|
+
* - sequences and their items are written with undefined length and closed
|
|
260
|
+
* with item/sequence delimitation items;
|
|
261
|
+
* - encapsulated pixel data is written with undefined length, an empty
|
|
262
|
+
* Basic Offset Table, one item per incoming fragment (odd fragments are
|
|
263
|
+
* padded), and a sequence delimiter.
|
|
264
|
+
*
|
|
265
|
+
* Byte-identical round-tripping remains a non-goal (spec §4.5 / D15): output
|
|
266
|
+
* is correct, semantically faithful Part 10, not a byte-for-byte copy of any
|
|
267
|
+
* source.
|
|
268
|
+
*
|
|
269
|
+
* Not supported (throws): deflated transfer syntax, bulkDataReference events
|
|
270
|
+
* (the bytes are elsewhere — materialize them first), sequences inside the
|
|
271
|
+
* file meta group.
|
|
272
|
+
*/
|
|
273
|
+
var StreamingPart10Writer = /*#__PURE__*/function (_EventStreamListener) {
|
|
274
|
+
/**
|
|
275
|
+
* @param {Object} [options]
|
|
276
|
+
* @param {(chunk: Uint8Array) => void} [options.onChunk] - Byte sink;
|
|
277
|
+
* defaults to accumulating on `this.chunks`.
|
|
278
|
+
* @param {Object} [options.writeOptions] - Forwarded to the element
|
|
279
|
+
* encoders (e.g. { allowInvalidVRLength }).
|
|
280
|
+
* @param {...Object} filters - Event-stream filters, as for any listener.
|
|
281
|
+
*/
|
|
282
|
+
function StreamingPart10Writer() {
|
|
283
|
+
var _options$onChunk, _options$writeOptions;
|
|
284
|
+
var _this;
|
|
285
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
286
|
+
_classCallCheck(this, StreamingPart10Writer);
|
|
287
|
+
for (var _len = arguments.length, filters = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
288
|
+
filters[_key - 1] = arguments[_key];
|
|
289
|
+
}
|
|
290
|
+
_this = _callSuper(this, StreamingPart10Writer, [].concat(filters));
|
|
291
|
+
_this.chunks = [];
|
|
292
|
+
_this._onChunk = (_options$onChunk = options.onChunk) !== null && _options$onChunk !== void 0 ? _options$onChunk : function (chunk) {
|
|
293
|
+
return _this.chunks.push(chunk);
|
|
294
|
+
};
|
|
295
|
+
_this._writeOptions = (_options$writeOptions = options.writeOptions) !== null && _options$writeOptions !== void 0 ? _options$writeOptions : {
|
|
296
|
+
allowInvalidVRLength: false
|
|
297
|
+
};
|
|
298
|
+
_this.bytesWritten = 0;
|
|
299
|
+
_this._inMeta = false;
|
|
300
|
+
_this._meta = {};
|
|
301
|
+
_this._bodySyntax = EXPLICIT_LITTLE_ENDIAN;
|
|
302
|
+
_this._current = null;
|
|
303
|
+
_this._encapsulated = false;
|
|
304
|
+
_this.done = false;
|
|
305
|
+
return _this;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Endianness for every header the writer builds itself (sequence, item,
|
|
310
|
+
* delimiter and encapsulated pixel-data headers). Review finding 5: these
|
|
311
|
+
* were hardcoded little-endian while plain elements followed the body
|
|
312
|
+
* syntax, so an Explicit VR Big Endian file disagreed with itself. File
|
|
313
|
+
* Meta streams do NOT use this — the meta group is always explicit
|
|
314
|
+
* little endian (PS3.10 §7.1).
|
|
315
|
+
* @private
|
|
316
|
+
*/
|
|
317
|
+
_inherits(StreamingPart10Writer, _EventStreamListener);
|
|
318
|
+
return _createClass(StreamingPart10Writer, [{
|
|
319
|
+
key: "_bodyLittleEndian",
|
|
320
|
+
get: function get() {
|
|
321
|
+
return this._bodySyntax !== EXPLICIT_BIG_ENDIAN;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** @param {ArrayBuffer|Uint8Array} bytes @private */
|
|
325
|
+
}, {
|
|
326
|
+
key: "_emit",
|
|
327
|
+
value: function _emit(bytes) {
|
|
328
|
+
var chunk = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
329
|
+
this.bytesWritten += chunk.byteLength;
|
|
330
|
+
this._onChunk(chunk);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** @private */
|
|
334
|
+
}, {
|
|
335
|
+
key: "_emitStream",
|
|
336
|
+
value: function _emitStream(stream) {
|
|
337
|
+
this._emit(stream.getBuffer(0, stream.size));
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Writes a bare tag + 32-bit length, the form shared by item and
|
|
342
|
+
* delimitation items (never a VR).
|
|
343
|
+
* @private
|
|
344
|
+
*/
|
|
345
|
+
}, {
|
|
346
|
+
key: "_writeRawTag",
|
|
347
|
+
value: function _writeRawTag(stream, group, element, length) {
|
|
348
|
+
stream.writeUint16(group);
|
|
349
|
+
stream.writeUint16(element);
|
|
350
|
+
stream.writeUint32(length);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Writes an element header in the long (reserved + 32-bit length) form —
|
|
355
|
+
* the only form needed for the headers this writer emits itself (SQ and
|
|
356
|
+
* encapsulated OB/OW/UN); everything else goes through
|
|
357
|
+
* core's writeTagObject.
|
|
358
|
+
* @private
|
|
359
|
+
*/
|
|
360
|
+
}, {
|
|
361
|
+
key: "_writeLongHeader",
|
|
362
|
+
value: function _writeLongHeader(stream, tag, vr, length) {
|
|
363
|
+
stream.writeUint16(parseInt(tag.substring(0, 4), 16));
|
|
364
|
+
stream.writeUint16(parseInt(tag.substring(4, 8), 16));
|
|
365
|
+
if (this._bodySyntax !== IMPLICIT_LITTLE_ENDIAN) {
|
|
366
|
+
stream.writeAsciiString(vr);
|
|
367
|
+
stream.writeUint16(0);
|
|
368
|
+
}
|
|
369
|
+
stream.writeUint32(length);
|
|
370
|
+
}
|
|
371
|
+
}, {
|
|
372
|
+
key: "_baseStartDataSet",
|
|
373
|
+
value: function _baseStartDataSet() {}
|
|
374
|
+
}, {
|
|
375
|
+
key: "_baseEndDataSet",
|
|
376
|
+
value: function _baseEndDataSet() {
|
|
377
|
+
this.done = true;
|
|
378
|
+
}
|
|
379
|
+
}, {
|
|
380
|
+
key: "_baseStartFileMetaInformation",
|
|
381
|
+
value: function _baseStartFileMetaInformation() {
|
|
382
|
+
this._inMeta = true;
|
|
383
|
+
var stream = new WriteBufferStream(160, true);
|
|
384
|
+
stream.writeUint8Repeat(0, 128);
|
|
385
|
+
stream.writeAsciiString("DICM");
|
|
386
|
+
this._emitStream(stream);
|
|
387
|
+
}
|
|
388
|
+
}, {
|
|
389
|
+
key: "_baseEndFileMetaInformation",
|
|
390
|
+
value: function _baseEndFileMetaInformation() {
|
|
391
|
+
this._inMeta = false;
|
|
392
|
+
if (!this._meta[TRANSFER_SYNTAX_UID]) {
|
|
393
|
+
this._meta[TRANSFER_SYNTAX_UID] = {
|
|
394
|
+
vr: "UI",
|
|
395
|
+
Value: [EXPLICIT_LITTLE_ENDIAN]
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
this._bodySyntax = this._meta[TRANSFER_SYNTAX_UID].Value[0];
|
|
399
|
+
if (this._bodySyntax === DEFLATED_EXPLICIT_LITTLE_ENDIAN) {
|
|
400
|
+
throw new Error("StreamingPart10Writer does not support the deflated " + "transfer syntax; inflate the source or use Part10Writer");
|
|
401
|
+
}
|
|
402
|
+
// Group length must cover the encoded meta elements, so the group is
|
|
403
|
+
// the one part of the file that cannot stream element-by-element.
|
|
404
|
+
delete this._meta[FMI_GROUP_LENGTH];
|
|
405
|
+
var metaStream = new WriteBufferStream(1024, true);
|
|
406
|
+
writeDataSet(this._meta, metaStream, EXPLICIT_LITTLE_ENDIAN, this._writeOptions);
|
|
407
|
+
var out = new WriteBufferStream(1024 + 16, true);
|
|
408
|
+
writeTagObject(out, FMI_GROUP_LENGTH, "UL", metaStream.size, EXPLICIT_LITTLE_ENDIAN, this._writeOptions);
|
|
409
|
+
out.concat(metaStream);
|
|
410
|
+
this._emitStream(out);
|
|
411
|
+
this._meta = {};
|
|
412
|
+
}
|
|
413
|
+
}, {
|
|
414
|
+
key: "_baseStartElement",
|
|
415
|
+
value: function _baseStartElement(tag) {
|
|
416
|
+
var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
417
|
+
this._current = {
|
|
418
|
+
tag: tag,
|
|
419
|
+
vr: info.vr,
|
|
420
|
+
values: [],
|
|
421
|
+
emitted: false
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}, {
|
|
425
|
+
key: "_baseValue",
|
|
426
|
+
value: function _baseValue(v) {
|
|
427
|
+
this._current.values.push(v);
|
|
428
|
+
}
|
|
429
|
+
}, {
|
|
430
|
+
key: "_baseEndElement",
|
|
431
|
+
value: function _baseEndElement() {
|
|
432
|
+
var el = this._current;
|
|
433
|
+
this._current = null;
|
|
434
|
+
if (el.emitted) {
|
|
435
|
+
return; // encapsulated path already streamed it
|
|
436
|
+
}
|
|
437
|
+
if (this._inMeta) {
|
|
438
|
+
if (el.tag !== FMI_GROUP_LENGTH) {
|
|
439
|
+
this._meta[el.tag] = {
|
|
440
|
+
vr: el.vr,
|
|
441
|
+
Value: el.values
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
var stream = new WriteBufferStream(256, this._bodyLittleEndian);
|
|
447
|
+
writeTagObject(stream, el.tag, el.vr, el.values, this._bodySyntax, this._writeOptions);
|
|
448
|
+
this._emitStream(stream);
|
|
449
|
+
}
|
|
450
|
+
}, {
|
|
451
|
+
key: "_baseStartBinary",
|
|
452
|
+
value: function _baseStartBinary() {
|
|
453
|
+
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
454
|
+
if (opts.encapsulated && this._inMeta) {
|
|
455
|
+
throw new Error("encapsulated binary is not legal in the file meta group");
|
|
456
|
+
}
|
|
457
|
+
if (!opts.encapsulated) {
|
|
458
|
+
// Defined-length binary: fragments accumulate on the element and
|
|
459
|
+
// endElement serializes it whole (bounded by that one element).
|
|
460
|
+
this._encapsulated = false;
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
this._encapsulated = true;
|
|
464
|
+
this._current.emitted = true;
|
|
465
|
+
var stream = new WriteBufferStream(24, this._bodyLittleEndian);
|
|
466
|
+
this._writeLongHeader(stream, this._current.tag, this._current.vr || "OB", UNDEFINED_LENGTH);
|
|
467
|
+
// Empty Basic Offset Table: generators emit data fragments only (the
|
|
468
|
+
// source table, if any, indexes source offsets that no longer hold).
|
|
469
|
+
this._writeRawTag(stream, 0xfffe, 0xe000, 0);
|
|
470
|
+
this._emitStream(stream);
|
|
471
|
+
}
|
|
472
|
+
}, {
|
|
473
|
+
key: "_baseBinaryFragment",
|
|
474
|
+
value: function _baseBinaryFragment(chunk) {
|
|
475
|
+
var bytes = chunk instanceof Uint8Array ? chunk : chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
476
|
+
if (!this._encapsulated) {
|
|
477
|
+
// Review finding 11: bytes.buffer is the WHOLE backing store, so
|
|
478
|
+
// a subarray view would write (and retain) every byte of it.
|
|
479
|
+
// Slice out the view's own window when they differ.
|
|
480
|
+
this._current.values.push(bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength ? bytes.buffer : bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength));
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
var pad = bytes.byteLength % 2;
|
|
484
|
+
var header = new WriteBufferStream(8, this._bodyLittleEndian);
|
|
485
|
+
this._writeRawTag(header, 0xfffe, 0xe000, bytes.byteLength + pad);
|
|
486
|
+
this._emitStream(header);
|
|
487
|
+
this._emit(bytes);
|
|
488
|
+
if (pad) {
|
|
489
|
+
this._emit(new Uint8Array(1));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}, {
|
|
493
|
+
key: "_baseEndBinary",
|
|
494
|
+
value: function _baseEndBinary() {
|
|
495
|
+
if (this._encapsulated) {
|
|
496
|
+
var stream = new WriteBufferStream(8, this._bodyLittleEndian);
|
|
497
|
+
this._writeRawTag(stream, 0xfffe, 0xe0dd, 0);
|
|
498
|
+
this._emitStream(stream);
|
|
499
|
+
this._encapsulated = false;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}, {
|
|
503
|
+
key: "_baseStartSequence",
|
|
504
|
+
value: function _baseStartSequence(tag) {
|
|
505
|
+
var info = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
506
|
+
if (this._inMeta) {
|
|
507
|
+
throw new Error("sequences are not legal in the file meta group");
|
|
508
|
+
}
|
|
509
|
+
// Undefined length streams without backpatching; items and the
|
|
510
|
+
// delimiters below close it out (PS3.5 §7.5).
|
|
511
|
+
var stream = new WriteBufferStream(16, this._bodyLittleEndian);
|
|
512
|
+
this._writeLongHeader(stream, tag, info.vr || "SQ", UNDEFINED_LENGTH);
|
|
513
|
+
this._emitStream(stream);
|
|
514
|
+
}
|
|
515
|
+
}, {
|
|
516
|
+
key: "_baseStartItem",
|
|
517
|
+
value: function _baseStartItem() {
|
|
518
|
+
var stream = new WriteBufferStream(8, this._bodyLittleEndian);
|
|
519
|
+
this._writeRawTag(stream, 0xfffe, 0xe000, UNDEFINED_LENGTH);
|
|
520
|
+
this._emitStream(stream);
|
|
521
|
+
}
|
|
522
|
+
}, {
|
|
523
|
+
key: "_baseEndItem",
|
|
524
|
+
value: function _baseEndItem() {
|
|
525
|
+
var stream = new WriteBufferStream(8, this._bodyLittleEndian);
|
|
526
|
+
this._writeRawTag(stream, 0xfffe, 0xe00d, 0);
|
|
527
|
+
this._emitStream(stream);
|
|
528
|
+
}
|
|
529
|
+
}, {
|
|
530
|
+
key: "_baseEndSequence",
|
|
531
|
+
value: function _baseEndSequence() {
|
|
532
|
+
var stream = new WriteBufferStream(8, this._bodyLittleEndian);
|
|
533
|
+
this._writeRawTag(stream, 0xfffe, 0xe0dd, 0);
|
|
534
|
+
this._emitStream(stream);
|
|
535
|
+
}
|
|
536
|
+
}, {
|
|
537
|
+
key: "_baseBulkDataReference",
|
|
538
|
+
value: function _baseBulkDataReference() {
|
|
539
|
+
var _ref$uri;
|
|
540
|
+
var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
541
|
+
throw new Error("StreamingPart10Writer cannot serialize a bulkDataReference " + "(".concat((_ref$uri = ref.uri) !== null && _ref$uri !== void 0 ? _ref$uri : "no uri", "); materialize bulk data before writing"));
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Testing convenience for the accumulate mode: the collected chunks as
|
|
546
|
+
* one ArrayBuffer. Meaningless when onChunk was provided.
|
|
547
|
+
* @returns {ArrayBuffer}
|
|
548
|
+
*/
|
|
549
|
+
}, {
|
|
550
|
+
key: "toArrayBuffer",
|
|
551
|
+
value: function toArrayBuffer() {
|
|
552
|
+
var total = this.chunks.reduce(function (n, c) {
|
|
553
|
+
return n + c.byteLength;
|
|
554
|
+
}, 0);
|
|
555
|
+
var out = new Uint8Array(total);
|
|
556
|
+
var offset = 0;
|
|
557
|
+
var _iterator = _createForOfIteratorHelper(this.chunks),
|
|
558
|
+
_step;
|
|
559
|
+
try {
|
|
560
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
561
|
+
var chunk = _step.value;
|
|
562
|
+
out.set(chunk, offset);
|
|
563
|
+
offset += chunk.byteLength;
|
|
564
|
+
}
|
|
565
|
+
} catch (err) {
|
|
566
|
+
_iterator.e(err);
|
|
567
|
+
} finally {
|
|
568
|
+
_iterator.f();
|
|
569
|
+
}
|
|
570
|
+
return out.buffer;
|
|
571
|
+
}
|
|
572
|
+
}]);
|
|
573
|
+
}(EventStreamListener);
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* The recommended public source/sink API (spec §32) — thin, ergonomic wrappers
|
|
577
|
+
* over the event-stream generators and listeners.
|
|
578
|
+
*
|
|
579
|
+
* const events = DicomEventStream.fromPart10(bytes);
|
|
580
|
+
* const metadata = await Naturalized.from(events); // or events.toNaturalized()
|
|
581
|
+
* const json = await DicomWebJson.from(events); // or events.toDicomWebJson()
|
|
582
|
+
*
|
|
583
|
+
* A DicomEventStream wraps a re-runnable `run(listener)` thunk, so the same
|
|
584
|
+
* source can drive multiple sinks. Explicit listener usage remains available via
|
|
585
|
+
* `events.process(listener)`.
|
|
586
|
+
*
|
|
587
|
+
* Scope: the byte, dataset, and DICOMweb JSON sources and sinks, and the
|
|
588
|
+
* media factories (fromImage, fromPdf, fromVideo, fromVideoStream, toPdf,
|
|
589
|
+
* toVideo). The FHIR mapping is the separate, preliminary
|
|
590
|
+
* `@dcmjs-org/fhir` package: `toFhir(await events.toNaturalized())`.
|
|
591
|
+
*
|
|
592
|
+
* The media source factories pass `options` unchanged to their builder,
|
|
593
|
+
* which takes the `createInstance` options of `@dcmjs-org/core` (`instance`,
|
|
594
|
+
* `dataLevel`, `levels`, `predecessor`, `postProcess`, `baseDicom`,
|
|
595
|
+
* `overrides`). The DICOM attributes go in `overrides`, for example
|
|
596
|
+
* `fromPdf(bytes, { instance, overrides: { DocumentTitle: "Report" } })`.
|
|
597
|
+
*/
|
|
598
|
+
var DicomEventStream = /*#__PURE__*/function () {
|
|
599
|
+
/**
|
|
600
|
+
* @param {(listener: import("./EventStreamListener").EventStreamListener) => Promise<void>} run
|
|
601
|
+
*/
|
|
602
|
+
function DicomEventStream(run) {
|
|
603
|
+
_classCallCheck(this, DicomEventStream);
|
|
604
|
+
this._run = run;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/** A Part 10 byte buffer source. */
|
|
608
|
+
return _createClass(DicomEventStream, [{
|
|
609
|
+
key: "process",
|
|
610
|
+
value: /** Drive an arbitrary listener; resolves when the stream completes. */
|
|
611
|
+
function process(listener) {
|
|
612
|
+
return Promise.resolve(this._run(listener)).then(function () {
|
|
613
|
+
return listener;
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/** Materialize the naturalized object (slice D1). */
|
|
618
|
+
}, {
|
|
619
|
+
key: "toNaturalized",
|
|
620
|
+
value: (function () {
|
|
621
|
+
var _toNaturalized = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
622
|
+
var options,
|
|
623
|
+
listener,
|
|
624
|
+
_args = arguments;
|
|
625
|
+
return _regeneratorRuntime().wrap(function (_context) {
|
|
626
|
+
while (1) switch (_context.prev = _context.next) {
|
|
627
|
+
case 0:
|
|
628
|
+
options = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
|
|
629
|
+
listener = new NaturalizedListener(options);
|
|
630
|
+
_context.next = 1;
|
|
631
|
+
return this._run(listener);
|
|
632
|
+
case 1:
|
|
633
|
+
return _context.abrupt("return", listener.result);
|
|
634
|
+
case 2:
|
|
635
|
+
case "end":
|
|
636
|
+
return _context.stop();
|
|
637
|
+
}
|
|
638
|
+
}, _callee, this);
|
|
639
|
+
}));
|
|
640
|
+
function toNaturalized() {
|
|
641
|
+
return _toNaturalized.apply(this, arguments);
|
|
642
|
+
}
|
|
643
|
+
return toNaturalized;
|
|
644
|
+
}() /** Serialize to the DICOM JSON model (slice E1). */)
|
|
645
|
+
}, {
|
|
646
|
+
key: "toDicomWebJson",
|
|
647
|
+
value: (function () {
|
|
648
|
+
var _toDicomWebJson = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
649
|
+
var writer;
|
|
650
|
+
return _regeneratorRuntime().wrap(function (_context2) {
|
|
651
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
652
|
+
case 0:
|
|
653
|
+
writer = new DicomWebJsonWriter();
|
|
654
|
+
_context2.next = 1;
|
|
655
|
+
return this._run(writer);
|
|
656
|
+
case 1:
|
|
657
|
+
return _context2.abrupt("return", writer.result);
|
|
658
|
+
case 2:
|
|
659
|
+
case "end":
|
|
660
|
+
return _context2.stop();
|
|
661
|
+
}
|
|
662
|
+
}, _callee2, this);
|
|
663
|
+
}));
|
|
664
|
+
function toDicomWebJson() {
|
|
665
|
+
return _toDicomWebJson.apply(this, arguments);
|
|
666
|
+
}
|
|
667
|
+
return toDicomWebJson;
|
|
668
|
+
}() /** Serialize to a Part 10 ArrayBuffer (slice E2). */)
|
|
669
|
+
}, {
|
|
670
|
+
key: "toPart10",
|
|
671
|
+
value: (function () {
|
|
672
|
+
var _toPart = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(writeOptions) {
|
|
673
|
+
var writer;
|
|
674
|
+
return _regeneratorRuntime().wrap(function (_context3) {
|
|
675
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
676
|
+
case 0:
|
|
677
|
+
writer = new Part10Writer();
|
|
678
|
+
_context3.next = 1;
|
|
679
|
+
return this._run(writer);
|
|
680
|
+
case 1:
|
|
681
|
+
return _context3.abrupt("return", writer.write(writeOptions));
|
|
682
|
+
case 2:
|
|
683
|
+
case "end":
|
|
684
|
+
return _context3.stop();
|
|
685
|
+
}
|
|
686
|
+
}, _callee3, this);
|
|
687
|
+
}));
|
|
688
|
+
function toPart10(_x) {
|
|
689
|
+
return _toPart.apply(this, arguments);
|
|
690
|
+
}
|
|
691
|
+
return toPart10;
|
|
692
|
+
}() /** Rebuild a tag-keyed { meta, dict } tree (the reference collector). */)
|
|
693
|
+
}, {
|
|
694
|
+
key: "toDataSet",
|
|
695
|
+
value: (function () {
|
|
696
|
+
var _toDataSet = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
697
|
+
var collector;
|
|
698
|
+
return _regeneratorRuntime().wrap(function (_context4) {
|
|
699
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
700
|
+
case 0:
|
|
701
|
+
collector = new CollectorListener();
|
|
702
|
+
_context4.next = 1;
|
|
703
|
+
return this._run(collector);
|
|
704
|
+
case 1:
|
|
705
|
+
return _context4.abrupt("return", collector.result);
|
|
706
|
+
case 2:
|
|
707
|
+
case "end":
|
|
708
|
+
return _context4.stop();
|
|
709
|
+
}
|
|
710
|
+
}, _callee4, this);
|
|
711
|
+
}));
|
|
712
|
+
function toDataSet() {
|
|
713
|
+
return _toDataSet.apply(this, arguments);
|
|
714
|
+
}
|
|
715
|
+
return toDataSet;
|
|
716
|
+
}()
|
|
717
|
+
/**
|
|
718
|
+
* Extract the embedded PDF from an Encapsulated PDF instance:
|
|
719
|
+
* { bytes, mimeType, title }. Throws (naming the expected SOP class)
|
|
720
|
+
* when the stream is not an Encapsulated PDF instance.
|
|
721
|
+
*/
|
|
722
|
+
)
|
|
723
|
+
}, {
|
|
724
|
+
key: "toPdf",
|
|
725
|
+
value: (function () {
|
|
726
|
+
var _toPdf = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
|
|
727
|
+
var _t;
|
|
728
|
+
return _regeneratorRuntime().wrap(function (_context5) {
|
|
729
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
730
|
+
case 0:
|
|
731
|
+
_t = extractEncapsulatedPdf;
|
|
732
|
+
_context5.next = 1;
|
|
733
|
+
return this.toNaturalized();
|
|
734
|
+
case 1:
|
|
735
|
+
return _context5.abrupt("return", _t(_context5.sent));
|
|
736
|
+
case 2:
|
|
737
|
+
case "end":
|
|
738
|
+
return _context5.stop();
|
|
739
|
+
}
|
|
740
|
+
}, _callee5, this);
|
|
741
|
+
}));
|
|
742
|
+
function toPdf() {
|
|
743
|
+
return _toPdf.apply(this, arguments);
|
|
744
|
+
}
|
|
745
|
+
return toPdf;
|
|
746
|
+
}()
|
|
747
|
+
/**
|
|
748
|
+
* Recover the verbatim video stream from an encapsulated video
|
|
749
|
+
* instance: { bytes, transferSyntaxUID, declaredLength }. Fragments
|
|
750
|
+
* are concatenated and truncated to the declared (7FE0,0003) total
|
|
751
|
+
* length, so the result is byte-identical to the originally
|
|
752
|
+
* encapsulated MP4. Buffered — for multi-GB instances stream the
|
|
753
|
+
* fragments instead. Throws a corrective error when the stream is
|
|
754
|
+
* not a video instance.
|
|
755
|
+
*/
|
|
756
|
+
)
|
|
757
|
+
}, {
|
|
758
|
+
key: "toVideo",
|
|
759
|
+
value: (function () {
|
|
760
|
+
var _toVideo = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
|
|
761
|
+
var _t2;
|
|
762
|
+
return _regeneratorRuntime().wrap(function (_context6) {
|
|
763
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
764
|
+
case 0:
|
|
765
|
+
_t2 = extractEncapsulatedVideo;
|
|
766
|
+
_context6.next = 1;
|
|
767
|
+
return this.toNaturalized();
|
|
768
|
+
case 1:
|
|
769
|
+
return _context6.abrupt("return", _t2(_context6.sent));
|
|
770
|
+
case 2:
|
|
771
|
+
case "end":
|
|
772
|
+
return _context6.stop();
|
|
773
|
+
}
|
|
774
|
+
}, _callee6, this);
|
|
775
|
+
}));
|
|
776
|
+
function toVideo() {
|
|
777
|
+
return _toVideo.apply(this, arguments);
|
|
778
|
+
}
|
|
779
|
+
return toVideo;
|
|
780
|
+
}() /** Consume the stream as an async-iterable of `{ type, args }` events. */)
|
|
781
|
+
}, {
|
|
782
|
+
key: "asyncIterable",
|
|
783
|
+
value: function asyncIterable(options) {
|
|
784
|
+
var _this = this;
|
|
785
|
+
return createEventAsyncIterable(function (listener) {
|
|
786
|
+
return _this._run(listener);
|
|
787
|
+
}, options);
|
|
788
|
+
}
|
|
789
|
+
}], [{
|
|
790
|
+
key: "fromPart10",
|
|
791
|
+
value: function fromPart10$1(buffer) {
|
|
792
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
793
|
+
return new DicomEventStream(function (listener) {
|
|
794
|
+
return fromPart10(buffer, listener, options);
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* A chunked bytes→events source (slice K).
|
|
800
|
+
*
|
|
801
|
+
* Accepted `input` forms — see `fromPart10Stream` for the full contract:
|
|
802
|
+
* - `ArrayBuffer | Uint8Array` → re-runnable (each `.process()` starts fresh)
|
|
803
|
+
* - `AsyncIterable | ReadableStream` → single-use; second `.process()` rejects
|
|
804
|
+
* with a clear "already been consumed" error.
|
|
805
|
+
*
|
|
806
|
+
* `noCopy` is not accepted; the streaming source forces noCopy off because
|
|
807
|
+
* zero-copy views alias chunk memory that `consume()` releases (decision D-E).
|
|
808
|
+
*
|
|
809
|
+
* @param {ArrayBuffer|Uint8Array|AsyncIterable|ReadableStream} input
|
|
810
|
+
* @param {Object} [options]
|
|
811
|
+
* @param {boolean} [options.forceStoreRaw]
|
|
812
|
+
* @param {boolean} [options.ignoreErrors]
|
|
813
|
+
* @returns {DicomEventStream}
|
|
814
|
+
*/
|
|
815
|
+
}, {
|
|
816
|
+
key: "fromPart10Stream",
|
|
817
|
+
value: function fromPart10Stream$1(input) {
|
|
818
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
819
|
+
var isBuffer = input instanceof ArrayBuffer || ArrayBuffer.isView(input);
|
|
820
|
+
if (isBuffer) {
|
|
821
|
+
// Buffer inputs are re-runnable: every _run() creates a fresh parse.
|
|
822
|
+
return new DicomEventStream(function (listener) {
|
|
823
|
+
return fromPart10Stream(input, listener, options);
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// Iterable / ReadableStream inputs are single-use: the iterator is
|
|
828
|
+
// consumed on the first .process() call. A second call rejects with
|
|
829
|
+
// a clear error so callers are not silently handed an empty stream.
|
|
830
|
+
var consumed = false;
|
|
831
|
+
return new DicomEventStream(function (listener) {
|
|
832
|
+
if (consumed) {
|
|
833
|
+
return Promise.reject(new Error("DicomEventStream.fromPart10Stream: this stream source " + "has already been consumed and cannot be re-run. " + "Only ArrayBuffer/Uint8Array inputs are re-runnable. " + "To drive multiple sinks, convert the bytes to an " + "ArrayBuffer first and pass that instead."));
|
|
834
|
+
}
|
|
835
|
+
consumed = true;
|
|
836
|
+
return fromPart10Stream(input, listener, options);
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/** A DICOMweb JSON (DICOM JSON model) source. */
|
|
841
|
+
}, {
|
|
842
|
+
key: "fromDicomWebJson",
|
|
843
|
+
value: function fromDicomWebJson$1(json) {
|
|
844
|
+
return new DicomEventStream(function (listener) {
|
|
845
|
+
return fromDicomWebJson(json, listener);
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/** A parsed dcmjs dataset ({ meta, dict }) source. */
|
|
850
|
+
}, {
|
|
851
|
+
key: "fromDataSet",
|
|
852
|
+
value: function fromDataSet$1(dataset) {
|
|
853
|
+
return new DicomEventStream(function (listener) {
|
|
854
|
+
return fromDataSet(dataset, listener);
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* A naturalized dataset source; a `_meta` member becomes the file meta
|
|
860
|
+
* information. toNaturalized() gives no `_meta`, so set
|
|
861
|
+
* `dataset._meta` to keep the transfer syntax of the source. Set it on
|
|
862
|
+
* the dataset itself: a copy loses the deletes of private aliases.
|
|
863
|
+
*/
|
|
864
|
+
}, {
|
|
865
|
+
key: "fromNaturalized",
|
|
866
|
+
value: function fromNaturalized$1(dataset) {
|
|
867
|
+
return new DicomEventStream(function (listener) {
|
|
868
|
+
return fromNaturalized(dataset, listener);
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* An already-decoded image source: builds a full instance dataset via
|
|
874
|
+
* buildImageDataset of @dcmjs-org/imaging (geometry from the pixels,
|
|
875
|
+
* context from `instance` and `overrides`, `derivedFrom` for a derived
|
|
876
|
+
* instance), then streams it. The dataset is built eagerly so minted
|
|
877
|
+
* UIDs are stable across re-runs of the same stream.
|
|
878
|
+
*
|
|
879
|
+
* @param {Object} decodedImage - { pixels, rows, columns, ... }
|
|
880
|
+
* @param {Object} [options] - buildImageDataset options (the
|
|
881
|
+
* createInstance options plus `derivedFrom`, `lossy`, `encapsulated`)
|
|
882
|
+
* @returns {DicomEventStream}
|
|
883
|
+
*/
|
|
884
|
+
}, {
|
|
885
|
+
key: "fromImage",
|
|
886
|
+
value: function fromImage(decodedImage) {
|
|
887
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
888
|
+
var dataset = buildImageDataset(decodedImage, options);
|
|
889
|
+
var dicomDict = datasetToDict(dataset);
|
|
890
|
+
if (options.encapsulated && dicomDict.dict["7FE00010"]) {
|
|
891
|
+
// fromDataSet reads this flag to emit startBinary({encapsulated:true})
|
|
892
|
+
dicomDict.dict["7FE00010"].encapsulatedPixelData = true;
|
|
893
|
+
}
|
|
894
|
+
return new DicomEventStream(function (listener) {
|
|
895
|
+
return fromDataSet(dicomDict, listener);
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* A PDF source: wraps the bytes into an Encapsulated PDF instance
|
|
901
|
+
* (encapsulatePdf of @dcmjs-org/encapsulated) and streams it.
|
|
902
|
+
*
|
|
903
|
+
* @param {ArrayBuffer|Uint8Array} pdfBytes
|
|
904
|
+
* @param {Object} [options] - encapsulatePdf options (the
|
|
905
|
+
* createInstance options plus `mimeType`); the attributes
|
|
906
|
+
* (PatientName, DocumentTitle, ...) go in `overrides`
|
|
907
|
+
* @returns {DicomEventStream}
|
|
908
|
+
*/
|
|
909
|
+
}, {
|
|
910
|
+
key: "fromPdf",
|
|
911
|
+
value: function fromPdf(pdfBytes) {
|
|
912
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
913
|
+
var dicomDict = datasetToDict(encapsulatePdf(pdfBytes, options));
|
|
914
|
+
return new DicomEventStream(function (listener) {
|
|
915
|
+
return fromDataSet(dicomDict, listener);
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* An MP4 video source, buffered (mirrors fromPdf): the H.264 stream is
|
|
921
|
+
* carried verbatim as encapsulated PixelData in a Video Photographic
|
|
922
|
+
* Image instance (Supplement 225) — geometry, frame count, and frame
|
|
923
|
+
* rate are read from the MP4's moov metadata; no pixels are decoded.
|
|
924
|
+
* The source is created lazily on the first consumption and cached,
|
|
925
|
+
* so minted UIDs stay stable across re-runs AND a bad input cannot
|
|
926
|
+
* raise an unhandled rejection before the caller drives the stream
|
|
927
|
+
* (review finding 14) — the parse error surfaces from .process() or
|
|
928
|
+
* whichever sink runs first. Unsupported codecs (anything but H.264
|
|
929
|
+
* Baseline/Main/High up to Level 4.2) throw a corrective error naming
|
|
930
|
+
* the transcode.
|
|
931
|
+
*
|
|
932
|
+
* @param {ArrayBuffer|Uint8Array} mp4Bytes
|
|
933
|
+
* @param {Object} [options] - buildVideoDataset options (the
|
|
934
|
+
* createInstance options; the attributes go in `overrides`) plus
|
|
935
|
+
* `fragmentBytes`
|
|
936
|
+
* @returns {DicomEventStream}
|
|
937
|
+
*/
|
|
938
|
+
}, {
|
|
939
|
+
key: "fromVideo",
|
|
940
|
+
value: function fromVideo(mp4Bytes) {
|
|
941
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
942
|
+
var sourcePromise = null;
|
|
943
|
+
return new DicomEventStream(/*#__PURE__*/function () {
|
|
944
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(listener) {
|
|
945
|
+
return _regeneratorRuntime().wrap(function (_context7) {
|
|
946
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
947
|
+
case 0:
|
|
948
|
+
if (!sourcePromise) {
|
|
949
|
+
sourcePromise = createVideoEventSource(mp4Bytes, options);
|
|
950
|
+
}
|
|
951
|
+
_context7.next = 1;
|
|
952
|
+
return sourcePromise;
|
|
953
|
+
case 1:
|
|
954
|
+
return _context7.abrupt("return", _context7.sent.run(listener));
|
|
955
|
+
case 2:
|
|
956
|
+
case "end":
|
|
957
|
+
return _context7.stop();
|
|
958
|
+
}
|
|
959
|
+
}, _callee7);
|
|
960
|
+
}));
|
|
961
|
+
return function (_x2) {
|
|
962
|
+
return _ref.apply(this, arguments);
|
|
963
|
+
};
|
|
964
|
+
}());
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* The streaming form of fromVideo, for MP4s too large to buffer: the
|
|
969
|
+
* caller supplies a random-access reader and fragments are read one at
|
|
970
|
+
* a time, so peak memory is one fragment regardless of file size. Pair
|
|
971
|
+
* with StreamingPart10Writer for a bounded-memory MP4 → Part 10 write.
|
|
972
|
+
* Lazy like fromVideo: the reader is not touched until the first
|
|
973
|
+
* consumption (finding 14).
|
|
974
|
+
*
|
|
975
|
+
* @param {{size: number, read: (offset: number, length: number) =>
|
|
976
|
+
* Promise<Uint8Array>}} reader
|
|
977
|
+
* @param {Object} [options] - as fromVideo
|
|
978
|
+
* @returns {DicomEventStream}
|
|
979
|
+
*/
|
|
980
|
+
}, {
|
|
981
|
+
key: "fromVideoStream",
|
|
982
|
+
value: function fromVideoStream(reader) {
|
|
983
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
984
|
+
var sourcePromise = null;
|
|
985
|
+
return new DicomEventStream(/*#__PURE__*/function () {
|
|
986
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(listener) {
|
|
987
|
+
return _regeneratorRuntime().wrap(function (_context8) {
|
|
988
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
989
|
+
case 0:
|
|
990
|
+
if (!sourcePromise) {
|
|
991
|
+
sourcePromise = createVideoEventSource(reader, options);
|
|
992
|
+
}
|
|
993
|
+
_context8.next = 1;
|
|
994
|
+
return sourcePromise;
|
|
995
|
+
case 1:
|
|
996
|
+
return _context8.abrupt("return", _context8.sent.run(listener));
|
|
997
|
+
case 2:
|
|
998
|
+
case "end":
|
|
999
|
+
return _context8.stop();
|
|
1000
|
+
}
|
|
1001
|
+
}, _callee8);
|
|
1002
|
+
}));
|
|
1003
|
+
return function (_x3) {
|
|
1004
|
+
return _ref2.apply(this, arguments);
|
|
1005
|
+
};
|
|
1006
|
+
}());
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
/**
|
|
1010
|
+
* Auto-detecting source factory: an ArrayBuffer/typed array is treated as
|
|
1011
|
+
* Part 10 bytes; an object with a `dict` (or `meta`) is a parsed dataset;
|
|
1012
|
+
* any other object is the DICOM JSON model.
|
|
1013
|
+
*/
|
|
1014
|
+
}, {
|
|
1015
|
+
key: "from",
|
|
1016
|
+
value: function from(source) {
|
|
1017
|
+
if (source instanceof ArrayBuffer || ArrayBuffer.isView(source)) {
|
|
1018
|
+
return DicomEventStream.fromPart10(source);
|
|
1019
|
+
}
|
|
1020
|
+
if (source && _typeof(source) === "object") {
|
|
1021
|
+
if (source.dict || source.meta) {
|
|
1022
|
+
return DicomEventStream.fromDataSet(source);
|
|
1023
|
+
}
|
|
1024
|
+
return DicomEventStream.fromDicomWebJson(source);
|
|
1025
|
+
}
|
|
1026
|
+
throw new TypeError("DicomEventStream.from: unrecognized source (expected Part 10 bytes, a { meta, dict } dataset, or a DICOM JSON object)");
|
|
1027
|
+
}
|
|
1028
|
+
}]);
|
|
1029
|
+
}();
|
|
1030
|
+
|
|
1031
|
+
/** §32 sink helpers — `Naturalized.from(events)` / `DicomWebJson.from(events)`. */
|
|
1032
|
+
var Naturalized = {
|
|
1033
|
+
from: function from(source, options) {
|
|
1034
|
+
return source.toNaturalized(options);
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
var DicomWebJson = {
|
|
1038
|
+
from: function from(source) {
|
|
1039
|
+
return source.toDicomWebJson();
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* The dcmjs event-stream contract.
|
|
1045
|
+
*
|
|
1046
|
+
* The canonical, source-agnostic interchange layer: readers produce event
|
|
1047
|
+
* streams, listeners and writers consume them. This module grows as the
|
|
1048
|
+
* engine slices land; it currently exports the contract, the reference
|
|
1049
|
+
* collector, the byte / dataset / DICOMweb JSON sources, the async pull
|
|
1050
|
+
* adapter, the naturalized-model listener, and the collector-backed writer
|
|
1051
|
+
* sinks (Part 10 and DICOMweb JSON), plus the DicomEventStream facade over
|
|
1052
|
+
* all of them.
|
|
1053
|
+
*/
|
|
1054
|
+
var index = {
|
|
1055
|
+
EventStreamListener: EventStreamListener,
|
|
1056
|
+
EVENT_STREAM_VOCABULARY: EVENT_STREAM_VOCABULARY,
|
|
1057
|
+
CONTRACT_VERSION: CONTRACT_VERSION,
|
|
1058
|
+
CollectorListener: CollectorListener,
|
|
1059
|
+
NaturalizedListener: NaturalizedListener,
|
|
1060
|
+
fromDataSet: fromDataSet,
|
|
1061
|
+
fromNaturalized: fromNaturalized,
|
|
1062
|
+
fromDicomWebJson: fromDicomWebJson,
|
|
1063
|
+
createEventAsyncIterable: createEventAsyncIterable,
|
|
1064
|
+
fromPart10: fromPart10,
|
|
1065
|
+
fromPart10Stream: fromPart10Stream,
|
|
1066
|
+
Part10Writer: Part10Writer,
|
|
1067
|
+
DicomWebJsonWriter: DicomWebJsonWriter,
|
|
1068
|
+
StreamingPart10Writer: StreamingPart10Writer,
|
|
1069
|
+
DicomEventStream: DicomEventStream,
|
|
1070
|
+
Naturalized: Naturalized,
|
|
1071
|
+
DicomWebJson: DicomWebJson
|
|
1072
|
+
};
|
|
1073
|
+
|
|
1074
|
+
export { DicomEventStream, DicomWebJson, DicomWebJsonWriter, Naturalized, Part10Writer, StreamingPart10Writer, index as default };
|
|
1075
|
+
//# sourceMappingURL=eventStream.es.js.map
|