]> Creatis software - gdcm.git/blobdiff - Example/WriteDicomSimple.cxx
Fix mistypings
[gdcm.git] / Example / WriteDicomSimple.cxx
index 8503e531d7ffd51319179e029805c260be482701..80856449be282ebc5f2bcbc9bae93b92f80fd302 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: WriteDicomSimple.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/12/09 11:31:52 $
-  Version:   $Revision: 1.1 $
+  Date:      $Date: 2007/06/26 15:42:14 $
+  Version:   $Revision: 1.20 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
  * The image content is a horizontal grayscale from 
  * 
  */
-#include "gdcmHeader.h"
 #include "gdcmFile.h"
-
+#include "gdcmFileHelper.h"
+#include "gdcmArgMgr.h"
 #include <iostream>
+#include <sstream>
 
 // Image size
-#define SIZE_X          256
-#define SIZE_Y          256
+ uint16_t SIZE_X;
+ uint16_t SIZE_Y;
 // Number of components in the image (3 for RGB)
-#define COMPONENT       1
+ uint16_t  COMPONENT;
 // Size of each component (in byte)
-#define COMPONENT_SIZE  1
-// Window / Level
-#define COLOR_WINDOW    256
-#define COLOR_LEVEL     128
+ uint16_t  COMPONENT_SIZE;
+
 
-int main(int argc, charargv[])
+int main(int argc, char *argv[])
 {
-   if (argc < 3) 
+
+
+   START_USAGE(usage)
+   " \n exWriteDicomSimple : \n                                               ",
+   " Creates a Dicom image File                                               ",
+   " usage: exWriteDicomSimple {fileout=outputFileName}                       ",
+   "                       [nx=number of colomns] [ny=number of lines]        ",
+   "                       [components= 1: grey, 3 : RGB]                     ",
+   "                       [pixelsize= Pixel Size in Bytes : 1/2] } ] [debug] ",
+   FINISH_USAGE
+
+   // Initialize Arguments Manager   
+   GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
+  
+   if (argc == 1 || am->ArgMgrDefined("usage") )
    {
-      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;
+      am->ArgMgrUsage(usage); // Display 'usage'
+      delete am;
       return 1;
    }
 
-
-// Step 1 : Create the header of the image
-   gdcm::Header *header = new gdcm::Header();
+   if (am->ArgMgrDefined("debug"))
+      GDCM_NAME_SPACE::Debug::DebugOn();
+      
+   std::string fileOut = am->ArgMgrGetString("fileout",(char *)"WriteDicomSimple.dcm");   
+   SIZE_X = am->ArgMgrGetInt("NX", 128);
+   SIZE_Y = am->ArgMgrGetInt("NY", 128);
+   COMPONENT = am->ArgMgrGetInt("components", 1);
+   COMPONENT_SIZE = am->ArgMgrGetInt("size", 1);
+   
+   /* if unused Param we give up */
+   if ( am->ArgMgrPrintUnusedLabels() )
+   {
+      am->ArgMgrUsage(usage);
+      delete am;
+      return 1;
+   } 
+
+   delete am;  // we don't need Argument Manager any longer
+
+   // ----------- End Arguments Manager ---------
+   
+   
+// Step 1 : Create an empty GDCM_NAME_SPACE::FileHelper for the image
+//          (it deals with the acces to the pixels)
+   GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
+   
+//         Get the empty GDCM_NAME_SPACE::File of the image 
+//         (it deals with the 'entries' od the image header)  
+   GDCM_NAME_SPACE::File *header = fileH->GetFile();
+    
    std::ostringstream str;
 
    // Set the image size
    str.str("");
    str << SIZE_X;
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011);
+   header->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
 
    str.str("");
    str << SIZE_Y;
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010);
+   header->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
 
    // Set the pixel type
    str.str("");
    str << COMPONENT_SIZE * 8;
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100);
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101);
+   header->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
+   header->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
 
    str.str("");
-   str << COMPONENT_SIZE * 8 - 1;
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102);
+   str << ( COMPONENT_SIZE * 8 ) - 1;
+   header->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
 
    // Set the pixel representation
    str.str("");
    str << "0"; // Unsigned
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103);
+   header->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
 
    // Set the samples per pixel
    str.str("");
    str << COMPONENT;
-   header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002);
+   header->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
 
-   // 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;
@@ -116,76 +134,52 @@ int main(int argc, char* argv[])
       {
          for(int c=0;c<COMPONENT;c++)
          {
-            *tmp = j;
+            *tmp = (unsigned char)j;
             tmp += COMPONENT_SIZE; 
          }
       }
    }
 
-// Step 3 : Create the file of the image
-   gdcm::File *file = new gdcm::File(header);
-   file->SetImageData(imageData,size);
+// Step 3 : Set the 'Pixel Area' of the image
 
-// Step 4 : Set the writting mode and write the image
-   std::string fileName = argv[1]; 
-   std::string mode = argv[2];
+   fileH->SetImageData(imageData,size);
+   header->Print();
+   std::cout << "-------------------------------" << std::endl;
+   
+      // Step 4 : Set the writting mode and write the image
 
-   file->SetWriteModeToDecompressed();
-   switch (mode[0])
+/*
+// Warning : SetImageData does *not* add the 7FE0|0010 Element!
+//           IsReadable() is always false
+   if( !header->IsReadable() )
    {
-      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;
+      std::cerr << "-------------------------------\n"
+                << "Error while creating the file\n"
+                << "This file is considered to be not readable\n";
+      return 1;
    }
+*/
+   fileH->SetWriteModeToRaw(); // no LUT, no compression.
+
+    // Write a DICOM Explicit VR file
+
+    fileH->SetWriteTypeToDcmExplVR();
+    std::cout << "Write DCM Explicit VR" << std::endl
+              << "File :" << fileOut << std::endl;
 
-   if( !file->Write(fileName) )
+
+   if( !fileH->Write(fileOut) )
    {
       std::cout << "-------------------------------\n"
-                   << "Error when writting the file " << fileName << "\n"
+                   << "Error when writting the file " << fileOut << "\n"
                 << "No file written\n";
    }
 
+   header->Print();
+
    delete[] imageData;
-   delete file;
-   delete header;
+   fileH->Delete();
 
    return 0;
 }
+