1 /*=========================================================================
4 Module: $RCSfile: RawToDicom.cxx,v $
6 Date: $Date: 2005/12/16 15:42:54 $
7 Version: $Revision: 1.3 $
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 * The image content is a horizontal grayscale from
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 " rows=nb of Rows, lines=nb of Lines, ",
42 " [frames = nb of Frames] //defaulted to 1 ",
43 " pixeltype={8U|8S|16U|16S} ",
44 " [samples = {1|3}} //defaulted to 1; 3 = RGB ",
45 " fileout=outputFileName ",
47 " debug : user wants to run the program in 'debug mode' ",
51 // Initialize Arguments Manager
52 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
54 if (argc == 1 || am->ArgMgrDefined("usage") )
56 am->ArgMgrUsage(usage); // Display 'usage'
61 char *inputFileName = am->ArgMgrGetString("filein",(char *)0);
62 char *outputFileName = am->ArgMgrGetString("fileout",(char *)0);
63 //char *dirName = am->ArgMgrGetString("dirin",(char *)0);
65 int nX = am->ArgMgrWantInt("rows", usage);
66 int nY = am->ArgMgrWantInt("lines", usage);
67 int nZ = am->ArgMgrGetInt("frames", 1);
68 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
70 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
72 if (am->ArgMgrDefined("debug"))
73 gdcm::Debug::DebugOn();
75 /* if unused Param we give up */
76 if ( am->ArgMgrPrintUnusedLabels() )
78 am->ArgMgrUsage(usage);
83 delete am; // we don't need Argument Manager any longer
85 // ----------- End Arguments Manager ---------
89 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
92 std::cout << "Cannot open file: " << inputFileName;
98 std::string strPixelType(pixelType);
102 if (strPixelType == "8S")
107 else if (strPixelType == "8U")
112 else if (strPixelType == "16S")
117 else if (strPixelType == "16U")
123 int dataSize = nX*nY*nZ*pixelSize*samplesPerPixel;
124 uint8_t *pixels = new uint8_t[dataSize];
126 Fp->read((char*)pixels, (size_t)dataSize);
129 // Create an empty FileHelper
131 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
133 // Get the (empty) image header.
134 gdcm::File *fileToBuild = fileH->GetFile();
135 std::ostringstream str;
137 // Set the image size
140 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
143 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
147 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
149 // Set the pixel type
153 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
157 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
160 str << ( pixelSize*8 - 1 );
161 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
165 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
168 str << samplesPerPixel;
169 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
172 // Set the image Pixel Data
173 fileH->SetImageData(pixels,dataSize);
175 // Set the writting mode and write the image
176 fileH->SetWriteModeToRaw();
178 // Write a DICOM Explicit VR file
179 fileH->SetWriteTypeToDcmExplVR();
181 if( !fileH->Write(outputFileName ) )
183 std::cout << "Failed for [" << outputFileName << "]\n"
184 << " File is unwrittable\n";