]> Creatis software - gdcm.git/blob - Example/Volume2Dicom.cxx
Fix mistypings
[gdcm.git] / Example / Volume2Dicom.cxx
1 /*=========================================================================
2                                                                                  
3   Program:   gdcm
4   Module:    $RCSfile: Volume2Dicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/30 11:05:47 $
7   Version:   $Revision: 1.13 $
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 patched 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 "gdcmFileHelper.h"
29 #include "gdcmUtil.h"
30
31 #define USAGE "USAGE: Input3DImage OutputDirectory"
32
33 #include <time.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #ifdef _WIN32
37    #define stat _stat
38 #endif
39
40 #if defined(__BORLANDC__)
41  #include <mem.h> // for memset, memcpy
42 #endif 
43
44 //const unsigned int Dimension = 3;
45
46 void gdcmwrite(const char *inputfile, std::string directory);
47 void GetFileDateAndTime(const char *inputfile, 
48                         std::string &date, std::string &time);
49
50 int main( int argc, char *argv[] )
51 {
52    if (argc < 2) 
53    {
54       std::cerr << argv[0] << USAGE << std::endl;
55       return 1;
56    }    
57
58    //const char *inputfile = argv[1];
59    std::string directory = argv[1];
60    //   itksys::SystemTools::ConvertToUnixSlashes( directory );
61    if (directory[directory.size()-1] != '/') 
62    directory += '/';
63
64    std::cout << "Converting image into dicom in " << directory << std::endl;
65
66    ////////////////////////////////////////////////////////////
67    // Reading input image and getting some information       //
68    ////////////////////////////////////////////////////////////
69    //std::cout << "Loading image " << inputfile << std::endl;
70    //PixelType* imageData = input->GetPixelContainer()->GetImportPointer();
71    uint8_t *imageData = new uint8_t[256*256*10];
72    memset( imageData, 0, 256*256*10);
73    std::cout << "Image Loaded." << std::endl;
74
75    int   sizex      = 256;
76    int   sizey      = 256;
77    int   sizez      = 10;
78    //float spacing[3] = { 1.0, 1.0, 1.5 };
79    //float orig[3]    = { 0.0, 0.0, 0.0 };
80    int   sliceSize  = sizex*sizey*sizeof(uint8_t);
81
82    ////////////////////////////////////////////////////////////
83    // compute window center and window width                 //
84    ////////////////////////////////////////////////////////////
85    uint8_t min, max; min = max = imageData[0];
86    for (int i=1; i<sizex*sizey*sizez; i++) 
87    {
88       uint8_t val = imageData[i];
89       if (val > max) 
90          max = val;
91       if (val < min) 
92          min = val;
93    }
94    //float wcenter = (max+min) / 2.;
95    //float wwidth  = (max-min)>0 ? (max-min) : 1.;
96
97    ////////////////////////////////////////////////////////////
98    // Get file date and time                                 //
99    ////////////////////////////////////////////////////////////
100    std::string filedate, filetime;    
101    //GetFileDateAndTime(inputfile, filedate, filetime);
102
103    ////////////////////////////////////////////////////////////
104    // Create a new dicom header and fill in some info        //
105    ////////////////////////////////////////////////////////////
106    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
107
108    ////////////////////////////////////////////////////////////
109    // Create a new dicom file object from the header         //
110    ////////////////////////////////////////////////////////////
111    GDCM_NAME_SPACE::FileHelper  *fh = GDCM_NAME_SPACE::FileHelper::New(f);
112    uint8_t *myData = fh->GetImageData(); // Get an Image pointer
113    fh->SetImageData( myData, sliceSize); // This callback ensures that the internal
114                                         // Pixel_Data of fh is set correctly
115
116
117    ////////////////////////////////////////////////////////////
118    // Iterate through the slices and save them to file       //
119    ////////////////////////////////////////////////////////////
120    for (int z=0; z<sizez; z++) 
121    {
122       // Set dicom relevant information for that slice
123       //f->SetImageUIDFromSliceNumber(z);
124       //f->SetImageLocation(orig[0],orig[1],orig[2]+z*spacing[2]);
125
126       // copy image slice content
127       memcpy(myData,imageData+z*sizex*sizey,sliceSize);
128
129       // write the image
130       std::string filename = directory + GDCM_NAME_SPACE::Util::Format("%Image_%05d.dcm", z);
131       std::cout << "Writing file " << filename;
132       fh->WriteDcmExplVR(filename);
133       std::cout << " OK" << std::endl;
134    }
135
136    ////////////////////////////////////////////////////////////
137    // Free the allocated objects                             //
138    ////////////////////////////////////////////////////////////
139    fh->Delete();
140    f->Delete();
141
142    return 0;
143 }
144
145
146 // just an utility function to retrieve date and time of a file
147 void GetFileDateAndTime(const char *inputfile, std::string &date, 
148                                                std::string &time)
149 {
150    struct stat buf;    
151    if (stat(inputfile, &buf) == 0) 
152    {
153       char tmp[512];
154
155       strftime(tmp,512,"%Y%m%d", localtime(&buf.st_mtime) );
156       date = tmp;
157
158       strftime(tmp,512,"%H%M%S", localtime(&buf.st_mtime) );
159       time = tmp;
160    }
161    else
162    {
163       date = "20040101";
164       time = "120000";
165    }
166 }