]> Creatis software - gdcm.git/blob - Example/WriteDicomSimple.cxx
Fix mistypings
[gdcm.git] / Example / WriteDicomSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteDicomSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/26 15:42:14 $
7   Version:   $Revision: 1.20 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 /**
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 
23  * 
24  */
25 #include "gdcmFile.h"
26 #include "gdcmFileHelper.h"
27 #include "gdcmArgMgr.h"
28  
29 #include <iostream>
30 #include <sstream>
31
32 // Image size
33  uint16_t SIZE_X;
34  uint16_t SIZE_Y;
35 // Number of components in the image (3 for RGB)
36  uint16_t  COMPONENT;
37 // Size of each component (in byte)
38  uint16_t  COMPONENT_SIZE;
39
40
41 int main(int argc, char *argv[])
42 {
43
44
45    START_USAGE(usage)
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] ",
52    FINISH_USAGE
53
54    // Initialize Arguments Manager   
55    GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
56   
57    if (argc == 1 || am->ArgMgrDefined("usage") )
58    {
59       am->ArgMgrUsage(usage); // Display 'usage'
60       delete am;
61       return 1;
62    }
63
64    if (am->ArgMgrDefined("debug"))
65       GDCM_NAME_SPACE::Debug::DebugOn();
66       
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);
72    
73    /* if unused Param we give up */
74    if ( am->ArgMgrPrintUnusedLabels() )
75    {
76       am->ArgMgrUsage(usage);
77       delete am;
78       return 1;
79    } 
80
81    delete am;  // we don't need Argument Manager any longer
82
83    // ----------- End Arguments Manager ---------
84    
85    
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();
89    
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();
93     
94    std::ostringstream str;
95
96    // Set the image size
97    str.str("");
98    str << SIZE_X;
99    header->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
100
101    str.str("");
102    str << SIZE_Y;
103    header->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
104
105    // Set the pixel type
106    str.str("");
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
110
111    str.str("");
112    str << ( COMPONENT_SIZE * 8 ) - 1;
113    header->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
114
115    // Set the pixel representation
116    str.str("");
117    str << "0"; // Unsigned
118    header->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
119
120    // Set the samples per pixel
121    str.str("");
122    str << COMPONENT;
123    header->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
124
125
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];
129
130    unsigned char *tmp = imageData;
131    for(int j=0;j<SIZE_Y;j++)
132    {
133       for(int i=0;i<SIZE_X;i++)
134       {
135          for(int c=0;c<COMPONENT;c++)
136          {
137             *tmp = (unsigned char)j;
138             tmp += COMPONENT_SIZE; 
139          }
140       }
141    }
142
143 // Step 3 : Set the 'Pixel Area' of the image
144
145    fileH->SetImageData(imageData,size);
146    header->Print();
147    std::cout << "-------------------------------" << std::endl;
148    
149       // Step 4 : Set the writting mode and write the image
150
151 /*
152 // Warning : SetImageData does *not* add the 7FE0|0010 Element!
153 //           IsReadable() is always false
154    if( !header->IsReadable() )
155    {
156       std::cerr << "-------------------------------\n"
157                 << "Error while creating the file\n"
158                 << "This file is considered to be not readable\n";
159       return 1;
160    }
161 */
162    fileH->SetWriteModeToRaw(); // no LUT, no compression.
163
164     // Write a DICOM Explicit VR file
165
166     fileH->SetWriteTypeToDcmExplVR();
167     std::cout << "Write DCM Explicit VR" << std::endl
168               << "File :" << fileOut << std::endl;
169
170
171    if( !fileH->Write(fileOut) )
172    {
173       std::cout << "-------------------------------\n"
174                    << "Error when writting the file " << fileOut << "\n"
175                 << "No file written\n";
176    }
177
178    header->Print();
179
180    delete[] imageData;
181    fileH->Delete();
182
183    return 0;
184 }
185