背景,使用VTK、ITK读取dicom图像,出现中文路径,无法读取的情况,才找到了DCMTK作为替换方案
#include <dcmtk/dcmdata/dctk.h>
#include <dcmtk/dcmdata/dcfilefo.h>
#include <dcmtk/dcmimgle/dcmimage.h>
#include <dcmtk/dcmimage/diregist.h>
#include <dcmtk/dcmjpeg/djdecode.h>
#include <dcmtk/ofstd/ofstring.h>
#include <dcmtk/dcmdata/dcostrmb.h>
DcmFileFormat* dcmff = new DcmFileFormat;
OFCondition oc = dcmff->loadFile(OFFilename(DicomFileName.c_str()));
if (oc.bad())
{
std::cout << "Fail:" << oc.text();
return false;
}
DcmDataset* dataset = dcmff->getDataset();
OFString bits;
if (dataset->findAndGetOFString(DCM_BitsAllocated, bits).bad())
{
return false;
}
int iBits = atoi(bits.c_str());
// decompress data set if compressed
if (dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL).good() &&
dataset->canWriteXfer(EXS_LittleEndianExplicit))
{
DicomImage* img = new DicomImage(dataset, EXS_LittleEndianExplicit);
int ingCount = img->getFrameCount();
int nWidth = img->getWidth(); //获得图像宽度
int nHeight = img->getHeight(); //获得图像高度
Size[0] = nWidth;
Size[1] = nHeight;
Size[2] = ingCount;
if (iBits == 8)
{
charDataPtr = new unsigned char[Size[0] * Size[1] * Size[2]];
DataType = "unsigned_char";
for (int i = 0; i < ingCount; i++)
{
unsigned char* pixelData = (unsigned char*)(img->getOutputData(8, i));
memcpy(charDataPtr + i * nHeight * nWidth, pixelData, nHeight* nWidth * sizeof(unsigned char));
}
}
else if (iBits == 16)
{
shortDataPtr = new short[Size[0] * Size[1] * Size[2]];
DataType = "short";
for (int i = 0; i < ingCount; i++)
{
short* pixelData = (short*)(img->getOutputData(16, i));
memcpy(shortDataPtr + i * nHeight * nWidth, pixelData, nHeight* nWidth * sizeof(short));
}
}
}