]> Creatis software - gdcm.git/blobdiff - vtk/vtkGdcmWriter.cxx
ENH: I believe we have to enter floating point values with fixed number instead of...
[gdcm.git] / vtk / vtkGdcmWriter.cxx
index 720b1fc67d101caaa8b083fc3ac33e9c6e7ada45..478db385420114b63bba4cfd010537ea85ecbd4d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: vtkGdcmWriter.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/01/08 15:04:00 $
-  Version:   $Revision: 1.9 $
+  Date:      $Date: 2005/02/25 19:54:06 $
+  Version:   $Revision: 1.17 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
                                                                                 
 =========================================================================*/
                                                                                 
-#include "gdcmHeader.h"
 #include "gdcmFile.h"
+#include "gdcmFileHelper.h"
 #include "gdcmDebug.h"
+#include "gdcmUtil.h"
 #include "vtkGdcmWriter.h"
 
 #include <vtkObjectFactory.h>
@@ -26,7 +27,7 @@
 #include <vtkPointData.h>
 #include <vtkLookupTable.h>
 
-vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.9 $");
+vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.17 $");
 vtkStandardNewMacro(vtkGdcmWriter);
 
 //-----------------------------------------------------------------------------
@@ -36,6 +37,11 @@ vtkGdcmWriter::vtkGdcmWriter()
    this->LookupTable = NULL;
    this->FileDimensionality = 3;
    this->WriteType = VTK_GDCM_WRITE_TYPE_EXPLICIT_VR;
+
+   UIDPrefix = "";
+   StudyInstanceUID = "";
+   SeriesInstanceUID = "";
+   FrameOfReferenceInstanceUID = "";
 }
 
 vtkGdcmWriter::~vtkGdcmWriter()
@@ -70,6 +76,31 @@ 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
 /**
@@ -120,7 +151,7 @@ size_t ReverseData(vtkImageData *image,unsigned char **data)
 /**
  * Set the datas informations in the file
  */
-void SetImageInformation(gdcm::File *file,vtkImageData *image)
+void SetImageInformation(gdcm::FileHelper *file,vtkImageData *image)
 {
    std::ostringstream str;
 
@@ -132,29 +163,29 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image)
 
    str.str("");
    str << dim[0];
-   file->ReplaceOrCreate(str.str(),0x0028,0x0011); // Columns
+   file->InsertValEntry(str.str(),0x0028,0x0011); // Columns
 
    str.str("");
    str << dim[1];
-   file->ReplaceOrCreate(str.str(),0x0028,0x0010); // Rows
+   file->InsertValEntry(str.str(),0x0028,0x0010); // Rows
 
    if(dim[2]>1)
    {
       str.str("");
       str << dim[2];
-      //file->ReplaceOrCreate(str.str(),0x0028,0x0012); // Planes
-      file->ReplaceOrCreate(str.str(),0x0028,0x0008); // Number of Frames
+      //file->Insert(str.str(),0x0028,0x0012); // Planes
+      file->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
    }
 
    // Pixel type
    str.str("");
    str << image->GetScalarSize()*8;
-   file->ReplaceOrCreate(str.str(),0x0028,0x0100); // Bits Allocated
-   file->ReplaceOrCreate(str.str(),0x0028,0x0101); // Bits Stored
+   file->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
+   file->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
 
    str.str("");
    str << image->GetScalarSize()*8-1;
-   file->ReplaceOrCreate(str.str(),0x0028,0x0102); // High Bit
+   file->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
 
    // Pixel Repr
    // FIXME : what do we do when the ScalarType is 
@@ -171,39 +202,43 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image)
    {
       str << "1"; // Signed
    }
-   file->ReplaceOrCreate(str.str(),0x0028,0x0103); // Pixel Representation
+   file->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
 
    // Samples per pixel
    str.str("");
    str << image->GetNumberOfScalarComponents();
-   file->ReplaceOrCreate(str.str(),0x0028,0x0002); // Samples per Pixel
+   file->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
 
    // Spacing
    double *sp = image->GetSpacing();
 
    str.str("");
+   // 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[0] << "\\" << sp[1];
-   file->ReplaceOrCreate(str.str(),0x0028,0x0030); // Pixel Spacing
+   file->InsertValEntry(str.str(),0x0028,0x0030); // Pixel Spacing
    str.str("");
    str << sp[2];
-   file->ReplaceOrCreate(str.str(),0x0018,0x0088); // Spacing Between Slices
+   file->InsertValEntry(str.str(),0x0018,0x0088); // Spacing Between Slices
 
    // Origin
    double *org = image->GetOrigin();
 
    str.str("");
    str << org[0] << "\\" << org[1] << "\\" << org[2];
-   file->ReplaceOrCreate(str.str(),0x0020,0x0032); // Image Position Patient
+   file->InsertValEntry(str.str(),0x0020,0x0032); // Image Position Patient
+   str.unsetf( std::ios::fixed ); //done with floating point values
 
    // Window / Level
    double *rng=image->GetScalarRange();
 
    str.str("");
    str << rng[1]-rng[0];
-   file->ReplaceOrCreate(str.str(),0x0028,0x1051); // Window Width
+   file->InsertValEntry(str.str(),0x0028,0x1051); // Window Width
    str.str("");
    str << (rng[1]+rng[0])/2.0;
-   file->ReplaceOrCreate(str.str(),0x0028,0x1050); // Window Center
+   file->InsertValEntry(str.str(),0x0028,0x1050); // Window Center
 
    // Pixels
    unsigned char *data;
@@ -312,7 +347,22 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
 void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
 {
    // From here, the write of the file begins
-   gdcm::File *dcmFile = new gdcm::File();
+   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 image informations
    SetImageInformation(dcmFile,image);
@@ -340,7 +390,6 @@ void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
    {
       vtkErrorMacro( << "File "  <<  this->FileName  <<  "couldn't be written by "
                      << " the gdcm library");
-      std::cerr << "not written \n";
    }
 
    // Clean up