]> Creatis software - gdcm.git/commitdiff
* vtk/vtkGdcmWriter.[h|cxx] : now can write stack of images.
authorregrain <regrain>
Thu, 9 Dec 2004 17:08:35 +0000 (17:08 +0000)
committerregrain <regrain>
Thu, 9 Dec 2004 17:08:35 +0000 (17:08 +0000)
   * vtk/vtkWriteDicom.cxx : can write 2D stack or 3D image
   -- BeNours

ChangeLog
vtk/vtkGdcmWriter.cxx
vtk/vtkWriteDicom.cxx

index 341459e52f835221c12475487b565e8c2a2880b4..ea4843ed8fcedb03698941067774bbf3a5f0fe47 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2004-12-09 Benoit Regrain <Benoit.Regrain@creatis.insa-lyon.fr>
+   * vtk/vtkGdcmWriter.[h|cxx] : now can write stack of images.
+   * vtk/vtkWriteDicom.cxx : can write 2D stack or 3D image
+
 2004-12-09 Benoit Regrain <Benoit.Regrain@creatis.insa-lyon.fr>
    * Example/WriteDicomSimple.cxx : example to write a dicom file from nothing.
      At this time, this image isn't readable by e-film... waiting JPR help to
index e4ba6795cbf9b980e326e1844f9b664a04febb30..c9843e32136c2b746bf9962a956b76dfc36fbfc9 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: vtkGdcmWriter.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/12/09 11:31:52 $
-  Version:   $Revision: 1.5 $
+  Date:      $Date: 2004/12/09 17:08:36 $
+  Version:   $Revision: 1.6 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -26,7 +26,7 @@
 #include <vtkPointData.h>
 #include <vtkLookupTable.h>
 
-vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.5 $");
+vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.6 $");
 vtkStandardNewMacro(vtkGdcmWriter);
 
 //-----------------------------------------------------------------------------
@@ -34,6 +34,7 @@ vtkStandardNewMacro(vtkGdcmWriter);
 vtkGdcmWriter::vtkGdcmWriter()
 {
    this->LookupTable = NULL;
+   this->FileDimensionality = 3;
 }
 
 vtkGdcmWriter::~vtkGdcmWriter()
@@ -56,26 +57,32 @@ void vtkGdcmWriter::PrintSelf(ostream& os, vtkIndent indent)
  * Copy the image and reverse the Y axis
  */
 // The output datas must be deleted by the user of the method !!!
-size_t ReverseData(vtkImageData *img,unsigned char **data)
+size_t ReverseData(vtkImageData *image,unsigned char **data)
 {
-   int *dim = img->GetDimensions();
-   size_t lineSize = dim[0] * img->GetScalarSize()
-                   * img->GetNumberOfScalarComponents();
+   int inc[3];
+   int *extent = image->GetUpdateExtent();
+   int dim[3] = {extent[1]-extent[0]+1,
+                 extent[3]-extent[2]+1,
+                 extent[5]-extent[4]+1};
+
+   size_t lineSize = dim[0] * image->GetScalarSize()
+                   * image->GetNumberOfScalarComponents();
    size_t planeSize = dim[1] * lineSize;
    size_t size = dim[2] * planeSize;
 
    *data = new unsigned char[size];
 
-   unsigned char *src = (unsigned char *)img->GetScalarPointer();
+   image->GetIncrements(inc);
+   unsigned char *src = (unsigned char *)image->GetScalarPointerForExtent(extent);
    unsigned char *dst = *data + planeSize - lineSize;
-   for (int plane = 0; plane < dim[2]; plane++)
+   for (int plane = extent[4]; plane <= extent[5]; plane++)
    {
-      for (int line = 0; line < dim[1]; line++)
+      for (int line = extent[2]; line <= extent[3]; line++)
       {
          // Copy one line at proper destination:
          memcpy((void*)dst, (void*)src, lineSize);
 
-         src += lineSize;
+         src += inc[1]*image->GetScalarSize();
          dst -= lineSize;
       }
       dst += 2 * planeSize;
@@ -92,7 +99,10 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image)
    std::ostringstream str;
 
    // Image size
-   int *dim = image->GetDimensions();
+   int *extent = image->GetUpdateExtent();
+   int dim[3] = {extent[1]-extent[0]+1,
+                 extent[3]-extent[2]+1,
+                 extent[5]-extent[4]+1};
 
    str.str("");
    str << dim[0];
@@ -180,7 +190,6 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image)
  */ 
 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file)
 {
-   (void)axis; // To avoid warning
    if(file)
    {
       vtkErrorMacro( <<  "File musn't be opened");
@@ -196,16 +205,81 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file
       return;
    }
 
-   WriteDcmFile(this->FileName,image);
+   RecursiveWrite(axis,image,image,file);
+   //WriteDcmFile(this->FileName,image);
 }
 
