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