Loading a DICOM file, displaying the patient name

DCMTK

Taken from DCMTK documentation
DcmFileFormat fileformat;
OFCondition status = fileformat.loadFile("test.dcm");
if (status.good())
{
    OFString patientName;
    if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientName).good())
    {
        cout << "Patient's Name: " << patientName << endl;
    }
    else
    {
        cerr << "Error: cannot access Patient's Name!" << endl;
    }
}

DICOMHERO

With simple error checking
using namespace dicomhero;
try
{
    DataSet dicomDataSet = CodecFactory::load("test.dcm");
    const std::string patientNameUtf8 = dicomDataSet.getString(TagId(tagId_t::PatientName_0010_0010), 0);
    std::cout << "Patient's name: " << patientNameUtf8 << std::endl;
}
catch(const std::runtime_error& e)
{
    std::cout << e.what() << std::endl;
}
With detailed error checking
using namespace dicomhero;
try
{
    DataSet dicomDataSet = CodecFactory::load("test.dcm");
    const std::string patientName = dicomDataSet.getString(TagId(tagId_t::PatientName_0010_0010), 0);
    std::cout << "Patient's name: " << patientName << std::endl;
}
catch(const MissingDataElementError&)
{
    std::cout << "Error: cannot access Patient's name!" << std::endl;
}
catch(const DataHandlerConversionError&)
{
    std::cout << "Error: the patient's name cannot be converted to a string!" << std::endl;
}
catch(const CharsetConversionError&)
{
    std::cout << "Error: the patient's name cannot be coverted to UTF8" << std::endl;
}
catch(const CodecWrongFormatError&)
{
    std::cout << "Error: couldn't find a codec able to decode the file!" << std::endl;
}
Get all the patient’s name components
using namespace dicomhero;
try
{
    DataSet dicomDataSet = CodecFactory::load("test.dcm");
    const PersonName patientName = dicomDataSet.getPatientName(TagId(tagId_t::PatientName_0010_0010), 0);
    std::cout << "Patient's name (alphabetic representation: " << patientName.alphabeticRepresentation() << std::endl;
    std::cout << "Patient's name (ideographic representation: " << patientName.ideographicRepresentation() << std::endl;
    std::cout << "Patient's name (phonetic representation: " << patientName.phoneticRepresentation() << std::endl;
}
catch(const std::runtime_error& e)
{
    std::cout << e.what() << std::endl;
}

Creating a DICOM file (Secondary Capture Image Storage), saving it

DCMTK

Taken from DCMTK documentation
char uid[100];
DcmFileFormat fileformat;
DcmDataset *dataset = fileformat.getDataset();
dataset->putAndInsertString(DCM_SOPClassUID, UID_SecondaryCaptureImageStorage);
dataset->putAndInsertString(DCM_SOPInstanceUID, dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT));
dataset->putAndInsertString(DCM_PatientName, "Doe^John");
/* ... */
dataset->putAndInsertUint8Array(DCM_PixelData, pixelData, pixelLength);
OFCondition status = fileformat.saveFile("test.dcm", EXS_LittleEndianExplicit);
if (status.bad())
  cerr << "Error: cannot write DICOM file (" << status.text() << ")" << endl;

DICOMHERO

Using DICOMHERO core
using namespace dicomhero;
RandomUIDGenerator uidGenerator(SITE_INSTANCE_UID_ROOT, DEPARTMENT_ID, MODEL_ID);
DataSet dicomDataSet(uidJPEGBaselineProcess1_1_2_840_10008_1_2_4_50);
dicomDataSet.setString(TagId(tagId_t::SOPClassUID_0008_0016), uidSecondaryCaptureImageStorage_1_2_840_10008_5_1_4_1_1_7);
dicomDataSet.setString(TagId(tagId_t::SOPInstanceUID_0008_0018), uidGenerator.getUID());
dicomDataSet.setString(TagId(tagId_t::PatientName_0010_0010), "Doe^John");
/* ... */
Image myImage(300, 200, bitDepth_t::depthU8, "YBR", 7);
{
    WritingDataHandlerNumeric writePixels = myImage.getWritingDataHandler();
    /* Use the handler to fill the pixels */
}
dicomDataSet.setImage(0, myImage, imageQuality_t::veryHigh);
try
{
    CodecFactory.save(dicomDataSet, "test.dcm", codecType_t::dicom);
}
catch(const std::runtime_error& e)
{
    std::cout << e.what() << std::endl;
}    
Using DICOMHERO objects (experimental)
using namespace dicomhero;
RandomUIDGenerator uidGenerator(SITE_INSTANCE_UID_ROOT, DEPARTMENT_ID, MODEL_ID);
UIDGeneratorFactory::registerUIDGenerator("random", uidGenerator);
try
{
    ciods::SecondaryCaptureImage secondaryCapture(uidJPEGBaselineProcess1_1_2_840_10008_1_2_4_50);
    secondaryCapture.addPatientModule()
        .setPatientNameValue(PersonName("Doe^John", "ideographic", "phonetic"))
        .setPatientSexEnum(module::Patient::patientSex_t::M);
    /* ... */
    Image myImage(300, 200, bitDepth_t::depthU8, "YBR", 7);
    {
        WritingDataHandlerNumeric writePixels = myImage.getWritingDataHandler();
        /* Use the handler to fill the pixels */
    }
    secondaryCapture.getDataSet().setImage(0, myImage, imageQuality_t::veryHigh);
    CodecFactory.save(secondaryCapture.getDataSet(), "test.dcm", codecType_t::dicom);
}
catch(const std::runtime_error& e)
{
    std::cout << e.what() << std::endl;
}    

Comments are closed