comparison UnitTestsSources/DicomMapTests.cpp @ 3194:47ef29168698

support of value multiplicity in ITagVisitor
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 05 Feb 2019 18:34:27 +0100
parents c6cfd502bf79
children 880e4161c312
comparison
equal deleted inserted replaced
3193:c6cfd502bf79 3194:47ef29168698
555 555
556 556
557 557
558 #if 0 558 #if 0
559 559
560 #include <boost/math/special_functions/round.hpp>
561
560 namespace Orthanc 562 namespace Orthanc
561 { 563 {
562 class DicomJsonVisitor : public ITagVisitor 564 class DicomJsonVisitor : public ITagVisitor
563 { 565 {
564 private: 566 private:
634 else 636 else
635 { 637 {
636 (*node) [t] = Json::objectValue; 638 (*node) [t] = Json::objectValue;
637 return (*node) [t]; 639 return (*node) [t];
638 } 640 }
639 } 641 }
642
643 static Json::Value FormatInteger(int64_t value)
644 {
645 if (value < 0)
646 {
647 return Json::Value(static_cast<int32_t>(value));
648 }
649 else
650 {
651 return Json::Value(static_cast<uint32_t>(value));
652 }
653 }
654
655 static Json::Value FormatDouble(double value)
656 {
657 long long a = boost::math::llround<double>(value);
658
659 double d = fabs(value - static_cast<double>(a));
660
661 if (d <= std::numeric_limits<double>::epsilon() * 100.0)
662 {
663 return FormatInteger(a);
664 }
665 else
666 {
667 return Json::Value(value);
668 }
669 }
640 670
641 public: 671 public:
642 DicomJsonVisitor() 672 DicomJsonVisitor()
643 { 673 {
644 Clear(); 674 Clear();
662 virtual void VisitUnknown(const std::vector<DicomTag>& parentTags, 692 virtual void VisitUnknown(const std::vector<DicomTag>& parentTags,
663 const std::vector<size_t>& parentIndexes, 693 const std::vector<size_t>& parentIndexes,
664 const DicomTag& tag, 694 const DicomTag& tag,
665 ValueRepresentation vr) ORTHANC_OVERRIDE 695 ValueRepresentation vr) ORTHANC_OVERRIDE
666 { 696 {
697 Json::Value& node = CreateNode(parentTags, parentIndexes, tag);
698 node["vr"] = EnumerationToString(vr);
667 } 699 }
668 700
669 virtual void VisitBinary(const std::vector<DicomTag>& parentTags, 701 virtual void VisitBinary(const std::vector<DicomTag>& parentTags,
670 const std::vector<size_t>& parentIndexes, 702 const std::vector<size_t>& parentIndexes,
671 const DicomTag& tag, 703 const DicomTag& tag,
679 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); 711 Json::Value& node = CreateNode(parentTags, parentIndexes, tag);
680 node["vr"] = EnumerationToString(vr); 712 node["vr"] = EnumerationToString(vr);
681 } 713 }
682 } 714 }
683 715
684 virtual void VisitInteger(const std::vector<DicomTag>& parentTags, 716 virtual void VisitIntegers(const std::vector<DicomTag>& parentTags,
717 const std::vector<size_t>& parentIndexes,
718 const DicomTag& tag,
719 ValueRepresentation vr,
720 const std::vector<int64_t>& values) ORTHANC_OVERRIDE
721 {
722 if (vr != ValueRepresentation_NotSupported)
723 {
724 Json::Value& node = CreateNode(parentTags, parentIndexes, tag);
725 node["vr"] = EnumerationToString(vr);
726
727 Json::Value content = Json::arrayValue;
728 for (size_t i = 0; i < values.size(); i++)
729 {
730 content.append(FormatInteger(values[i]));
731 }
732
733 node["Value"] = content;
734 }
735 }
736
737 virtual void VisitDoubles(const std::vector<DicomTag>& parentTags,
685 const std::vector<size_t>& parentIndexes, 738 const std::vector<size_t>& parentIndexes,
686 const DicomTag& tag, 739 const DicomTag& tag,
687 ValueRepresentation vr, 740 ValueRepresentation vr,
688 int64_t value) ORTHANC_OVERRIDE 741 const std::vector<double>& values) ORTHANC_OVERRIDE
689 { 742 {
690 if (vr != ValueRepresentation_NotSupported) 743 if (vr != ValueRepresentation_NotSupported)
691 { 744 {
692 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); 745 Json::Value& node = CreateNode(parentTags, parentIndexes, tag);
693 node["vr"] = EnumerationToString(vr); 746 node["vr"] = EnumerationToString(vr);
694 747
695 Json::Value content = Json::arrayValue; 748 Json::Value content = Json::arrayValue;
696 if (value < 0) 749 for (size_t i = 0; i < values.size(); i++)
697 { 750 {
698 content.append(static_cast<int32_t>(value)); 751 content.append(FormatDouble(values[i]));
699 }
700 else
701 {
702 content.append(static_cast<uint32_t>(value));
703 } 752 }
704 node["Value"] = content; 753 node["Value"] = content;
705 } 754 }
706 } 755 }
707 756
708 virtual void VisitDouble(const std::vector<DicomTag>& parentTags, 757 virtual void VisitAttributes(const std::vector<DicomTag>& parentTags,
709 const std::vector<size_t>& parentIndexes, 758 const std::vector<size_t>& parentIndexes,
710 const DicomTag& tag, 759 const DicomTag& tag,
711 ValueRepresentation vr, 760 ValueRepresentation vr,
712 double value) ORTHANC_OVERRIDE 761 const std::vector<DicomTag>& values) ORTHANC_OVERRIDE
713 { 762 {
714 if (vr != ValueRepresentation_NotSupported) 763 if (vr != ValueRepresentation_NotSupported)
715 { 764 {
716 Json::Value& node = CreateNode(parentTags, parentIndexes, tag); 765 Json::Value& node = CreateNode(parentTags, parentIndexes, tag);
717 node["vr"] = EnumerationToString(vr); 766 node["vr"] = EnumerationToString(vr);
718 767
719 Json::Value content = Json::arrayValue; 768
720 content.append(value);
721 node["Value"] = content;
722 }
723 }
724
725 virtual void VisitAttribute(const std::vector<DicomTag>& parentTags,
726 const std::vector<size_t>& parentIndexes,
727 const DicomTag& tag,
728 ValueRepresentation vr,
729 const DicomTag& value) ORTHANC_OVERRIDE
730 {
731 if (vr != ValueRepresentation_NotSupported)
732 {
733 Json::Value& node = CreateNode(parentTags, parentIndexes, tag);
734 node["vr"] = EnumerationToString(vr);
735 } 769 }
736 } 770 }
737 771
738 virtual Action VisitString(std::string& newValue, 772 virtual Action VisitString(std::string& newValue,
739 const std::vector<DicomTag>& parentTags, 773 const std::vector<DicomTag>& parentTags,
766 } 800 }
767 801
768 case ValueRepresentation_IntegerString: 802 case ValueRepresentation_IntegerString:
769 { 803 {
770 int64_t value = boost::lexical_cast<int64_t>(tokens[i]); 804 int64_t value = boost::lexical_cast<int64_t>(tokens[i]);
771 if (value < 0) 805 node["Value"].append(FormatInteger(value));
772 {
773 node["Value"].append(static_cast<int32_t>(value));
774 }
775 else
776 {
777 node["Value"].append(static_cast<uint32_t>(value));
778 }
779 break; 806 break;
780 } 807 }
781 808
782 case ValueRepresentation_DecimalString: 809 case ValueRepresentation_DecimalString:
783 { 810 {
784 double value = boost::lexical_cast<double>(tokens[i]); 811 double value = boost::lexical_cast<double>(tokens[i]);
785 node["Value"].append(value); 812 node["Value"].append(FormatDouble(value));
786 break; 813 break;
787 } 814 }
788 815
789 default: 816 default:
817 {
818 size_t l = tokens[i].size();
819
820 if (l > 0 &&
821 tokens[i][l - 1] == '\0')
822 {
823 tokens[i] = tokens[i].substr(0, l - 1);
824 }
825
790 node["Value"].append(tokens[i]); 826 node["Value"].append(tokens[i]);
791 break; 827 break;
828 }
792 } 829 }
793 } 830 }
794 catch (boost::bad_lexical_cast&) 831 catch (boost::bad_lexical_cast&)
795 { 832 {
796 throw OrthancException(ErrorCode_BadFileFormat); 833 throw OrthancException(ErrorCode_BadFileFormat);
806 #include "../Core/SystemToolbox.h" 843 #include "../Core/SystemToolbox.h"
807 844
808 845
809 /* 846 /*
810 847
811 cd /home/jodogne/Subversion/orthanc/s/dcmtk-3.6.2/i/bin 848 cat << EOF > /tmp/tutu.py
812 DCMDICTPATH=/home/jodogne/Downloads/dcmtk-3.6.2/dcmdata/data/dicom.dic ./dcm2json ~/Subversion/orthanc-tests/Database/DummyCT.dcm | python -mjson.tool > /tmp/a.json 849 import json
813 850 import sys
814 make -j4 && ./UnitTests --gtest_filter=DicomWeb* && python -mjson.tool /tmp/tutu.json > /tmp/b.json 851 j = json.loads(sys.stdin.read().decode("utf-8-sig"))
815 diff /tmp/a.json /tmp/b.json 852 print(json.dumps(j, indent=4, sort_keys=True, ensure_ascii=False).encode('utf-8'))
853 EOF
854
855 DCMDICTPATH=/home/jodogne/Downloads/dcmtk-3.6.2/dcmdata/data/dicom.dic /home/jodogne/Downloads/dcmtk-3.6.2/i/bin/dcm2json ~/Subversion/orthanc-tests/Database/DummyCT.dcm | tr -d '\0' | sed 's/\\u0000//g' | sed 's/\.0$//' | python /tmp/tutu.py | grep -v 'InlineBinary' > /tmp/a.json
856
857 make -j4 && ./UnitTests --gtest_filter=DicomWeb* && python /tmp/tutu.py < /tmp/tutu.json > /tmp/b.json && diff -i /tmp/a.json /tmp/b.json
816 858
817 */ 859 */
818 860
819 TEST(DicomWebJson, Basic) 861 TEST(DicomWebJson, Basic)
820 { 862 {