DicomIO.cpp 10 KB

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