1 /*=========================================================================
2 
3   Program: GDCM (Grassroots DICOM). A DICOM library
4 
5   Copyright (c) 2006-2011 Mathieu Malaterre
6   All rights reserved.
7   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 
9      This software is distributed WITHOUT ANY WARRANTY; without even
10      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11      PURPOSE.  See the above copyright notice for more information.
12 
13 =========================================================================*/
14 /*
15  * iU22 Raw Data extractor
16  */
17 #include "gdcmReader.h"
18 #include "gdcmImageWriter.h"
19 #include "gdcmAttribute.h"
20 #include "gdcmPrivateTag.h"
21 
22 #include <math.h>
23 
main(int argc,char * argv[])24 int main(int argc, char *argv [])
25 {
26   if( argc < 2 ) return 1;
27   // IM_001
28   const char *filename = argv[1];
29 
30   gdcm::Reader reader; // Do not use ImageReader
31   reader.SetFileName( filename );
32   if( !reader.Read() )
33     {
34     std::cerr << "Failed to read: " << filename << std::endl;
35     return 1;
36     }
37 
38  // * The data is simply 8-bit unsigned in the obvious x/y/z order
39  // * 200D,300B contains the data
40  // * 200D,3001 contains the no. of voxels (416,412,256 in this case)
41  // * 200D,3003 contains the voxel sizes (0.156184527398215 /
42  // 0.1223749613981957 / 0.328479990704639 in this case)
43 
44   const gdcm::File &file = reader.GetFile();
45   const gdcm::DataSet &ds = file.GetDataSet();
46   const gdcm::PrivateTag trawdataus( 0x200d, 0x0b, "Philips US Imaging DD 033" );
47   const gdcm::DataElement &rawdataus = ds.GetDataElement( trawdataus );
48 
49   const gdcm::PrivateTag tcolsrowsframes( 0x200d, 0x01, "Philips US Imaging DD 036" );
50   const gdcm::DataElement &colsrowsframes = ds.GetDataElement( tcolsrowsframes );
51   // const gdcm::PrivateTag tcolsrowsframes( 0x200d, 0x02, "Philips US Imaging DD 036" );
52   // this is just a duplicate previous tag.
53   const gdcm::PrivateTag tvoxelspacing( 0x200d, 0x03, "Philips US Imaging DD 036" );
54   const gdcm::DataElement &voxelspacing = ds.GetDataElement( tvoxelspacing );
55 
56   gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> dims; // Use DS to interpret value stored in LO
57   dims.SetFromDataElement( colsrowsframes );
58 
59   gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> spacing;
60   spacing.SetFromDataElement( voxelspacing );
61 
62   gdcm::ImageWriter writer;
63 
64   gdcm::Image &image = writer.GetImage();
65   image.SetNumberOfDimensions( 3 ); // good default
66   image.SetDimension(0, (unsigned int)dims[0] );
67   image.SetDimension(1, (unsigned int)dims[1] );
68   image.SetDimension(2, (unsigned int)dims[2] );
69   image.SetSpacing(0, spacing[0] );
70   image.SetSpacing(1, spacing[1] );
71   image.SetSpacing(2, spacing[2] );
72   gdcm::PixelFormat pixeltype = gdcm::PixelFormat::UINT8;
73 
74   gdcm::PhotometricInterpretation pi;
75   pi = gdcm::PhotometricInterpretation::MONOCHROME2;
76   image.SetPhotometricInterpretation( pi );
77   image.SetPixelFormat( pixeltype );
78 
79   image.SetDataElement( rawdataus );
80 
81   std::string outfilename = "outiu22.dcm";
82 
83   gdcm::DataElement de( gdcm::Tag(0x8,0x16) ); // SOP Class UID
84   de.SetVR( gdcm::VR::UI );
85   gdcm::MediaStorage ms(
86     gdcm::MediaStorage::UltrasoundMultiFrameImageStorage );
87 //    gdcm::MediaStorage::MultiframeGrayscaleByteSecondaryCaptureImageStorage );
88   de.SetByteValue( ms.GetString(), (uint32_t)strlen(ms.GetString()));
89   writer.GetFile().GetDataSet().Replace( de );
90 
91   writer.SetFileName( outfilename.c_str() );
92   if( !writer.Write() )
93     {
94     std::cerr << "could not write: " << outfilename << std::endl;
95     return 1;
96     }
97 
98 
99   return 0;
100 }
101