Path: blob/master/modules/imgcodecs/src/grfmt_gdcm.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "grfmt_gdcm.hpp"4445#ifdef HAVE_GDCM4647//#define DBG(...) printf(__VA_ARGS__)48#define DBG(...)4950#include <gdcmImageReader.h>5152static const size_t preamble_skip = 128;53static const size_t magic_len = 4;5455inline cv::String getMagic()56{57return cv::String("\x44\x49\x43\x4D", 4);58}5960namespace cv61{6263/************************ DICOM decoder *****************************/6465DICOMDecoder::DICOMDecoder()66{67// DICOM preamble is 128 bytes (can have any value, defaults to 0) + 4 bytes magic number (DICM)68m_signature = String(preamble_skip, (char)'\x0') + getMagic();69m_buf_supported = false;70}7172bool DICOMDecoder::checkSignature( const String& signature ) const73{74if (signature.size() >= preamble_skip + magic_len)75{76if (signature.substr(preamble_skip, magic_len) == getMagic())77{78return true;79}80}81DBG("GDCM | Signature does not match\n");82return false;83}8485ImageDecoder DICOMDecoder::newDecoder() const86{87return makePtr<DICOMDecoder>();88}8990bool DICOMDecoder::readHeader()91{92gdcm::ImageReader csImageReader;93csImageReader.SetFileName(m_filename.c_str());94if(!csImageReader.Read())95{96DBG("GDCM | Failed to open DICOM file\n");97return(false);98}99100const gdcm::Image &csImage = csImageReader.GetImage();101bool bOK = true;102switch (csImage.GetPhotometricInterpretation().GetType())103{104case gdcm::PhotometricInterpretation::MONOCHROME1:105case gdcm::PhotometricInterpretation::MONOCHROME2:106{107switch (csImage.GetPixelFormat().GetScalarType())108{109case gdcm::PixelFormat::INT8: m_type = CV_8SC1; break;110case gdcm::PixelFormat::UINT8: m_type = CV_8UC1; break;111case gdcm::PixelFormat::INT16: m_type = CV_16SC1; break;112case gdcm::PixelFormat::UINT16: m_type = CV_16UC1; break;113case gdcm::PixelFormat::INT32: m_type = CV_32SC1; break;114case gdcm::PixelFormat::FLOAT32: m_type = CV_32FC1; break;115case gdcm::PixelFormat::FLOAT64: m_type = CV_64FC1; break;116default: bOK = false; DBG("GDCM | Monochrome scalar type not supported\n"); break;117}118break;119}120121case gdcm::PhotometricInterpretation::RGB:122{123switch (csImage.GetPixelFormat().GetScalarType())124{125case gdcm::PixelFormat::UINT8: m_type = CV_8UC3; break;126default: bOK = false; DBG("GDCM | RGB scalar type not supported\n"); break;127}128break;129}130131default:132{133bOK = false;134DBG("GDCM | PI not supported: %s\n", csImage.GetPhotometricInterpretation().GetString());135break;136}137}138139if(bOK)140{141unsigned int ndim = csImage.GetNumberOfDimensions();142if (ndim != 2)143{144DBG("GDCM | Invalid dimensions number: %d\n", ndim);145bOK = false;146}147}148if (bOK)149{150const unsigned int *piDimension = csImage.GetDimensions();151m_height = piDimension[0];152m_width = piDimension[1];153if( ( m_width <=0 ) || ( m_height <=0 ) )154{155DBG("GDCM | Invalid dimensions: %d x %d\n", piDimension[0], piDimension[1]);156bOK = false;157}158}159160return(bOK);161}162163164bool DICOMDecoder::readData( Mat& csImage )165{166csImage.create(m_width,m_height,m_type);167168gdcm::ImageReader csImageReader;169csImageReader.SetFileName(m_filename.c_str());170if(!csImageReader.Read())171{172DBG("GDCM | Failed to Read\n");173return false;174}175176const gdcm::Image &img = csImageReader.GetImage();177178unsigned long len = img.GetBufferLength();179if (len > csImage.elemSize() * csImage.total())180{181DBG("GDCM | Buffer is bigger than Mat: %ld > %ld * %ld\n", len, csImage.elemSize(), csImage.total());182return false;183}184185if (!img.GetBuffer((char*)csImage.ptr()))186{187DBG("GDCM | Failed to GetBuffer\n");188return false;189}190DBG("GDCM | Read OK\n");191return true;192}193194}195196#endif // HAVE_GDCM197198