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