1 /*=========================================================================
4 Module: $RCSfile: WriteDicomSimple.cxx,v $
6 Date: $Date: 2005/01/21 11:40:53 $
7 Version: $Revision: 1.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.
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"
34 // Number of components in the image (3 for RGB)
36 // Size of each component (in byte)
37 #define COMPONENT_SIZE 1
39 #define COLOR_WINDOW 256
40 #define COLOR_LEVEL 128
42 int main(int argc, char* argv[])
46 std::cerr << "usage: \n"
47 << argv[0] << " Output Mode " << std::endl
48 << "Output : output file name\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"
59 // Step 1 : Create the header of the image
60 gdcm::File *header = new gdcm::File();
61 std::ostringstream str;
66 header->ReplaceOrCreate(str.str(),0x0028,0x0011); // Columns
70 header->ReplaceOrCreate(str.str(),0x0028,0x0010); // Rows
74 str << COMPONENT_SIZE * 8;
75 header->ReplaceOrCreate(str.str(),0x0028,0x0100); // Bits Allocated
76 header->ReplaceOrCreate(str.str(),0x0028,0x0101); // Bits Stored
79 str << COMPONENT_SIZE * 8 - 1;
80 header->ReplaceOrCreate(str.str(),0x0028,0x0102); // High Bit
82 // Set the pixel representation
84 str << "0"; // Unsigned
85 header->ReplaceOrCreate(str.str(),0x0028,0x0103); // Pixel Representation
87 // Set the samples per pixel
90 header->ReplaceOrCreate(str.str(),0x0028,0x0002); // Samples per Pixel
92 // Set the Window / Level
95 header->ReplaceOrCreate(str.str(),0x0028,0x1051); // Window Width
98 header->ReplaceOrCreate(str.str(),0x0028,0x1050); // Window Center
100 if( !header->IsReadable() )
102 std::cerr << "-------------------------------\n"
103 << "Error while creating the file\n"
104 << "This file is considered to be not readable\n";
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];
113 unsigned char *tmp = imageData;
114 for(int j=0;j<SIZE_Y;j++)
116 for(int i=0;i<SIZE_X;i++)
118 for(int c=0;c<COMPONENT;c++)
121 tmp += COMPONENT_SIZE;
126 // Step 3 : Create the file of the image
127 gdcm::FileHelper *file = new gdcm::FileHelper(header);
128 file->SetImageData(imageData,size);
130 // Step 4 : Set the writting mode and write the image
131 std::string fileName = argv[1];
132 std::string mode = argv[2];
134 file->SetWriteModeToRaw();
137 case 'a' : // Write an ACR file
139 file->SetWriteTypeToAcr();
140 std::cout << "Write ACR" << std::endl
141 << "File :" << fileName << std::endl;
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;
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;
158 case 'r' : // Write a RAW file
160 file->WriteRawData(fileName);
161 std::cout << "Write Raw" << std::endl
162 << "File :" << fileName << std::endl;
170 std::cout << "-------------------------------\n"
171 << "Write mode undefined...\n"
172 << "No file written\n";
180 if( !file->Write(fileName) )
182 std::cout << "-------------------------------\n"
183 << "Error when writting the file " << fileName << "\n"
184 << "No file written\n";