-void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, 
-                                   vtkImageData *cache, ofstream *file)
+void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache, 
+                                   vtkImageData *image, ofstream *file)
 {
-   (void)axis; // To avoid warning
-   (void)image; // To avoid warning
-   (void)cache; // To avoid warning
-   (void)file; // To avoid warning
+   int idx, min, max;
+
+   // if the file is already open then just write to it
+   if( file )
+   {
+      vtkErrorMacro( <<  "File musn't be opened");
+      return;
+   }
+
+   // if we need to open another slice, do it
+   if( (axis + 1) == this->FileDimensionality )
+   {
+      // determine the name
+      if (this->FileName)
+      {
+         sprintf(this->InternalFileName,"%s",this->FileName);
+      }
+      else 
+      {
+         if (this->FilePrefix)
+         {
+            sprintf(this->InternalFileName, this->FilePattern, 
+            this->FilePrefix, this->FileNumber);
+         }
+         else
+         {
+            sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
+         }
+         if (this->FileNumber < this->MinimumFileNumber)
+         {
+            this->MinimumFileNumber = this->FileNumber;
+         }
+         else if (this->FileNumber > this->MaximumFileNumber)
+         {
+            this->MaximumFileNumber = this->FileNumber;
+         }
+      }
+
+      // Write the file
+      WriteDcmFile(this->InternalFileName,image);
+      ++this->FileNumber;
+      return;
+   }
+
+   // if the current region is too high a dimension forthe file
+   // the we will split the current axis
+   cache->GetAxisUpdateExtent(axis, min, max);
+
+   // if it is the y axis then flip by default
+   if (axis == 1 && !this->FileLowerLeft)
+   {
+      for(idx = max; idx >= min; idx--)
+      {
+         cache->SetAxisUpdateExtent(axis, idx, idx);
+         this->RecursiveWrite(axis - 1, cache, image, file);
+      }
+   }
+   else
+   {
+      for(idx = min; idx <= max; idx++)
+      {
+         cache->SetAxisUpdateExtent(axis, idx, idx);
+         this->RecursiveWrite(axis - 1, cache, image, file);
+      }
+   }
+
+   // restore original extent
+   cache->SetAxisUpdateExtent(axis, min, max);
 }
 
 void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
index 19452b26fb63411aa1b54a8ea657355c9cdd52ee..a67cbe79058286e0c7226efea323b90048e039dc 100644 (file)
 //----------------------------------------------------------------------------
 int main(int argc, char *argv[])
 {
-   if( argc < 2 )
+   if( argc < 3 )
+   {
       return 0;
+   }
   
    vtkGdcmReader *reader = vtkGdcmReader::New();
    reader->AllowLookupTableOff();
-
-   if( argc == 2 )
-      reader->SetFileName( argv[1] );
-   else
-      for(int i=1; i< argc; i++)
-         reader->AddFileName( argv[i] );
-
+   reader->SetFileName( argv[1] );
    reader->Update();
 
    vtkImageData *output;
@@ -51,17 +47,30 @@ int main(int argc, char *argv[])
    }
   
    //print debug info:
-   output->Print( cout );
+   output->Print(cout);
 
    //////////////////////////////////////////////////////////
    // WRITE...
    //if you wish you can export dicom to a vtk file 
    // this file will have the add of .tmp.dcm extention
-   std::string fileName = argv[1];
-   fileName += ".tmp.dcm";
+   std::string fileName = argv[2];
+   fileName += ".dcm";
 
    vtkGdcmWriter *writer = vtkGdcmWriter::New();
+
+   // For 3D
+   writer->SetFileDimensionality(3);
    writer->SetFileName(fileName.c_str());
+   if(argc >= 4)
+   {
+      if( strcmp(argv[3],"2D" )==0 )
+      {
+         writer->SetFileDimensionality(2);
+         writer->SetFilePrefix(argv[2]);
+         writer->SetFilePattern("%s%d.dcm");
+      }
+   }
+
    writer->SetInput(output);
    writer->Write();
    //////////////////////////////////////////////////////////