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