1 /*=========================================================================
4 Module: $RCSfile: Volume2Dicom.cxx,v $
6 Date: $Date: 2005/02/02 10:06:32 $
7 Version: $Revision: 1.6 $
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 "gdcmBinEntry.h"
29 #include "gdcmFileHelper.h"
32 #define USAGE "USAGE: Input3DImage OutputDirectory"
35 #include <sys/types.h>
41 const unsigned int Dimension = 3;
43 void gdcmwrite(const char *inputfile, std::string directory);
44 void GetFileDateAndTime(const char *inputfile,
45 std::string &date, std::string &time);
47 int main( int argc, char *argv[] )
51 std::cerr << argv[0] << USAGE << std::endl;
55 //const char *inputfile = argv[1];
56 std::string directory = argv[1];
57 // itksys::SystemTools::ConvertToUnixSlashes( directory );
58 if (directory[directory.size()-1] != '/')
61 std::cout << "Converting image into dicom in " << directory << std::endl;
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;
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);
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++)
85 uint8_t val = imageData[i];
91 //float wcenter = (max+min) / 2.;
92 //float wwidth = (max-min)>0 ? (max-min) : 1.;
94 ////////////////////////////////////////////////////////////
95 // Get file date and time //
96 ////////////////////////////////////////////////////////////
97 std::string filedate, filetime;
98 //GetFileDateAndTime(inputfile, filedate, filetime);
100 ////////////////////////////////////////////////////////////
101 // Create a new dicom header and fill in some info //
102 ////////////////////////////////////////////////////////////
103 gdcm::File *h1 = new gdcm::File();
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);
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
132 ////////////////////////////////////////////////////////////
133 // Iterate through the slices and save them to file //
134 ////////////////////////////////////////////////////////////
135 for (int z=0; z<sizez; z++)
137 // Set dicom relevant information for that slice
138 //h1->SetImageUIDFromSliceNumber(z);
139 //h1->SetImageLocation(orig[0],orig[1],orig[2]+z*spacing[2]);
141 // copy image slice content
142 memcpy(myData,imageData+z*sizex*sizey,sliceSize);
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;
151 ////////////////////////////////////////////////////////////
152 // Free the allocated objects //
153 ////////////////////////////////////////////////////////////
154 // delete f1; // FIXME: these calls sometimes crashes under .NET ????
161 // just an utility function to retrieve date and time of a file
162 void GetFileDateAndTime(const char *inputfile, std::string &date,
166 if (stat(inputfile, &buf) == 0)
170 strftime(tmp,512,"%Y%m%d", localtime(&buf.st_mtime) );
173 strftime(tmp,512,"%H%M%S", localtime(&buf.st_mtime) );