Mercurial > hg > orthanc
comparison OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp @ 4682:d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 07 Jun 2021 18:35:46 +0200 |
parents | c5528c7847a6 |
children | 7182f5732480 |
comparison
equal
deleted
inserted
replaced
4681:c5528c7847a6 | 4682:d38a7040474a |
---|---|
2263 // Those are samples inspired by those from "man dcmodify" | 2263 // Those are samples inspired by those from "man dcmodify" |
2264 | 2264 |
2265 static const DicomTag DICOM_TAG_ACQUISITION_MATRIX(0x0018, 0x1310); | 2265 static const DicomTag DICOM_TAG_ACQUISITION_MATRIX(0x0018, 0x1310); |
2266 static const DicomTag DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE(0x0008, 0x1111); | 2266 static const DicomTag DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE(0x0008, 0x1111); |
2267 | 2267 |
2268 DicomPath path = DicomPath::Parse("(0010,0010)", true); | 2268 DicomPath path = DicomPath::Parse("(0010,0010)"); |
2269 ASSERT_FALSE(path.HasUniversal()); | |
2269 ASSERT_EQ(0u, path.GetPrefixLength()); | 2270 ASSERT_EQ(0u, path.GetPrefixLength()); |
2270 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2271 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2271 ASSERT_THROW(path.GetPrefixTag(0), OrthancException); | 2272 ASSERT_THROW(path.GetPrefixTag(0), OrthancException); |
2272 | 2273 |
2273 path = DicomPath::Parse("0018,1310", true); | 2274 path = DicomPath::Parse("0018,1310"); |
2275 ASSERT_FALSE(path.HasUniversal()); | |
2274 ASSERT_EQ(0u, path.GetPrefixLength()); | 2276 ASSERT_EQ(0u, path.GetPrefixLength()); |
2275 ASSERT_EQ(DICOM_TAG_ACQUISITION_MATRIX, path.GetFinalTag()); | 2277 ASSERT_EQ(DICOM_TAG_ACQUISITION_MATRIX, path.GetFinalTag()); |
2276 ASSERT_EQ("(0018,1310)", path.Format()); | 2278 ASSERT_EQ("(0018,1310)", path.Format()); |
2277 | 2279 |
2278 // The following sample won't work without DCMTK | 2280 // The following sample won't work without DCMTK |
2279 path = DicomPath::Parse("PatientID", true); | 2281 path = DicomPath::Parse("PatientID"); |
2282 ASSERT_FALSE(path.HasUniversal()); | |
2280 ASSERT_EQ(0u, path.GetPrefixLength()); | 2283 ASSERT_EQ(0u, path.GetPrefixLength()); |
2281 ASSERT_EQ(DICOM_TAG_PATIENT_ID, path.GetFinalTag()); | 2284 ASSERT_EQ(DICOM_TAG_PATIENT_ID, path.GetFinalTag()); |
2282 ASSERT_EQ("(0010,0020)", path.Format()); | 2285 ASSERT_EQ("(0010,0020)", path.Format()); |
2283 | 2286 |
2284 path = DicomPath::Parse("(0018,1310)", true); | 2287 path = DicomPath::Parse("(0018,1310)"); |
2288 ASSERT_FALSE(path.HasUniversal()); | |
2285 ASSERT_EQ(0u, path.GetPrefixLength()); | 2289 ASSERT_EQ(0u, path.GetPrefixLength()); |
2286 ASSERT_EQ(DICOM_TAG_ACQUISITION_MATRIX, path.GetFinalTag()); | 2290 ASSERT_EQ(DICOM_TAG_ACQUISITION_MATRIX, path.GetFinalTag()); |
2287 ASSERT_EQ("(0018,1310)", path.Format()); | 2291 ASSERT_EQ("(0018,1310)", path.Format()); |
2288 | 2292 |
2289 path = DicomPath::Parse("(0008,1111)[0].PatientName", true); | 2293 path = DicomPath::Parse("(0008,1111)[0].PatientName"); |
2294 ASSERT_FALSE(path.HasUniversal()); | |
2290 ASSERT_EQ(1u, path.GetPrefixLength()); | 2295 ASSERT_EQ(1u, path.GetPrefixLength()); |
2291 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); | 2296 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); |
2292 ASSERT_FALSE(path.IsPrefixUniversal(0)); | 2297 ASSERT_FALSE(path.IsPrefixUniversal(0)); |
2293 ASSERT_EQ(0, path.GetPrefixIndex(0)); | 2298 ASSERT_EQ(0, path.GetPrefixIndex(0)); |
2294 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); | 2299 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); |
2295 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2300 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2296 | 2301 |
2297 path = DicomPath::Parse("(0008,1111)[1].(0008,1111)[2].(0010,0010)", true); | 2302 path = DicomPath::Parse("(0008,1111)[1].(0008,1111)[2].(0010,0010)"); |
2303 ASSERT_FALSE(path.HasUniversal()); | |
2298 ASSERT_EQ(2u, path.GetPrefixLength()); | 2304 ASSERT_EQ(2u, path.GetPrefixLength()); |
2299 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); | 2305 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); |
2300 ASSERT_FALSE(path.IsPrefixUniversal(0)); | 2306 ASSERT_FALSE(path.IsPrefixUniversal(0)); |
2301 ASSERT_EQ(1, path.GetPrefixIndex(0)); | 2307 ASSERT_EQ(1, path.GetPrefixIndex(0)); |
2302 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(1)); | 2308 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(1)); |
2303 ASSERT_FALSE(path.IsPrefixUniversal(1)); | 2309 ASSERT_FALSE(path.IsPrefixUniversal(1)); |
2304 ASSERT_EQ(2, path.GetPrefixIndex(1)); | 2310 ASSERT_EQ(2, path.GetPrefixIndex(1)); |
2305 ASSERT_THROW(path.GetPrefixTag(2), OrthancException); | 2311 ASSERT_THROW(path.GetPrefixTag(2), OrthancException); |
2306 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2312 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2307 | 2313 |
2308 path = DicomPath::Parse("(0008,1111)[*].PatientName", true); | 2314 path = DicomPath::Parse("(0008,1111)[*].PatientName"); |
2315 ASSERT_TRUE(path.HasUniversal()); | |
2309 ASSERT_EQ(1u, path.GetPrefixLength()); | 2316 ASSERT_EQ(1u, path.GetPrefixLength()); |
2310 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); | 2317 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); |
2311 ASSERT_TRUE(path.IsPrefixUniversal(0)); | 2318 ASSERT_TRUE(path.IsPrefixUniversal(0)); |
2312 ASSERT_THROW(path.GetPrefixIndex(0), OrthancException); | 2319 ASSERT_THROW(path.GetPrefixIndex(0), OrthancException); |
2313 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); | 2320 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); |
2314 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2321 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2315 ASSERT_EQ("(0008,1111)[*].(0010,0010)", path.Format()); | 2322 ASSERT_EQ("(0008,1111)[*].(0010,0010)", path.Format()); |
2316 | 2323 |
2317 ASSERT_THROW(DicomPath::Parse("(0008,1111)[*].PatientName", false), OrthancException); | 2324 path = DicomPath::Parse("(0008,1111)[1].(0008,1111)[*].(0010,0010)"); |
2318 | 2325 ASSERT_TRUE(path.HasUniversal()); |
2319 path = DicomPath::Parse("(0008,1111)[1].(0008,1111)[*].(0010,0010)", true); | |
2320 ASSERT_EQ(2u, path.GetPrefixLength()); | 2326 ASSERT_EQ(2u, path.GetPrefixLength()); |
2321 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); | 2327 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); |
2322 ASSERT_FALSE(path.IsPrefixUniversal(0)); | 2328 ASSERT_FALSE(path.IsPrefixUniversal(0)); |
2323 ASSERT_EQ(1, path.GetPrefixIndex(0)); | 2329 ASSERT_EQ(1, path.GetPrefixIndex(0)); |
2324 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); | 2330 ASSERT_EQ(DICOM_TAG_REFERENCED_PERFORMED_PROCEDURE_STEP_SEQUENCE, path.GetPrefixTag(0)); |
2325 ASSERT_TRUE(path.IsPrefixUniversal(1)); | 2331 ASSERT_TRUE(path.IsPrefixUniversal(1)); |
2326 ASSERT_THROW(path.GetPrefixIndex(1), OrthancException); | 2332 ASSERT_THROW(path.GetPrefixIndex(1), OrthancException); |
2327 ASSERT_THROW(path.GetPrefixTag(2), OrthancException); | 2333 ASSERT_THROW(path.GetPrefixTag(2), OrthancException); |
2328 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2334 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2329 | 2335 |
2330 path = DicomPath::Parse("PatientID[1].PatientName", true); | 2336 path = DicomPath::Parse("PatientID[1].PatientName"); |
2337 ASSERT_FALSE(path.HasUniversal()); | |
2331 ASSERT_EQ(1u, path.GetPrefixLength()); | 2338 ASSERT_EQ(1u, path.GetPrefixLength()); |
2332 ASSERT_EQ(DICOM_TAG_PATIENT_ID, path.GetPrefixTag(0)); | 2339 ASSERT_EQ(DICOM_TAG_PATIENT_ID, path.GetPrefixTag(0)); |
2333 ASSERT_FALSE(path.IsPrefixUniversal(0)); | 2340 ASSERT_FALSE(path.IsPrefixUniversal(0)); |
2334 ASSERT_EQ(1, path.GetPrefixIndex(0)); | 2341 ASSERT_EQ(1, path.GetPrefixIndex(0)); |
2335 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); | 2342 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); |
2336 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2343 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2337 | 2344 |
2338 path = DicomPath::Parse(" PatientID [ 42 ] . PatientName ", true); | 2345 path = DicomPath::Parse(" PatientID [ 42 ] . PatientName "); |
2346 ASSERT_FALSE(path.HasUniversal()); | |
2339 ASSERT_EQ(1u, path.GetPrefixLength()); | 2347 ASSERT_EQ(1u, path.GetPrefixLength()); |
2340 ASSERT_EQ(DICOM_TAG_PATIENT_ID, path.GetPrefixTag(0)); | 2348 ASSERT_EQ(DICOM_TAG_PATIENT_ID, path.GetPrefixTag(0)); |
2341 ASSERT_FALSE(path.IsPrefixUniversal(0)); | 2349 ASSERT_FALSE(path.IsPrefixUniversal(0)); |
2342 ASSERT_EQ(42, path.GetPrefixIndex(0)); | 2350 ASSERT_EQ(42, path.GetPrefixIndex(0)); |
2343 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); | 2351 ASSERT_THROW(path.GetPrefixTag(1), OrthancException); |
2344 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); | 2352 ASSERT_EQ(DICOM_TAG_PATIENT_NAME, path.GetFinalTag()); |
2345 ASSERT_EQ("(0010,0020)[42].(0010,0010)", path.Format()); | 2353 ASSERT_EQ("(0010,0020)[42].(0010,0010)", path.Format()); |
2346 | 2354 |
2347 ASSERT_THROW(DicomPath::Parse("nope", true), OrthancException); | 2355 ASSERT_THROW(DicomPath::Parse("nope"), OrthancException); |
2348 ASSERT_THROW(DicomPath::Parse("(0010,0010)[.PatientID", true), OrthancException); | 2356 ASSERT_THROW(DicomPath::Parse("(0010,0010)[.PatientID"), OrthancException); |
2349 ASSERT_THROW(DicomPath::Parse("(0010,0010)[].PatientID", true), OrthancException); | 2357 ASSERT_THROW(DicomPath::Parse("(0010,0010)[].PatientID"), OrthancException); |
2350 ASSERT_THROW(DicomPath::Parse("(0010,0010[].PatientID", true), OrthancException); | 2358 ASSERT_THROW(DicomPath::Parse("(0010,0010[].PatientID"), OrthancException); |
2351 ASSERT_THROW(DicomPath::Parse("(0010,0010)0].PatientID", true), OrthancException); | 2359 ASSERT_THROW(DicomPath::Parse("(0010,0010)0].PatientID"), OrthancException); |
2352 ASSERT_THROW(DicomPath::Parse("(0010,0010)[-1].PatientID", true), OrthancException); | 2360 ASSERT_THROW(DicomPath::Parse("(0010,0010)[-1].PatientID"), OrthancException); |
2361 } | |
2362 | |
2363 | |
2364 | |
2365 TEST(ParsedDicomFile, RemovePath) | |
2366 { | |
2367 { | |
2368 Json::Value v = Json::arrayValue; | |
2369 | |
2370 Json::Value item = Json::objectValue; | |
2371 item["PatientID"] = "HELLO"; | |
2372 v.append(item); | |
2373 | |
2374 std::unique_ptr<DcmElement> d(FromDcmtkBridge::FromJson(DICOM_TAG_SOURCE_IMAGE_SEQUENCE, | |
2375 v, false, Encoding_Latin1, "")); | |
2376 d->writeXML(std::cout); | |
2377 } | |
2378 | |
2379 { | |
2380 Json::Value v = "Hello"; | |
2381 std::unique_ptr<DcmElement> d(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_ID, | |
2382 v, false, Encoding_Latin1, "")); | |
2383 d->writeXML(std::cout); | |
2384 } | |
2385 | |
2386 printf("\n"); | |
2387 | |
2388 { | |
2389 Json::Value v = Json::objectValue; | |
2390 v["PatientID"] = "Hello"; | |
2391 | |
2392 { | |
2393 Json::Value a = Json::arrayValue; | |
2394 | |
2395 { | |
2396 Json::Value item = Json::objectValue; | |
2397 item["ReferencedSOPClassUID"] = "1.2.840.10008.5.1.4.1.1.4"; | |
2398 item["ReferencedSOPInstanceUID"] = "1.2.840.113619.2.176.2025.1499492.7040.1171286241.719"; | |
2399 a.append(item); | |
2400 } | |
2401 | |
2402 { | |
2403 Json::Value item = Json::objectValue; | |
2404 item["ReferencedSOPClassUID"] = "1.2.840.10008.5.1.4.1.1.4"; | |
2405 item["ReferencedSOPInstanceUID"] = "1.2.840.113619.2.176.2025.1499492.7040.1171286241.726"; | |
2406 a.append(item); | |
2407 } | |
2408 | |
2409 v["ReferencedImageSequence"] = a; | |
2410 } | |
2411 | |
2412 { | |
2413 Json::Value a = Json::arrayValue; | |
2414 | |
2415 { | |
2416 Json::Value item = Json::objectValue; | |
2417 item["StudyInstanceUID"] = "1.2.840.113704.1.111.7016.1342451220.40"; | |
2418 | |
2419 { | |
2420 Json::Value b = Json::arrayValue; | |
2421 | |
2422 { | |
2423 Json::Value c = Json::objectValue; | |
2424 c["CodeValue"] = "122403"; | |
2425 b.append(c); | |
2426 } | |
2427 | |
2428 item["PurposeOfReferenceCodeSequence"] = b; | |
2429 } | |
2430 | |
2431 a.append(item); | |
2432 } | |
2433 | |
2434 v["RelatedSeriesSequence"] = a; | |
2435 } | |
2436 | |
2437 std::unique_ptr<DcmDataset> d(FromDcmtkBridge::FromJson(v, false /* generate UID */, false, Encoding_Latin1, "")); | |
2438 | |
2439 static const DicomTag DICOM_TAG_REFERENCED_SOP_CLASS_UID(0x0008, 0x1150); | |
2440 static const DicomTag DICOM_TAG_REFERENCED_IMAGE_SEQUENCE(0x0008, 0x1140); | |
2441 | |
2442 DicomPath path(DICOM_TAG_REFERENCED_SOP_CLASS_UID); | |
2443 path.AddIndexedTagToPrefix(DICOM_TAG_REFERENCED_IMAGE_SEQUENCE, 2); | |
2444 //path.AddUniversalTagToPrefix(DICOM_TAG_REFERENCED_IMAGE_SEQUENCE); | |
2445 | |
2446 //DicomPath path(DicomTag(0x0008, 0x0100)); | |
2447 //path.AddIndexedTagToPrefix(DicomTag(0x0008, 0x1250), 0); | |
2448 //path.AddIndexedTagToPrefix(DicomTag(0x0040, 0xa170), 1); | |
2449 | |
2450 //FromDcmtkBridge::RemovePath(*d, DicomPath::Parse("ReferencedImageSequence[*].ReferencedSOPClassUID")); | |
2451 //FromDcmtkBridge::RemovePath(*d, DicomPath::Parse("ReferencedImageSequence[0].ReferencedSOPClassUID")); | |
2452 //FromDcmtkBridge::RemovePath(*d, DicomPath::Parse("ReferencedImageSequence[1].ReferencedSOPClassUID")); | |
2453 FromDcmtkBridge::RemovePath(*d, DicomPath::Parse("RelatedSeriesSequence[0].PurposeOfReferenceCodeSequence[0].CodeValue")); | |
2454 //FromDcmtkBridge::RemovePath(*d, DicomPath::Parse("RelatedSeriesSequence[0].PurposeOfReferenceCodeSequence")); | |
2455 //FromDcmtkBridge::RemovePath(*d, DicomPath::Parse("RelatedSeriesSequence")); | |
2456 | |
2457 { | |
2458 Json::Value v = "Hello"; | |
2459 std::unique_ptr<DcmElement> e(FromDcmtkBridge::FromJson(DicomTag(0x0008, 0x0100), v, false, Encoding_Latin1, "")); | |
2460 FromDcmtkBridge::ReplacePath(*d, DicomPath::Parse("RelatedSeriesSequence[0].PurposeOfReferenceCodeSequence[0].CodeValue"), *e); | |
2461 } | |
2462 | |
2463 { | |
2464 Json::Value v = "Hello"; | |
2465 std::unique_ptr<DcmElement> e(FromDcmtkBridge::FromJson(DicomTag(0x0008, 0x1150), v, false, Encoding_Latin1, "")); | |
2466 FromDcmtkBridge::ReplacePath(*d, DicomPath::Parse("ReferencedImageSequence[*].ReferencedSOPClassUID"), *e); | |
2467 } | |
2468 | |
2469 Json::Value vv; | |
2470 std::set<DicomTag> ignoreTagLength; | |
2471 FromDcmtkBridge::ExtractDicomAsJson(vv, *d, DicomToJsonFormat_Short, DicomToJsonFlags_None, 0, ignoreTagLength); | |
2472 std::cout << vv.toStyledString(); | |
2473 } | |
2353 } | 2474 } |
2354 | 2475 |
2355 | 2476 |
2356 | 2477 |
2357 #if ORTHANC_ENABLE_DCMTK_TRANSCODING == 1 | 2478 #if ORTHANC_ENABLE_DCMTK_TRANSCODING == 1 |