]> Creatis software - gdcm.git/blobdiff - vtk/vtkGdcmWriter.cxx
Aware user (who *does* know all the files whose names
[gdcm.git] / vtk / vtkGdcmWriter.cxx
index c516e01a3614c3cf4f1f33ea770dca20d27f6157..d73ddccc8decb526cf072b7bdc72e96e55c8a318 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: vtkGdcmWriter.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/01/20 16:17:01 $
-  Version:   $Revision: 1.11 $
+  Date:      $Date: 2005/04/11 17:01:16 $
+  Version:   $Revision: 1.21 $
                                                                                 
   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.11 $");
+vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.21 $");
 vtkStandardNewMacro(vtkGdcmWriter);
 
 //-----------------------------------------------------------------------------
@@ -44,9 +45,9 @@ vtkGdcmWriter::~vtkGdcmWriter()
 
 //-----------------------------------------------------------------------------
 // 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();
 }
@@ -75,7 +76,7 @@ const char *vtkGdcmWriter::GetWriteTypeAsString()
 /**
  * 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)
 {
    int inc[3];
@@ -118,9 +119,9 @@ size_t ReverseData(vtkImageData *image,unsigned char **data)
 }
 
 /**
- * Set the datas informations in the file
+ * Set the data informations in the file
  */
-void SetImageInformation(gdcm::FileHelper *file,vtkImageData *image)
+void SetImageInformation(gdcm::FileHelper *file, vtkImageData *image)
 {
    std::ostringstream str;
 
@@ -132,29 +133,29 @@ void SetImageInformation(gdcm::FileHelper *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 +172,49 @@ void SetImageInformation(gdcm::FileHelper *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
+
+   /// \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();
 
    str.str("");
-   str << sp[0] << "\\" << sp[1];
-   file->ReplaceOrCreate(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->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();
 
+   /// \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->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;
@@ -215,24 +226,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 "
+      vtkErrorMacro(<< "Bad input type. Scalar type must not be of type "
                     << "VTK_FLOAT or VTKDOUBLE (found:"
                     << image->GetScalarTypeAsString());
       return;
    }
 
-   RecursiveWrite(axis,image,image,file);
+   RecursiveWrite(axis,image, image, file);
    //WriteDcmFile(this->FileName,image);
 }
 
@@ -244,7 +256,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;
    }
 
@@ -254,7 +266,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 
       {
@@ -283,7 +295,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);
 
@@ -309,13 +321,13 @@ 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)
 {
    // From here, the write of the file begins
    gdcm::FileHelper *dcmFile = new gdcm::FileHelper();
 
    // Set the image informations
-   SetImageInformation(dcmFile,image);
+   SetImageInformation(dcmFile, image);
 
    // Write the image
    switch(this->WriteType)
@@ -338,7 +350,7 @@ void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
 
    if(!dcmFile->Write(fileName))
    {
-      vtkErrorMacro( << "File "  <<  this->FileName  <<  "couldn't be written by "
+      vtkErrorMacro( << "File "  <<  this->FileName  <<  "cannot be written by "
                      << " the gdcm library");
    }