/** Check whether given type 1 attribute is present and has a length > 0. * @param key - [in] The attribute tag check * @param targetDset - [out] targetDset * @param defaultValue - [in] value to be inserted if attribute is missing * (needs invent option for type 1 attributes enabled) * @return A string with an error message if attribute is not present * or has length of 0 */ OFString D2DCommon::checkAndInventType1Attrib(const DcmTagKey& key, DcmDataset* targetDset, const OFString& defaultValue) const { OFString err; OFBool exists = targetDset->tagExists(key); if (!exists) { OFString err = "Document2Dcm: Missing type 1 attribute: "; err += DcmTag(key).getTagName(); err += "\n"; return err; } DcmElement *elem; OFCondition cond = targetDset->findAndGetElement(key, elem); if (cond.bad() || !elem || (elem->getLength() == 0)) { if (!m_inventMissingType1Attribs) { err += "Document2Dcm: Empty value for type 1 attribute: "; err += DcmTag(key).getTagName(); err += "\n"; return err; } //holds element to insert in item DcmElement *elem = NULL; DcmTag tag(key); OFBool wasError = OFFalse; //if dicom element could be created, insert in to item and modify to value if (newDicomElement(elem, tag).good()) { if (targetDset->insert(elem, OFTrue).good()) { if (elem->putString(defaultValue.c_str()).good()) { if (m_debug) { OFString msg = "Document2Dcm: Inserting missing type 1 attribute "; msg += tag.getTagName(); msg += " with value "; msg += defaultValue; printMessage(m_logStream, msg); return err; } } else wasError = OFTrue; } else wasError = OFTrue; } else wasError = OFTrue; if (wasError) { err += "Unable to insert type 1 attribute "; err += tag.getTagName(); err += " with value "; err += defaultValue; err += "\n"; } } return err; }
bool t_getTagValInt(DcmDataset *DataSet, DcmTagKey tagKey, int &value) { DcmElement *elem = NULL; DcmTag tag(tagKey); if ( DataSet->findAndGetElement(tag, elem).bad() || elem->getLength() == 0) return false; Sint16 rvS16; Sint32 rvS32; Uint8 rvU8 ; Uint16 rvU16; Uint32 rvU32; if ( elem->getSint16( rvS16 ).good() ) { value = (int)rvS16; } else if ( elem->getSint32( rvS32 ).good() ) { value = (int)rvS32; } else if ( elem->getUint8 ( rvU8 ).good() ) { value = (int)rvU8 ; } else if ( elem->getUint16( rvU16 ).good() ) { value = (int)rvU16; } else if ( elem->getUint32( rvU32 ).good() ) { value = (int)rvU32; } else { t_info("%s is not integer value", tag.getTagName()); return false; } return true; }
tissuestack::imaging::DicomFileWrapper::DicomFileWrapper(const std::string filename, const bool isTempFile) : _file_name(filename), _isTemp(isTempFile) { if (!tissuestack::utils::System::fileExists(filename)) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "Supposed Dicom File does not exist in that location!"); // load file to read header tags DcmFileFormat dicomFormat; OFCondition status = dicomFormat.loadFile(filename.c_str()); if (!status.good()) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "Given dicom file is no good!"); OFString value; if (dicomFormat.getDataset()->findAndGetOFString(DCM_TransferSyntaxUID, value).good()) { std::string transferSyntaxUID = value.c_str(); std::transform(transferSyntaxUID.begin(), transferSyntaxUID.end(), transferSyntaxUID.begin(), toupper); if (transferSyntaxUID.find("JPEG2000") != std::string::npos) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "We don't support JPEG2000!"); } if (!dicomFormat.getDataset()->findAndGetOFString(DCM_SeriesInstanceUID, value).good()) if (!dicomFormat.getDataset()->findAndGetOFString(DCM_SeriesNumber, value).good()) this->_series_number = "0"; else this->_series_number = std::string(value.c_str()); else this->_series_number = std::string(value.c_str()); if (!dicomFormat.getDataset()->findAndGetOFString(DCM_InstanceNumber, value).good()) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "Could not read dicom instance number!"); this->_instance_number = strtoul(value.c_str(), NULL, 10); if (!dicomFormat.getDataset()->findAndGetOFStringArray(DCM_ImagePositionPatient, value).good()) this->_image_position_patient = "0\\0\\0"; else this->_image_position_patient = std::string(value.c_str()); if (dicomFormat.getDataset()->findAndGetOFString(DCM_MRAcquisitionType, value).good()) // are we 3D this->_acquisitionType = std::string(value.c_str()); if (dicomFormat.getDataset()->findAndGetOFString(DCM_PatientPosition, value).good()) this->_patient_position = std::string(value.c_str()); if (dicomFormat.getDataset()->findAndGetOFStringArray(DCM_ImageType, value).good()) // are we a mosaic { std::string v = std::string(value.c_str()); std::transform(v.begin(), v.end(), v.begin(), toupper); if (v.find("MOSAIC") != std::string::npos) // have a look for numbers of images in mosaic { if (dicomFormat.getDataset()->findAndGetOFString(CSA_NumberOfImagesInMosaic, value).good()) this->_number_of_images_in_mosaic = strtoul(value.c_str(), NULL, 10); DcmElement * csaImageHeaderInfo = const_cast<DcmElement *>( this->findDcmElement(dicomFormat.getDataset(), CSA_ImageSeriesHeaderInfo)); if (csaImageHeaderInfo != nullptr) { Uint8 * csaImageHeaderContent = nullptr; unsigned long int length = csaImageHeaderInfo->getLength(); status = csaImageHeaderInfo->getUint8Array(csaImageHeaderContent); if (status.good()) { std::string tmp = std::string((const char *) csaImageHeaderContent, length); size_t ascconf_start = tmp.find(ASCCONV_BEGIN); if (ascconf_start == std::string::npos) // plan B: try other header { csaImageHeaderInfo = const_cast<DcmElement *>( this->findDcmElement(dicomFormat.getDataset(), CSA_ImageHeaderInfo)); if (csaImageHeaderInfo != nullptr) { csaImageHeaderContent = nullptr; length = csaImageHeaderInfo->getLength(); status = csaImageHeaderInfo->getUint8Array(csaImageHeaderContent); tmp = std::string((const char *) csaImageHeaderContent, length); ascconf_start = tmp.find(ASCCONV_BEGIN); // try again with backup header } } if (ascconf_start != std::string::npos) { ascconf_start += strlen(ASCCONV_BEGIN) + 1; size_t ascconf_end = tmp.find(ASCCONV_END) - 1; if (ascconf_end != std::string::npos) this->_ascconv = tmp.substr(ascconf_start, ascconf_end-ascconf_start); } } } if (this->_number_of_images_in_mosaic == 0 && !this->_ascconv.empty()) // plan B for number of images in mosaic { size_t lSize_start = this->_ascconv.find(LSIZE); if (lSize_start != std::string::npos) { lSize_start = this->_ascconv.find("=", lSize_start); if (lSize_start != std::string::npos) { lSize_start++; size_t lSize_end = this->_ascconv.find("\n", lSize_start); if (lSize_end != std::string::npos) { std::string lSizeString = this->_ascconv.substr(lSize_start, lSize_start-lSize_end); this->_number_of_images_in_mosaic = strtoul(lSizeString.c_str(), NULL, 10); } } } } } } if (dicomFormat.getDataset()->findAndGetOFStringArray(DCM_ImagesInAcquisition, value).good()) this->_number_of_images_in_series_or_acquisition = strtoul(value.c_str(), NULL, 10); if (this->_number_of_images_in_series_or_acquisition == 0 && dicomFormat.getDataset()->findAndGetOFStringArray(DCM_ACR_NEMA_ImagesInSeries, value).good()) this->_number_of_images_in_series_or_acquisition = strtoul(value.c_str(), NULL, 10); if (!dicomFormat.getDataset()->findAndGetOFStringArray(DCM_PixelSpacing, value).good()) this->_pixel_spacing = "1\\1"; else this->_pixel_spacing = std::string(value.c_str()); if (!dicomFormat.getDataset()->findAndGetOFStringArray(DCM_ImageOrientationPatient, value).good()) this->_image_orientation = "1\\0\\0\\0\\1\\0"; else this->_image_orientation = std::string(value.c_str()); if (!dicomFormat.getDataset()->findAndGetOFString(DCM_Rows, value).good()) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "Could not extract number of rows for given dicom!"); this->_rows = strtoull(value.c_str(), NULL, 10); if (!dicomFormat.getDataset()->findAndGetOFString(DCM_Columns, value).good()) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "Could not extract number of columns for given dicom!"); this->_columns = strtoull(value.c_str(), NULL, 10); if (!dicomFormat.getDataset()->findAndGetOFString(DCM_BitsAllocated, value).good()) THROW_TS_EXCEPTION(tissuestack::common::TissueStackApplicationException, "Could not extract number of allocated bits for given dicom!"); this->_allocated_bits = strtoul(value.c_str(), NULL, 10); if (!dicomFormat.getDataset()->findAndGetOFString(DCM_PixelRepresentation, value).good()) this->_is_signed_data = 0; else this->_is_signed_data = static_cast<unsigned short>(strtoul(value.c_str(), NULL, 10)); if (!dicomFormat.getDataset()->findAndGetOFString(DCM_PhotometricInterpretation, value).good()) this->_photometric_interpretation = "MONOCHROME2"; else this->_photometric_interpretation = std::string(value.c_str()); if (this->isColor()) { if (!dicomFormat.getDataset()->findAndGetOFString(DCM_PlanarConfiguration, value).good()) this->_planar_configuration = 0; else this->_planar_configuration = static_cast<unsigned short>(strtoul(value.c_str(), NULL, 10)); } }