X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=Example%2FWriteDicomSimple.cxx;h=80856449be282ebc5f2bcbc9bae93b92f80fd302;hb=7a111e0401fb1a09aa0d2dd4ab8d5bc6e4890f82;hp=19f8a9c818560a8dda99c4febeeb8f3edc6d2891;hpb=d00078b5e19310b379c8339fa8fe38362e8ca392;p=gdcm.git diff --git a/Example/WriteDicomSimple.cxx b/Example/WriteDicomSimple.cxx index 19f8a9c8..80856449 100644 --- a/Example/WriteDicomSimple.cxx +++ b/Example/WriteDicomSimple.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: WriteDicomSimple.cxx,v $ Language: C++ - Date: $Date: 2005/01/20 16:16:58 $ - Version: $Revision: 1.7 $ + 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 @@ -22,89 +22,106 @@ * The image content is a horizontal grayscale from * */ -#include "gdcmHeader.h" +#include "gdcmFile.h" #include "gdcmFileHelper.h" - +#include "gdcmArgMgr.h" + #include #include // 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, char* argv[]) +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->ReplaceOrCreate(str.str(),0x0028,0x0011); // Columns + header->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns str.str(""); str << SIZE_Y; - header->ReplaceOrCreate(str.str(),0x0028,0x0010); // Rows + header->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows // Set the pixel type str.str(""); str << COMPONENT_SIZE * 8; - header->ReplaceOrCreate(str.str(),0x0028,0x0100); // Bits Allocated - header->ReplaceOrCreate(str.str(),0x0028,0x0101); // Bits Stored + 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->ReplaceOrCreate(str.str(),0x0028,0x0102); // High Bit + str << ( COMPONENT_SIZE * 8 ) - 1; + header->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit // Set the pixel representation str.str(""); str << "0"; // Unsigned - header->ReplaceOrCreate(str.str(),0x0028,0x0103); // Pixel Representation + header->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation // Set the samples per pixel str.str(""); str << COMPONENT; - header->ReplaceOrCreate(str.str(),0x0028,0x0002); // Samples per Pixel - - // Set the Window / Level - str.str(""); - str << COLOR_WINDOW; - header->ReplaceOrCreate(str.str(),0x0028,0x1051); // Window Width - str.str(""); - str << COLOR_LEVEL; - header->ReplaceOrCreate(str.str(),0x0028,0x1050); // Window Center - - if( !header->IsReadable() ) - { - std::cerr << "-------------------------------\n" - << "Error while creating the file\n" - << "This file is considered to be not readable\n"; + header->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel - return 1; - } // Step 2 : Create the output image size_t size = SIZE_X * SIZE_Y * COMPONENT * COMPONENT_SIZE; @@ -117,76 +134,51 @@ int main(int argc, char* argv[]) { for(int c=0;cSetImageData(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->SetWriteModeToRaw(); - 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 - if( !file->Write(fileName) ) + fileH->SetWriteTypeToDcmExplVR(); + std::cout << "Write DCM Explicit VR" << std::endl + << "File :" << fileOut << std::endl; + + + 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; }