]> Creatis software - gdcm.git/blob - Example/WriteDicomSimple.cxx
According to Benoit's suggestion, and without any objection from anybody
[gdcm.git] / Example / WriteDicomSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteDicomSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/08 15:03:57 $
7   Version:   $Revision: 1.5 $
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 "gdcmHeader.h"
26 #include "gdcmFile.h"
27
28 #include <iostream>
29
30 // Image size
31 #define SIZE_X          256
32 #define SIZE_Y          256
33 // Number of components in the image (3 for RGB)
34 #define COMPONENT       1
35 // Size of each component (in byte)
36 #define COMPONENT_SIZE  1
37 // Window / Level
38 #define COLOR_WINDOW    256
39 #define COLOR_LEVEL     128
40
41 int main(int argc, char* argv[])
42 {
43    if (argc < 3) 
44    {
45       std::cerr << "usage: \n" 
46                 << argv[0] << " Output Mode " << std::endl 
47                 << "Output : output file name\n"
48                 << "Mode : \n"
49                 << "   a : ACR, produces a file named Output.ACR\n"
50                 << "   e : DICOM Explicit VR, produces a file named Output.E.DCM\n"
51                 << "   i : DICOM Implicit VR, produces a file named Output.I.DCM\n"
52                 << "   r : RAW, produces a file named Output.RAW\n"
53                 << std::endl;
54       return 1;
55    }
56
57
58 // Step 1 : Create the header of the image
59    gdcm::Header *header = new gdcm::Header();
60    std::ostringstream str;
61
62    // Set the image size
63    str.str("");
64    str << SIZE_X;
65    header->ReplaceOrCreate(str.str(),0x0028,0x0011); // Columns
66
67    str.str("");
68    str << SIZE_Y;
69    header->ReplaceOrCreate(str.str(),0x0028,0x0010); // Rows
70
71    // Set the pixel type
72    str.str("");
73    str << COMPONENT_SIZE * 8;
74    header->ReplaceOrCreate(str.str(),0x0028,0x0100); // Bits Allocated
75    header->ReplaceOrCreate(str.str(),0x0028,0x0101); // Bits Stored
76
77    str.str("");
78    str << COMPONENT_SIZE * 8 - 1;
79    header->ReplaceOrCreate(str.str(),0x0028,0x0102); // High Bit
80
81    // Set the pixel representation
82    str.str("");
83    str << "0"; // Unsigned
84    header->ReplaceOrCreate(str.str(),0x0028,0x0103); // Pixel Representation
85
86    // Set the samples per pixel
87    str.str("");
88    str << COMPONENT;
89    header->ReplaceOrCreate(str.str(),0x0028,0x0002); // Samples per Pixel
90
91    // Set the Window / Level
92    str.str("");
93    str << COLOR_WINDOW;
94    header->ReplaceOrCreate(str.str(),0x0028,0x1051); // Window Width
95    str.str("");
96    str << COLOR_LEVEL;
97    header->ReplaceOrCreate(str.str(),0x0028,0x1050); // Window Center
98
99    if( !header->IsReadable() )
100    {
101       std::cerr << "-------------------------------\n"
102                 << "Error while creating the file\n"
103                 << "This file is considered to be not readable\n";
104
105       return 1;
106    }
107
108 // Step 2 : Create the output image
109    size_t size = SIZE_X * SIZE_Y * COMPONENT * COMPONENT_SIZE;
110    unsigned char *imageData = new unsigned char[size];
111
112    unsigned char *tmp = imageData;
113    for(int j=0;j<SIZE_Y;j++)
114    {
115       for(int i=0;i<SIZE_X;i++)
116       {
117          for(int c=0;c<COMPONENT;c++)
118          {
119             *tmp = j;
120             tmp += COMPONENT_SIZE; 
121          }
122       }
123    }
124
125 // Step 3 : Create the file of the image
126    gdcm::File *file = new gdcm::File(header);
127    file->SetImageData(imageData,size);
128
129 // Step 4 : Set the writting mode and write the image
130    std::string fileName = argv[1]; 
131    std::string mode = argv[2];
132
133    file->SetWriteModeToRaw();
134    switch (mode[0])
135    {
136       case 'a' : // Write an ACR file
137          fileName += ".ACR";
138          file->SetWriteTypeToAcr();
139          std::cout << "Write ACR" << std::endl
140                    << "File :" << fileName << std::endl;
141          break;
142
143       case 'e' : // Write a DICOM Explicit VR file
144          fileName += ".E.DCM";
145          file->SetWriteTypeToDcmExplVR();
146          std::cout << "Write DCM Explicit VR" << std::endl
147                    << "File :" << fileName << std::endl;
148          break;
149
150       case 'i' : // Write a DICOM Implicit VR file
151          fileName += ".I.DCM";
152          file->SetWriteTypeToDcmImplVR();
153          std::cout << "Write DCM Implicit VR" << std::endl
154                    << "File :" << fileName << std::endl;
155          break;
156
157       case 'r' : // Write a RAW file
158          fileName += ".RAW";
159          file->WriteRawData(fileName);
160          std::cout << "Write Raw" << std::endl
161                    << "File :" << fileName << std::endl;
162
163          delete[] imageData;
164          delete file;
165          delete header;
166          return 0;
167
168       default :
169          std::cout << "-------------------------------\n"
170                    << "Write mode undefined...\n"
171                    << "No file written\n";
172
173          delete[] imageData;
174          delete file;
175          delete header;
176          return 1;
177    }
178
179    if( !file->Write(fileName) )
180    {
181       std::cout << "-------------------------------\n"
182                    << "Error when writting the file " << fileName << "\n"
183                 << "No file written\n";
184    }
185
186    delete[] imageData;
187    delete file;
188    delete header;
189
190    return 0;
191 }
192