]> Creatis software - gdcm.git/commitdiff
* Example/WriteDicomSimple.cxx : example to write a dicom file from nothing.
authorregrain <regrain>
Thu, 9 Dec 2004 11:31:51 +0000 (11:31 +0000)
committerregrain <regrain>
Thu, 9 Dec 2004 11:31:51 +0000 (11:31 +0000)
     At this time, this image isn't readable by e-film... waiting JPR help to
     solve it.
   -- BeNours

ChangeLog
Example/CMakeLists.txt
Example/WriteDicomSimple.cxx [new file with mode: 0644]
vtk/vtkGdcmWriter.cxx

index bf159f0abbd6ca9473855bed93b0dd23e0f202ab..341459e52f835221c12475487b565e8c2a2880b4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+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
+     solve it.
+
 2004-12-09 Benoit Regrain <Benoit.Regrain@creatis.insa-lyon.fr>
    * src/gdcmPixelReadConvert.cxx : bug fix when would forcing load of a
      DocEntry. Now use methods of the Document !
index 4a16d5b9b7f4dc354fc7352e6502744998d7359c..328f2f03600d724ddc5bc1612eccde9c274c6f33 100644 (file)
@@ -20,6 +20,7 @@ SET(EXAMPLE_SOURCES
   TestWrite
   TestWriteSimple
   Volume2Dicom
+  WriteDicomSimple
   Write
   WriteRead
   WriteDicom
diff --git a/Example/WriteDicomSimple.cxx b/Example/WriteDicomSimple.cxx
new file mode 100644 (file)
index 0000000..8503e53
--- /dev/null
@@ -0,0 +1,191 @@
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: WriteDicomSimple.cxx,v $
+  Language:  C++
+  Date:      $Date: 2004/12/09 11:31:52 $
+  Version:   $Revision: 1.1 $
+                                                                                
+  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.
+                                                                                
+=========================================================================*/
+
+/**
+ * Write a dicom file from nothing
+ * The written image is 256x256, 8 bits, unsigned char
+ * The image content is a horizontal grayscale from 
+ * 
+ */
+#include "gdcmHeader.h"
+#include "gdcmFile.h"
+
+#include <iostream>
+
+// Image size
+#define SIZE_X          256
+#define SIZE_Y          256
+// Number of components in the image (3 for RGB)
+#define COMPONENT       1
+// Size of each component (in byte)
+#define COMPONENT_SIZE  1
+// Window / Level
+#define COLOR_WINDOW    256
+#define COLOR_LEVEL     128
+
+int main(int argc, char* argv[])
+{
+   if (argc < 3) 
+   {
+      std::cerr << "usage: \n" 
+                << argv[0] << " Output Mode " << std::endl 
+                << "Output : output file name\n"
+                << "Mode : \n"
+                << "   a : ACR, produces a file named Output.ACR\n"
+                << "   e : DICOM Explicit VR, produces a file named Output.E.DCM\n"
+                << "   i : DICOM Implicit VR, produces a file named Output.I.DCM\n"
+                << "   r : RAW, produces a file named Output.RAW\n"
+                << std::endl;
+      return 1;
+   }
+
+
+// Step 1 : Create the header of the image
+   gdcm::Header *header = new gdcm::Header();
+   std::ostringstream str;
+
+   // Set the image size
+   str.str("");
+   str << SIZE_X;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011);
+
+   str.str("");
+   str << SIZE_Y;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010);
+
+   // Set the pixel type
+   str.str("");
+   str << COMPONENT_SIZE * 8;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100);
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101);
+
+   str.str("");
+   str << COMPONENT_SIZE * 8 - 1;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102);
+
+   // Set the pixel representation
+   str.str("");
+   str << "0"; // Unsigned
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103);
+
+   // Set the samples per pixel
+   str.str("");
+   str << COMPONENT;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002);
+
+   // Set the Window / Level
+   str.str("");
+   str << COLOR_WINDOW;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x1051);
+   str.str("");
+   str << COLOR_LEVEL;
+   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050);
+
+   if( !header->IsReadable() )
+   {
+      std::cerr << "-------------------------------\n"
+                << "Error while creating the file\n"
+                << "This file is considered to be not readable\n";
+
+      return 1;
+   }
+
+// Step 2 : Create the output image
+   size_t size = SIZE_X * SIZE_Y * COMPONENT * COMPONENT_SIZE;
+   unsigned char *imageData = new unsigned char[size];
+
+   unsigned char *tmp = imageData;
+   for(int j=0;j<SIZE_Y;j++)
+   {
+      for(int i=0;i<SIZE_X;i++)
+      {
+         for(int c=0;c<COMPONENT;c++)
+         {
+            *tmp = j;
+            tmp += COMPONENT_SIZE; 
+         }
+      }
+   }
+
+// Step 3 : Create the file of the image
+   gdcm::File *file = new gdcm::File(header);
+   file->SetImageData(imageData,size);
+
+// Step 4 : Set the writting mode and write the image
+   std::string fileName = argv[1]; 
+   std::string mode = argv[2];
+
+   file->SetWriteModeToDecompressed();
+   switch (mode[0])
+   {
+      case 'a' : // Write an ACR file
+         fileName += ".ACR";
+         file->SetWriteTypeToAcr();
+         std::cout << "Write ACR" << std::endl
+                   << "File :" << fileName << std::endl;
+         break;
+
+      case 'e' : // Write a DICOM Explicit VR file
+         fileName += ".E.DCM";
+         file->SetWriteTypeToDcmExplVR();
+         std::cout << "Write DCM Explicit VR" << std::endl
+                   << "File :" << fileName << std::endl;
+         break;
+
+      case 'i' : // Write a DICOM Implicit VR file
+         fileName += ".I.DCM";
+         file->SetWriteTypeToDcmImplVR();
+         std::cout << "Write DCM Implicit VR" << std::endl
+                   << "File :" << fileName << std::endl;
+         break;
+
+      case 'r' : // Write a RAW file
+         fileName += ".RAW";
+         file->WriteRawData(fileName);
+         std::cout << "Write Raw" << std::endl
+                   << "File :" << fileName << std::endl;
+
+         delete[] imageData;
+         delete file;
+         delete header;
+         return 0;
+
+      default :
+         std::cout << "-------------------------------\n"
+                   << "Write mode undefined...\n"
+                   << "No file written\n";
+
+         delete[] imageData;
+         delete file;
+         delete header;
+         return 1;
+   }
+
+   if( !file->Write(fileName) )
+   {
+      std::cout << "-------------------------------\n"
+                   << "Error when writting the file " << fileName << "\n"
+                << "No file written\n";
+   }
+
+   delete[] imageData;
+   delete file;
+   delete header;
+
+   return 0;
+}
index c104c2a269a45bb4674103c592b47d9b91932dd7..e4ba6795cbf9b980e326e1844f9b664a04febb30 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: vtkGdcmWriter.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/12/09 10:59:59 $
-  Version:   $Revision: 1.4 $
+  Date:      $Date: 2004/12/09 11:31:52 $
+  Version:   $Revision: 1.5 $
                                                                                 
   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.4 $");
+vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.5 $");
 vtkStandardNewMacro(vtkGdcmWriter);
 
 //-----------------------------------------------------------------------------
@@ -95,28 +95,28 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image)
    int *dim = image->GetDimensions();
 
    str.str("");
-   str<<dim[0];
+   str << dim[0];
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011);
 
    str.str("");
-   str<<dim[1];
+   str << dim[1];
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010);
 
    if(dim[2]>1)
    {
       str.str("");
-      str<<dim[2];
+      str << dim[2];
       file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0012);
    }
 
    // Pixel type
    str.str("");
-   str<<image->GetScalarSize()*8;
+   str << image->GetScalarSize()*8;
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100);
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101);
 
    str.str("");
-   str<<image->GetScalarSize()*8-1;
+   str << image->GetScalarSize()*8-1;
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102);
 
    // Pixel Repr
@@ -128,44 +128,44 @@ void SetImageInformation(gdcm::File *file,vtkImageData *image)
        image->GetScalarType() == VTK_UNSIGNED_INT ||
        image->GetScalarType() == VTK_UNSIGNED_LONG )
    {
-      str<<"0"; // Unsigned
+      str << "0"; // Unsigned
    }
    else
    {
-      str<<"1"; // Signed
+      str << "1"; // Signed
    }
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103);
 
    // Samples per pixel
    str.str("");
-   str<<image->GetNumberOfScalarComponents();
+   str << image->GetNumberOfScalarComponents();
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002);
 
    // Spacing
    double *sp = image->GetSpacing();
 
    str.str("");
-   str<<sp[0]<<"\\"<<sp[1];
+   str << sp[0] << "\\" << sp[1];
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0030);
    str.str("");
-   str<<sp[2];
+   str << sp[2];
    file->ReplaceOrCreateByNumber(str.str(),0x0018,0x0088);
 
    // Origin
    double *org = image->GetOrigin();
 
    str.str("");
-   str<<org[0]<<"\\"<<org[1]<<"\\"<<org[2];
+   str << org[0] << "\\" << org[1] << "\\" << org[2];
    file->ReplaceOrCreateByNumber(str.str(),0x0020,0x0032);
 
    // Window / Level
    double *rng=image->GetScalarRange();
 
    str.str("");
-   str<<rng[1]-rng[0];
+   str << rng[1]-rng[0];
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1051);
    str.str("");
-   str<<(rng[1]+rng[0])/2.0;
+   str << (rng[1]+rng[0])/2.0;
    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050);
 
    // Pixels
@@ -183,7 +183,7 @@ void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file
    (void)axis; // To avoid warning
    if(file)
    {
-      vtkErrorMacro(<< "File musn't be opened");
+      vtkErrorMacro( <<  "File musn't be opened");
       return;
    }
 
@@ -219,9 +219,9 @@ void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
    // Write the image
    if(!dcmFile->Write(fileName))
    {
-      vtkErrorMacro(<< "File " << this->FileName << "couldn't be written by "
-                    << " the gdcm library");
-      std::cerr<<"not written \n";
+      vtkErrorMacro( << "File "  <<  this->FileName  <<  "couldn't be written by "
+                     << " the gdcm library");
+      std::cerr << "not written \n";
    }
 
    delete dcmFile;