From 6f22f49c027be4802e56d17af669f0577a968986 Mon Sep 17 00:00:00 2001 From: look4pritam Date: Mon, 20 Apr 2015 17:57:57 +0530 Subject: [PATCH 1/3] Grassroots DiCoM i.e. GDCM based DICOM image reader is added. --- CMakeLists.txt | 8 + cmake/OpenCVFindLibsGrfmt.cmake | 12 ++ cmake/templates/cvconfig.h.in | 3 + modules/imgcodecs/CMakeLists.txt | 11 ++ modules/imgcodecs/src/gdcm_dicom.cpp | 216 +++++++++++++++++++++++++++ modules/imgcodecs/src/gdcm_dicom.hpp | 71 +++++++++ modules/imgcodecs/src/loadsave.cpp | 6 + 7 files changed, 327 insertions(+) create mode 100644 modules/imgcodecs/src/gdcm_dicom.cpp create mode 100644 modules/imgcodecs/src/gdcm_dicom.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ea42680b9..317edbb139 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,6 +191,7 @@ OCV_OPTION(WITH_OPENGL "Include OpenGL support" OFF OCV_OPTION(WITH_OPENNI "Include OpenNI support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_OPENNI2 "Include OpenNI2 support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_PNG "Include PNG support" ON) +OCV_OPTION(WITH_GDCM "Include DICOM support" OFF) OCV_OPTION(WITH_PVAPI "Include Prosilica GigE support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_GIGEAPI "Include Smartek GigE support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_QT "Build with Qt Backend support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) @@ -1008,6 +1009,7 @@ if(WITH_PNG) else() status(" PNG:" "NO") endif() + if(WITH_TIFF) if(TIFF_VERSION_STRING AND TIFF_FOUND) status(" TIFF:" "${TIFF_LIBRARY} (ver ${TIFF_VERSION} - ${TIFF_VERSION_STRING})") @@ -1034,6 +1036,12 @@ else() status(" GDAL:" "NO") endif() +if(WITH_GDCM) + status(" GDCM:" GDCM_FOUND THEN "YES (ver ${GDCM_VERSION})" ELSE "NO") +else() + status(" GDCM:" "NO") +endif() + # ========================== VIDEO IO ========================== status("") status(" Video I/O:") diff --git a/cmake/OpenCVFindLibsGrfmt.cmake b/cmake/OpenCVFindLibsGrfmt.cmake index 614f844c7b..ced3c8ef7f 100644 --- a/cmake/OpenCVFindLibsGrfmt.cmake +++ b/cmake/OpenCVFindLibsGrfmt.cmake @@ -212,3 +212,15 @@ if(WITH_GDAL) ocv_include_directories(${GDAL_INCLUDE_DIR}) endif() endif() + +if (WITH_GDCM) + find_package(GDCM) + if(NOT GDCM_FOUND) + set(HAVE_GDCM NO) + ocv_clear_vars(GDCM_VERSION GDCM_LIBRARIES) + else() + set(HAVE_GDCM YES) + # include(${GDCM_USE_FILE}) + set(GDCM_LIBRARIES gdcmMSFF) # GDCM does not set this variable for some reason + endif() +endif() diff --git a/cmake/templates/cvconfig.h.in b/cmake/templates/cvconfig.h.in index b86d44a6c1..2312742130 100644 --- a/cmake/templates/cvconfig.h.in +++ b/cmake/templates/cvconfig.h.in @@ -111,6 +111,9 @@ /* libpng/png.h needs to be included */ #cmakedefine HAVE_LIBPNG_PNG_H +/* GDCM DICOM codec */ +#cmakedefine HAVE_GDCM + /* V4L/V4L2 capturing support via libv4l */ #cmakedefine HAVE_LIBV4L diff --git a/modules/imgcodecs/CMakeLists.txt b/modules/imgcodecs/CMakeLists.txt index 8b8c577166..1c7cc25b9e 100644 --- a/modules/imgcodecs/CMakeLists.txt +++ b/modules/imgcodecs/CMakeLists.txt @@ -35,6 +35,11 @@ if(HAVE_PNG) list(APPEND GRFMT_LIBS ${PNG_LIBRARIES}) endif() +if(HAVE_GDCM) + ocv_include_directories(${GDCM_INCLUDE_DIRS}) + list(APPEND GRFMT_LIBS ${GDCM_LIBRARIES}) +endif() + if(HAVE_TIFF) ocv_include_directories(${TIFF_INCLUDE_DIR}) list(APPEND GRFMT_LIBS ${TIFF_LIBRARIES}) @@ -57,6 +62,12 @@ endif() file(GLOB grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/grfmt*.hpp) file(GLOB grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/grfmt*.cpp) + +if(HAVE_GDCM) + list(APPEND grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/gdcm_dicom.hpp) + list(APPEND grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/gdcm_dicom.cpp) +endif() + list(APPEND grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/bitstrm.hpp) list(APPEND grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/bitstrm.cpp) list(APPEND grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/rgbe.hpp) diff --git a/modules/imgcodecs/src/gdcm_dicom.cpp b/modules/imgcodecs/src/gdcm_dicom.cpp new file mode 100644 index 0000000000..58e38b95d0 --- /dev/null +++ b/modules/imgcodecs/src/gdcm_dicom.cpp @@ -0,0 +1,216 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "precomp.hpp" +#include "gdcm_dicom.hpp" + +#include + +namespace cv +{ + +/************************ DICOM decoder *****************************/ + +DICOMDecoder::DICOMDecoder() +{ + /// DICOM preable is 128 bytes (can have any vale, defaults to x00) + 4 bytes magic number (DICM) + m_signature = ""; + for(int iSize=0; iSize<128; iSize++) + { + m_signature = m_signature + "\xFF"; + } + + m_signature = m_signature + "\x44\x49\x43\x4D"; + + m_buf_supported = false; +} + + +DICOMDecoder::~DICOMDecoder() +{ +} + +bool DICOMDecoder::checkSignature( const String& signature ) const +{ + size_t len = signatureLength(); + bool bOK = signature.size() >= len; + for(int iIndex = 128; iIndex < len; iIndex++) + { + if(signature[iIndex] == m_signature[iIndex]) + { + continue; + } + else + { + bOK = false; + break; + } + } + + return(bOK); +} + +void DICOMDecoder::close() +{ +} + +ImageDecoder DICOMDecoder::newDecoder() const +{ + return makePtr(); +} + +bool DICOMDecoder::readHeader() +{ + gdcm::ImageReader csImageReader; + csImageReader.SetFileName(m_filename.c_str()); + if(!csImageReader.Read()) + { + return(false); + } + + bool bOK = true; + + const gdcm::Image &csImage = csImageReader.GetImage(); + if( ( csImage.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 ) + || ( csImage.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2 ) + ) + { + gdcm::PixelFormat ePixelFormat = csImage.GetPixelFormat(); + if( ePixelFormat == gdcm::PixelFormat::INT8) + { + m_type = CV_8SC1; + } + else if( ePixelFormat == gdcm::PixelFormat::UINT8) + { + m_type = CV_8UC1; + } + else if( ePixelFormat == gdcm::PixelFormat::INT16) + { + m_type = CV_16SC1; + } + else if( ePixelFormat == gdcm::PixelFormat::UINT16) + { + m_type = CV_16UC1; + } + else if( ePixelFormat == gdcm::PixelFormat::INT32) + { + m_type = CV_32SC1; + } + else if( ePixelFormat == gdcm::PixelFormat::FLOAT32) + { + m_type = CV_32FC1; + } + else if( ePixelFormat == gdcm::PixelFormat::FLOAT64) + { + m_type = CV_64FC1; + } + else if( ePixelFormat == gdcm::PixelFormat::INT12) + { + bOK = false; + } + else if( ePixelFormat == gdcm::PixelFormat::UINT12) + { + bOK = false; + } + else if( ePixelFormat == gdcm::PixelFormat::UINT32) + { + bOK = false; + } + else if( ePixelFormat == gdcm::PixelFormat::SINGLEBIT) + { + bOK = false; + } + else + { + bOK = false; + } + } + else if( csImage.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::RGB ) + { + gdcm::PixelFormat ePixelFormat = csImage.GetPixelFormat(); + if( ePixelFormat == gdcm::PixelFormat::UINT8) + { + m_type = CV_8UC3; + } + else + { + bOK = false; + } + } + else + { + bOK = false; + } + + if(bOK) + { + const unsigned int *piDimension = csImage.GetDimensions(); + m_width = piDimension[0]; + m_height = piDimension[1]; + if( ( m_width <=0 ) || ( m_height <=0 ) ) + { + 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()) + { + return(false); + } + + bool bOK = true; + const gdcm::Image &csGDCMImage = csImageReader.GetImage(); + bOK = csGDCMImage.GetBuffer((char*)csImage.ptr()); + + return(bOK); +} +} diff --git a/modules/imgcodecs/src/gdcm_dicom.hpp b/modules/imgcodecs/src/gdcm_dicom.hpp new file mode 100644 index 0000000000..c9c5c23f27 --- /dev/null +++ b/modules/imgcodecs/src/gdcm_dicom.hpp @@ -0,0 +1,71 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef _GDCM_DICOM_H_ +#define _GDCM_DICOM_H_ + +#include "grfmt_base.hpp" + +namespace cv +{ + +// DICOM image reader using GDCM +class DICOMDecoder : public BaseImageDecoder +{ +public: + + DICOMDecoder(); + ~DICOMDecoder(); + + bool readData( Mat& img ); + bool readHeader(); + void close(); + + ImageDecoder newDecoder() const; + virtual bool checkSignature( const String& signature ) const; + +protected: +}; + +} + +#endif/*_GDCM_DICOM_H_*/ diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index 70a31c37a2..c62b0bc722 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -45,6 +45,9 @@ #include "precomp.hpp" #include "grfmts.hpp" +#ifdef HAVE_GDCM +#include "gdcm_dicom.hpp" +#endif #undef min #undef max #include @@ -93,6 +96,9 @@ struct ImageCodecInitializer decoders.push_back( makePtr() ); encoders.push_back( makePtr() ); #endif + #ifdef HAVE_GDCM + decoders.push_back( makePtr() ); + #endif #ifdef HAVE_JASPER decoders.push_back( makePtr() ); encoders.push_back( makePtr() ); From 0fd0acf2e3697fd98ad89f214e3c6acc22bc55e3 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Thu, 7 Jul 2016 12:33:24 +0300 Subject: [PATCH 2/3] GDCM: modified compilation scheme - renamed source files - guard the contents - always include --- modules/imgcodecs/CMakeLists.txt | 5 ----- modules/imgcodecs/src/{gdcm_dicom.cpp => grfmt_gdcm.cpp} | 6 +++++- modules/imgcodecs/src/{gdcm_dicom.hpp => grfmt_gdcm.hpp} | 6 ++++++ modules/imgcodecs/src/grfmts.hpp | 1 + modules/imgcodecs/src/loadsave.cpp | 3 --- 5 files changed, 12 insertions(+), 9 deletions(-) rename modules/imgcodecs/src/{gdcm_dicom.cpp => grfmt_gdcm.cpp} (99%) rename modules/imgcodecs/src/{gdcm_dicom.hpp => grfmt_gdcm.hpp} (98%) diff --git a/modules/imgcodecs/CMakeLists.txt b/modules/imgcodecs/CMakeLists.txt index 1c7cc25b9e..c614d79cdd 100644 --- a/modules/imgcodecs/CMakeLists.txt +++ b/modules/imgcodecs/CMakeLists.txt @@ -63,11 +63,6 @@ endif() file(GLOB grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/grfmt*.hpp) file(GLOB grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/grfmt*.cpp) -if(HAVE_GDCM) - list(APPEND grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/gdcm_dicom.hpp) - list(APPEND grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/gdcm_dicom.cpp) -endif() - list(APPEND grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/bitstrm.hpp) list(APPEND grfmt_srcs ${CMAKE_CURRENT_LIST_DIR}/src/bitstrm.cpp) list(APPEND grfmt_hdrs ${CMAKE_CURRENT_LIST_DIR}/src/rgbe.hpp) diff --git a/modules/imgcodecs/src/gdcm_dicom.cpp b/modules/imgcodecs/src/grfmt_gdcm.cpp similarity index 99% rename from modules/imgcodecs/src/gdcm_dicom.cpp rename to modules/imgcodecs/src/grfmt_gdcm.cpp index 58e38b95d0..7aecf35f09 100644 --- a/modules/imgcodecs/src/gdcm_dicom.cpp +++ b/modules/imgcodecs/src/grfmt_gdcm.cpp @@ -41,7 +41,9 @@ //M*/ #include "precomp.hpp" -#include "gdcm_dicom.hpp" +#include "grfmt_gdcm.hpp" + +#ifdef HAVE_GDCM #include @@ -214,3 +216,5 @@ bool DICOMDecoder::readData( Mat& csImage ) return(bOK); } } + +#endif \ No newline at end of file diff --git a/modules/imgcodecs/src/gdcm_dicom.hpp b/modules/imgcodecs/src/grfmt_gdcm.hpp similarity index 98% rename from modules/imgcodecs/src/gdcm_dicom.hpp rename to modules/imgcodecs/src/grfmt_gdcm.hpp index c9c5c23f27..8a8e10032c 100644 --- a/modules/imgcodecs/src/gdcm_dicom.hpp +++ b/modules/imgcodecs/src/grfmt_gdcm.hpp @@ -43,6 +43,10 @@ #ifndef _GDCM_DICOM_H_ #define _GDCM_DICOM_H_ +#include "cvconfig.h" + +#ifdef HAVE_GDCM + #include "grfmt_base.hpp" namespace cv @@ -68,4 +72,6 @@ protected: } +#endif + #endif/*_GDCM_DICOM_H_*/ diff --git a/modules/imgcodecs/src/grfmts.hpp b/modules/imgcodecs/src/grfmts.hpp index c9e31530a8..7db1ac94a2 100644 --- a/modules/imgcodecs/src/grfmts.hpp +++ b/modules/imgcodecs/src/grfmts.hpp @@ -54,5 +54,6 @@ #include "grfmt_webp.hpp" #include "grfmt_hdr.hpp" #include "grfmt_gdal.hpp" +#include "grfmt_gdcm.hpp" #endif/*_GRFMTS_H_*/ diff --git a/modules/imgcodecs/src/loadsave.cpp b/modules/imgcodecs/src/loadsave.cpp index c62b0bc722..1c0b794b8e 100644 --- a/modules/imgcodecs/src/loadsave.cpp +++ b/modules/imgcodecs/src/loadsave.cpp @@ -45,9 +45,6 @@ #include "precomp.hpp" #include "grfmts.hpp" -#ifdef HAVE_GDCM -#include "gdcm_dicom.hpp" -#endif #undef min #undef max #include From f3bd508e6d96c3f988da2bb14a3a8ec7930193df Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Fri, 8 Jul 2016 16:08:11 +0300 Subject: [PATCH 3/3] GDCM: several improvements - fixed width and height order - removed unused methods - simplified signature matching - rewrote pixel format matching in more compact form - added dimensions number check (only 2 is allowed) - added target buffer size check - added debug messages in all failing points --- modules/imgcodecs/src/grfmt_gdcm.cpp | 175 ++++++++++++--------------- modules/imgcodecs/src/grfmt_gdcm.hpp | 9 +- 2 files changed, 77 insertions(+), 107 deletions(-) diff --git a/modules/imgcodecs/src/grfmt_gdcm.cpp b/modules/imgcodecs/src/grfmt_gdcm.cpp index 7aecf35f09..a1d9e9d0af 100644 --- a/modules/imgcodecs/src/grfmt_gdcm.cpp +++ b/modules/imgcodecs/src/grfmt_gdcm.cpp @@ -45,8 +45,19 @@ #ifdef HAVE_GDCM +//#define DBG(...) printf(__VA_ARGS__) +#define DBG(...) + #include +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 { @@ -54,45 +65,22 @@ namespace cv DICOMDecoder::DICOMDecoder() { - /// DICOM preable is 128 bytes (can have any vale, defaults to x00) + 4 bytes magic number (DICM) - m_signature = ""; - for(int iSize=0; iSize<128; iSize++) - { - m_signature = m_signature + "\xFF"; - } - - m_signature = m_signature + "\x44\x49\x43\x4D"; - + // DICOM preamble is 128 bytes (can have any value, defaults to 0) + 4 bytes magic number (DICM) + m_signature = String(preamble_skip, (char)'\x0') + getMagic(); m_buf_supported = false; } - -DICOMDecoder::~DICOMDecoder() -{ -} - bool DICOMDecoder::checkSignature( const String& signature ) const { - size_t len = signatureLength(); - bool bOK = signature.size() >= len; - for(int iIndex = 128; iIndex < len; iIndex++) + if (signature.size() >= preamble_skip + magic_len) { - if(signature[iIndex] == m_signature[iIndex]) + if (signature.substr(preamble_skip, magic_len) == getMagic()) { - continue; - } - else - { - bOK = false; - break; + return true; } } - - return(bOK); -} - -void DICOMDecoder::close() -{ + DBG("GDCM | Signature does not match\n"); + return false; } ImageDecoder DICOMDecoder::newDecoder() const @@ -106,90 +94,66 @@ bool DICOMDecoder::readHeader() csImageReader.SetFileName(m_filename.c_str()); if(!csImageReader.Read()) { + DBG("GDCM | Failed to open DICOM file\n"); return(false); } - bool bOK = true; - const gdcm::Image &csImage = csImageReader.GetImage(); - if( ( csImage.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 ) - || ( csImage.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2 ) - ) + bool bOK = true; + switch (csImage.GetPhotometricInterpretation().GetType()) { - gdcm::PixelFormat ePixelFormat = csImage.GetPixelFormat(); - if( ePixelFormat == gdcm::PixelFormat::INT8) - { - m_type = CV_8SC1; - } - else if( ePixelFormat == gdcm::PixelFormat::UINT8) - { - m_type = CV_8UC1; - } - else if( ePixelFormat == gdcm::PixelFormat::INT16) - { - m_type = CV_16SC1; - } - else if( ePixelFormat == gdcm::PixelFormat::UINT16) - { - m_type = CV_16UC1; - } - else if( ePixelFormat == gdcm::PixelFormat::INT32) - { - m_type = CV_32SC1; - } - else if( ePixelFormat == gdcm::PixelFormat::FLOAT32) - { - m_type = CV_32FC1; - } - else if( ePixelFormat == gdcm::PixelFormat::FLOAT64) - { - m_type = CV_64FC1; - } - else if( ePixelFormat == gdcm::PixelFormat::INT12) - { - bOK = false; - } - else if( ePixelFormat == gdcm::PixelFormat::UINT12) - { - bOK = false; - } - else if( ePixelFormat == gdcm::PixelFormat::UINT32) - { - bOK = false; + 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; } - else if( ePixelFormat == gdcm::PixelFormat::SINGLEBIT) + + case gdcm::PhotometricInterpretation::RGB: { - bOK = false; + 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; } - else + + default: { bOK = false; + DBG("GDCM | PI not supported: %s\n", csImage.GetPhotometricInterpretation().GetString()); + break; } } - else if( csImage.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::RGB ) + + if(bOK) { - gdcm::PixelFormat ePixelFormat = csImage.GetPixelFormat(); - if( ePixelFormat == gdcm::PixelFormat::UINT8) - { - m_type = CV_8UC3; - } - else + unsigned int ndim = csImage.GetNumberOfDimensions(); + if (ndim != 2) { + DBG("GDCM | Invalid dimensions number: %d\n", ndim); bOK = false; } } - else - { - bOK = false; - } - - if(bOK) + if (bOK) { const unsigned int *piDimension = csImage.GetDimensions(); - m_width = piDimension[0]; - m_height = piDimension[1]; + 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; } } @@ -206,15 +170,28 @@ bool DICOMDecoder::readData( Mat& csImage ) csImageReader.SetFileName(m_filename.c_str()); if(!csImageReader.Read()) { - return(false); + DBG("GDCM | Failed to Read\n"); + return false; } - bool bOK = true; - const gdcm::Image &csGDCMImage = csImageReader.GetImage(); - bOK = csGDCMImage.GetBuffer((char*)csImage.ptr()); + const gdcm::Image &img = csImageReader.GetImage(); - return(bOK); + 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 \ No newline at end of file +#endif // HAVE_GDCM \ No newline at end of file diff --git a/modules/imgcodecs/src/grfmt_gdcm.hpp b/modules/imgcodecs/src/grfmt_gdcm.hpp index 8a8e10032c..d8dc60f522 100644 --- a/modules/imgcodecs/src/grfmt_gdcm.hpp +++ b/modules/imgcodecs/src/grfmt_gdcm.hpp @@ -56,22 +56,15 @@ namespace cv class DICOMDecoder : public BaseImageDecoder { public: - DICOMDecoder(); - ~DICOMDecoder(); - bool readData( Mat& img ); bool readHeader(); - void close(); - ImageDecoder newDecoder() const; virtual bool checkSignature( const String& signature ) const; - -protected: }; } -#endif +#endif // HAVE_GDCM #endif/*_GDCM_DICOM_H_*/