1 /*=========================================================================
4 Module: $RCSfile: RawToDicom.cxx,v $
6 Date: $Date: 2008/06/12 13:21:20 $
7 Version: $Revision: 1.16 $
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"
28 #include "gdcmArgMgr.h"
33 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize);
35 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize)
40 uint16_t *im16 = (uint16_t*)Raw;
41 for( i = 0; i < RawSize / 2; i++ )
43 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
46 else if ( pixelSize == 4 )
51 uint32_t *im32 = (uint32_t*)Raw;
53 for( i = 0; i < RawSize / 4; i++ )
55 low = im32[i] & 0x0000ffff; // 3412
58 im32[i] = ( s32 << 16 ) | high;
64 int main(int argc, char *argv[])
67 " \n RawToDicom : \n ",
68 " Writes a Dicom file from a Raw File ",
69 " usage: RawToDicom filein=inputFileName ",
70 " fileout=outputFileName ",
72 " lines=nb of Lines, ",
73 " [frames = nb of Frames] //defaulted to 1 ",
74 " pixeltype={8U|8S|16U|16S|32U|32S} ",
75 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
76 " [samples = {1|3}} //(1:Gray,3:RGB) defaulted to 1",
78 " [studyid = ] [patientname = Patient's name] ",
81 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
82 " studyUID : *aware* user wants to add the serie ",
83 " to an already existing study ",
84 " debug : developper wants to run the program in 'debug mode' ",
88 // ------------ Initialize Arguments Manager ----------------
89 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
91 if (argc == 1 || am->ArgMgrDefined("usage") )
93 am->ArgMgrUsage(usage); // Display 'usage'
98 const char *inputFileName = am->ArgMgrGetString("filein");
99 const char *outputFileName = am->ArgMgrGetString("fileout");
101 const char *patientName = am->ArgMgrGetString("patientname", "g^Fantomas");
103 int nX = am->ArgMgrWantInt("rows", usage);
104 int nY = am->ArgMgrWantInt("lines", usage);
105 int nZ = am->ArgMgrGetInt("frames", 1);
106 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
108 int b = am->ArgMgrDefined("b");
109 int l = am->ArgMgrDefined("l");
111 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
113 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
115 if (am->ArgMgrDefined("debug"))
116 GDCM_NAME_SPACE::Debug::DebugOn();
118 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
119 const char *studyUID;
120 if (userDefinedStudy)
121 studyUID = am->ArgMgrGetString("studyUID");
123 // not described *on purpose* in the Usage !
124 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
125 const char *serieUID;
127 serieUID = am->ArgMgrGetString("serieUID");
129 /* if unused Param we give up */
130 if ( am->ArgMgrPrintUnusedLabels() )
132 am->ArgMgrUsage(usage);
137 delete am; // we don't need Argument Manager any longer
139 // ----------- End Arguments Manager ---------
141 /// \TODO Deal with all the images of a directory
144 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
147 std::cout << "Cannot open file: " << inputFileName;
153 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
155 std::string strPixelType(pixelType);
159 if (strPixelType == "8S")
164 else if (strPixelType == "8U")
169 else if (strPixelType == "16S")
174 else if (strPixelType == "16U")
179 else if (strPixelType == "32S")
184 else if (strPixelType == "32U")
191 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
195 std::string strStudyUID;
196 std::string strSerieUID;
198 if (userDefinedStudy)
199 strSerieUID = studyUID;
201 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
203 if (userDefinedSerie)
204 strSerieUID = serieUID;
206 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
210 int dataSize = nX*nY*nZ*samplesPerPixel*pixelSize;
212 uint8_t *pixels = new uint8_t[dataSize];
214 Fp->read((char*)pixels, (size_t)dataSize);
216 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
218 ConvertSwapZone(pixelSize, pixels, dataSize);
221 // Create an empty FileHelper
223 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
225 // Get the (empty) image header.
226 GDCM_NAME_SPACE::File *fileToBuild = fileH->GetFile();
228 // 'Study Instance UID'
229 // The user is allowed to create his own Study,
230 // keeping the same 'Study Instance UID' for various images
231 // The user may add images to a 'Manufacturer Study',
232 // adding new Series to an already existing Study
234 fileToBuild->InsertEntryString(strStudyUID,0x0020,0x000d,"UI"); // Study UID
236 // 'Serie Instance UID'
237 // The user is allowed to create his own Series,
238 // keeping the same 'Serie Instance UID' for various images
239 // The user shouldn't add any image to a 'Manufacturer Serie'
240 // but there is no way no to prevent him for doing that
242 fileToBuild->InsertEntryString(strSerieUID,0x0020,0x000e,"UI"); // Serie UID
244 std::ostringstream str;
246 // Set the image size
249 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
252 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
256 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
258 // Set the pixel type
262 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
266 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
269 str << ( pixelSize*8 - 1 );
270 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
274 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
277 str << samplesPerPixel;
279 // If you deal with a Serie of images, as slices of a volume,
280 // it up to you to tell gdcm, for each image, what are the values of :
282 // 0020 0032 DS 3 Image Position (Patient)
283 // 0020 0037 DS 6 Image Orientation (Patient)
285 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
287 if (strlen(patientName) != 0)
288 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
292 fileH->SetPhotometricInterpretationToMonochrome1();
294 // Set the image Pixel Data
295 fileH->SetImageData(pixels,dataSize);
297 // Set the writting mode and write the image
298 fileH->SetWriteModeToRaw();
300 // Write a DICOM Explicit VR file
301 fileH->SetWriteTypeToDcmExplVR();
303 if( !fileH->Write(outputFileName ) )
305 std::cout << "Failed for [" << outputFileName << "]\n"
306 << " File is unwrittable\n";