comparison OrthancFramework/Sources/DicomFormat/DicomStreamReader.cpp @ 4222:7461f98c23a0

remove debug printf from DicomStreamReader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Sep 2020 15:53:17 +0200
parents e4c0218b6b23
children c5ca798b158a
comparison
equal deleted inserted replaced
4221:e4c0218b6b23 4222:7461f98c23a0
138 vr == ValueRepresentation_UnsignedLong /* UL */ || 138 vr == ValueRepresentation_UnsignedLong /* UL */ ||
139 vr == ValueRepresentation_UnsignedShort /* US */); 139 vr == ValueRepresentation_UnsignedShort /* US */);
140 } 140 }
141 141
142 142
143 static void PrintBlock(const std::string& block)
144 {
145 for (size_t i = 0; i < block.size(); i++)
146 {
147 printf("%02x ", static_cast<uint8_t>(block[i]));
148 if (i % 16 == 15)
149 printf("\n");
150 }
151 printf("\n");
152 }
153
154
155
156 bool DicomStreamReader::IsLittleEndian() const 143 bool DicomStreamReader::IsLittleEndian() const
157 { 144 {
158 return (transferSyntax_ != DicomTransferSyntax_BigEndianExplicit); 145 return (transferSyntax_ != DicomTransferSyntax_BigEndianExplicit);
159 } 146 }
160 147
161 148
162 void DicomStreamReader::HandlePreamble(IVisitor& visitor, 149 void DicomStreamReader::HandlePreamble(IVisitor& visitor,
163 const std::string& block) 150 const std::string& block)
164 { 151 {
165 //printf("PREAMBLE:\n");
166 //PrintBlock(block);
167
168 assert(block.size() == 144u); 152 assert(block.size() == 144u);
169 assert(reader_.GetProcessedBytes() == 144u); 153 assert(reader_.GetProcessedBytes() == 144u);
170 154
171 /** 155 /**
172 * The "DICOM file meta information" is always encoded using 156 * The "DICOM file meta information" is always encoded using
193 177
194 178
195 void DicomStreamReader::HandleMetaHeader(IVisitor& visitor, 179 void DicomStreamReader::HandleMetaHeader(IVisitor& visitor,
196 const std::string& block) 180 const std::string& block)
197 { 181 {
198 //printf("META-HEADER:\n");
199 //PrintBlock(block);
200
201 size_t pos = 0; 182 size_t pos = 0;
202 const char* p = block.c_str(); 183 const char* p = block.c_str();
203 184
204 bool hasTransferSyntax = false; 185 bool hasTransferSyntax = false;
205 186
296 277
297 if (tag == DICOM_TAG_SEQUENCE_ITEM || 278 if (tag == DICOM_TAG_SEQUENCE_ITEM ||
298 tag == DICOM_TAG_SEQUENCE_DELIMITATION_ITEM || 279 tag == DICOM_TAG_SEQUENCE_DELIMITATION_ITEM ||
299 tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE) 280 tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE)
300 { 281 {
301 //printf("SEQUENCE TAG:\n");
302 //PrintBlock(block);
303
304 // The special sequence items are encoded like "Implicit VR" 282 // The special sequence items are encoded like "Implicit VR"
305 uint32_t length = ReadUnsignedInteger32(block.c_str() + 4, littleEndian); 283 uint32_t length = ReadUnsignedInteger32(block.c_str() + 4, littleEndian);
306 284
307 if (tag == DICOM_TAG_SEQUENCE_ITEM) 285 if (tag == DICOM_TAG_SEQUENCE_ITEM)
308 { 286 {
309 for (unsigned int i = 0; i <= sequenceDepth_; i++)
310 printf(" ");
311 if (length == 0xffffffffu) 287 if (length == 0xffffffffu)
312 { 288 {
313 // Undefined length: Need to loop over the tags of the nested dataset 289 // Undefined length: Need to loop over the tags of the nested dataset
314 printf("...next dataset in sequence...\n");
315 reader_.Schedule(8); 290 reader_.Schedule(8);
316 state_ = State_DatasetTag; 291 state_ = State_DatasetTag;
317 } 292 }
318 else 293 else
319 { 294 {
320 // Explicit length: Can skip the full sequence at once 295 // Explicit length: Can skip the full sequence at once
321 printf("...next dataset in sequence... %u bytes\n", length);
322 reader_.Schedule(length); 296 reader_.Schedule(length);
323 state_ = State_DatasetValue; 297 state_ = State_DatasetValue;
324 } 298 }
325 } 299 }
326 else if (tag == DICOM_TAG_SEQUENCE_DELIMITATION_ITEM || 300 else if (tag == DICOM_TAG_SEQUENCE_DELIMITATION_ITEM ||
332 throw OrthancException(ErrorCode_BadFileFormat); 306 throw OrthancException(ErrorCode_BadFileFormat);
333 } 307 }
334 308
335 if (tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE) 309 if (tag == DICOM_TAG_SEQUENCE_DELIMITATION_SEQUENCE)
336 { 310 {
337 for (unsigned int i = 0; i < sequenceDepth_; i++)
338 printf(" ");
339 printf("...leaving sequence...\n");
340
341 sequenceDepth_ --; 311 sequenceDepth_ --;
342 }
343 else
344 {
345 if (sequenceDepth_ == 0)
346 {
347 throw OrthancException(ErrorCode_BadFileFormat);
348 }
349 } 312 }
350 313
351 reader_.Schedule(8); 314 reader_.Schedule(8);
352 state_ = State_DatasetTag; 315 state_ = State_DatasetTag;
353 } 316 }
356 throw OrthancException(ErrorCode_InternalError); 319 throw OrthancException(ErrorCode_InternalError);
357 } 320 }
358 } 321 }
359 else 322 else
360 { 323 {
361 //printf("DATASET TAG:\n");
362 //PrintBlock(block);
363
364 previousTag_ = tag;
365
366 ValueRepresentation vr = ValueRepresentation_Unknown; 324 ValueRepresentation vr = ValueRepresentation_Unknown;
367 325
368 if (transferSyntax_ == DicomTransferSyntax_LittleEndianImplicit) 326 if (transferSyntax_ == DicomTransferSyntax_LittleEndianImplicit)
369 { 327 {
370 if (sequenceDepth_ == 0) 328 if (sequenceDepth_ == 0)
381 // This in an explicit transfer syntax 339 // This in an explicit transfer syntax
382 340
383 vr = StringToValueRepresentation( 341 vr = StringToValueRepresentation(
384 std::string(block.c_str() + 4, 2), false /* ignore unknown VR */); 342 std::string(block.c_str() + 4, 2), false /* ignore unknown VR */);
385 343
386 if (vr != ValueRepresentation_Sequence &&
387 sequenceDepth_ > 0)
388 {
389 for (unsigned int i = 0; i <= sequenceDepth_; i++)
390 printf(" ");
391 printf("%s\n", tag.Format().c_str());
392 }
393
394 if (vr == ValueRepresentation_Sequence) 344 if (vr == ValueRepresentation_Sequence)
395 { 345 {
396 for (unsigned int i = 0; i <= sequenceDepth_; i++)
397 printf(" ");
398 printf("...entering sequence... %s\n", tag.Format().c_str());
399 sequenceDepth_ ++; 346 sequenceDepth_ ++;
400 reader_.Schedule(4); 347 reader_.Schedule(4);
401 state_ = State_SequenceExplicitLength; 348 state_ = State_SequenceExplicitLength;
402 } 349 }
403 else if (IsShortExplicitTag(vr)) 350 else if (IsShortExplicitTag(vr))
437 * This is the case of pixel data with compressed transfer 384 * This is the case of pixel data with compressed transfer
438 * syntaxes. Schedule the reading of the first tag of the 385 * syntaxes. Schedule the reading of the first tag of the
439 * nested dataset. 386 * nested dataset.
440 * http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_7.5.html 387 * http://dicom.nema.org/medical/dicom/current/output/chtml/part05/sect_7.5.html
441 **/ 388 **/
442
443 for (unsigned int i = 0; i <= sequenceDepth_; i++)
444 printf(" ");
445 printf("...entering sequence... %s\n", previousTag_.Format().c_str());
446
447 state_ = State_DatasetTag; 389 state_ = State_DatasetTag;
448 reader_.Schedule(8); 390 reader_.Schedule(8);
449 sequenceDepth_ ++; 391 sequenceDepth_ ++;
450 } 392 }
451 else 393 else
456 } 398 }
457 399
458 400
459 void DicomStreamReader::HandleDatasetExplicitLength(const std::string& block) 401 void DicomStreamReader::HandleDatasetExplicitLength(const std::string& block)
460 { 402 {
461 //printf("DATASET TAG LENGTH:\n");
462 //PrintBlock(block);
463
464 assert(block.size() == 4); 403 assert(block.size() == 4);
465 404
466 uint32_t length = ReadUnsignedInteger32(block.c_str(), IsLittleEndian()); 405 uint32_t length = ReadUnsignedInteger32(block.c_str(), IsLittleEndian());
467 HandleDatasetExplicitLength(length); 406 HandleDatasetExplicitLength(length);
468 } 407 }
469 408
470 409
471 void DicomStreamReader::HandleSequenceExplicitLength(const std::string& block) 410 void DicomStreamReader::HandleSequenceExplicitLength(const std::string& block)
472 { 411 {
473 //printf("DATASET TAG LENGTH:\n");
474 //PrintBlock(block);
475
476 assert(block.size() == 4); 412 assert(block.size() == 4);
477 413
478 uint32_t length = ReadUnsignedInteger32(block.c_str(), IsLittleEndian()); 414 uint32_t length = ReadUnsignedInteger32(block.c_str(), IsLittleEndian());
479 if (length == 0xffffffffu) 415 if (length == 0xffffffffu)
480 { 416 {
481 state_ = State_DatasetTag; 417 state_ = State_DatasetTag;
482 reader_.Schedule(8); 418 reader_.Schedule(8);
483 } 419 }
484 else 420 else
485 { 421 {
486 for (unsigned int i = 0; i <= sequenceDepth_; i++)
487 printf(" ");
488 printf("...skipping sequence thanks to explicit length... %d\n", length);
489
490 reader_.Schedule(length); 422 reader_.Schedule(length);
491 state_ = State_SequenceExplicitValue; 423 state_ = State_SequenceExplicitValue;
492 } 424 }
493 } 425 }
494 426
538 470
539 DicomStreamReader::DicomStreamReader(std::istream& stream) : 471 DicomStreamReader::DicomStreamReader(std::istream& stream) :
540 reader_(stream), 472 reader_(stream),
541 state_(State_Preamble), 473 state_(State_Preamble),
542 transferSyntax_(DicomTransferSyntax_LittleEndianImplicit), // Dummy 474 transferSyntax_(DicomTransferSyntax_LittleEndianImplicit), // Dummy
543 previousTag_(0x0000, 0x0000), // Dummy
544 danglingTag_(0x0000, 0x0000), // Dummy 475 danglingTag_(0x0000, 0x0000), // Dummy
545 danglingVR_(ValueRepresentation_Unknown), // Dummy 476 danglingVR_(ValueRepresentation_Unknown), // Dummy
546 sequenceDepth_(0) 477 sequenceDepth_(0)
547 { 478 {
548 reader_.Schedule(128 /* empty header */ + 479 reader_.Schedule(128 /* empty header */ +