comparison OrthancFramework/Sources/DicomFormat/DicomMap.cpp @ 4213:be2eca8b02e1

testing DicomMap::ParseDicomMetaInformation()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Sep 2020 15:23:04 +0200
parents fbc49a65340a
children 7b011cfda135
comparison
equal deleted inserted replaced
4212:cb3af14626d4 4213:be2eca8b02e1
491 tags.insert(it->first); 491 tags.insert(it->first);
492 } 492 }
493 } 493 }
494 494
495 495
496 static uint16_t ReadUnsignedInteger16(const char* dicom) 496 static uint16_t ReadLittleEndianUint16(const char* dicom)
497 { 497 {
498 return le16toh(*reinterpret_cast<const uint16_t*>(dicom)); 498 const uint8_t* p = reinterpret_cast<const uint8_t*>(dicom);
499 } 499
500 500 return (static_cast<uint16_t>(p[0]) |
501 501 (static_cast<uint16_t>(p[1]) << 8));
502 static uint32_t ReadUnsignedInteger32(const char* dicom) 502 }
503 { 503
504 return le32toh(*reinterpret_cast<const uint32_t*>(dicom)); 504
505 static uint32_t ReadLittleEndianUint32(const char* dicom)
506 {
507 const uint8_t* p = reinterpret_cast<const uint8_t*>(dicom);
508
509 return (static_cast<uint16_t>(p[0]) |
510 (static_cast<uint16_t>(p[1]) << 8) |
511 (static_cast<uint16_t>(p[2]) << 16) |
512 (static_cast<uint16_t>(p[3]) << 24));
505 } 513 }
506 514
507 515
508 static bool ValidateTag(const ValueRepresentation& vr, 516 static bool ValidateTag(const ValueRepresentation& vr,
509 const std::string& value) 517 const std::string& value)
668 if (position + 6 > size) 676 if (position + 6 > size)
669 { 677 {
670 return false; 678 return false;
671 } 679 }
672 680
673 tag = DicomTag(ReadUnsignedInteger16(dicom + position), 681 tag = DicomTag(ReadLittleEndianUint16(dicom + position),
674 ReadUnsignedInteger16(dicom + position + 2)); 682 ReadLittleEndianUint16(dicom + position + 2));
675 683
676 vr = StringToValueRepresentation(std::string(dicom + position + 4, 2), true); 684 vr = StringToValueRepresentation(std::string(dicom + position + 4, 2), true);
677 if (vr == ValueRepresentation_NotSupported) 685 if (vr == ValueRepresentation_NotSupported)
678 { 686 {
679 return false; 687 return false;
680 } 688 }
681 689
682 if (vr == ValueRepresentation_OtherByte || 690 // http://dicom.nema.org/medical/dicom/current/output/chtml/part05/chapter_7.html#sect_7.1.2
683 vr == ValueRepresentation_OtherDouble || 691 if (vr == ValueRepresentation_ApplicationEntity /* AE */ ||
684 vr == ValueRepresentation_OtherFloat || 692 vr == ValueRepresentation_AgeString /* AS */ ||
685 vr == ValueRepresentation_OtherLong || 693 vr == ValueRepresentation_AttributeTag /* AT */ ||
686 vr == ValueRepresentation_OtherWord || 694 vr == ValueRepresentation_CodeString /* CS */ ||
687 vr == ValueRepresentation_Sequence || 695 vr == ValueRepresentation_Date /* DA */ ||
688 vr == ValueRepresentation_UnlimitedCharacters || 696 vr == ValueRepresentation_DecimalString /* DS */ ||
689 vr == ValueRepresentation_UniversalResource || 697 vr == ValueRepresentation_DateTime /* DT */ ||
690 vr == ValueRepresentation_UnlimitedText || 698 vr == ValueRepresentation_FloatingPointSingle /* FL */ ||
691 vr == ValueRepresentation_Unknown) // Note that "UN" should never appear in the Meta Information 699 vr == ValueRepresentation_FloatingPointDouble /* FD */ ||
692 { 700 vr == ValueRepresentation_IntegerString /* IS */ ||
701 vr == ValueRepresentation_LongString /* LO */ ||
702 vr == ValueRepresentation_LongText /* LT */ ||
703 vr == ValueRepresentation_PersonName /* PN */ ||
704 vr == ValueRepresentation_ShortString /* SH */ ||
705 vr == ValueRepresentation_SignedLong /* SL */ ||
706 vr == ValueRepresentation_SignedShort /* SS */ ||
707 vr == ValueRepresentation_ShortText /* ST */ ||
708 vr == ValueRepresentation_Time /* TM */ ||
709 vr == ValueRepresentation_UniqueIdentifier /* UI */ ||
710 vr == ValueRepresentation_UnsignedLong /* UL */ ||
711 vr == ValueRepresentation_UnsignedShort /* US */)
712 {
713 /**
714 * This is Table 7.1-2. "Data Element with Explicit VR of AE,
715 * AS, AT, CS, DA, DS, DT, FL, FD, IS, LO, LT, PN, SH, SL, SS,
716 * ST, TM, UI, UL and US"
717 **/
718 if (position + 8 > size)
719 {
720 return false;
721 }
722
723 uint16_t length = ReadLittleEndianUint16(dicom + position + 6);
724 if (position + 8 + length > size)
725 {
726 return false;
727 }
728
729 value.assign(dicom + position + 8, length);
730 position += (8 + length);
731 }
732 else
733 {
734 /**
735 * This is Table 7.1-1. "Data Element with Explicit VR other
736 * than as shown in Table 7.1-2"
737 **/
693 if (position + 12 > size) 738 if (position + 12 > size)
694 { 739 {
695 return false; 740 return false;
696 } 741 }
697 742
698 uint32_t length = ReadUnsignedInteger32(dicom + position + 8); 743 uint16_t reserved = ReadLittleEndianUint16(dicom + position + 6);
699 744 if (reserved != 0)
745 {
746 return false;
747 }
748
749 uint32_t length = ReadLittleEndianUint32(dicom + position + 8);
700 if (position + 12 + length > size) 750 if (position + 12 + length > size)
701 { 751 {
702 return false; 752 return false;
703 } 753 }
704 754
705 value.assign(dicom + position + 12, length); 755 value.assign(dicom + position + 12, length);
706 position += (12 + length); 756 position += (12 + length);
707 }
708 else
709 {
710 if (position + 8 > size)
711 {
712 return false;
713 }
714
715 uint16_t length = ReadUnsignedInteger16(dicom + position + 6);
716
717 if (position + 8 + length > size)
718 {
719 return false;
720 }
721
722 value.assign(dicom + position + 8, length);
723 position += (8 + length);
724 } 757 }
725 758
726 if (!ValidateTag(vr, value)) 759 if (!ValidateTag(vr, value))
727 { 760 {
728 return false; 761 return false;
786 value.size() != 4) 819 value.size() != 4)
787 { 820 {
788 return false; 821 return false;
789 } 822 }
790 823
791 size_t stopPosition = position + ReadUnsignedInteger32(value.c_str()); 824 size_t stopPosition = position + ReadLittleEndianUint32(value.c_str());
792 if (stopPosition > size) 825 if (stopPosition > size)
793 { 826 {
794 return false; 827 return false;
795 } 828 }
796 829