--- /dev/null
+/*=========================================================================\r
+ \r
+ Program: gdcm\r
+ Module: $RCSfile: Volume2Dicom.cxx,v $\r
+ Language: C++\r
+ Date: $Date: 2004/12/04 08:46:10 $\r
+ Version: $Revision: 1.1 $\r
+ \r
+ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de\r
+ l'Image). All rights reserved. See Doc/License.txt or\r
+ http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.\r
+ \r
+ This software is distributed WITHOUT ANY WARRANTY; without even\r
+ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
+ PURPOSE. See the above copyright notices for more information.\r
+ \r
+=========================================================================*/\r
+\r
+/**\r
+ * This example was proposed by Jean-Michel Rouet\r
+ * It was patch by Mathieu Malaterre to remove ITK reference and be more\r
+ * independant from other toolkit\r
+ * It's aim is to show people how to write their data volume into DICOM slices\r
+ */\r
+\r
+#include "gdcmHeader.h"\r
+#include "gdcmDocEntry.h"\r
+#include "gdcmBinEntry.h"\r
+#include "gdcmFile.h"\r
+#include "gdcmUtil.h"\r
+\r
+#define USAGE "USAGE: Input3DImage OutputDirectory"\r
+\r
+#include <time.h>\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+#ifdef WIN32\r
+#define stat _stat\r
+#endif\r
+\r
+const unsigned int Dimension = 3;\r
+\r
+void gdcmwrite(const char *inputfile, std::string directory);\r
+void GetFileDateAndTime(const char *inputfile, std::string &date, std::string &time);\r
+\r
+int main( int argc, char * argv[] )\r
+{\r
+ \r
+ if (argc < 2) \r
+ {\r
+ std::cerr << argv[0] << USAGE << std::endl;\r
+ return 1;\r
+ } \r
+\r
+ //const char *inputfile = argv[1];\r
+ std::string directory = argv[1];\r
+// itksys::SystemTools::ConvertToUnixSlashes( directory );\r
+ if (directory[directory.size()-1] != '/') directory += '/';\r
+\r
+ std::cout << "Converting image into dicom in " << directory << std::endl;\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // Reading input image and getting some information //\r
+ ////////////////////////////////////////////////////////////\r
+ //std::cout << "Loading image " << inputfile << std::endl;\r
+ //PixelType* imageData = input->GetPixelContainer()->GetImportPointer();\r
+ uint8_t *imageData = new uint8_t[256*256*10];\r
+ memset( imageData, 0, 256*256*10);\r
+ std::cout << "Image Loaded." << std::endl;\r
+\r
+ int sizex = 256;\r
+ int sizey = 256;\r
+ int sizez = 10;\r
+ float spacing[3] = { 1.0, 1.0, 1.5 };\r
+ float orig[3] = { 0.0, 0.0, 0.0 };\r
+ int sliceSize = sizex*sizey*sizeof(uint8_t);\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // compute window center and window width //\r
+ ////////////////////////////////////////////////////////////\r
+ uint8_t min, max; min = max = imageData[0];\r
+ for (int i=1; i<sizex*sizey*sizez; i++) \r
+ {\r
+ uint8_t val = imageData[i];\r
+ if (val > max) max = val;\r
+ if (val < min) min = val;\r
+ }\r
+ float wcenter = (max+min) / 2.;\r
+ float wwidth = (max-min)>0 ? (max-min) : 1.;\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // Get file date and time //\r
+ ////////////////////////////////////////////////////////////\r
+ std::string filedate, filetime; \r
+ //GetFileDateAndTime(inputfile, filedate, filetime);\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // Create a new dicom header and fill in some info //\r
+ ////////////////////////////////////////////////////////////\r
+ gdcm::Header *h1 = new gdcm::Header();\r
+\r
+ //h1->SetDateAndTime(filedate, filetime);\r
+ //h1->SetModality("CT");\r
+ //h1->SetPatientName( "TestPatient");\r
+ //h1->SetPatientID( "TestID");\r
+ //h1->SetStudyID( "1");\r
+ //h1->SetSeriesNumber( "1");\r
+ //h1->SetSliceThickness(spacing[2]);\r
+ //h1->SetSpaceBetweenSlices(spacing[2]);\r
+ //h1->SetXYSpacing( spacing[0], spacing[1]);\r
+ //h1->SetXSize(sizex);\r
+ //h1->SetYSize(sizey);\r
+ //h1->SetNbBitsAllocated(16);\r
+ //h1->SetNbBitsStored(12);\r
+ //h1->SetNbBitsStored(12);\r
+ //h1->SetPixelSigned(true);\r
+ //h1->SetCenter( wcenter);\r
+ //h1->SetWindow( wwidth);\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // Create a new dicom file object from the header //\r
+ ////////////////////////////////////////////////////////////\r
+ gdcm::File *f1 = new gdcm::File(h1);\r
+ uint8_t *myData = f1->GetImageData(); // Get an Image pointer\r
+ f1->SetImageData( myData, sliceSize); // This callback ensures that the internal\r
+ // Pixel_Data of f1 is set correctly\r
+\r
+ \r
+ ////////////////////////////////////////////////////////////\r
+ // Iterate through the slices and save them to file //\r
+ ////////////////////////////////////////////////////////////\r
+ for (int z=0; z<sizez; z++) \r
+ {\r
+ // Set dicom relevant information for that slice\r
+ //h1->SetImageUIDFromSliceNumber(z);\r
+ //h1->SetImageLocation(orig[0],orig[1],orig[2]+z*spacing[2]);\r
+\r
+ // copy image slice content\r
+ memcpy(myData,imageData+z*sizex*sizey,sliceSize);\r
+\r
+ // write the image\r
+ std::string filename = directory + gdcm::Util::Format("%Image_%05d.dcm", z);\r
+ std::cout << "Writing file " << filename;\r
+ f1->WriteDcmExplVR(filename);\r
+ std::cout << " OK" << std::endl;\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // Free the allocated objects //\r
+ ////////////////////////////////////////////////////////////\r
+ // delete f1; // FIXME: these calls sometimes crashes under .NET ????\r
+ // delete h1;\r
+}\r
+\r
+\r
+// just an utility function to retrieve date and time of a file\r
+void GetFileDateAndTime(const char *inputfile, std::string &date, std::string &time)\r
+{\r
+ struct stat buf; \r
+ if (stat(inputfile, &buf) == 0) \r
+ {\r
+ char tmp[512];\r
+\r
+ strftime(tmp,512,"%Y%m%d", localtime(&buf.st_mtime) );\r
+ date = tmp;\r
+\r
+ strftime(tmp,512,"%H%M%S", localtime(&buf.st_mtime) );\r
+ time = tmp;\r
+ }\r
+ else\r
+ {\r
+ date = "20040101";\r
+ time = "120000";\r
+ }\r
+}\r
+\r