]> Creatis software - gdcm.git/blob - Example/Volume2Dicom.cxx
* Example/Volume2Dicom.cxx : commit with the good format
[gdcm.git] / Example / Volume2Dicom.cxx
1 /*=========================================================================\r
2                                                                                  \r
3   Program:   gdcm\r
4   Module:    $RCSfile: Volume2Dicom.cxx,v $\r
5   Language:  C++\r
6   Date:      $Date: 2004/12/04 08:46:10 $\r
7   Version:   $Revision: 1.1 $\r
8                                                                                  \r
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de\r
10   l'Image). All rights reserved. See Doc/License.txt or\r
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.\r
12                                                                                  \r
13      This software is distributed WITHOUT ANY WARRANTY; without even\r
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
15      PURPOSE.  See the above copyright notices for more information.\r
16                                                                                  \r
17 =========================================================================*/\r
18 \r
19 /**\r
20  * This example was proposed by Jean-Michel Rouet\r
21  * It was patch by Mathieu Malaterre to remove ITK reference and be more\r
22  * independant from other toolkit\r
23  * It's aim is to show people how to write their data volume into DICOM slices\r
24  */\r
25 \r
26 #include "gdcmHeader.h"\r
27 #include "gdcmDocEntry.h"\r
28 #include "gdcmBinEntry.h"\r
29 #include "gdcmFile.h"\r
30 #include "gdcmUtil.h"\r
31 \r
32 #define USAGE "USAGE: Input3DImage OutputDirectory"\r
33 \r
34 #include <time.h>\r
35 #include <sys/types.h>\r
36 #include <sys/stat.h>\r
37 #ifdef WIN32\r
38 #define stat _stat\r
39 #endif\r
40 \r
41 const unsigned int Dimension = 3;\r
42 \r
43 void gdcmwrite(const char *inputfile, std::string directory);\r
44 void GetFileDateAndTime(const char *inputfile, std::string &date, std::string &time);\r
45 \r
46 int main( int argc, char * argv[] )\r
47 {\r
48    \r
49    if (argc < 2) \r
50    {\r
51       std::cerr << argv[0] << USAGE << std::endl;\r
52       return 1;\r
53    }    \r
54 \r
55    //const char *inputfile = argv[1];\r
56    std::string directory = argv[1];\r
57 //   itksys::SystemTools::ConvertToUnixSlashes( directory );\r
58    if (directory[directory.size()-1] != '/') directory += '/';\r
59 \r
60    std::cout << "Converting image into dicom in " << directory << std::endl;\r
61 \r
62     ////////////////////////////////////////////////////////////\r
63     // Reading input image and getting some information       //\r
64     ////////////////////////////////////////////////////////////\r
65     //std::cout << "Loading image " << inputfile << std::endl;\r
66     //PixelType* imageData = input->GetPixelContainer()->GetImportPointer();\r
67     uint8_t *imageData = new uint8_t[256*256*10];\r
68     memset( imageData, 0, 256*256*10);\r
69     std::cout << "Image Loaded." << std::endl;\r
70 \r
71     int   sizex      = 256;\r
72     int   sizey      = 256;\r
73     int   sizez      = 10;\r
74     float spacing[3] = { 1.0, 1.0, 1.5 };\r
75     float orig[3]    = { 0.0, 0.0, 0.0 };\r
76     int   sliceSize  = sizex*sizey*sizeof(uint8_t);\r
77 \r
78     ////////////////////////////////////////////////////////////\r
79     // compute window center and window width                 //\r
80     ////////////////////////////////////////////////////////////\r
81     uint8_t min, max; min = max = imageData[0];\r
82     for (int i=1; i<sizex*sizey*sizez; i++) \r
83     {\r
84        uint8_t val = imageData[i];\r
85        if (val > max) max = val;\r
86        if (val < min) min = val;\r
87     }\r
88     float wcenter = (max+min) / 2.;\r
89     float wwidth  = (max-min)>0 ? (max-min) : 1.;\r
90 \r
91     ////////////////////////////////////////////////////////////\r
92     // Get file date and time                                 //\r
93     ////////////////////////////////////////////////////////////\r
94     std::string filedate, filetime;    \r
95     //GetFileDateAndTime(inputfile, filedate, filetime);\r
96 \r
97     ////////////////////////////////////////////////////////////\r
98     // Create a new dicom header and fill in some info        //\r
99     ////////////////////////////////////////////////////////////\r
100     gdcm::Header *h1 = new gdcm::Header();\r
101 \r
102     //h1->SetDateAndTime(filedate, filetime);\r
103     //h1->SetModality("CT");\r
104     //h1->SetPatientName( "TestPatient");\r
105     //h1->SetPatientID( "TestID");\r
106     //h1->SetStudyID( "1");\r
107     //h1->SetSeriesNumber( "1");\r
108     //h1->SetSliceThickness(spacing[2]);\r
109     //h1->SetSpaceBetweenSlices(spacing[2]);\r
110     //h1->SetXYSpacing( spacing[0], spacing[1]);\r
111     //h1->SetXSize(sizex);\r
112     //h1->SetYSize(sizey);\r
113     //h1->SetNbBitsAllocated(16);\r
114     //h1->SetNbBitsStored(12);\r
115     //h1->SetNbBitsStored(12);\r
116     //h1->SetPixelSigned(true);\r
117     //h1->SetCenter( wcenter);\r
118     //h1->SetWindow( wwidth);\r
119 \r
120     ////////////////////////////////////////////////////////////\r
121     // Create a new dicom file object from the header         //\r
122     ////////////////////////////////////////////////////////////\r
123     gdcm::File  *f1 = new gdcm::File(h1);\r
124     uint8_t *myData = f1->GetImageData(); // Get an Image pointer\r
125     f1->SetImageData( myData, sliceSize); // This callback ensures that the internal\r
126                                           // Pixel_Data of f1 is set correctly\r
127 \r
128     \r
129     ////////////////////////////////////////////////////////////\r
130     // Iterate through the slices and save them to file       //\r
131     ////////////////////////////////////////////////////////////\r
132     for (int z=0; z<sizez; z++) \r
133     {\r
134        // Set dicom relevant information for that slice\r
135        //h1->SetImageUIDFromSliceNumber(z);\r
136        //h1->SetImageLocation(orig[0],orig[1],orig[2]+z*spacing[2]);\r
137 \r
138        // copy image slice content\r
139        memcpy(myData,imageData+z*sizex*sizey,sliceSize);\r
140 \r
141        // write the image\r
142        std::string filename = directory + gdcm::Util::Format("%Image_%05d.dcm", z);\r
143        std::cout << "Writing file " << filename;\r
144        f1->WriteDcmExplVR(filename);\r
145        std::cout << " OK" << std::endl;\r
146     }\r
147 \r
148     ////////////////////////////////////////////////////////////\r
149     // Free the allocated objects                             //\r
150     ////////////////////////////////////////////////////////////\r
151     // delete f1; // FIXME: these calls sometimes crashes under .NET ????\r
152     // delete h1;\r
153 }\r
154 \r
155 \r
156 // just an utility function to retrieve date and time of a file\r
157 void GetFileDateAndTime(const char *inputfile, std::string &date, std::string &time)\r
158 {\r
159    struct stat buf;    \r
160    if (stat(inputfile, &buf) == 0) \r
161    {\r
162       char tmp[512];\r
163 \r
164       strftime(tmp,512,"%Y%m%d", localtime(&buf.st_mtime) );\r
165       date = tmp;\r
166 \r
167       strftime(tmp,512,"%H%M%S", localtime(&buf.st_mtime) );\r
168       time = tmp;\r
169    }\r
170    else\r
171    {\r
172       date = "20040101";\r
173       time = "120000";\r
174    }\r
175 }\r
176 \r