Mercurial > hg > orthanc
comparison Core/DicomParsing/IDicomTranscoder.cpp @ 3945:0b3256c3ee14 transcoding
simplified IDicomTranscoder
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 19 May 2020 11:24:00 +0200 |
parents | aae045f802f4 |
children | 1f33ed7f82e6 |
comparison
equal
deleted
inserted
replaced
3944:aae045f802f4 | 3945:0b3256c3ee14 |
---|---|
42 | 42 |
43 namespace Orthanc | 43 namespace Orthanc |
44 { | 44 { |
45 void IDicomTranscoder::DicomImage::Parse() | 45 void IDicomTranscoder::DicomImage::Parse() |
46 { | 46 { |
47 if (parsed_.get() != NULL || | 47 if (parsed_.get() != NULL) |
48 buffer_.get() == NULL) | 48 { |
49 { | 49 // Already parsed |
50 throw OrthancException(ErrorCode_BadSequenceOfCalls); | 50 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
51 } | 51 } |
52 else | 52 else if (buffer_.get() != NULL) |
53 { | 53 { |
54 parsed_.reset(FromDcmtkBridge::LoadFromMemoryBuffer( | 54 if (isExternalBuffer_) |
55 buffer_->empty() ? NULL : buffer_->c_str(), buffer_->size())); | 55 { |
56 | 56 throw OrthancException(ErrorCode_InternalError); |
57 } | |
58 else | |
59 { | |
60 parsed_.reset(FromDcmtkBridge::LoadFromMemoryBuffer( | |
61 buffer_->empty() ? NULL : buffer_->c_str(), buffer_->size())); | |
62 | |
63 if (parsed_.get() == NULL) | |
64 { | |
65 throw OrthancException(ErrorCode_BadFileFormat); | |
66 } | |
67 } | |
68 } | |
69 else if (isExternalBuffer_) | |
70 { | |
71 parsed_.reset(FromDcmtkBridge::LoadFromMemoryBuffer(externalBuffer_, externalSize_)); | |
72 | |
57 if (parsed_.get() == NULL) | 73 if (parsed_.get() == NULL) |
58 { | 74 { |
59 throw OrthancException(ErrorCode_BadFileFormat); | 75 throw OrthancException(ErrorCode_BadFileFormat); |
60 } | 76 } |
61 } | 77 } |
78 else | |
79 { | |
80 // No buffer is available | |
81 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
82 } | |
62 } | 83 } |
63 | 84 |
64 | 85 |
65 void IDicomTranscoder::DicomImage::Serialize() | 86 void IDicomTranscoder::DicomImage::Serialize() |
66 { | 87 { |
67 if (parsed_.get() == NULL || | 88 if (parsed_.get() == NULL || |
68 buffer_.get() != NULL) | 89 buffer_.get() != NULL || |
90 isExternalBuffer_) | |
69 { | 91 { |
70 throw OrthancException(ErrorCode_BadSequenceOfCalls); | 92 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
71 } | 93 } |
72 else if (parsed_->getDataset() == NULL) | 94 else if (parsed_->getDataset() == NULL) |
73 { | 95 { |
79 FromDcmtkBridge::SaveToMemoryBuffer(*buffer_, *parsed_->getDataset()); | 101 FromDcmtkBridge::SaveToMemoryBuffer(*buffer_, *parsed_->getDataset()); |
80 } | 102 } |
81 } | 103 } |
82 | 104 |
83 | 105 |
106 IDicomTranscoder::DicomImage::DicomImage() : | |
107 isExternalBuffer_(false) | |
108 { | |
109 } | |
110 | |
111 | |
84 void IDicomTranscoder::DicomImage::Clear() | 112 void IDicomTranscoder::DicomImage::Clear() |
85 { | 113 { |
86 parsed_.reset(NULL); | 114 parsed_.reset(NULL); |
87 buffer_.reset(NULL); | 115 buffer_.reset(NULL); |
116 isExternalBuffer_ = false; | |
88 } | 117 } |
89 | 118 |
90 | 119 |
91 void IDicomTranscoder::DicomImage::AcquireParsed(ParsedDicomFile& parsed) | 120 void IDicomTranscoder::DicomImage::AcquireParsed(ParsedDicomFile& parsed) |
92 { | 121 { |
98 { | 127 { |
99 if (parsed == NULL) | 128 if (parsed == NULL) |
100 { | 129 { |
101 throw OrthancException(ErrorCode_NullPointer); | 130 throw OrthancException(ErrorCode_NullPointer); |
102 } | 131 } |
132 else if (parsed->getDataset() == NULL) | |
133 { | |
134 throw OrthancException(ErrorCode_InternalError); | |
135 } | |
103 else if (parsed_.get() != NULL) | 136 else if (parsed_.get() != NULL) |
104 { | 137 { |
105 throw OrthancException(ErrorCode_BadSequenceOfCalls); | 138 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
106 } | 139 } |
107 else if (parsed->getDataset() == NULL) | |
108 { | |
109 throw OrthancException(ErrorCode_InternalError); | |
110 } | |
111 else | 140 else |
112 { | 141 { |
113 parsed_.reset(parsed); | 142 parsed_.reset(parsed); |
114 } | 143 } |
115 } | 144 } |
121 } | 150 } |
122 | 151 |
123 | 152 |
124 void IDicomTranscoder::DicomImage::AcquireBuffer(std::string& buffer /* will be swapped */) | 153 void IDicomTranscoder::DicomImage::AcquireBuffer(std::string& buffer /* will be swapped */) |
125 { | 154 { |
126 if (buffer_.get() != NULL) | 155 if (buffer_.get() != NULL || |
156 isExternalBuffer_) | |
127 { | 157 { |
128 throw OrthancException(ErrorCode_BadSequenceOfCalls); | 158 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
129 } | 159 } |
130 else | 160 else |
131 { | 161 { |
135 } | 165 } |
136 | 166 |
137 | 167 |
138 void IDicomTranscoder::DicomImage::AcquireBuffer(DicomImage& other) | 168 void IDicomTranscoder::DicomImage::AcquireBuffer(DicomImage& other) |
139 { | 169 { |
140 if (buffer_.get() != NULL) | 170 if (buffer_.get() != NULL || |
141 { | 171 isExternalBuffer_) |
142 throw OrthancException(ErrorCode_BadSequenceOfCalls); | 172 { |
143 } | 173 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
144 else if (other.buffer_.get() == NULL) | 174 } |
175 else if (other.isExternalBuffer_) | |
176 { | |
177 assert(other.buffer_.get() == NULL); | |
178 isExternalBuffer_ = true; | |
179 externalBuffer_ = other.externalBuffer_; | |
180 externalSize_ = other.externalSize_; | |
181 } | |
182 else if (other.buffer_.get() != NULL) | |
183 { | |
184 buffer_.reset(other.buffer_.release()); | |
185 } | |
186 else | |
145 { | 187 { |
146 buffer_.reset(NULL); | 188 buffer_.reset(NULL); |
147 } | |
148 else | |
149 { | |
150 buffer_.reset(other.buffer_.release()); | |
151 } | 189 } |
152 } | 190 } |
153 | 191 |
154 | 192 |
193 void IDicomTranscoder::DicomImage::SetExternalBuffer(const void* buffer, | |
194 size_t size) | |
195 { | |
196 if (buffer_.get() != NULL || | |
197 isExternalBuffer_) | |
198 { | |
199 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
200 } | |
201 else | |
202 { | |
203 isExternalBuffer_ = true; | |
204 externalBuffer_ = buffer; | |
205 externalSize_ = size; | |
206 } | |
207 } | |
208 | |
209 | |
210 void IDicomTranscoder::DicomImage::SetExternalBuffer(const std::string& buffer) | |
211 { | |
212 SetExternalBuffer(buffer.empty() ? NULL : buffer.c_str(), buffer.size()); | |
213 } | |
214 | |
215 | |
155 DcmFileFormat& IDicomTranscoder::DicomImage::GetParsed() | 216 DcmFileFormat& IDicomTranscoder::DicomImage::GetParsed() |
156 { | 217 { |
157 if (parsed_.get() != NULL) | 218 if (parsed_.get() != NULL) |
158 { | 219 { |
159 return *parsed_; | 220 return *parsed_; |
160 } | 221 } |
161 else if (buffer_.get() != NULL) | 222 else if (buffer_.get() != NULL || |
223 isExternalBuffer_) | |
162 { | 224 { |
163 Parse(); | 225 Parse(); |
164 return *parsed_; | 226 return *parsed_; |
165 } | 227 } |
166 else | 228 else |
167 { | 229 { |
168 throw OrthancException(ErrorCode_BadSequenceOfCalls, | 230 throw OrthancException( |
169 "AcquireParsed() or AcquireBuffer() should have been called"); | 231 ErrorCode_BadSequenceOfCalls, |
232 "AcquireParsed(), AcquireBuffer() or SetExternalBuffer() should have been called"); | |
170 } | 233 } |
171 } | 234 } |
172 | 235 |
173 | 236 |
174 DcmFileFormat* IDicomTranscoder::DicomImage::ReleaseParsed() | 237 DcmFileFormat* IDicomTranscoder::DicomImage::ReleaseParsed() |
176 if (parsed_.get() != NULL) | 239 if (parsed_.get() != NULL) |
177 { | 240 { |
178 buffer_.reset(NULL); | 241 buffer_.reset(NULL); |
179 return parsed_.release(); | 242 return parsed_.release(); |
180 } | 243 } |
181 else if (buffer_.get() != NULL) | 244 else if (buffer_.get() != NULL || |
245 isExternalBuffer_) | |
182 { | 246 { |
183 Parse(); | 247 Parse(); |
184 buffer_.reset(NULL); | 248 buffer_.reset(NULL); |
185 return parsed_.release(); | 249 return parsed_.release(); |
186 } | 250 } |
187 else | 251 else |
188 { | 252 { |
189 throw OrthancException(ErrorCode_BadSequenceOfCalls, | 253 throw OrthancException( |
190 "AcquireParsed() or AcquireBuffer() should have been called"); | 254 ErrorCode_BadSequenceOfCalls, |
191 } | 255 "AcquireParsed(), AcquireBuffer() or SetExternalBuffer() should have been called"); |
256 } | |
257 } | |
258 | |
259 | |
260 ParsedDicomFile* IDicomTranscoder::DicomImage::ReleaseAsParsedDicomFile() | |
261 { | |
262 return ParsedDicomFile::AcquireDcmtkObject(ReleaseParsed()); | |
192 } | 263 } |
193 | 264 |
194 | 265 |
195 const void* IDicomTranscoder::DicomImage::GetBufferData() | 266 const void* IDicomTranscoder::DicomImage::GetBufferData() |
196 { | 267 { |
197 if (buffer_.get() == NULL) | 268 if (isExternalBuffer_) |
198 { | 269 { |
199 Serialize(); | 270 assert(buffer_.get() == NULL); |
200 } | 271 return externalBuffer_; |
201 | 272 } |
202 assert(buffer_.get() != NULL); | 273 else |
203 return buffer_->empty() ? NULL : buffer_->c_str(); | 274 { |
275 if (buffer_.get() == NULL) | |
276 { | |
277 Serialize(); | |
278 } | |
279 | |
280 assert(buffer_.get() != NULL); | |
281 return buffer_->empty() ? NULL : buffer_->c_str(); | |
282 } | |
204 } | 283 } |
205 | 284 |
206 | 285 |
207 size_t IDicomTranscoder::DicomImage::GetBufferSize() | 286 size_t IDicomTranscoder::DicomImage::GetBufferSize() |
208 { | 287 { |
209 if (buffer_.get() == NULL) | 288 if (isExternalBuffer_) |
210 { | 289 { |
211 Serialize(); | 290 assert(buffer_.get() == NULL); |
212 } | 291 return externalSize_; |
213 | 292 } |
214 assert(buffer_.get() != NULL); | 293 else |
215 return buffer_->size(); | 294 { |
216 } | 295 if (buffer_.get() == NULL) |
217 | 296 { |
218 | 297 Serialize(); |
219 IDicomTranscoder::TranscodedDicom::TranscodedDicom(bool hasSopInstanceUidChanged) : | 298 } |
220 external_(NULL), | 299 |
221 hasSopInstanceUidChanged_(hasSopInstanceUidChanged) | 300 assert(buffer_.get() != NULL); |
222 { | 301 return buffer_->size(); |
223 } | 302 } |
224 | |
225 | |
226 IDicomTranscoder::TranscodedDicom* | |
227 IDicomTranscoder::TranscodedDicom::CreateFromExternal(DcmFileFormat& dicom, | |
228 bool hasSopInstanceUidChanged) | |
229 { | |
230 std::unique_ptr<TranscodedDicom> transcoded(new TranscodedDicom(hasSopInstanceUidChanged)); | |
231 transcoded->external_ = &dicom; | |
232 return transcoded.release(); | |
233 } | |
234 | |
235 | |
236 IDicomTranscoder::TranscodedDicom* | |
237 IDicomTranscoder::TranscodedDicom::CreateFromInternal(DcmFileFormat* dicom, | |
238 bool hasSopInstanceUidChanged) | |
239 { | |
240 if (dicom == NULL) | |
241 { | |
242 throw OrthancException(ErrorCode_NullPointer); | |
243 } | |
244 else | |
245 { | |
246 std::unique_ptr<TranscodedDicom> transcoded(new TranscodedDicom(hasSopInstanceUidChanged)); | |
247 transcoded->internal_.reset(dicom); | |
248 return transcoded.release(); | |
249 } | |
250 } | |
251 | |
252 | |
253 DcmFileFormat& IDicomTranscoder::TranscodedDicom::GetDicom() const | |
254 { | |
255 if (internal_.get() != NULL) | |
256 { | |
257 return *internal_.get(); | |
258 } | |
259 else if (external_ != NULL) | |
260 { | |
261 return *external_; | |
262 } | |
263 else | |
264 { | |
265 // Probably results from a call to "ReleaseDicom()" | |
266 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
267 } | |
268 } | |
269 | |
270 | |
271 DcmFileFormat* IDicomTranscoder::TranscodedDicom::ReleaseDicom() | |
272 { | |
273 if (internal_.get() != NULL) | |
274 { | |
275 return internal_.release(); | |
276 } | |
277 else if (external_ != NULL) | |
278 { | |
279 return new DcmFileFormat(*external_); // Clone | |
280 } | |
281 else | |
282 { | |
283 // Probably results from a call to "ReleaseDicom()" | |
284 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
285 } | |
286 } | 303 } |
287 } | 304 } |