]> Creatis software - gdcm.git/blobdiff - vtk/vtkGdcmWriter.cxx
Fix mistypings
[gdcm.git] / vtk / vtkGdcmWriter.cxx
index 4d0168780397b00aba09bb2ca9e0d774ef05adb8..2b5764734e7bf721b050436b65e9f878b76bfac8 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: vtkGdcmWriter.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/01/28 10:07:35 $
-  Version:   $Revision: 1.16 $
+  Date:      $Date: 2007/12/13 15:16:19 $
+  Version:   $Revision: 1.36 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include <vtkImageData.h>
 #include <vtkPointData.h>
 #include <vtkLookupTable.h>
-
-vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.16 $");
-vtkStandardNewMacro(vtkGdcmWriter);
-
+#if (VTK_MAJOR_VERSION >= 5)
+#include <vtkMedicalImageProperties.h>
+#endif
+#ifndef vtkFloatingPointType
+#define vtkFloatingPointType float
+#endif
+
+vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.36 $")
+vtkStandardNewMacro(vtkGdcmWriter)
+
+vtkCxxSetObjectMacro(vtkGdcmWriter,LookupTable,vtkLookupTable)
+#if (VTK_MAJOR_VERSION >= 5)
+vtkCxxSetObjectMacro(vtkGdcmWriter,MedicalImageProperties,vtkMedicalImageProperties)
+#endif
 //-----------------------------------------------------------------------------
 // Constructor / Destructor
 vtkGdcmWriter::vtkGdcmWriter()
 {
    this->LookupTable = NULL;
+   this->MedicalImageProperties = NULL;
    this->FileDimensionality = 3;
    this->WriteType = VTK_GDCM_WRITE_TYPE_EXPLICIT_VR;
-
-   UIDPrefix = "";
-   StudyInstanceUID = "";
-   SeriesInstanceUID = "";
-   FrameOfReferenceInstanceUID = "";
+   this->GdcmFile = 0;
+   this->ContentType = VTK_GDCM_WRITE_TYPE_USER_OWN_IMAGE;
 }
 
 vtkGdcmWriter::~vtkGdcmWriter()
 {
+   this->SetMedicalImageProperties(NULL);
+   this->SetLookupTable(NULL);
 }
 
 //-----------------------------------------------------------------------------
 // Print
-void vtkGdcmWriter::PrintSelf(ostreamos, vtkIndent indent)
+void vtkGdcmWriter::PrintSelf(ostream &os, vtkIndent indent)
 {
-   this->Superclass::PrintSelf(os,indent);
+   this->Superclass::PrintSelf(os, indent);
 
    os << indent << "Write type : " << this->GetWriteTypeAsString();
 }
@@ -76,40 +86,19 @@ const char *vtkGdcmWriter::GetWriteTypeAsString()
    }
 }
 
-void vtkGdcmWriter::SetUIDPrefix(const char *prefix)
-{
-   UIDPrefix = prefix;
-}
-
-const char *vtkGdcmWriter::GetUIDPrefix()
-{
-   return UIDPrefix.c_str();
-}
-
-void vtkGdcmWriter::NewStudyInstanceUID()
-{
-   StudyInstanceUID = "";
-}
-
-void vtkGdcmWriter::NewSeriesInstanceUID()
-{
-   SeriesInstanceUID = "";
-}
-
-void vtkGdcmWriter::NewFrameOfReferenceInstanceUID()
-{
-   FrameOfReferenceInstanceUID = "";
-}
-
 //-----------------------------------------------------------------------------
 // Protected
 /**
  * Copy the image and reverse the Y axis
  */
