diff --git a/src/DicomMessage.js b/src/DicomMessage.js index 87f503f8..72b89f00 100644 --- a/src/DicomMessage.js +++ b/src/DicomMessage.js @@ -378,7 +378,7 @@ class DicomMessage { if (typeof value === "string") { const delimiterChar = String.fromCharCode(VM_DELIMITER); rawValues = vr.dropPadByte(rawValue.split(delimiterChar)) - values = value.split(delimiterChar); + values = vr.dropPadByte(value.split(delimiterChar)); } } else if (vr.type == "SQ") { rawValues = rawValue; diff --git a/src/ValueRepresentation.js b/src/ValueRepresentation.js index 3abe1a3f..d84aa741 100644 --- a/src/ValueRepresentation.js +++ b/src/ValueRepresentation.js @@ -149,7 +149,8 @@ class ValueRepresentation { * @returns {string[]} The modified array, with the padding byte potentially removed from the last value. */ dropPadByte(values) { - if (!Array.isArray(values) || !this.maxLength || !this.padByte) { + const maxLength = this.maxLength ?? this.maxCharLength; + if (!Array.isArray(values) || !maxLength || !this.padByte) { return values; } @@ -162,8 +163,8 @@ class ValueRepresentation { // If the last element exceeds the max length by exactly one character and ends with the padding byte, // trim the padding byte to avoid a max length violation during write. - if (lastValue.length === this.maxLength + 1 && lastValue.endsWith(padChar)) { - values[lastIdx] = lastValue.substring(0, this.maxLength); // Trim the padding byte + if (lastValue.length === maxLength + 1 && lastValue.endsWith(padChar)) { + values[lastIdx] = lastValue.substring(0, maxLength); // Trim the padding byte } } @@ -355,10 +356,6 @@ class AsciiStringRepresentation extends ValueRepresentation { return stream.readAsciiString(length); } - applyFormatting(value) { - return value.trim(); - } - writeBytes(stream, value, writeOptions) { const written = super.write(stream, "AsciiString", value); diff --git a/test/lossless-read-write.test.js b/test/lossless-read-write.test.js index 1d53f459..3418fc98 100644 --- a/test/lossless-read-write.test.js +++ b/test/lossless-read-write.test.js @@ -2,11 +2,11 @@ import "regenerator-runtime/runtime.js"; import fs from "fs"; import dcmjs from "../src/index.js"; -import {deepEqual} from "../src/utilities/deepEqual"; +import { deepEqual } from "../src/utilities/deepEqual"; -import {getTestDataset} from "./testUtils"; +import { getTestDataset } from "./testUtils"; -const {DicomDict, DicomMessage} = dcmjs.data; +const { DicomDict, DicomMessage } = dcmjs.data; describe('lossless-read-write', () => { @@ -192,21 +192,6 @@ describe('lossless-read-write', () => { expect(deepEqual(expectedDataset, outputDicomDictPass2.dict)).toBeTruthy(); }); - test('test DA with multiplicity > 1 and added space for even padding is read and written correctly', () => { - const dataset = { - '00081160': { - vr: 'DA', - Value: ["20140601", "20140601 "], - } - }; - - const dicomDict = new DicomDict({}); - dicomDict.dict = dataset; - - // write and re-read - const outputDicomDict = DicomMessage.readFile(dicomDict.write()); - }); - describe('Multiplicity for non-binary String VRs', () => { const maxLengthCases = [ { @@ -249,6 +234,27 @@ describe('lossless-read-write', () => { Value: [123456789012, 123456789012], _rawValue: ["123456789012", "123456789012"] }, + { + vr: 'LO', + Value: ["ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP"], + _rawValue: ["ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOP"] + }, + { + vr: 'SH', + Value: ["ABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOP"], + _rawValue: ["ABCDEFGHIJKLMNOP", "ABCDEFGHIJKLMNOP"] + }, + { + vr: 'UI', + Value: ["1.2.840.12345678901234567890123456789012345678901234567890123456", "1.2.840.12345678901234567890123456789012345678901234567890123456"], + _rawValue: ["1.2.840.12345678901234567890123456789012345678901234567890123456", "1.2.840.12345678901234567890123456789012345678901234567890123456"] + }, + { + vr: 'TM', + Value: ["142530.1234567", "142530.1234567"], + _rawValue: ["142530.1234567", "142530.1234567"], + }, + ]; test.each(maxLengthCases)( @@ -274,7 +280,7 @@ describe('lossless-read-write', () => { } }; - expect(expectedDataset).toEqual(outputDicomDict.dict); + expect(outputDicomDict.dict).toEqual(expectedDataset); // re-write should succeed without max length issues const outputDicomDictPass2 = DicomMessage.readFile(outputDicomDict.write());