1 /*=========================================================================
4 Module: $RCSfile: Volume2Dicom.cxx,v $
6 Date: $Date: 2005/10/25 14:52:27 $
7 Version: $Revision: 1.10 $
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.
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.
17 =========================================================================*/
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
27 #include "gdcmDocEntry.h"
28 #include "gdcmFileHelper.h"
31 #define USAGE "USAGE: Input3DImage OutputDirectory"
34 #include <sys/types.h>
40 //const unsigned int Dimension = 3;
42 void gdcmwrite(const char *inputfile, std::string directory);
43 void GetFileDateAndTime(const char *inputfile,
44 std::string &date, std::string &time);
46 int main( int argc, char *argv[] )
50 std::cerr << argv[0] << USAGE << std::endl;
54 //const char *inputfile = argv[1];
55 std::string directory = argv[1];
56 // itksys::SystemTools::ConvertToUnixSlashes( directory );
57 if (directory[directory.size()-1] != '/')
60 std::cout << "Converting image into dicom in " << directory << std::endl;
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;
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);
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++)
84 uint8_t val = imageData[i];
90 //float wcenter = (max+min) / 2.;
91 //float wwidth = (max-min)>0 ? (max-min) : 1.;
93 ////////////////////////////////////////////////////////////
94 // Get file date and time //
95 ////////////////////////////////////////////////////////////
96 std::string filedate, filetime;
97 //GetFileDateAndTime(inputfile, filedate, filetime);
99 ////////////////////////////////////////////////////////////
100 // Create a new dicom header and fill in some info //
101 ////////////////////////////////////////////////////////////
102 gdcm::File *f = gdcm::File::New();
104 ////////////////////////////////////////////////////////////
105 // Create a new dicom file object from the header //
106 ////////////////////////////////////////////////////////////
107 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
108 uint8_t *myData = fh->GetImageData(); // Get an Image pointer
109 fh->SetImageData( myData, sliceSize); // This callback ensures that the internal
110 // Pixel_Data of fh is set correctly
113 ////////////////////////////////////////////////////////////
114 // Iterate through the slices and save them to file //
115 ////////////////////////////////////////////////////////////
116 for (int z=0; z<sizez; z++)
118 // Set dicom relevant information for that slice
119 //f->SetImageUIDFromSliceNumber(z);
120 //f->SetImageLocation(orig[0],orig[1],orig[2]+z*spacing[2]);
122 // copy image slice content
123 memcpy(myData,imageData+z*sizex*sizey,sliceSize);
126 std::string filename = directory + gdcm::Util::Format("%Image_%05d.dcm", z);
127 std::cout << "Writing file " << filename;
128 fh->WriteDcmExplVR(filename);
129 std::cout << " OK" << std::endl;
132 ////////////////////////////////////////////////////////////
133 // Free the allocated objects //
134 ////////////////////////////////////////////////////////////
142 // just an utility function to retrieve date and time of a file
143 void GetFileDateAndTime(const char *inputfile, std::string &date,
147 if (stat(inputfile, &buf) == 0)
151 strftime(tmp,512,"%Y%m%d", localtime(&buf.st_mtime) );
154 strftime(tmp,512,"%H%M%S", localtime(&buf.st_mtime) );