1 /*=========================================================================
4 Module: $RCSfile: WriteDicomSimple.cxx,v $
6 Date: $Date: 2004/12/10 16:48:37 $
7 Version: $Revision: 1.4 $
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
25 #include "gdcmHeader.h"
33 // Number of components in the image (3 for RGB)
35 // Size of each component (in byte)
36 #define COMPONENT_SIZE 1
38 #define COLOR_WINDOW 256
39 #define COLOR_LEVEL 128
41 int main(int argc, char* argv[])
45 std::cerr << "usage: \n"
46 << argv[0] << " Output Mode " << std::endl
47 << "Output : output file name\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"
58 // Step 1 : Create the header of the image
59 gdcm::Header *header = new gdcm::Header();
60 std::ostringstream str;
65 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011); // Columns
69 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010); // Rows
73 str << COMPONENT_SIZE * 8;
74 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100); // Bits Allocated
75 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101); // Bits Stored
78 str << COMPONENT_SIZE * 8 - 1;
79 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102); // High Bit
81 // Set the pixel representation
83 str << "0"; // Unsigned
84 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103); // Pixel Representation
86 // Set the samples per pixel
89 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002); // Samples per Pixel
91 // Set the Window / Level
94 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x1051); // Window Width
97 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050); // Window Center
99 if( !header->IsReadable() )
101 std::cerr << "-------------------------------\n"
102 << "Error while creating the file\n"
103 << "This file is considered to be not readable\n";
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];
112 unsigned char *tmp = imageData;
113 for(int j=0;j<SIZE_Y;j++)
115 for(int i=0;i<SIZE_X;i++)
117 for(int c=0;c<COMPONENT;c++)
120 tmp += COMPONENT_SIZE;
125 // Step 3 : Create the file of the image
126 gdcm::File *file = new gdcm::File(header);
127 file->SetImageData(imageData,size);
129 // Step 4 : Set the writting mode and write the image
130 std::string fileName = argv[1];
131 std::string mode = argv[2];
133 file->SetWriteModeToRaw();
136 case 'a' : // Write an ACR file
138 file->SetWriteTypeToAcr();
139 std::cout << "Write ACR" << std::endl
140 << "File :" << fileName << std::endl;
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;
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;
157 case 'r' : // Write a RAW file
159 file->WriteRawData(fileName);
160 std::cout << "Write Raw" << std::endl
161 << "File :" << fileName << std::endl;
169 std::cout << "-------------------------------\n"
170 << "Write mode undefined...\n"
171 << "No file written\n";
179 if( !file->Write(fileName) )
181 std::cout << "-------------------------------\n"
182 << "Error when writting the file " << fileName << "\n"
183 << "No file written\n";