#include "writedcm.h"
#include "patientdata.h"
#include "doctordata.h"
#include "examinationdata.h"
#include "dcmmutualqt.h"
#include "readdcm.h"
#include "alldefine.h"
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmimgle/dcmimage.h"
#include "dcmtk/dcmimage/diregist.h"
#include "dcmtk/dcmjpeg/djdecode.h"
#include "dcmtk/dcmdata/dcrledrg.h"
#include "dcmtk/dcmdata/libi2d/i2dimgs.h"
#include "dcmtk/dcmdata/libi2d/i2djpgs.h"
#include "dcmtk/dcmdata/libi2d/i2dbmps.h"
#include "dcmtk/dcmdata/libi2d/i2dplsc.h"
#include "dcmtk/dcmdata/libi2d/i2d.h"
#include "dcmtk/dcmdata/dcpxitem.h"
#include <QDateTime>
#include <QImage>
#include <QDebug>
WriteDcm::WriteDcm():
m_fileFormat(new DcmFileFormat()),
m_readPatient(false),
m_readStudy(false),
m_readSeries(false),
m_addImage(false)
{
}
WriteDcm::~WriteDcm()
{
}
void WriteDcm::addImage(const char *imgName)
{
m_addImage = true;
DcmDataset *dataSet = m_fileFormat->getDataset();
I2DImgSource *imgSource = 0;
if(QString(imgName).endsWith("jpg", Qt::CaseInsensitive)) {
imgSource = new I2DJpegSource();
}
else if(QString(imgName).endsWith("bmp", Qt::CaseInsensitive)) {
imgSource = new I2DBmpSource();
}
else {
return;
}
imgSource->setImageFile(OFString(imgName));
readAndInsertPixelData(imgSource, dataSet);
OFCondition cond;
// Insert Lossy Image Compression and Lossy Image Compression Method attributes if necessary
OFBool srcIsLossy = OFFalse;
OFString comprMethod;
if (imgSource->getLossyComprInfo(srcIsLossy, comprMethod).good()) {
if(srcIsLossy) {
cond = dataSet->putAndInsertOFStringArray(DCM_LossyImageCompression, DCMMutualQt::QString2OFString("01"));
if (cond.good() && !comprMethod.empty()) {
cond = dataSet->putAndInsertOFStringArray(DCM_LossyImageCompressionMethod, comprMethod);
}
if (cond.bad()) {
qDebug()<<"Unable to write attribute Lossy Image Compression and/or Lossy Image Compression Method to result dataset"<<endl;
}
}
}
else {
qDebug()<<"Image2Dcm: No information regarding lossy compression available";
}
// Insert SOP Class specific attributes (and values)
I2DOutputPlug *outPlug = new I2DOutputPlugSC();
cond = outPlug->convert(*dataSet);
if (cond.bad()) {
qDebug()<<"out convert failed:"<<cond.text()<<endl;
delete outPlug;
outPlug = 0;
delete imgSource;
imgSource = 0;
return;
}
OFString err = outPlug->isValid(*dataSet);
if (!err.empty()) {
qDebug()<<"image basic info isempty"<<err.data();
return;
}
}
OFString WriteDcm::checkAndInventType1Attrib(const DcmTagKey &key, DcmDataset *targetDset, \
const char *defaultValue) const
{
OFBool exists = targetDset->tagExists(key);
if (!exists) {
QString value = "Image2Dcm:: Missing type 1 attribute: ";
OFString err = DCMMutualQt::QString2OFString(value);
err += DcmTag(key).getTagName();
err += DCMMutualQt::QString2OFString("\n");
return err;
}
DcmElement *elem;
OFCondition cond = targetDset->findAndGetElement(key, elem);
if (cond.bad() || !elem || (elem->getLength() == 0)) {
// holds element to insert in item
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).good()) {
qDebug()<<"Image2Dcm: Inserting missing type 1 attribute: "<<tag.getTagName()<< " with value " << defaultValue<<endl;
}
else {
wasError = OFTrue;
}
}
else {
wasError = OFTrue;
}
}
else {
wasError = OFTrue;
}
if (wasError) {
OFString err = DCMMutualQt::QString2OFString("Unable to insert type 1 attribute ");
err += tag.getTagName();
err += DCMMutualQt::QString2OFString(" with value ");
err += defaultValue;
err += DCMMutualQt::QString2OFString("\n");
return err;
}
}
return "";
}
OFString WriteDcm::checkAndInventType2Attrib(const DcmTagKey &key, DcmDataset *targetDset) const
{
OFString err;
OFBool exists = targetDset->tagExists(key);
if (!exists) {
DcmTag tag(key);
qDebug()<<"Image2Dcm: Inserting missing type 2 attribute: " << tag.getTagName();
OFCondition status = targetDset->insertEmptyElement(tag);
if(status.bad()) {
err = status.text();
}
}
if(err.empty()) {
return "";
}
else {
return err.data();
}
}
OFString WriteDcm::isValid() const
{
DcmDataset *dset = m_fileFormat->getDataset();
OFString err;
// General Patient module attributes
err += checkAndInventType2Attrib(DCM_PatientName, dset);
err += checkAndInventType2Attrib(DCM_PatientSex, dset);
err += checkAndInventType2Attrib(DCM_PatientBirthDate, dset);
err += checkAndInventType2Attrib(DCM_PatientID, dset);
// General Study module attributes
err += checkAndInventType1Attrib(DCM_StudyInstanceUID, dset);
err += checkAndInventType2Attrib(DCM_StudyDate, dset);
err += checkAndInventType2Attrib(DCM_StudyTime, dset);
err += checkAndInventType2Attrib(DCM_ReferringPhysicianName, dset);
err += checkAndInventType2Attrib(DCM_StudyID, dset);
err += checkAndInventType2Attrib(DCM_AccessionNumber, dset);
// General Series module attributes
err += checkAndInventType1Attrib(DCM_SeriesInstanceUID, dset);
err += checkAndInventType2Attrib(DCM_SeriesNumber, dset);
err += checkAndInventType2Attrib(DCM_InstanceNumber, dset);
// General Image module attributes
/* Patient Orientation is of type 2C and must be written if not
Image Orientation (Patient) (0020,0037) and Image Position (Patient)
are required for the IOD. The current output IODs (SC, new SC, VLP)
therefore need Patient Orientation. Make sure any new output plugin
takes care about this attribute
*/
err += checkAndInventType2Attrib(DCM_PatientOrientation, dset);
// Image Pixel Module
err += checkAndInventType1Attrib(DCM_Rows, dset);
err += checkAndInventType1Attrib(DCM_Columns, dset);
err += checkAndInventType1Attrib(DCM_SamplesPerPixel, dset);
err += checkAndInventType1Attrib(DCM_PhotometricInterpretation, dset);
err += checkAndInventType1Attrib(DCM_BitsAllocated, dset);
err += checkAndInventType1Attrib(DCM_BitsStored, dset);
err += checkAndInventType1Attrib(DCM_HighBit, dset);
err += checkAndInventType1Attrib(DCM_PixelRepresentation, dset);
err += checkAndInventType1Attrib(DCM_SOPInstanceUID, dset);
if(err.empty()) {
return "";
}
else {
return err;
}
}
bool WriteDcm::readAndInsertPixelData(I2DImgSource *imgSource, DcmDataset *dataSet)
{
Uint16 samplesPerPixel, rows, cols, bitsAlloc, bitsStored, highBit, pixelRepr, planConf;
Uint16 pixAspectH =1; Uint16 pixAspectV = 1;
OFString photoMetrInt;
E_TransferSyntax outputTS = EXS_Unknown;
char* pixData = 0;
Uint32 length;
OFCondition cond = imgSource->readPixelData(rows, cols,
samplesPerPixel, photoMetrInt, bitsAlloc, bitsStored, highBit, pixelRepr,
planConf, pixAspectH, pixAspectV, pixData, length, outputTS);
if(cond.bad()) {
qDebug(
- 1
- 2
前往页