|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
#include "grfmt_gdcm.hpp"
|
|
|
|
|
|
#ifdef HAVE_GDCM
|
|
|
|
|
|
|
|
|
#define DBG(...)
|
|
|
|
|
|
#include <gdcmImageReader.h>
|
|
|
|
|
|
static const size_t preamble_skip = 128;
|
|
|
static const size_t magic_len = 4;
|
|
|
|
|
|
inline cv::String getMagic()
|
|
|
{
|
|
|
return cv::String("\x44\x49\x43\x4D", 4);
|
|
|
}
|
|
|
|
|
|
namespace cv
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
DICOMDecoder::DICOMDecoder()
|
|
|
{
|
|
|
|
|
|
m_signature = String(preamble_skip, (char)'\x0') + getMagic();
|
|
|
m_buf_supported = false;
|
|
|
}
|
|
|
|
|
|
bool DICOMDecoder::checkSignature( const String& signature ) const
|
|
|
{
|
|
|
if (signature.size() >= preamble_skip + magic_len)
|
|
|
{
|
|
|
if (signature.substr(preamble_skip, magic_len) == getMagic())
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
DBG("GDCM | Signature does not match\n");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
ImageDecoder DICOMDecoder::newDecoder() const
|
|
|
{
|
|
|
return makePtr<DICOMDecoder>();
|
|
|
}
|
|
|
|
|
|
bool DICOMDecoder::readHeader()
|
|
|
{
|
|
|
gdcm::ImageReader csImageReader;
|
|
|
csImageReader.SetFileName(m_filename.c_str());
|
|
|
if(!csImageReader.Read())
|
|
|
{
|
|
|
DBG("GDCM | Failed to open DICOM file\n");
|
|
|
return(false);
|
|
|
}
|
|
|
|
|
|
const gdcm::Image &csImage = csImageReader.GetImage();
|
|
|
bool bOK = true;
|
|
|
switch (csImage.GetPhotometricInterpretation().GetType())
|
|
|
{
|
|
|
case gdcm::PhotometricInterpretation::MONOCHROME1:
|
|
|
case gdcm::PhotometricInterpretation::MONOCHROME2:
|
|
|
{
|
|
|
switch (csImage.GetPixelFormat().GetScalarType())
|
|
|
{
|
|
|
case gdcm::PixelFormat::INT8: m_type = CV_8SC1; break;
|
|
|
case gdcm::PixelFormat::UINT8: m_type = CV_8UC1; break;
|
|
|
case gdcm::PixelFormat::INT16: m_type = CV_16SC1; break;
|
|
|
case gdcm::PixelFormat::UINT16: m_type = CV_16UC1; break;
|
|
|
case gdcm::PixelFormat::INT32: m_type = CV_32SC1; break;
|
|
|
case gdcm::PixelFormat::FLOAT32: m_type = CV_32FC1; break;
|
|
|
case gdcm::PixelFormat::FLOAT64: m_type = CV_64FC1; break;
|
|
|
default: bOK = false; DBG("GDCM | Monochrome scalar type not supported\n"); break;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case gdcm::PhotometricInterpretation::RGB:
|
|
|
{
|
|
|
switch (csImage.GetPixelFormat().GetScalarType())
|
|
|
{
|
|
|
case gdcm::PixelFormat::UINT8: m_type = CV_8UC3; break;
|
|
|
default: bOK = false; DBG("GDCM | RGB scalar type not supported\n"); break;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
{
|
|
|
bOK = false;
|
|
|
DBG("GDCM | PI not supported: %s\n", csImage.GetPhotometricInterpretation().GetString());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(bOK)
|
|
|
{
|
|
|
unsigned int ndim = csImage.GetNumberOfDimensions();
|
|
|
if (ndim != 2)
|
|
|
{
|
|
|
DBG("GDCM | Invalid dimensions number: %d\n", ndim);
|
|
|
bOK = false;
|
|
|
}
|
|
|
}
|
|
|
if (bOK)
|
|
|
{
|
|
|
const unsigned int *piDimension = csImage.GetDimensions();
|
|
|
m_height = piDimension[0];
|
|
|
m_width = piDimension[1];
|
|
|
if( ( m_width <=0 ) || ( m_height <=0 ) )
|
|
|
{
|
|
|
DBG("GDCM | Invalid dimensions: %d x %d\n", piDimension[0], piDimension[1]);
|
|
|
bOK = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return(bOK);
|
|
|
}
|
|
|
|
|
|
|
|
|
bool DICOMDecoder::readData( Mat& csImage )
|
|
|
{
|
|
|
csImage.create(m_width,m_height,m_type);
|
|
|
|
|
|
gdcm::ImageReader csImageReader;
|
|
|
csImageReader.SetFileName(m_filename.c_str());
|
|
|
if(!csImageReader.Read())
|
|
|
{
|
|
|
DBG("GDCM | Failed to Read\n");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
const gdcm::Image &img = csImageReader.GetImage();
|
|
|
|
|
|
unsigned long len = img.GetBufferLength();
|
|
|
if (len > csImage.elemSize() * csImage.total())
|
|
|
{
|
|
|
DBG("GDCM | Buffer is bigger than Mat: %ld > %ld * %ld\n", len, csImage.elemSize(), csImage.total());
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (!img.GetBuffer((char*)csImage.ptr()))
|
|
|
{
|
|
|
DBG("GDCM | Failed to GetBuffer\n");
|
|
|
return false;
|
|
|
}
|
|
|
DBG("GDCM | Read OK\n");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif |