@dcmjs-org/imaging 0.0.0-bootstrap.0 → 1.0.0-beta.3
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/README.md +252 -0
- package/package.json +7 -3
- package/src/buildImageDataset.js +371 -0
- package/src/derivedImageAttributes.js +126 -0
- package/src/h264.js +39 -0
- package/src/index.js +18 -0
- package/src/jpegInfo.js +154 -0
- package/test/buildImageDataset.test.js +550 -0
- package/test/h264.test.js +29 -0
- package/test/jpegInfo.test.js +154 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// test/jpegInfo.test.js
|
|
2
|
+
//
|
|
3
|
+
// parseJpegInfo: SOF geometry extraction and the JPEG → DICOM transfer
|
|
4
|
+
// syntax mapping, over bytes built inline — no binary fixtures. The
|
|
5
|
+
// truncation cases pin review finding 9: a JPEG cut inside the SOF field
|
|
6
|
+
// span must throw, never return zero-coerced geometry.
|
|
7
|
+
|
|
8
|
+
import { parseJpegInfo } from "../src/jpegInfo.js";
|
|
9
|
+
|
|
10
|
+
const JPEG_BASELINE_TS = "1.2.840.10008.1.2.4.50";
|
|
11
|
+
const JPEG_EXTENDED_TS = "1.2.840.10008.1.2.4.51";
|
|
12
|
+
const JPEG_LOSSLESS_TS = "1.2.840.10008.1.2.4.57";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A minimal structurally valid JPEG: SOI, APP0 stub, optional APP14 Adobe,
|
|
16
|
+
* SOF, EOI. `sampling` holds the H/V byte of each component.
|
|
17
|
+
*/
|
|
18
|
+
function makeJpeg({
|
|
19
|
+
sofMarker = 0xc0,
|
|
20
|
+
precision = 8,
|
|
21
|
+
rows = 480,
|
|
22
|
+
columns = 640,
|
|
23
|
+
components = 3,
|
|
24
|
+
ids = [1, 2, 3, 4],
|
|
25
|
+
sampling = [0x11, 0x11, 0x11, 0x11],
|
|
26
|
+
adobeTransform
|
|
27
|
+
} = {}) {
|
|
28
|
+
const componentSpecs = [];
|
|
29
|
+
for (let i = 0; i < components; i++) {
|
|
30
|
+
componentSpecs.push(ids[i], sampling[i], 0);
|
|
31
|
+
}
|
|
32
|
+
const adobe =
|
|
33
|
+
adobeTransform === undefined
|
|
34
|
+
? []
|
|
35
|
+
: [0xff, 0xee, 0x00, 0x0e, 0x41, 0x64, 0x6f, 0x62, 0x65].concat([
|
|
36
|
+
0,
|
|
37
|
+
100,
|
|
38
|
+
0,
|
|
39
|
+
0,
|
|
40
|
+
0,
|
|
41
|
+
0,
|
|
42
|
+
adobeTransform
|
|
43
|
+
]);
|
|
44
|
+
const sofLength = 8 + components * 3;
|
|
45
|
+
return Uint8Array.from([
|
|
46
|
+
0xff,
|
|
47
|
+
0xd8, // SOI
|
|
48
|
+
0xff,
|
|
49
|
+
0xe0,
|
|
50
|
+
0x00,
|
|
51
|
+
0x04,
|
|
52
|
+
0x4a,
|
|
53
|
+
0x46, // APP0 stub
|
|
54
|
+
...adobe,
|
|
55
|
+
0xff,
|
|
56
|
+
sofMarker,
|
|
57
|
+
(sofLength >> 8) & 0xff,
|
|
58
|
+
sofLength & 0xff,
|
|
59
|
+
precision,
|
|
60
|
+
(rows >> 8) & 0xff,
|
|
61
|
+
rows & 0xff,
|
|
62
|
+
(columns >> 8) & 0xff,
|
|
63
|
+
columns & 0xff,
|
|
64
|
+
components,
|
|
65
|
+
...componentSpecs,
|
|
66
|
+
0xff,
|
|
67
|
+
0xd9 // EOI
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe("parseJpegInfo", () => {
|
|
72
|
+
it("reads geometry and maps SOF0 to the baseline transfer syntax", () => {
|
|
73
|
+
const info = parseJpegInfo(makeJpeg());
|
|
74
|
+
expect(info.rows).toBe(480);
|
|
75
|
+
expect(info.columns).toBe(640);
|
|
76
|
+
expect(info.bitsAllocated).toBe(8);
|
|
77
|
+
expect(info.samplesPerPixel).toBe(3);
|
|
78
|
+
expect(info.sofMarker).toBe(0xc0);
|
|
79
|
+
expect(info.transferSyntaxUID).toBe(JPEG_BASELINE_TS);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("maps SOF1 to extended and SOF3 to lossless", () => {
|
|
83
|
+
expect(
|
|
84
|
+
parseJpegInfo(makeJpeg({ sofMarker: 0xc1 })).transferSyntaxUID
|
|
85
|
+
).toBe(JPEG_EXTENDED_TS);
|
|
86
|
+
expect(
|
|
87
|
+
parseJpegInfo(makeJpeg({ sofMarker: 0xc3, components: 1 }))
|
|
88
|
+
.transferSyntaxUID
|
|
89
|
+
).toBe(JPEG_LOSSLESS_TS);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("gives DICOM bit depths: 12-bit precision is BitsAllocated 16, BitsStored 12", () => {
|
|
93
|
+
const info = parseJpegInfo(
|
|
94
|
+
makeJpeg({ sofMarker: 0xc1, precision: 12, components: 1 })
|
|
95
|
+
);
|
|
96
|
+
expect(info.bitsAllocated).toBe(16);
|
|
97
|
+
expect(info.bitsStored).toBe(12);
|
|
98
|
+
expect(info.photometricInterpretation).toBe("MONOCHROME2");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it.each([
|
|
102
|
+
["subsampled chroma", { sampling: [0x21, 0x11, 0x11] }, "YBR_FULL_422"],
|
|
103
|
+
["no subsampling (4:4:4)", {}, "YBR_FULL"],
|
|
104
|
+
["component IDs R, G, B", { ids: [0x52, 0x47, 0x42] }, "RGB"],
|
|
105
|
+
["Adobe transform 0", { adobeTransform: 0 }, "RGB"],
|
|
106
|
+
[
|
|
107
|
+
"Adobe transform 1 over R, G, B IDs",
|
|
108
|
+
{ adobeTransform: 1, ids: [0x52, 0x47, 0x42] },
|
|
109
|
+
"YBR_FULL"
|
|
110
|
+
],
|
|
111
|
+
["lossless SOF3", { sofMarker: 0xc3 }, "RGB"],
|
|
112
|
+
["4 components (CMYK)", { components: 4 }, null]
|
|
113
|
+
])("photometric interpretation: %s", (_, options, expected) => {
|
|
114
|
+
expect(parseJpegInfo(makeJpeg(options)).photometricInterpretation).toBe(
|
|
115
|
+
expected
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("leaves the transfer syntax null for progressive (SOF2)", () => {
|
|
120
|
+
const info = parseJpegInfo(makeJpeg({ sofMarker: 0xc2 }));
|
|
121
|
+
expect(info.transferSyntaxUID).toBeNull();
|
|
122
|
+
expect(info.columns).toBe(640);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("rejects non-JPEG input (missing SOI)", () => {
|
|
126
|
+
expect(() => parseJpegInfo(new Uint8Array(16).fill(0x41))).toThrow(
|
|
127
|
+
/not JPEG/
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("rejects a JPEG with no SOF before EOI", () => {
|
|
132
|
+
expect(() =>
|
|
133
|
+
parseJpegInfo(Uint8Array.from([0xff, 0xd8, 0xff, 0xd9]))
|
|
134
|
+
).toThrow(/no frame header/);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Review finding 9: the exact vector from the review map — a 480×640
|
|
138
|
+
// JPEG cut one byte into the width field. The unfixed reader returned
|
|
139
|
+
// columns 512 (undefined coerced to 0 in the bitwise OR) and the wrong
|
|
140
|
+
// geometry flowed silently into the built dataset.
|
|
141
|
+
it("throws on a JPEG cut one byte into the SOF width field (finding 9)", () => {
|
|
142
|
+
const truncated = Uint8Array.from([
|
|
143
|
+
0xff, 0xd8, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x01, 0xe0, 0x02
|
|
144
|
+
]);
|
|
145
|
+
expect(() => parseJpegInfo(truncated)).toThrow(/truncated/);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Finding 9, second shape: cut right after the SOF length field. The
|
|
149
|
+
// unfixed reader returned rows 0, columns 0, and no bit depth.
|
|
150
|
+
it("throws on a JPEG cut right after the SOF length field (finding 9)", () => {
|
|
151
|
+
const truncated = Uint8Array.from([0xff, 0xd8, 0xff, 0xc0, 0x00, 0x11]);
|
|
152
|
+
expect(() => parseJpegInfo(truncated)).toThrow(/truncated/);
|
|
153
|
+
});
|
|
154
|
+
});
|