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