comparison OrthancFramework/UnitTestsSources/DicomMapTests.cpp @ 4493:b57ca702a430

DicomStreamReader::LookupPixelDataOffset()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Feb 2021 11:42:25 +0100
parents d9473bd5ed43
children 39192eb9b43d
comparison
equal deleted inserted replaced
4492:0b2484663233 4493:b57ca702a430
804 { 804 {
805 class V : public DicomStreamReader::IVisitor 805 class V : public DicomStreamReader::IVisitor
806 { 806 {
807 private: 807 private:
808 DicomMap map_; 808 DicomMap map_;
809 uint64_t pixelDataOffset_;
809 810
810 public: 811 public:
812 V() :
813 pixelDataOffset_(0)
814 {
815 }
816
811 const DicomMap& GetDicomMap() const 817 const DicomMap& GetDicomMap() const
812 { 818 {
813 return map_; 819 return map_;
814 } 820 }
815 821
826 } 832 }
827 833
828 virtual bool VisitDatasetTag(const DicomTag& tag, 834 virtual bool VisitDatasetTag(const DicomTag& tag,
829 const ValueRepresentation& vr, 835 const ValueRepresentation& vr,
830 const std::string& value, 836 const std::string& value,
831 bool isLittleEndian) ORTHANC_OVERRIDE 837 bool isLittleEndian,
838 uint64_t fileOffset) ORTHANC_OVERRIDE
832 { 839 {
833 if (!isLittleEndian) 840 if (!isLittleEndian)
834 printf("** "); 841 printf("** ");
835 if (tag.GetGroup() < 0x7f00) 842
843 if (tag == DICOM_TAG_PIXEL_DATA)
836 { 844 {
837 std::cout << "Dataset: " << tag.Format() << " " << EnumerationToString(vr) 845 std::cout << "Dataset: " << tag.Format() << " " << EnumerationToString(vr)
838 << " [" << Toolbox::ConvertToAscii(value).c_str() << "] (" << value.size() << ")" << std::endl; 846 << " [PIXEL] (" << value.size() << "), offset: " << std::hex << fileOffset << std::dec << std::endl;
847 pixelDataOffset_ = fileOffset;
848 return false;
839 } 849 }
840 else 850 else
841 { 851 {
842 std::cout << "Dataset: " << tag.Format() << " " << EnumerationToString(vr) 852 std::cout << "Dataset: " << tag.Format() << " " << EnumerationToString(vr)
843 << " [PIXEL] (" << value.size() << ")" << std::endl; 853 << " [" << Toolbox::ConvertToAscii(value).c_str() << "] (" << value.size()
854 << "), offset: " << std::hex << fileOffset << std::dec << std::endl;
844 } 855 }
845 856
846 map_.SetValue(tag, value, Toolbox::IsAsciiString(value)); 857 map_.SetValue(tag, value, Toolbox::IsAsciiString(value));
847 858
848 return true; 859 return true;
849 } 860 }
861
862 uint64_t GetPixelDataOffset() const
863 {
864 return pixelDataOffset_;
865 }
850 }; 866 };
851 } 867 }
852 868
853 869
854 870
855 TEST(DicomStreamReader, DISABLED_Tutu) 871 TEST(DicomStreamReader, DISABLED_Tutu)
856 { 872 {
857 static const std::string PATH = "/home/jodogne/Subversion/orthanc-tests/Database/TransferSyntaxes/"; 873 static const std::string PATH = "/home/jodogne/Subversion/orthanc-tests/Database/TransferSyntaxes/";
858 874
859 std::string dicom; 875 typedef std::list< std::pair<std::string, uint64_t> > Sources;
860 SystemToolbox::ReadFile(dicom, PATH + "../ColorTestMalaterre.dcm", false); 876
861 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.1.dcm", false); 877 Sources sources;
862 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.2.dcm", false); // Big Endian 878 sources.push_back(std::make_pair(PATH + "../ColorTestMalaterre.dcm", 0x03a0u));
863 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.50.dcm", false); 879 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.1.dcm", 0x037c));
864 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.51.dcm", false); 880 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.2.dcm", 0x03e8)); // Big Endian
865 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.57.dcm", false); 881 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.50.dcm", 0x04ac));
866 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.70.dcm", false); 882 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.51.dcm", 0x072c));
867 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.80.dcm", false); 883 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.57.dcm", 0x0620));
868 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.81.dcm", false); 884 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.70.dcm", 0x065a));
869 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.90.dcm", false); 885 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.80.dcm", 0x0b46));
870 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.91.dcm", false); 886 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.81.dcm", 0x073e));
871 //SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.5.dcm", false); 887 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.90.dcm", 0x0b66));
872 888 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.4.91.dcm", 0x19b8));
873 std::stringstream stream; 889 sources.push_back(std::make_pair(PATH + "1.2.840.10008.1.2.5.dcm", 0x0b0a));
874 size_t pos = 0; 890
875 891 {
876 DicomStreamReader r(stream); 892 std::string dicom;
877 V visitor; 893
878 894 uint64_t offset;
879 while (pos < dicom.size() && 895 // Not a DICOM image
880 !r.IsDone()) 896 SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.4.50.png", false);
881 { 897 ASSERT_FALSE(DicomStreamReader::LookupPixelDataOffset(offset, dicom));
882 //printf("."); 898
883 //printf("%d\n", pos); 899 // Image without valid DICOM preamble
900 SystemToolbox::ReadFile(dicom, PATH + "1.2.840.10008.1.2.dcm", false);
901 ASSERT_FALSE(DicomStreamReader::LookupPixelDataOffset(offset, dicom));
902 }
903
904 for (Sources::const_iterator it = sources.begin(); it != sources.end(); ++it)
905 {
906 std::string dicom;
907 SystemToolbox::ReadFile(dicom, it->first, false);
908
909 {
910 uint64_t offset;
911 ASSERT_TRUE(DicomStreamReader::LookupPixelDataOffset(offset, dicom));
912 ASSERT_EQ(it->second, offset);
913 }
914
915 ParsedDicomFile a(dicom);
916 Json::Value aa;
917 a.DatasetToJson(aa, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0);
918
919 std::stringstream stream;
920 size_t pos = 0;
921
922 DicomStreamReader r(stream);
923 V visitor;
924
925 // Test reading byte per byte
926 while (pos < dicom.size() &&
927 !r.IsDone())
928 {
929 r.Consume(visitor);
930 stream.clear();
931 stream.put(dicom[pos++]);
932 }
933
884 r.Consume(visitor); 934 r.Consume(visitor);
885 stream.clear(); 935
886 stream.put(dicom[pos++]); 936 ASSERT_EQ(it->second, visitor.GetPixelDataOffset());
887 } 937
888 938 // Truncate the original DICOM up to pixel data
889 r.Consume(visitor); 939 dicom.resize(visitor.GetPixelDataOffset());
890 940
891 printf(">> %d\n", static_cast<int>(r.GetProcessedBytes())); 941 ParsedDicomFile b(dicom);
942 Json::Value bb;
943 b.DatasetToJson(bb, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0);
944
945 aa.removeMember("7fe0,0010");
946 aa.removeMember("fffc,fffc"); // For "1.2.840.10008.1.2.5.dcm"
947 ASSERT_EQ(aa.toStyledString(), bb.toStyledString());
948 }
892 } 949 }
893 950
894 TEST(DicomStreamReader, DISABLED_Tutu2) 951 TEST(DicomStreamReader, DISABLED_Tutu2)
895 { 952 {
896 //static const std::string PATH = "/home/jodogne/Subversion/orthanc-tests/Database/TransferSyntaxes/"; 953 //static const std::string PATH = "/home/jodogne/Subversion/orthanc-tests/Database/TransferSyntaxes/";