]> Creatis software - gdcm.git/blob - Example/Volume2Dicom.cxx
Normalization
[gdcm.git] / Example / Volume2Dicom.cxx
1 /*=========================================================================
2                                                                                  
3   Program:   gdcm
4   Module:    $RCSfile: Volume2Dicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/02 10:06:32 $
7   Version:   $Revision: 1.6 $
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, 
45                         std::string &date, std::string &time);
46
47 int main( int argc, char *argv[] )
48 {
49    if (argc < 2) 
50    {
51       std::cerr << argv[0] << USAGE << std::endl;
52       return 1;
53    }    
54
55    //const char *inputfile = argv[1];
56    std::string directory = argv[1];
57 //   itksys::SystemTools::ConvertToUnixSlashes( directory );
58    if (directory[directory.size()-1] != '/') 
59       directory += '/';
60
61    std::cout << "Converting image into dicom in " << directory << std::endl;
62
63     ////////////////////////////////////////////////////////////
64     // Reading input image and getting some information       //
65     ////////////////////////////////////////////////////////////
66     //std::cout << "Loading image " << inputfile << std::endl;
67     //PixelType* imageData = input->GetPixelContainer()->GetImportPointer();
68     uint8_t *imageData = new uint8_t[256*256*10];
69     memset( imageData, 0, 256*256*10);
70     std::cout << "Image Loaded." << std::endl;
71
72     int   sizex      = 256;
73     int   sizey      = 256;
74     int   sizez      = 10;
75     //float spacing[3] = { 1.0, 1.0, 1.5 };
76     //float orig[3]    = { 0.0, 0.0, 0.0 };
77     int   sliceSize  = sizex*sizey*sizeof(uint8_t);
78
79     ////////////////////////////////////////////////////////////
80     // compute window center and window width                 //
81     ////////////////////////////////////////////////////////////
82     uint8_t min, max; min = max = imageData[0];
83     for (int i=1; i<sizex*sizey*sizez; i++) 
84     {
85        uint8_t val = imageData[i];
86        if (val > max) 
87           max = val;
88        if (val < min) 
89           min = val;
90     }
91     //float wcenter = (max+min) / 2.;
92     //float wwidth  = (max-min)>0 ? (max-min) : 1.;
93
94     ////////////////////////////////////////////////////////////
95     // Get file date and time                                 //
96     ////////////////////////////////////////////////////////////
97     std::string filedate, filetime;    
98     //GetFileDateAndTime(inputfile, filedate, filetime);
99
100     ////////////////////////////////////////////////////////////
101     // Create a new dicom header and fill in some info        //
102     ////////////////////////////////////////////////////////////
103     gdcm::File *h1 = new gdcm::File();
104
105     //h1->SetDateAndTime(filedate, filetime);
106     //h1->SetModality("CT");
107     //h1->SetPatientName( "TestPatient");
108     //h1->SetPatientID( "TestID");
109     //h1->SetStudyID( "1");
110     //h1->SetSeriesNumber( "1");
111     //h1->SetSliceThickness(spacing[2]);
112     //h1->SetSpaceBetweenSlices(spacing[2]);
113     //h1->SetXYSpacing( spacing[0], spacing[1]);
114     //h1->SetXSize(sizex);
115     //h1->SetYSize(sizey);
116     //h1->SetNbBitsAllocated(16);
117     //h1->SetNbBitsStored(12);
118     //h1->SetNbBitsStored(12);
119     //h1->SetPixelSigned(true);
120     //h1->SetCenter( wcenter);
121     //h1->SetWindow( wwidth);
122
123     ////////////////////////////////////////////////////////////
124     // Create a new dicom file object from the header         //
125     ////////////////////////////////////////////////////////////
126     gdcm::FileHelper  *f1 = new gdcm::FileHelper(h1);
127     uint8_t *myData = f1->GetImageData(); // Get an Image pointer
128     f1->SetImageData( myData, sliceSize); // This callback ensures that the internal
129                                           // Pixel_Data of f1 is set correctly
130
131     
132     ////////////////////////////////////////////////////////////
133     // Iterate through the slices and save them to file       //
134     ////////////////////////////////////////////////////////////
135     for (int z=0; z<sizez; z++) 
136     {
137        // Set dicom relevant information for that slice
138        //h1->SetImageUIDFromSliceNumber(z);
139        //h1->SetImageLocation(orig[0],orig[1],orig[2]+z*spacing[2]);
140
141        // copy image slice content
142        memcpy(myData,imageData+z*sizex*sizey,sliceSize);
143
144        // write the image
145        std::string filename = directory + gdcm::Util::Format("%Image_%05d.dcm", z);
146        std::cout << "Writing file " << filename;
147        f1->WriteDcmExplVR(filename);
148        std::cout << " OK" << std::endl;
149     }
150
151     ////////////////////////////////////////////////////////////
152     // Free the allocated objects                             //
153     ////////////////////////////////////////////////////////////
154     // delete f1; // FIXME: these calls sometimes crashes under .NET ????
155     // delete h1;
156
157     return 0;
158 }
159
160
161 // just an utility function to retrieve date and time of a file
162 void GetFileDateAndTime(const char *inputfile, std::string &date, 
163                                                std::string &time)
164 {
165    struct stat buf;    
166    if (stat(inputfile, &buf) == 0) 
167    {
168       char tmp[512];
169
170       strftime(tmp,512,"%Y%m%d", localtime(&buf.st_mtime) );
171       date = tmp;
172
173       strftime(tmp,512,"%H%M%S", localtime(&buf.st_mtime) );
174       time = tmp;
175    }
176    else
177    {
178       date = "20040101";
179       time = "120000";
180    }
181 }