|
| 1 | +import { |
| 2 | + BinaryStream, |
| 3 | + InterfaceTypes, |
| 4 | + runPipeline |
| 5 | +} from 'itk-wasm' |
| 6 | + |
| 7 | +import ReadDicomEncapsulatedPdfOptions from './ReadDicomEncapsulatedPdfOptions.js' |
| 8 | +import ReadDicomEncapsulatedPdfResult from './ReadDicomEncapsulatedPdfResult.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * Extract PDF file from DICOM encapsulated PDF. |
| 12 | + * |
| 13 | + * @param {Uint8Array} dicomFile - Input DICOM file |
| 14 | + * |
| 15 | + * @returns {Promise<ReadDicomEncapsulatedPdfResult>} - result object |
| 16 | + */ |
| 17 | +async function readDicomEncapsulatedPdf( |
| 18 | + webWorker: null | Worker, |
| 19 | + dicomFile: Uint8Array, |
| 20 | + options: ReadDicomEncapsulatedPdfOptions = {}) |
| 21 | + : Promise<ReadDicomEncapsulatedPdfResult> { |
| 22 | + |
| 23 | + const desiredOutputs = [ |
| 24 | + { type: InterfaceTypes.BinaryStream }, |
| 25 | + ] |
| 26 | + const inputs = [ |
| 27 | + { type: InterfaceTypes.BinaryFile, data: { data: dicomFile, path: "file0" } }, |
| 28 | + ] |
| 29 | + |
| 30 | + const args = [] |
| 31 | + // Inputs |
| 32 | + args.push('file0') |
| 33 | + // Outputs |
| 34 | + args.push('0') |
| 35 | + // Options |
| 36 | + args.push('--memory-io') |
| 37 | + if (options.readFileOnly) { |
| 38 | + args.push('--read-file-only') |
| 39 | + } |
| 40 | + if (options.readDataset) { |
| 41 | + args.push('--read-dataset') |
| 42 | + } |
| 43 | + if (options.readXferAuto) { |
| 44 | + args.push('--read-xfer-auto') |
| 45 | + } |
| 46 | + if (options.readXferDetect) { |
| 47 | + args.push('--read-xfer-detect') |
| 48 | + } |
| 49 | + if (options.readXferLittle) { |
| 50 | + args.push('--read-xfer-little') |
| 51 | + } |
| 52 | + if (options.readXferBig) { |
| 53 | + args.push('--read-xfer-big') |
| 54 | + } |
| 55 | + if (options.readXferImplicit) { |
| 56 | + args.push('--read-xfer-implicit') |
| 57 | + } |
| 58 | + if (options.acceptOddLength) { |
| 59 | + args.push('--accept-odd-length') |
| 60 | + } |
| 61 | + if (options.assumeEvenLength) { |
| 62 | + args.push('--assume-even-length') |
| 63 | + } |
| 64 | + if (options.enableCp246) { |
| 65 | + args.push('--enable-cp246') |
| 66 | + } |
| 67 | + if (options.disableCp246) { |
| 68 | + args.push('--disable-cp246') |
| 69 | + } |
| 70 | + if (options.retainUn) { |
| 71 | + args.push('--retain-un') |
| 72 | + } |
| 73 | + if (options.convertUn) { |
| 74 | + args.push('--convert-un') |
| 75 | + } |
| 76 | + if (options.enableCorrection) { |
| 77 | + args.push('--enable-correction') |
| 78 | + } |
| 79 | + if (options.disableCorrection) { |
| 80 | + args.push('--disable-correction') |
| 81 | + } |
| 82 | + |
| 83 | + const pipelinePath = 'read-dicom-encapsulated-pdf' |
| 84 | + |
| 85 | + const { |
| 86 | + webWorker: usedWebWorker, |
| 87 | + returnValue, |
| 88 | + stderr, |
| 89 | + outputs |
| 90 | + } = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs) |
| 91 | + if (returnValue !== 0) { |
| 92 | + throw new Error(stderr) |
| 93 | + } |
| 94 | + |
| 95 | + const result = { |
| 96 | + webWorker: usedWebWorker as Worker, |
| 97 | + pdfBinaryOutput: (outputs[0].data as BinaryStream).data, |
| 98 | + } |
| 99 | + return result |
| 100 | +} |
| 101 | + |
| 102 | +export default readDicomEncapsulatedPdf |
0 commit comments