comparison UnitTestsSources/FromDcmtkTests.cpp @ 2202:9b373b7d6713

Fix handling of encodings in C-FIND requests
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 Dec 2016 12:45:06 +0100
parents 5a8840920121
children 6dc3bdb4088b
comparison
equal deleted inserted replaced
2201:307365d0991a 2202:9b373b7d6713
49 #include "../OrthancServer/DicomProtocol/DicomFindAnswers.h" 49 #include "../OrthancServer/DicomProtocol/DicomFindAnswers.h"
50 #include "../OrthancServer/Internals/DicomImageDecoder.h" 50 #include "../OrthancServer/Internals/DicomImageDecoder.h"
51 #include "../Plugins/Engine/PluginsEnumerations.h" 51 #include "../Plugins/Engine/PluginsEnumerations.h"
52 52
53 #include <dcmtk/dcmdata/dcelem.h> 53 #include <dcmtk/dcmdata/dcelem.h>
54 #include <dcmtk/dcmdata/dcdeftag.h>
54 55
55 using namespace Orthanc; 56 using namespace Orthanc;
56 57
57 TEST(DicomFormat, Tag) 58 TEST(DicomFormat, Tag)
58 { 59 {
223 { 224 {
224 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.12.1.1.2 225 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.12.1.1.2
225 Encoding e; 226 Encoding e;
226 227
227 ASSERT_FALSE(GetDicomEncoding(e, "")); 228 ASSERT_FALSE(GetDicomEncoding(e, ""));
228 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 6")); ASSERT_EQ(Encoding_Utf8, e); 229 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 6")); ASSERT_EQ(Encoding_Ascii, e);
229 230
230 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#table_C.12-2 231 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#table_C.12-2
231 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 100")); ASSERT_EQ(Encoding_Latin1, e); 232 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 100")); ASSERT_EQ(Encoding_Latin1, e);
232 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 101")); ASSERT_EQ(Encoding_Latin2, e); 233 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 101")); ASSERT_EQ(Encoding_Latin2, e);
233 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 109")); ASSERT_EQ(Encoding_Latin3, e); 234 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 109")); ASSERT_EQ(Encoding_Latin3, e);
239 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 148")); ASSERT_EQ(Encoding_Latin5, e); 240 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 148")); ASSERT_EQ(Encoding_Latin5, e);
240 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 13")); ASSERT_EQ(Encoding_Japanese, e); 241 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 13")); ASSERT_EQ(Encoding_Japanese, e);
241 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 166")); ASSERT_EQ(Encoding_Thai, e); 242 ASSERT_TRUE(GetDicomEncoding(e, "ISO_IR 166")); ASSERT_EQ(Encoding_Thai, e);
242 243
243 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#table_C.12-3 244 // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#table_C.12-3
244 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 6")); ASSERT_EQ(Encoding_Utf8, e); 245 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 6")); ASSERT_EQ(Encoding_Ascii, e);
245 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 100")); ASSERT_EQ(Encoding_Latin1, e); 246 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 100")); ASSERT_EQ(Encoding_Latin1, e);
246 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 101")); ASSERT_EQ(Encoding_Latin2, e); 247 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 101")); ASSERT_EQ(Encoding_Latin2, e);
247 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 109")); ASSERT_EQ(Encoding_Latin3, e); 248 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 109")); ASSERT_EQ(Encoding_Latin3, e);
248 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 110")); ASSERT_EQ(Encoding_Latin4, e); 249 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 110")); ASSERT_EQ(Encoding_Latin4, e);
249 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 144")); ASSERT_EQ(Encoding_Cyrillic, e); 250 ASSERT_TRUE(GetDicomEncoding(e, "ISO 2022 IR 144")); ASSERT_EQ(Encoding_Cyrillic, e);
1040 const void* b = decoded->GetConstRow(y); 1041 const void* b = decoded->GetConstRow(y);
1041 ASSERT_EQ(0, memcmp(a, b, 512)); 1042 ASSERT_EQ(0, memcmp(a, b, 512));
1042 } 1043 }
1043 } 1044 }
1044 } 1045 }
1046
1047
1048
1049 static void CheckEncoding(const ParsedDicomFile& dicom,
1050 Encoding expected)
1051 {
1052 const char* value = NULL;
1053 ASSERT_TRUE(dicom.GetDcmtkObject().getDataset()->findAndGetString(DCM_SpecificCharacterSet, value).good());
1054
1055 Encoding encoding;
1056 ASSERT_TRUE(GetDicomEncoding(encoding, value));
1057 ASSERT_EQ(expected, encoding);
1058 }
1059
1060
1061 TEST(ParsedDicomFile, DicomMapEncodings1)
1062 {
1063 Configuration::SetDefaultEncoding(Encoding_Ascii);
1064 ASSERT_EQ(Encoding_Ascii, Configuration::GetDefaultEncoding());
1065
1066 {
1067 DicomMap m;
1068 ParsedDicomFile dicom(m);
1069 ASSERT_EQ(1, dicom.GetDcmtkObject().getDataset()->card());
1070 CheckEncoding(dicom, Encoding_Ascii);
1071 }
1072
1073 {
1074 DicomMap m;
1075 ParsedDicomFile dicom(m, Encoding_Latin4);
1076 ASSERT_EQ(1, dicom.GetDcmtkObject().getDataset()->card());
1077 CheckEncoding(dicom, Encoding_Latin4);
1078 }
1079
1080 {
1081 DicomMap m;
1082 m.SetValue(DICOM_TAG_SPECIFIC_CHARACTER_SET, "ISO_IR 148", false);
1083 ParsedDicomFile dicom(m);
1084 ASSERT_EQ(1, dicom.GetDcmtkObject().getDataset()->card());
1085 CheckEncoding(dicom, Encoding_Latin5);
1086 }
1087
1088 {
1089 DicomMap m;
1090 m.SetValue(DICOM_TAG_SPECIFIC_CHARACTER_SET, "ISO_IR 148", false);
1091 ParsedDicomFile dicom(m, Encoding_Latin1);
1092 ASSERT_EQ(1, dicom.GetDcmtkObject().getDataset()->card());
1093 CheckEncoding(dicom, Encoding_Latin5);
1094 }
1095 }
1096
1097
1098 TEST(ParsedDicomFile, DicomMapEncodings2)
1099 {
1100 const char* utf8 = NULL;
1101 for (unsigned int i = 0; i < testEncodingsCount; i++)
1102 {
1103 if (testEncodings[i] == Encoding_Utf8)
1104 {
1105 utf8 = testEncodingsEncoded[i];
1106 break;
1107 }
1108 }
1109
1110 ASSERT_TRUE(utf8 != NULL);
1111
1112 for (unsigned int i = 0; i < testEncodingsCount; i++)
1113 {
1114 // 1251 codepage is not supported by the core DICOM standard, ignore it
1115 if (testEncodings[i] != Encoding_Windows1251)
1116 {
1117 {
1118 // Sanity check to test the proper behavior of "EncodingTests.py"
1119 std::string encoded = Toolbox::ConvertFromUtf8(testEncodingsExpected[i], testEncodings[i]);
1120 ASSERT_STREQ(testEncodingsEncoded[i], encoded.c_str());
1121 std::string decoded = Toolbox::ConvertToUtf8(encoded, testEncodings[i]);
1122 ASSERT_STREQ(testEncodingsExpected[i], decoded.c_str());
1123
1124 if (testEncodings[i] != Encoding_Chinese)
1125 {
1126 // A specific source string is used in "EncodingTests.py" to
1127 // test against Chinese, it is normal that it does not correspond to UTF8
1128
1129 std::string encoded = Toolbox::ConvertToUtf8(Toolbox::ConvertFromUtf8(utf8, testEncodings[i]), testEncodings[i]);
1130 ASSERT_STREQ(testEncodingsExpected[i], encoded.c_str());
1131 }
1132 }
1133
1134
1135 Json::Value v;
1136
1137 {
1138 DicomMap m;
1139 m.SetValue(DICOM_TAG_PATIENT_NAME, testEncodingsExpected[i], false);
1140
1141 ParsedDicomFile dicom(m, testEncodings[i]);
1142
1143 const char* encoded = NULL;
1144 ASSERT_TRUE(dicom.GetDcmtkObject().getDataset()->findAndGetString(DCM_PatientName, encoded).good());
1145 ASSERT_STREQ(testEncodingsEncoded[i], encoded);
1146
1147 dicom.DatasetToJson(v, DicomToJsonFormat_Human, DicomToJsonFlags_Default, 0);
1148
1149 Encoding encoding;
1150 ASSERT_TRUE(GetDicomEncoding(encoding, v["SpecificCharacterSet"].asCString()));
1151 ASSERT_EQ(encoding, testEncodings[i]);
1152 ASSERT_STREQ(testEncodingsExpected[i], v["PatientName"].asCString());
1153 }
1154
1155
1156 {
1157 DicomMap m;
1158 m.SetValue(DICOM_TAG_SPECIFIC_CHARACTER_SET, GetDicomSpecificCharacterSet(testEncodings[i]), false);
1159 m.SetValue(DICOM_TAG_PATIENT_NAME, testEncodingsExpected[i], false);
1160
1161 ParsedDicomFile dicom(m, testEncodings[i]);
1162
1163 Json::Value v2;
1164 dicom.DatasetToJson(v2, DicomToJsonFormat_Human, DicomToJsonFlags_Default, 0);
1165
1166 ASSERT_EQ(v2["PatientName"].asString(), v["PatientName"].asString());
1167 ASSERT_EQ(v2["SpecificCharacterSet"].asString(), v["SpecificCharacterSet"].asString());
1168 }
1169 }
1170 }
1171 }