/
DicomParts.scala
106 lines (78 loc) · 4.08 KB
/
DicomParts.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright 2017 Lars Edenbrandt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package se.nimsa.dcm4che.streams
import akka.util.ByteString
import org.dcm4che3.data.{SpecificCharacterSet, VR}
object DicomParts {
trait DicomPart {
def bigEndian: Boolean
def bytes: ByteString
}
case class DicomPreamble(bytes: ByteString) extends DicomPart {
def bigEndian = false
}
case class DicomHeader(tag: Int, vr: VR, length: Int, isFmi: Boolean, bigEndian: Boolean, explicitVR: Boolean, bytes: ByteString) extends DicomPart {
def withUpdatedLength(newLength: Int) : DicomHeader = {
val updated = if ((bytes.size >= 8) && (explicitVR) && (vr.headerLength == 8)) { //explicit vr
bytes.take(6) ++ DicomParsing.shortToBytes(newLength.toShort, bigEndian)
} else if ((bytes.size >= 12) && (explicitVR) && (vr.headerLength == 12)) { //explicit vr
bytes.take(8) ++ DicomParsing.intToBytes(newLength, bigEndian)
} else { //implicit vr
bytes.take(4) ++ DicomParsing.intToBytes(newLength, bigEndian)
}
DicomHeader(tag, vr, newLength, isFmi, bigEndian, explicitVR, updated)
}
}
case class DicomValueChunk(bigEndian: Boolean, bytes: ByteString, last: Boolean) extends DicomPart
case class DicomDeflatedChunk(bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomItem(length: Int, bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomItemDelimitation(bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomSequence(tag: Int, bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomSequenceDelimitation(bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomFragments(tag: Int, vr: VR, bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomFragmentsDelimitation(bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomUnknownPart(bigEndian: Boolean, bytes: ByteString) extends DicomPart
case class DicomFragment(bigEndian: Boolean, valueChunks: Seq[DicomValueChunk]) extends DicomPart {
def bytes = valueChunks.map(_.bytes).fold(ByteString.empty)(_ ++ _)
}
case class DicomAttribute(header: DicomHeader, valueChunks: Seq[DicomValueChunk]) extends DicomPart {
def bytes = valueChunks.map(_.bytes).fold(ByteString.empty)(_ ++ _)
def bigEndian = header.bigEndian
// LO: long string 64 chars max
// SH: short string 16 chars max
// PN: person name
// UI: 64 chars max
def withUpdatedStringValue(newValue: String, cs: SpecificCharacterSet = SpecificCharacterSet.ASCII): DicomAttribute = {
val newBytes = header.vr.toBytes(newValue, cs)
val needsPadding = newBytes.size % 2 == 1
val newLength = if (needsPadding) newBytes.size + 1 else newBytes.size
val updatedHeader = header.withUpdatedLength(newLength.toShort)
val updatedValue = if (needsPadding) {
ByteString.fromArray(newBytes :+ header.vr.paddingByte().toByte)
} else {
ByteString.fromArray(newBytes)
}
DicomAttribute(updatedHeader, Seq(DicomValueChunk(header.bigEndian, updatedValue, true)))
}
// DA: A string of characters of the format YYYYMMDD, 8 bytes fixed
def withUpdatedDateValue(newValue: String, cs: SpecificCharacterSet = SpecificCharacterSet.ASCII): DicomAttribute = {
val newBytes = header.vr.toBytes(newValue, cs)
val updatedValue = ByteString.fromArray(newBytes)
DicomAttribute(header, Seq(DicomValueChunk(header.bigEndian, updatedValue, true)))
}
def asDicomParts: Seq[DicomPart] = header +: valueChunks
}
}