-// The output datas must be deleted by the user of the method !!!
+// The output data must be deleted by the user of the method !!!
 size_t ReverseData(vtkImageData *image,unsigned char **data)
 {
+#if (VTK_MAJOR_VERSION >= 5)
+   vtkIdType inc[3];
+#else
    int inc[3];
+#endif
    int *extent = image->GetUpdateExtent();
    int dim[3] = {extent[1]-extent[0]+1,
                  extent[3]-extent[2]+1,
@@ -149,9 +138,66 @@ size_t ReverseData(vtkImageData *image,unsigned char **data)
 }
 
 /**
- * Set the datas informations in the file
+ * Set the medical informations in the file, based on the user passed
+ * vtkMedicalImageProperties
+ */
+#if (VTK_MAJOR_VERSION >= 5)
+void SetMedicalImageInformation(GDCM_NAME_SPACE::FileHelper *file, vtkMedicalImageProperties *medprop)
+{
+   // For now only do:
+   // PatientName, PatientID, PatientAge, PatientSex, PatientBirthDate, StudyID
+   std::ostringstream str;
+   if( medprop )
+     {
+     if (medprop->GetPatientName())
+        {
+        str.str("");
+        str << medprop->GetPatientName();
+        file->InsertEntryString(str.str(),0x0010,0x0010,"PN"); // PN 1 Patient's Name
+        }
+
+     if (medprop->GetPatientID())
+        {
+        str.str("");
+        str << medprop->GetPatientID();
+        file->InsertEntryString(str.str(),0x0010,0x0020,"LO"); // LO 1 Patient ID
+        }
+
+     if (medprop->GetPatientAge())
+        {
+        str.str("");
+        str << medprop->GetPatientAge();
+        file->InsertEntryString(str.str(),0x0010,0x1010,"AS"); // AS 1 Patient's Age
+        }
+
+     if (medprop->GetPatientSex())
+        {
+        str.str("");
+        str << medprop->GetPatientSex();
+        file->InsertEntryString(str.str(),0x0010,0x0040,"CS"); // CS 1 Patient's Sex
+        }
+
+     if (medprop->GetPatientBirthDate())
+        {
+        str.str("");
+        str << medprop->GetPatientBirthDate();
+        file->InsertEntryString(str.str(),0x0010,0x0030,"DA"); // DA 1 Patient's Birth Date
+        }
+
+     if (medprop->GetStudyID())
+        {
+        str.str("");
+        str << medprop->GetStudyID();
+        file->InsertEntryString(str.str(),0x0020,0x0010,"SH"); // SH 1 Study ID
+        }
+     }
+}
+#endif
+
+/**
+ * Set the data informations in the file
  */
-void SetImageInformation(gdcm::FileHelper *file,vtkImageData *image)
+void SetImageInformation(GDCM_NAME_SPACE::FileHelper *file, vtkImageData *image)
 {
    std::ostringstream str;
 
@@ -163,37 +209,37 @@ void SetImageInformation(gdcm::FileHelper *file,vtkImageData *image)
 
    str.str("");
    str << dim[0];
-   file->InsertValEntry(str.str(),0x0028,0x0011); // Columns
+   file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
 
    str.str("");
    str << dim[1];
-   file->InsertValEntry(str.str(),0x0028,0x0010); // Rows
+   file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
 
    if(dim[2]>1)
    {
       str.str("");
       str << dim[2];
       //file->Insert(str.str(),0x0028,0x0012); // Planes
-      file->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
+      file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
    }
 
    // Pixel type
    str.str("");
    str << image->GetScalarSize()*8;
-   file->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
-   file->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
+   file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
+   file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
 
    str.str("");
    str << image->GetScalarSize()*8-1;
-   file->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
+   file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
 
    // Pixel Repr
    // FIXME : what do we do when the ScalarType is 
    // VTK_UNSIGNED_INT or VTK_UNSIGNED_LONG
    str.str("");
-   if( image->GetScalarType() == VTK_UNSIGNED_CHAR ||
+   if( image->GetScalarType() == VTK_UNSIGNED_CHAR  ||
        image->GetScalarType() == VTK_UNSIGNED_SHORT ||
-       image->GetScalarType() == VTK_UNSIGNED_INT ||
+       image->GetScalarType() == VTK_UNSIGNED_INT   ||
        image->GetScalarType() == VTK_UNSIGNED_LONG )
    {
       str << "0"; // Unsigned
@@ -202,39 +248,50 @@ void SetImageInformation(gdcm::FileHelper *file,vtkImageData *image)
    {
       str << "1"; // Signed
    }
-   file->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
+   file->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
 
    // Samples per pixel
    str.str("");
    str << image->GetNumberOfScalarComponents();
-   file->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
+   file->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
+
+   /// \todo : Spacing Between Slices is meaningfull ONLY for CT an MR modality
+   ///       We should perform some checkings before forcing the Entry creation
 
    // Spacing
-   double *sp = image->GetSpacing();
+   vtkFloatingPointType *sp = image->GetSpacing();
 
    str.str("");
-   str << sp[0] << "\\" << sp[1];
-   file->InsertValEntry(str.str(),0x0028,0x0030); // Pixel Spacing
+   // We are about to enter floating point value. 
+   // By default ostringstream are smart and don't do fixed point
+   // thus forcing to fixed point value
+   str.setf( std::ios::fixed );
+   str << sp[1] << "\\" << sp[0];
+   file->InsertEntryString(str.str(),0x0028,0x0030,"DS"); // Pixel Spacing
    str.str("");
    str << sp[2];
-   file->InsertValEntry(str.str(),0x0018,0x0088); // Spacing Between Slices
+   file->InsertEntryString(str.str(),0x0018,0x0088,"DS"); // Spacing Between Slices
 
    // Origin
-   double *org = image->GetOrigin();
+   vtkFloatingPointType *org = image->GetOrigin();
+
+   /// \todo : Image Position Patient is meaningfull ONLY for CT an MR modality
+   ///       We should perform some checkings before forcing the Entry creation
 
    str.str("");
    str << org[0] << "\\" << org[1] << "\\" << org[2];
-   file->InsertValEntry(str.str(),0x0020,0x0032); // Image Position Patient
+   file->InsertEntryString(str.str(),0x0020,0x0032,"DS"); // Image Position Patient
+   str.unsetf( std::ios::fixed ); //done with floating point values
 
    // Window / Level
-   double *rng=image->GetScalarRange();
+   vtkFloatingPointType *rng = image->GetScalarRange();
 
    str.str("");
    str << rng[1]-rng[0];
-   file->InsertValEntry(str.str(),0x0028,0x1051); // Window Width
+   file->InsertEntryString(str.str(),0x0028,0x1051,"DS"); // Window Width
    str.str("");
    str << (rng[1]+rng[0])/2.0;
-   file->InsertValEntry(str.str(),0x0028,0x1050); // Window Center
+   file->InsertEntryString(str.str(),0x0028,0x1050,"DS"); // Window Center
 
    // Pixels
    unsigned char *data;
@@ -246,24 +303,25 @@ void SetImageInformation(gdcm::FileHelper *file,vtkImageData *image)
  * Write of the files
  * The call to this method is recursive if there is some files to write
  */ 
-void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file)
+void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, 
+                    ofstream *file)
 {
    if(file)
    {
-      vtkErrorMacro( <<  "File musn't be opened");
+      vtkErrorMacro( <<  "File must not be open");
       return;
    }
 
    if( image->GetScalarType() == VTK_FLOAT || 
        image->GetScalarType() == VTK_DOUBLE )
    {
-      vtkErrorMacro(<< "Bad input type. Scalar type musn't be of type "
-                    << "VTK_FLOAT or VTKDOUBLE (found:"
-                    << image->GetScalarTypeAsString());
+      vtkErrorMacro(<< "Bad input type. Scalar type must not be of type "
+                    << "VTK_FLOAT or VTK_DOUBLE (found:"
+                    << image->GetScalarTypeAsString() << ")" );
       return;
    }
 
-   RecursiveWrite(axis,image,image,file);
+   RecursiveWrite(axis,image, image, file);
    //WriteDcmFile(this->FileName,image);
 }
 
@@ -275,7 +333,7 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
    // if the file is already open then just write to it
    if( file )
    {
-      vtkErrorMacro( <<  "File musn't be opened");
+      vtkErrorMacro( <<  "File musn't be open");
       return;
    }
 
@@ -285,7 +343,7 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
       // determine the name
       if (this->FileName)
       {
-         sprintf(this->InternalFileName,"%s",this->FileName);
+         sprintf(this->InternalFileName, "%s", this->FileName);
       }
       else 
       {
@@ -298,6 +356,8 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
          {
             sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
          }
+// Remove this code in case user is using VTK 4.2...
+#if !(VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION == 2)
          if (this->FileNumber < this->MinimumFileNumber)
          {
             this->MinimumFileNumber = this->FileNumber;
@@ -306,6 +366,7 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
          {
             this->MaximumFileNumber = this->FileNumber;
          }
+#endif
       }
 
       // Write the file
@@ -314,7 +375,7 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
       return;
    }
 
-   // if the current region is too high a dimension forthe file
+   // if the current region is too high a dimension for the file
    // the we will split the current axis
    cache->GetAxisUpdateExtent(axis, min, max);
 
@@ -340,28 +401,23 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
    cache->SetAxisUpdateExtent(axis, min, max);
 }
 
-void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
+void vtkGdcmWriter::WriteDcmFile(char *fileName, vtkImageData *image)
 {
+   GDCM_NAME_SPACE::FileHelper *dcmFile;
+   if ( GdcmFile != 0)
+      dcmFile = GDCM_NAME_SPACE::FileHelper::New(GdcmFile);
+   else
+      dcmFile = GDCM_NAME_SPACE::FileHelper::New();
+   
    // From here, the write of the file begins
-   gdcm::FileHelper *dcmFile = new gdcm::FileHelper();
-
-   // Set the image UID
-   if( StudyInstanceUID.empty() )
-      StudyInstanceUID = gdcm::Util::CreateUniqueUID( UIDPrefix );
-   if( SeriesInstanceUID.empty() )
-      SeriesInstanceUID = gdcm::Util::CreateUniqueUID( UIDPrefix );
-   if( FrameOfReferenceInstanceUID.empty() )
-      FrameOfReferenceInstanceUID = gdcm::Util::CreateUniqueUID( UIDPrefix );
-   std::string uid = gdcm::Util::CreateUniqueUID( UIDPrefix );
-
-   dcmFile->InsertValEntry(uid,0x0008,0x0018); //[SOP Instance UID]
-   dcmFile->InsertValEntry(uid,0x0002,0x0003); //[Media Stored SOP Instance UID]
-   dcmFile->InsertValEntry(StudyInstanceUID,0x0020,0x000d); //[Study Instance UID]
-   dcmFile->InsertValEntry(SeriesInstanceUID,0x0020,0x000e); //[Series Instance UID]
-   dcmFile->InsertValEntry(FrameOfReferenceInstanceUID,0x0020, 0x0052); //[Frame of Reference UID] 
 
+   // Set the medical informations:
+#if (VTK_MAJOR_VERSION >= 5)  
+   SetMedicalImageInformation(dcmFile, this->MedicalImageProperties);
+#endif
+      
    // Set the image informations
-   SetImageInformation(dcmFile,image);
+   SetImageInformation(dcmFile, image);
 
    // Write the image
    switch(this->WriteType)
@@ -381,19 +437,20 @@ void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
       default :
          dcmFile->SetWriteTypeToDcmExplVR();
    }
-
+  
+   dcmFile->SetContentType((GDCM_NAME_SPACE::ImageContentType)ContentType);
    if(!dcmFile->Write(fileName))
    {
-      vtkErrorMacro( << "File "  <<  this->FileName  <<  "couldn't be written by "
+      vtkErrorMacro( << "File "  <<  this->FileName  <<  "cannot be written by "
                      << " the gdcm library");
    }
-
    // Clean up
    if( dcmFile->GetUserData() && dcmFile->GetUserDataSize()>0 )
    {
       delete[] dcmFile->GetUserData();
    }
-   delete dcmFile;
+   dcmFile->Delete();
 }
 
 //-----------------------------------------------------------------------------