comparison OrthancServer/FromDcmtkBridge.cpp @ 1934:72a2fd7fed8b

FromDcmtkBridge::FromJson
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Mar 2016 15:11:53 +0100
parents 1c147c3c3121
children e251606c1433
comparison
equal deleted inserted replaced
1933:ff11ba08e5d0 1934:72a2fd7fed8b
1577 else 1577 else
1578 { 1578 {
1579 return pixelSequence; 1579 return pixelSequence;
1580 } 1580 }
1581 } 1581 }
1582
1583
1584 Encoding FromDcmtkBridge::ExtractEncoding(const Json::Value& json,
1585 Encoding defaultEncoding)
1586 {
1587 if (json.type() != Json::objectValue)
1588 {
1589 throw OrthancException(ErrorCode_BadParameterType);
1590 }
1591
1592 Encoding encoding = defaultEncoding;
1593
1594 const Json::Value::Members tags = json.getMemberNames();
1595
1596 // Look for SpecificCharacterSet (0008,0005) in the JSON file
1597 for (size_t i = 0; i < tags.size(); i++)
1598 {
1599 DicomTag tag = FromDcmtkBridge::ParseTag(tags[i]);
1600 if (tag == DICOM_TAG_SPECIFIC_CHARACTER_SET)
1601 {
1602 const Json::Value& value = json[tags[i]];
1603 if (value.type() != Json::stringValue ||
1604 !GetDicomEncoding(encoding, value.asCString()))
1605 {
1606 LOG(ERROR) << "Unknown encoding while creating DICOM from JSON: " << value;
1607 throw OrthancException(ErrorCode_BadRequest);
1608 }
1609 }
1610 }
1611
1612 return encoding;
1613 }
1614
1615
1616 static void SetString(DcmDataset& target,
1617 const DcmTag& tag,
1618 const std::string& value)
1619 {
1620 if (!target.putAndInsertString(tag, value.c_str()).good())
1621 {
1622 throw OrthancException(ErrorCode_InternalError);
1623 }
1624 }
1625
1626
1627 DcmDataset* FromDcmtkBridge::FromJson(const Json::Value& json, // Encoded using UTF-8
1628 bool generateIdentifiers,
1629 bool decodeDataUriScheme,
1630 Encoding defaultEncoding)
1631 {
1632 std::auto_ptr<DcmDataset> result(new DcmDataset);
1633 Encoding encoding = ExtractEncoding(json, defaultEncoding);
1634
1635 SetString(*result, DCM_SpecificCharacterSet, GetDicomSpecificCharacterSet(encoding));
1636
1637 const Json::Value::Members tags = json.getMemberNames();
1638
1639 bool hasPatientId = false;
1640 bool hasStudyInstanceUid = false;
1641 bool hasSeriesInstanceUid = false;
1642 bool hasSopInstanceUid = false;
1643
1644 for (size_t i = 0; i < tags.size(); i++)
1645 {
1646 DicomTag tag = FromDcmtkBridge::ParseTag(tags[i]);
1647 const Json::Value& value = json[tags[i]];
1648
1649 if (tag == DICOM_TAG_PATIENT_ID)
1650 {
1651 hasPatientId = true;
1652 }
1653 else if (tag == DICOM_TAG_STUDY_INSTANCE_UID)
1654 {
1655 hasStudyInstanceUid = true;
1656 }
1657 else if (tag == DICOM_TAG_SERIES_INSTANCE_UID)
1658 {
1659 hasSeriesInstanceUid = true;
1660 }
1661 else if (tag == DICOM_TAG_SOP_INSTANCE_UID)
1662 {
1663 hasSopInstanceUid = true;
1664 }
1665
1666 if (tag != DICOM_TAG_SPECIFIC_CHARACTER_SET)
1667 {
1668 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, encoding));
1669 const DcmTagKey& tag = element->getTag();
1670
1671 result->findAndDeleteElement(tag);
1672
1673 DcmElement* tmp = element.release();
1674 if (!result->insert(tmp, false, false).good())
1675 {
1676 delete tmp;
1677 throw OrthancException(ErrorCode_InternalError);
1678 }
1679 }
1680 }
1681
1682 if (!hasPatientId &&
1683 generateIdentifiers)
1684 {
1685 SetString(*result, DCM_PatientID, GenerateUniqueIdentifier(ResourceType_Patient));
1686 }
1687
1688 if (!hasStudyInstanceUid &&
1689 generateIdentifiers)
1690 {
1691 SetString(*result, DCM_StudyInstanceUID, GenerateUniqueIdentifier(ResourceType_Study));
1692 }
1693
1694 if (!hasSeriesInstanceUid &&
1695 generateIdentifiers)
1696 {
1697 SetString(*result, DCM_SeriesInstanceUID, GenerateUniqueIdentifier(ResourceType_Series));
1698 }
1699
1700 if (!hasSopInstanceUid &&
1701 generateIdentifiers)
1702 {
1703 SetString(*result, DCM_SOPInstanceUID, GenerateUniqueIdentifier(ResourceType_Instance));
1704 }
1705
1706 return result.release();
1707 }
1582 } 1708 }