@@ -10,6 +10,7 @@ import { DicomDict } from "./DicomDict.js";
10
10
import { DicomMetaDictionary } from "./DicomMetaDictionary.js" ;
11
11
import { Tag } from "./Tag.js" ;
12
12
import { log } from "./log.js" ;
13
+ import { deepEqual } from "./utilities/deepEqual" ;
13
14
import { ValueRepresentation } from "./ValueRepresentation.js" ;
14
15
15
16
const singleVRs = [ "SQ" , "OF" , "OW" , "OB" , "UN" , "LT" ] ;
@@ -156,6 +157,7 @@ class DicomMessage {
156
157
vr : readInfo . vr . type
157
158
} ) ;
158
159
dict [ cleanTagString ] . Value = readInfo . values ;
160
+ dict [ cleanTagString ] . _rawValue = readInfo . rawValues ;
159
161
160
162
if ( untilTag && untilTag === cleanTagString ) {
161
163
break ;
@@ -193,7 +195,8 @@ class DicomMessage {
193
195
ignoreErrors : false ,
194
196
untilTag : null ,
195
197
includeUntilTagValue : false ,
196
- noCopy : false
198
+ noCopy : false ,
199
+ forceStoreRaw : false
197
200
}
198
201
) {
199
202
var stream = new ReadBufferStream ( buffer , null , {
@@ -251,8 +254,9 @@ class DicomMessage {
251
254
sortedTags . forEach ( function ( tagString ) {
252
255
var tag = Tag . fromString ( tagString ) ,
253
256
tagObject = jsonObjects [ tagString ] ,
254
- vrType = tagObject . vr ,
255
- values = tagObject . Value ;
257
+ vrType = tagObject . vr ;
258
+
259
+ var values = DicomMessage . _getTagWriteValues ( vrType , tagObject ) ;
256
260
257
261
written += tag . write (
258
262
useStream ,
@@ -266,6 +270,23 @@ class DicomMessage {
266
270
return written ;
267
271
}
268
272
273
+ static _getTagWriteValues ( vrType , tagObject ) {
274
+ if ( ! tagObject . _rawValue ) {
275
+ return tagObject . Value ;
276
+ }
277
+
278
+ // apply VR specific formatting to the original _rawValue and compare to the Value
279
+ const vr = ValueRepresentation . createByTypeString ( vrType ) ;
280
+ const originalValue = tagObject . _rawValue . map ( ( val ) => vr . applyFormatting ( val ) )
281
+
282
+ // if Value has not changed, write _rawValue unformatted back into the file
283
+ if ( deepEqual ( tagObject . Value , originalValue ) ) {
284
+ return tagObject . _rawValue ;
285
+ } else {
286
+ return tagObject . Value ;
287
+ }
288
+ }
289
+
269
290
static _readTag (
270
291
stream ,
271
292
syntax ,
@@ -340,25 +361,33 @@ class DicomMessage {
340
361
}
341
362
342
363
var values = [ ] ;
364
+ var rawValues = [ ] ;
343
365
if ( vr . isBinary ( ) && length > vr . maxLength && ! vr . noMultiple ) {
344
366
var times = length / vr . maxLength ,
345
367
i = 0 ;
346
368
while ( i ++ < times ) {
347
- values . push ( vr . read ( stream , vr . maxLength , syntax ) ) ;
369
+ const { rawValue, value } = vr . read ( stream , vr . maxLength , syntax , options ) ;
370
+ rawValues . push ( rawValue ) ;
371
+ values . push ( value ) ;
348
372
}
349
373
} else {
350
- var val = vr . read ( stream , length , syntax ) ;
374
+ const { rawValue , value } = vr . read ( stream , length , syntax , options ) ;
351
375
if ( ! vr . isBinary ( ) && singleVRs . indexOf ( vr . type ) == - 1 ) {
352
- values = val ;
353
- if ( typeof val === "string" ) {
354
- values = val . split ( String . fromCharCode ( VM_DELIMITER ) ) ;
376
+ rawValues = rawValue ;
377
+ values = value
378
+ if ( typeof value === "string" ) {
379
+ rawValues = rawValue . split ( String . fromCharCode ( VM_DELIMITER ) ) ;
380
+ values = value . split ( String . fromCharCode ( VM_DELIMITER ) ) ;
355
381
}
356
382
} else if ( vr . type == "SQ" ) {
357
- values = val ;
383
+ rawValues = rawValue ;
384
+ values = value ;
358
385
} else if ( vr . type == "OW" || vr . type == "OB" ) {
359
- values = val ;
386
+ rawValues = rawValue ;
387
+ values = value ;
360
388
} else {
361
- Array . isArray ( val ) ? ( values = val ) : values . push ( val ) ;
389
+ Array . isArray ( value ) ? ( values = value ) : values . push ( value ) ;
390
+ Array . isArray ( rawValue ) ? ( rawValues = rawValue ) : rawValues . push ( rawValue ) ;
362
391
}
363
392
}
364
393
stream . setEndian ( oldEndian ) ;
@@ -368,6 +397,7 @@ class DicomMessage {
368
397
vr : vr
369
398
} ) ;
370
399
retObj . values = values ;
400
+ retObj . rawValues = rawValues ;
371
401
return retObj ;
372
402
}
373
403
0 commit comments