|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import * as zlib from 'node:zlib'; |
| 4 | +import JSZip from 'jszip'; |
| 5 | +import { volViewPage } from '../pageobjects/volview.page'; |
| 6 | +import { TEMP_DIR } from '../../wdio.shared.conf'; |
| 7 | +import { waitForFileExists } from './utils'; |
| 8 | +import { ONE_CT_SLICE_DICOM, openConfigAndDataset } from './configTestUtils'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Parse NRRD header key-value pairs from a buffer (handles gzip). |
| 12 | + */ |
| 13 | +const parseNrrdHeader = (buf: Buffer): Map<string, string> => { |
| 14 | + const raw = buf[0] === 0x1f && buf[1] === 0x8b ? zlib.gunzipSync(buf) : buf; |
| 15 | + const text = raw.toString('ascii', 0, Math.min(raw.length, 16384)); |
| 16 | + const headerEnd = text.indexOf('\n\n'); |
| 17 | + const headerText = headerEnd >= 0 ? text.slice(0, headerEnd) : text; |
| 18 | + |
| 19 | + const entries = new Map<string, string>(); |
| 20 | + headerText.split('\n').forEach((line) => { |
| 21 | + const sepIdx = line.indexOf(':='); |
| 22 | + if (sepIdx >= 0) { |
| 23 | + entries.set(line.slice(0, sepIdx).trim(), line.slice(sepIdx + 2).trim()); |
| 24 | + return; |
| 25 | + } |
| 26 | + const colonIdx = line.indexOf(':'); |
| 27 | + if (colonIdx >= 0 && !line.startsWith('#') && !line.startsWith('NRRD')) { |
| 28 | + entries.set( |
| 29 | + line.slice(0, colonIdx).trim(), |
| 30 | + line.slice(colonIdx + 1).trim() |
| 31 | + ); |
| 32 | + } |
| 33 | + }); |
| 34 | + return entries; |
| 35 | +}; |
| 36 | + |
| 37 | +describe('Slicer-compatible seg.nrrd export', function () { |
| 38 | + this.timeout(120_000); |
| 39 | + |
| 40 | + it('session save includes Slicer metadata in seg.nrrd labelmap', async () => { |
| 41 | + const config = { io: { segmentGroupSaveFormat: 'seg.nrrd' } }; |
| 42 | + await openConfigAndDataset(config, 'seg-nrrd-export', ONE_CT_SLICE_DICOM); |
| 43 | + |
| 44 | + // Activate paint tool — creates a segment group |
| 45 | + await volViewPage.activatePaint(); |
| 46 | + |
| 47 | + // Paint a stroke so the labelmap has data |
| 48 | + const views2D = await volViewPage.getViews2D(); |
| 49 | + const canvas = await views2D[0].$('canvas'); |
| 50 | + const location = await canvas.getLocation(); |
| 51 | + const size = await canvas.getSize(); |
| 52 | + const cx = Math.round(location.x + size.width / 2); |
| 53 | + const cy = Math.round(location.y + size.height / 2); |
| 54 | + |
| 55 | + await browser |
| 56 | + .action('pointer') |
| 57 | + .move({ x: cx, y: cy }) |
| 58 | + .down() |
| 59 | + .move({ x: cx + 20, y: cy }) |
| 60 | + .up() |
| 61 | + .perform(); |
| 62 | + |
| 63 | + // Save session — downloads a .volview.zip containing the seg.nrrd |
| 64 | + const sessionFileName = await volViewPage.saveSession(); |
| 65 | + const downloadedPath = path.join(TEMP_DIR, sessionFileName); |
| 66 | + |
| 67 | + await waitForFileExists(downloadedPath, 30_000); |
| 68 | + |
| 69 | + // Wait for file to be fully written |
| 70 | + await browser.waitUntil( |
| 71 | + () => { |
| 72 | + try { |
| 73 | + return fs.statSync(downloadedPath).size > 0; |
| 74 | + } catch { |
| 75 | + return false; |
| 76 | + } |
| 77 | + }, |
| 78 | + { |
| 79 | + timeout: 10_000, |
| 80 | + interval: 500, |
| 81 | + timeoutMsg: 'Downloaded session zip remained 0 bytes', |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + // Extract the seg.nrrd file from the session zip |
| 86 | + const zipData = fs.readFileSync(downloadedPath); |
| 87 | + const zip = await JSZip.loadAsync(zipData); |
| 88 | + |
| 89 | + const segNrrdFile = Object.keys(zip.files).find((name) => |
| 90 | + name.endsWith('.seg.nrrd') |
| 91 | + ); |
| 92 | + expect(segNrrdFile).toBeDefined(); |
| 93 | + |
| 94 | + const nrrdBuffer = Buffer.from( |
| 95 | + await zip.files[segNrrdFile!].async('arraybuffer') |
| 96 | + ); |
| 97 | + const header = parseNrrdHeader(nrrdBuffer); |
| 98 | + |
| 99 | + // Global segmentation fields |
| 100 | + expect(header.get('Segmentation_MasterRepresentation')).toBe( |
| 101 | + 'Binary labelmap' |
| 102 | + ); |
| 103 | + expect(header.get('Segmentation_ContainedRepresentationNames')).toBe( |
| 104 | + 'Binary labelmap|' |
| 105 | + ); |
| 106 | + expect(header.get('Segmentation_ReferenceImageExtentOffset')).toBe('0 0 0'); |
| 107 | + |
| 108 | + // Per-segment fields — default first segment is "Segment 1" with value 1 |
| 109 | + expect(header.get('Segment0_ID')).toBe('Segment_1'); |
| 110 | + expect(header.get('Segment0_Name')).toBe('Segment 1'); |
| 111 | + expect(header.get('Segment0_LabelValue')).toBe('1'); |
| 112 | + expect(header.get('Segment0_Layer')).toBe('0'); |
| 113 | + expect(header.get('Segment0_Extent')).toBeDefined(); |
| 114 | + expect(header.get('Segment0_Tags')).toBe('|'); |
| 115 | + |
| 116 | + // Color should be 3 space-separated floats between 0 and 1 |
| 117 | + const colorStr = header.get('Segment0_Color'); |
| 118 | + expect(colorStr).toBeDefined(); |
| 119 | + const colorParts = colorStr!.split(' ').map(Number); |
| 120 | + expect(colorParts).toHaveLength(3); |
| 121 | + colorParts.forEach((c) => { |
| 122 | + expect(c).toBeGreaterThanOrEqual(0); |
| 123 | + expect(c).toBeLessThanOrEqual(1); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments