1 /*=========================================================================
4 Module: $RCSfile: RawToDicom.cxx,v $
6 Date: $Date: 2006/01/26 15:52:43 $
7 Version: $Revision: 1.5 $
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 * Writes a Dicom file from a Raw File
21 * User has to supply parameters.
25 #include "gdcmFileHelper.h"
26 #include "gdcmDebug.h"
27 #include "gdcmArgMgr.h"
34 int main(int argc, char *argv[])
38 " \n RawToDicom : \n ",
39 " Writes a Dicom file from a Raw File ",
40 " usage: RawToDicom filein=inputFileName ",
41 " fileout=outputFileName ",
43 " lines=nb of Lines, ",
44 " pixeltype={8U|8S|16U|16S} ",
45 " [frames = nb of Frames] //defaulted to 1 ",
46 " [samples = {1|3}} //defaulted to 1(1:Gray,3:RGB) ",
47 " [patientname = Patient's name] ",
50 " debug : user wants to run the program in 'debug mode' ",
54 // Initialize Arguments Manager
55 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
57 if (argc == 1 || am->ArgMgrDefined("usage") )
59 am->ArgMgrUsage(usage); // Display 'usage'
64 const char *inputFileName = am->ArgMgrGetString("filein");
65 const char *outputFileName = am->ArgMgrGetString("fileout");
66 //const char *dirName = am->ArgMgrGetString("dirin");
68 const char *patientName = am->ArgMgrGetString("patientname");
70 int nX = am->ArgMgrWantInt("rows", usage);
71 int nY = am->ArgMgrWantInt("lines", usage);
72 int nZ = am->ArgMgrGetInt("frames", 1);
73 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
76 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
78 if (am->ArgMgrDefined("debug"))
79 gdcm::Debug::DebugOn();
81 /* if unused Param we give up */
82 if ( am->ArgMgrPrintUnusedLabels() )
84 am->ArgMgrUsage(usage);
89 delete am; // we don't need Argument Manager any longer
91 // ----------- End Arguments Manager ---------
95 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
98 std::cout << "Cannot open file: " << inputFileName;
104 std::string strPixelType(pixelType);
108 if (strPixelType == "8S")
113 else if (strPixelType == "8U")
118 else if (strPixelType == "16S")
123 else if (strPixelType == "16U")
129 int dataSize = nX*nY*nZ*pixelSize*samplesPerPixel;
130 uint8_t *pixels = new uint8_t[dataSize];
132 Fp->read((char*)pixels, (size_t)dataSize);
135 // Create an empty FileHelper
137 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
139 // Get the (empty) image header.
140 gdcm::File *fileToBuild = fileH->GetFile();
141 std::ostringstream str;
143 // Set the image size
146 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
149 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
153 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
155 // Set the pixel type
159 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
163 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
166 str << ( pixelSize*8 - 1 );
167 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
171 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
174 str << samplesPerPixel;
175 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
177 if (strlen(patientName) != 0)
178 fileToBuild->InsertEntryString(patientName,0x0010,0x0010); // Patient's Name
180 // Set the image Pixel Data
181 fileH->SetImageData(pixels,dataSize);
183 // Set the writting mode and write the image
184 fileH->SetWriteModeToRaw();
186 // Write a DICOM Explicit VR file
187 fileH->SetWriteTypeToDcmExplVR();
189 if( !fileH->Write(outputFileName ) )
191 std::cout << "Failed for [" << outputFileName << "]\n"
192 << " File is unwrittable\n";