1 /*=========================================================================
4 Module: $RCSfile: WriteDicomSimple.cxx,v $
6 Date: $Date: 2007/06/26 15:42:14 $
7 Version: $Revision: 1.20 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 * Write a dicom file from nothing
21 * The written image is 256x256, 8 bits, unsigned char
22 * The image content is a horizontal grayscale from
26 #include "gdcmFileHelper.h"
27 #include "gdcmArgMgr.h"
35 // Number of components in the image (3 for RGB)
37 // Size of each component (in byte)
38 uint16_t COMPONENT_SIZE;
41 int main(int argc, char *argv[])
46 " \n exWriteDicomSimple : \n ",
47 " Creates a Dicom image File ",
48 " usage: exWriteDicomSimple {fileout=outputFileName} ",
49 " [nx=number of colomns] [ny=number of lines] ",
50 " [components= 1: grey, 3 : RGB] ",
51 " [pixelsize= Pixel Size in Bytes : 1/2] } ] [debug] ",
54 // Initialize Arguments Manager
55 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
57 if (argc == 1 || am->ArgMgrDefined("usage") )
59 am->ArgMgrUsage(usage); // Display 'usage'
64 if (am->ArgMgrDefined("debug"))
65 GDCM_NAME_SPACE::Debug::DebugOn();
67 std::string fileOut = am->ArgMgrGetString("fileout",(char *)"WriteDicomSimple.dcm");
68 SIZE_X = am->ArgMgrGetInt("NX", 128);
69 SIZE_Y = am->ArgMgrGetInt("NY", 128);
70 COMPONENT = am->ArgMgrGetInt("components", 1);
71 COMPONENT_SIZE = am->ArgMgrGetInt("size", 1);
73 /* if unused Param we give up */
74 if ( am->ArgMgrPrintUnusedLabels() )
76 am->ArgMgrUsage(usage);
81 delete am; // we don't need Argument Manager any longer
83 // ----------- End Arguments Manager ---------
86 // Step 1 : Create an empty GDCM_NAME_SPACE::FileHelper for the image
87 // (it deals with the acces to the pixels)
88 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
90 // Get the empty GDCM_NAME_SPACE::File of the image
91 // (it deals with the 'entries' od the image header)
92 GDCM_NAME_SPACE::File *header = fileH->GetFile();
94 std::ostringstream str;
99 header->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
103 header->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
105 // Set the pixel type
107 str << COMPONENT_SIZE * 8;
108 header->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
109 header->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
112 str << ( COMPONENT_SIZE * 8 ) - 1;
113 header->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
115 // Set the pixel representation
117 str << "0"; // Unsigned
118 header->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
120 // Set the samples per pixel
123 header->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
126 // Step 2 : Create the output image
127 size_t size = SIZE_X * SIZE_Y * COMPONENT * COMPONENT_SIZE;
128 unsigned char *imageData = new unsigned char[size];
130 unsigned char *tmp = imageData;
131 for(int j=0;j<SIZE_Y;j++)
133 for(int i=0;i<SIZE_X;i++)
135 for(int c=0;c<COMPONENT;c++)
137 *tmp = (unsigned char)j;
138 tmp += COMPONENT_SIZE;
143 // Step 3 : Set the 'Pixel Area' of the image
145 fileH->SetImageData(imageData,size);
147 std::cout << "-------------------------------" << std::endl;
149 // Step 4 : Set the writting mode and write the image
152 // Warning : SetImageData does *not* add the 7FE0|0010 Element!
153 // IsReadable() is always false
154 if( !header->IsReadable() )
156 std::cerr << "-------------------------------\n"
157 << "Error while creating the file\n"
158 << "This file is considered to be not readable\n";
162 fileH->SetWriteModeToRaw(); // no LUT, no compression.
164 // Write a DICOM Explicit VR file
166 fileH->SetWriteTypeToDcmExplVR();
167 std::cout << "Write DCM Explicit VR" << std::endl
168 << "File :" << fileOut << std::endl;
171 if( !fileH->Write(fileOut) )
173 std::cout << "-------------------------------\n"
174 << "Error when writting the file " << fileOut << "\n"
175 << "No file written\n";