grfmt_gdcm.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "precomp.hpp"
  43. #include "grfmt_gdcm.hpp"
  44. #ifdef HAVE_GDCM
  45. //#define DBG(...) printf(__VA_ARGS__)
  46. #define DBG(...)
  47. #include <gdcmImageReader.h>
  48. static const size_t preamble_skip = 128;
  49. static const size_t magic_len = 4;
  50. inline cv::String getMagic()
  51. {
  52. return cv::String("\x44\x49\x43\x4D", 4);
  53. }
  54. namespace cv
  55. {
  56. /************************ DICOM decoder *****************************/
  57. DICOMDecoder::DICOMDecoder()
  58. {
  59. // DICOM preamble is 128 bytes (can have any value, defaults to 0) + 4 bytes magic number (DICM)
  60. m_signature = String(preamble_skip, (char)'\x0') + getMagic();
  61. m_buf_supported = false;
  62. }
  63. bool DICOMDecoder::checkSignature( const String& signature ) const
  64. {
  65. if (signature.size() >= preamble_skip + magic_len)
  66. {
  67. if (signature.substr(preamble_skip, magic_len) == getMagic())
  68. {
  69. return true;
  70. }
  71. }
  72. DBG("GDCM | Signature does not match\n");
  73. return false;
  74. }
  75. ImageDecoder DICOMDecoder::newDecoder() const
  76. {
  77. return makePtr<DICOMDecoder>();
  78. }
  79. bool DICOMDecoder::readHeader()
  80. {
  81. gdcm::ImageReader csImageReader;
  82. csImageReader.SetFileName(m_filename.c_str());
  83. if(!csImageReader.Read())
  84. {
  85. DBG("GDCM | Failed to open DICOM file\n");
  86. return(false);
  87. }
  88. const gdcm::Image &csImage = csImageReader.GetImage();
  89. bool bOK = true;
  90. switch (csImage.GetPhotometricInterpretation().GetType())
  91. {
  92. case gdcm::PhotometricInterpretation::MONOCHROME1:
  93. case gdcm::PhotometricInterpretation::MONOCHROME2:
  94. {
  95. switch (csImage.GetPixelFormat().GetScalarType())
  96. {
  97. case gdcm::PixelFormat::INT8: m_type = CV_8SC1; break;
  98. case gdcm::PixelFormat::UINT8: m_type = CV_8UC1; break;
  99. case gdcm::PixelFormat::INT16: m_type = CV_16SC1; break;
  100. case gdcm::PixelFormat::UINT16: m_type = CV_16UC1; break;
  101. case gdcm::PixelFormat::INT32: m_type = CV_32SC1; break;
  102. case gdcm::PixelFormat::FLOAT32: m_type = CV_32FC1; break;
  103. case gdcm::PixelFormat::FLOAT64: m_type = CV_64FC1; break;
  104. default: bOK = false; DBG("GDCM | Monochrome scalar type not supported\n"); break;
  105. }
  106. break;
  107. }
  108. case gdcm::PhotometricInterpretation::RGB:
  109. {
  110. switch (csImage.GetPixelFormat().GetScalarType())
  111. {
  112. case gdcm::PixelFormat::UINT8: m_type = CV_8UC3; break;
  113. default: bOK = false; DBG("GDCM | RGB scalar type not supported\n"); break;
  114. }
  115. break;
  116. }
  117. default:
  118. {
  119. bOK = false;
  120. DBG("GDCM | PI not supported: %s\n", csImage.GetPhotometricInterpretation().GetString());
  121. break;
  122. }
  123. }
  124. if(bOK)
  125. {
  126. unsigned int ndim = csImage.GetNumberOfDimensions();
  127. if (ndim != 2)
  128. {
  129. DBG("GDCM | Invalid dimensions number: %d\n", ndim);
  130. bOK = false;
  131. }
  132. }
  133. if (bOK)
  134. {
  135. const unsigned int *piDimension = csImage.GetDimensions();
  136. m_height = piDimension[0];
  137. m_width = piDimension[1];
  138. if( ( m_width <=0 ) || ( m_height <=0 ) )
  139. {
  140. DBG("GDCM | Invalid dimensions: %d x %d\n", piDimension[0], piDimension[1]);
  141. bOK = false;
  142. }
  143. }
  144. return(bOK);
  145. }
  146. bool DICOMDecoder::readData( Mat& csImage )
  147. {
  148. csImage.create(m_width,m_height,m_type);
  149. gdcm::ImageReader csImageReader;
  150. csImageReader.SetFileName(m_filename.c_str());
  151. if(!csImageReader.Read())
  152. {
  153. DBG("GDCM | Failed to Read\n");
  154. return false;
  155. }
  156. const gdcm::Image &img = csImageReader.GetImage();
  157. unsigned long len = img.GetBufferLength();
  158. if (len > csImage.elemSize() * csImage.total())
  159. {
  160. DBG("GDCM | Buffer is bigger than Mat: %ld > %ld * %ld\n", len, csImage.elemSize(), csImage.total());
  161. return false;
  162. }
  163. if (!img.GetBuffer((char*)csImage.ptr()))
  164. {
  165. DBG("GDCM | Failed to GetBuffer\n");
  166. return false;
  167. }
  168. DBG("GDCM | Read OK\n");
  169. return true;
  170. }
  171. }
  172. #endif // HAVE_GDCM