DicomIO.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "DicomIO.h"
  2. #include <dcmtk/config/osconfig.h>
  3. #include <dcmtk/dcmdata/dcfilefo.h>
  4. #include <dcmtk/dcmdata/dcdeftag.h>
  5. #include <dcmtk/dcmdata/dcstack.h>
  6. #include <dcmtk/dcmdata/dcpixel.h>
  7. #include <dcmtk/dcmdata/dcxfer.h>
  8. #include <dcmtk/dcmjpeg/djdecode.h>
  9. #include <dcmtk/dcmimgle/dcmimage.h>
  10. #include <dcmtk/dcmimage/diregist.h>
  11. #include <dcmtk/dcmdata/dcrledrg.h>
  12. #include <dcmtk/dcmdata/dcuid.h>
  13. #include <sstream>
  14. #include <cmath>
  15. DicomIO::DicomIO()
  16. : Singleton< DicomIO >(),
  17. m_calibrated( false )
  18. {
  19. DcmRLEDecoderRegistration::registerCodecs();
  20. DJDecoderRegistration::registerCodecs();
  21. }
  22. DicomIO::~DicomIO()
  23. {
  24. DJDecoderRegistration::cleanup();
  25. DcmRLEDecoderRegistration::cleanup();
  26. }
  27. DicomIO::DicomIOType DicomIO::getType( std::string fileName )
  28. {
  29. DcmFileFormat fileFormat;
  30. OFCondition status = fileFormat.loadFile( fileName.c_str() );
  31. if ( status.bad() )
  32. {
  33. return DicomIO::DicomIOUnknown;
  34. }
  35. DcmDataset* dataset = fileFormat.getDataset();
  36. OFString SOPClassUID;
  37. if ( dataset->findAndGetOFString( DCM_SOPClassUID, SOPClassUID ).bad() )
  38. {
  39. return DicomIO::DicomIOUnknown;
  40. }
  41. if ( !SOPClassUID.compare( UID_UltrasoundImageStorage ) ||
  42. !SOPClassUID.compare( UID_SecondaryCaptureImageStorage ) )
  43. {
  44. return DicomIO::DicomIOImage;
  45. }
  46. if ( !SOPClassUID.compare( UID_UltrasoundMultiframeImageStorage ) )
  47. {
  48. return DicomIO::DicomIOMultiFrame;
  49. }
  50. return DicomIO::DicomIOUnmanaged;
  51. }
  52. bool DicomIO::readResolutions( DcmDataset* dataset,
  53. double& sizeX,
  54. double& sizeY )
  55. {
  56. if ( !dataset )
  57. {
  58. return false;
  59. }
  60. int nScales = 0;
  61. Float64 tmpFD = 0.0;
  62. if ( dataset->findAndGetFloat64( DCM_PixelSpacing, tmpFD, 0 ).good() )
  63. {
  64. sizeX = (double)tmpFD;
  65. nScales++;
  66. }
  67. if ( dataset->findAndGetFloat64( DCM_PixelSpacing, tmpFD, 1 ).good() )
  68. {
  69. sizeY = (double)tmpFD;
  70. nScales++;
  71. }
  72. if ( nScales != 2 )
  73. {
  74. nScales = 0;
  75. OFString SOPClassUID;
  76. if ( dataset->findAndGetOFString( DCM_SOPClassUID, SOPClassUID ).bad() )
  77. {
  78. return false;
  79. }
  80. if ( !SOPClassUID.compare( UID_UltrasoundImageStorage ) ||
  81. !SOPClassUID.compare( UID_UltrasoundMultiframeImageStorage ) )
  82. {
  83. DcmSequenceOfItems* seq = NULL;
  84. if ( dataset->findAndGetSequence( DCM_SequenceOfUltrasoundRegions,
  85. seq ).good() )
  86. {
  87. Uint16 type;
  88. bool found = false;
  89. unsigned long i, nItems = seq->card();
  90. for ( i = 0; !found && ( i < nItems ); i++ )
  91. {
  92. DcmItem* item = seq->getItem( i );
  93. if ( item->findAndGetUint16( DCM_RegionSpatialFormat, type ).good() )
  94. {
  95. if ( ( type == 1 ) || ( type == 2 ) )
  96. {
  97. found = true;
  98. if ( item->findAndGetFloat64( DCM_PhysicalDeltaX, tmpFD ).good() )
  99. {
  100. sizeX = 10.0 * fabs( tmpFD );
  101. nScales++;
  102. }
  103. if ( item->findAndGetFloat64( DCM_PhysicalDeltaY, tmpFD ).good() )
  104. {
  105. sizeY = 10.0 * fabs( tmpFD );
  106. nScales++;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return ( nScales == 2 ) ? true : false;
  115. }
  116. bool DicomIO::read( std::string fileName, ExtendedImage& image )
  117. {
  118. DcmFileFormat fileFormat;
  119. OFCondition status = fileFormat.loadFile( fileName.c_str() );
  120. m_calibrated = false;
  121. if ( status.bad() )
  122. {
  123. return false;
  124. }
  125. DcmDataset* dataset = fileFormat.getDataset();
  126. double sizeX = -1.0, sizeY = -1.0;
  127. m_calibrated = readResolutions( dataset, sizeX, sizeY );
  128. E_TransferSyntax xfer = dataset->getOriginalXfer();
  129. /*** hack for Medison jpeg encoded images ***/
  130. OFString manufacturer;
  131. if ( dataset->findAndGetOFString( DCM_Manufacturer, manufacturer ).good() )
  132. {
  133. if ( !manufacturer.compare( "MEDISON" ) &&
  134. ( xfer >= EXS_JPEGProcess1TransferSyntax ) &&
  135. ( xfer <= EXS_JPEGProcess14SV1TransferSyntax ) )
  136. {
  137. dataset->putAndInsertString( DCM_PhotometricInterpretation, "YBR_FULL" );
  138. }
  139. }
  140. /*** end of hack ***/
  141. DicomImage dcmImage( dataset, xfer );
  142. if ( dcmImage.getStatus() != EIS_Normal )
  143. {
  144. return false;
  145. }
  146. dcmImage.setMinMaxWindow();
  147. if ( !image.IsNull() )
  148. {
  149. image.Destroy();
  150. }
  151. int w = dcmImage.getWidth();
  152. int h = dcmImage.getHeight();
  153. if ( !image.Create( w, h, 24 ) )
  154. {
  155. return false;
  156. }
  157. unsigned char* p = (unsigned char*)dcmImage.getOutputData( 8 );
  158. unsigned char* l = (unsigned char*)image.GetBuffer();
  159. int n = w * h;
  160. if ( dcmImage.isMonochrome() )
  161. {
  162. while ( n-- )
  163. {
  164. *l++ = *p;
  165. *l++ = *p;
  166. *l++ = *p++;
  167. }
  168. }
  169. else
  170. {
  171. while ( n-- )
  172. {
  173. *l++ = *( p + 2 );
  174. *l++ = *( p + 1 );
  175. *l++ = *p;
  176. p += 3;
  177. }
  178. }
  179. image.SetResolution( (double)sizeX, (double)sizeY );
  180. return true;
  181. }
  182. bool DicomIO::read( std::string fileName, Video& video )
  183. {
  184. DcmFileFormat fileFormat;
  185. OFCondition status = fileFormat.loadFile( fileName.c_str() );
  186. m_calibrated = false;
  187. if ( status.bad() )
  188. {
  189. return false;
  190. }
  191. DcmDataset* dataset = fileFormat.getDataset();
  192. double sizeX = -1.0, sizeY = -1.0;
  193. m_calibrated = readResolutions( dataset, sizeX, sizeY );
  194. E_TransferSyntax xfer = dataset->getOriginalXfer();
  195. /*** hack for Medison jpeg encoded images ***/
  196. OFString manufacturer;
  197. if ( dataset->findAndGetOFString( DCM_Manufacturer, manufacturer ).good() )
  198. {
  199. if ( !manufacturer.compare( "MEDISON" ) &&
  200. ( xfer >= EXS_JPEGProcess1TransferSyntax ) &&
  201. ( xfer <= EXS_JPEGProcess14SV1TransferSyntax ) )
  202. {
  203. dataset->putAndInsertString( DCM_PhotometricInterpretation, "YBR_FULL" );
  204. }
  205. }
  206. /*** end of hack ***/
  207. DicomImage dcmImage( dataset, xfer );
  208. if ( dcmImage.getStatus() != EIS_Normal )
  209. {
  210. return false;
  211. }
  212. dcmImage.setMinMaxWindow();
  213. if ( !video.IsNull() )
  214. {
  215. video.Clear();
  216. }
  217. unsigned long f, nf = dcmImage.getFrameCount();
  218. bool isMonochrome = dcmImage.isMonochrome() ? true : false;
  219. video.SetSize( dcmImage.getWidth(), dcmImage.getHeight() );
  220. for ( f = 0; f < nf; f++ )
  221. {
  222. unsigned char* p = (unsigned char*)dcmImage.getOutputData( 8 );
  223. video.AddFrame( p, isMonochrome );
  224. }
  225. video.SetResolution( (double)sizeX, (double)sizeY );
  226. return true;
  227. }
  228. bool DicomIO::write( std::string fileName, ExtendedImage &image )
  229. {
  230. char uid[ 100 ];
  231. DcmFileFormat fileFormat;
  232. DcmDataset* dataset = fileFormat.getDataset();
  233. // SOP common
  234. dataset->putAndInsertString( DCM_SOPClassUID,
  235. UID_SecondaryCaptureImageStorage );
  236. dataset->putAndInsertString( DCM_SOPInstanceUID,
  237. dcmGenerateUniqueIdentifier( uid,
  238. SITE_INSTANCE_UID_ROOT ) );
  239. // Patient
  240. dataset->insertEmptyElement( DCM_PatientsName );
  241. dataset->insertEmptyElement( DCM_PatientID );
  242. dataset->insertEmptyElement( DCM_PatientsBirthDate );
  243. dataset->insertEmptyElement( DCM_PatientsSex );
  244. // Study
  245. dataset->putAndInsertString( DCM_StudyInstanceUID,
  246. dcmGenerateUniqueIdentifier( uid,
  247. SITE_STUDY_UID_ROOT ));
  248. dataset->insertEmptyElement( DCM_StudyDate );
  249. dataset->insertEmptyElement( DCM_StudyTime );
  250. dataset->insertEmptyElement( DCM_ReferringPhysiciansName );
  251. // Series
  252. dataset->putAndInsertString( DCM_Modality, "US" );
  253. dataset->putAndInsertString( DCM_SeriesInstanceUID,
  254. dcmGenerateUniqueIdentifier( uid,
  255. SITE_SERIES_UID_ROOT ));
  256. dataset->insertEmptyElement( DCM_SeriesNumber );
  257. // Equipment
  258. dataset->putAndInsertString( DCM_ConversionType, "WSD" );
  259. // Image
  260. Uint16 spp = 3;
  261. Uint16 bitsAlloc = 8;
  262. Uint16 bitsStored = 8;
  263. Uint16 highBits = 7;
  264. std::string photometric( "RGB" );
  265. switch ( image.GetBpp() )
  266. {
  267. case 8:
  268. spp = 1;
  269. bitsAlloc = 8;
  270. bitsStored = 8;
  271. highBits = 7;
  272. photometric = "MONOCHROME2";
  273. break;
  274. case 16:
  275. spp = 1;
  276. bitsAlloc = 16;
  277. bitsStored = 16;
  278. highBits = 15;
  279. photometric = "MONOCHROME2";
  280. break;
  281. case 24:
  282. spp = 3;
  283. bitsAlloc = 8;
  284. bitsStored = 8;
  285. highBits = 7;
  286. photometric = "RGB";
  287. break;
  288. default:
  289. return false;
  290. break;
  291. }
  292. Uint16 dx = image.GetWidth();
  293. Uint16 dy = image.GetHeight();
  294. dataset->putAndInsertString( DCM_InstanceNumber, "1" );
  295. dataset->putAndInsertUint16( DCM_SamplesPerPixel, spp );
  296. dataset->putAndInsertString( DCM_PhotometricInterpretation,
  297. photometric.c_str() );
  298. dataset->putAndInsertUint16( DCM_Rows, dy );
  299. dataset->putAndInsertUint16( DCM_Columns, dx );
  300. dataset->putAndInsertUint16( DCM_BitsAllocated, bitsAlloc );
  301. dataset->putAndInsertUint16( DCM_BitsStored, bitsStored );
  302. dataset->putAndInsertUint16( DCM_HighBit, highBits );
  303. dataset->putAndInsertUint16( DCM_PixelRepresentation, 0 );
  304. dataset->putAndInsertUint16( DCM_PlanarConfiguration, 0 );
  305. if ( image.HasCalibration() )
  306. {
  307. std::ostringstream pixelSpacingX, pixelSpacingY;
  308. pixelSpacingX << image.GetResolutionX();
  309. std::string str = pixelSpacingX.str();
  310. if ( str.length() > 16 )
  311. {
  312. str.resize( 16 );
  313. }
  314. str += "\\";
  315. pixelSpacingY << image.GetResolutionY();
  316. std::string str2 = pixelSpacingY.str();
  317. if ( str2.length() > 16 )
  318. {
  319. str2.resize( 16 );
  320. }
  321. str += str2;
  322. dataset->putAndInsertString( DCM_PixelSpacing, str.c_str() );
  323. }
  324. unsigned long n = dx * dy * image.GetBpp();
  325. dataset->putAndInsertUint8Array( DCM_PixelData,
  326. (Uint8*)image.GetBuffer(), n );
  327. OFCondition status = fileFormat.saveFile( fileName.c_str(),
  328. EXS_LittleEndianExplicit );
  329. if ( status.bad() )
  330. {
  331. return false;
  332. }
  333. return true;
  334. }
  335. bool DicomIO::HasCalibration()
  336. {
  337. return m_calibrated;
  338. }