Skip to content

Commit

Permalink
Trip padding byte from last element in multiple value arrays if it is…
Browse files Browse the repository at this point in the history
… odd length
  • Loading branch information
Craig Berry committed Sep 4, 2024
1 parent b803667 commit 2b57f10
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/ValueRepresentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ class ValueRepresentation {
const lastIdx = values.length - 1;
const lastValue = values[lastIdx];

// 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 === maxLength + 1 && lastValue.endsWith(padChar)) {
values[lastIdx] = lastValue.substring(0, maxLength); // Trim the padding byte
// If the last element is odd and ends with the padding byte trim to avoid potential max length violations during write
if (lastValue.length % 2 !== 0 && lastValue.endsWith(padChar)) {
values[lastIdx] = lastValue.substring(0, lastValue.length - 1); // Trim the padding byte
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/data.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ describe("test_un_vr", () => {
'00041130',
'CS',
new Uint8Array([0x4F, 0x52, 0x49, 0x47, 0x49, 0x4E, 0x41, 0x4C, 0x20, 0x20, 0x5C, 0x20, 0x50, 0x52, 0x49, 0x4D, 0x41, 0x52, 0x59, 0x20]).buffer,
["ORIGINAL ", " PRIMARY "],
["ORIGINAL ", " PRIMARY"],
["ORIGINAL", "PRIMARY"]
],
[
Expand Down
41 changes: 37 additions & 4 deletions test/lossless-read-write.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ describe('lossless-read-write', () => {
expect(deepEqual(expectedDataset, outputDicomDictPass2.dict)).toBeTruthy();
});

test('test DS with multiplicity > 1 with padding byte on last element within VR max length is losslessly read', () => {
const dataset = {
'00200037': {
vr: 'DS',
Value: [0.99924236548978, -0.0322633220972, -0.0217663285287, 0],
_rawValue: ["0.99924236548978", "-0.0322633220972", "-0.0217663285287", " +0.00 "]
}
};

const dicomDict = new DicomDict({});
dicomDict.dict = dataset;

// write and re-read
const outputDicomDict = DicomMessage.readFile(dicomDict.write());

// ensure _rawValue strings have no added trailing spaces and retain original encoding details for + and spaces
const expectedDataset = {
'00200037': {
vr: 'DS',
Value: [0.99924236548978, -0.0322633220972, -0.0217663285287, 0],
_rawValue: ["0.99924236548978", "-0.0322633220972", "-0.0217663285287", " +0.00"]
}
};

expect(outputDicomDict.dict).toEqual(expectedDataset);

// re-write should succeeed
const outputDicomDictPass2 = DicomMessage.readFile(outputDicomDict.write());

// dataset should still be equal
expect(outputDicomDictPass2.dict).toEqual(expectedDataset);
});

test('test IS with multiplicity > 1 and added space for even padding is read and written correctly', () => {
const dataset = {
'00081160': {
Expand All @@ -179,17 +212,17 @@ describe('lossless-read-write', () => {
'00081160': {
vr: 'IS',
Value: [1234, 5678],
_rawValue: ["1234", "5678 "]
_rawValue: ["1234", "5678"]
}
};

expect(deepEqual(expectedDataset, outputDicomDict.dict)).toBeTruthy();
expect(outputDicomDict.dict).toEqual(expectedDataset);

// re-write should succeeed
const outputDicomDictPass2 = DicomMessage.readFile(outputDicomDict.write());

// dataset should still be equal
expect(deepEqual(expectedDataset, outputDicomDictPass2.dict)).toBeTruthy();
expect(outputDicomDictPass2.dict).toEqual(expectedDataset);
});

describe('Multiplicity for non-binary String VRs', () => {
Expand Down Expand Up @@ -311,7 +344,7 @@ describe('lossless-read-write', () => {
},
{
vr: "CS",
_rawValue: ["ORIGINAL ", " PRIMARY "], // spaces non-significant for interpretation but allowed
_rawValue: ["ORIGINAL ", " PRIMARY"], // spaces non-significant for interpretation but allowed
Value: ["ORIGINAL", "PRIMARY"],
},
{
Expand Down

0 comments on commit 2b57f10

Please sign in to comment.