1 /*=========================================================================
4 Module: $RCSfile: RawToDicom.cxx,v $
6 Date: $Date: 2009/01/19 17:05:13 $
7 Version: $Revision: 1.17 $
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"
34 typedef unsigned char * PU8;
35 typedef short int * PS16;
36 typedef unsigned short int * PU16;
38 typedef unsigned int * PU32;
40 typedef double * PD64;
42 #define CRR(t1,t2) { for(int l=0;l<nX*nY*nZ*samplesPerPixel;l++) \
43 *((t2)planePixelsOut + l) = *(((t1)pixels)+ l);\
46 #define CFR(PPt) switch ( pixelTypeOutCode ) { \
47 case -8 : CRR(PPt,PS8); break; \
48 case 8 : CRR(PPt,PU8); break; \
49 case -16 : CRR(PPt,PS16); break; \
50 case 16 : CRR(PPt,PU16); break; \
51 case -32 : CRR(PPt,PS32); break; \
52 case 32 : CRR(PPt,PU32); break; \
53 case 33 : CRR(PPt,PF32); break; \
54 case 64 : CRR(PPt,PD64); break; \
58 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize);
60 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize)
65 uint16_t *im16 = (uint16_t*)Raw;
66 for( i = 0; i < RawSize / 2; i++ )
68 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
71 else if ( pixelSize == 4 )
76 uint32_t *im32 = (uint32_t*)Raw;
78 for( i = 0; i < RawSize / 4; i++ )
80 low = im32[i] & 0x0000ffff; // 3412
83 im32[i] = ( s32 << 16 ) | high;
89 int main(int argc, char *argv[])
92 " \n RawToDicom : \n ",
93 " Writes a Dicom file from a Raw File ",
94 " usage: RawToDicom filein=inputFileName ",
95 " fileout=outputFileName ",
97 " lines=nb of Lines, ",
98 " [frames = nb of Frames] //defaulted to 1 ",
99 " pixeltype={8U|8S|16U|16S|32U|32S|32F|64D} ",
100 " pixeltypeout={8U|8S|16U|16S|32U|32S} ",
101 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
102 " [samples = {1|3}} //(1:Gray,3:RGB) defaulted to 1",
104 " [studyid = ] [patientname = Patient's name] ",
107 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
108 " studyUID : *aware* user wants to add the serie ",
109 " to an already existing study ",
110 " debug : developper wants to run the program in 'debug mode' ",
114 // ------------ Initialize Arguments Manager ----------------
115 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
117 if (argc == 1 || am->ArgMgrDefined("usage") )
119 am->ArgMgrUsage(usage); // Display 'usage'
124 const char *inputFileName = am->ArgMgrGetString("filein");
125 const char *outputFileName = am->ArgMgrGetString("fileout");
127 const char *patientName = am->ArgMgrGetString("patientname", "g^Fantomas");
129 int nX = am->ArgMgrWantInt("rows", usage);
130 int nY = am->ArgMgrWantInt("lines", usage);
131 int nZ = am->ArgMgrGetInt("frames", 1);
132 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
134 int b = am->ArgMgrDefined("b");
135 int l = am->ArgMgrDefined("l");
137 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
138 const char *pixelTypeOut = am->ArgMgrGetString("pixeltypeout", pixelType);
140 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
142 if (am->ArgMgrDefined("debug"))
143 GDCM_NAME_SPACE::Debug::DebugOn();
145 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
146 const char *studyUID;
147 if (userDefinedStudy)
148 studyUID = am->ArgMgrGetString("studyUID");
150 // not described *on purpose* in the Usage !
151 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
152 const char *serieUID;
154 serieUID = am->ArgMgrGetString("serieUID");
156 /* if unused Param we give up */
157 if ( am->ArgMgrPrintUnusedLabels() )
159 am->ArgMgrUsage(usage);
164 delete am; // we don't need Argument Manager any longer
166 // ----------- End Arguments Manager ---------
168 /// \TODO Deal with all the images of a directory
171 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
174 std::cout << "Cannot open file: " << inputFileName;
180 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
182 std::string strPixelType(pixelType);
185 int pixelTypeCode; // for the switch case
187 if (strPixelType == "8S")
193 else if (strPixelType == "8U")
199 else if (strPixelType == "16S")
205 else if (strPixelType == "16U")
211 else if (strPixelType == "32S")
217 else if (strPixelType == "32U")
223 else if (strPixelType == "32F")
230 else if (strPixelType == "64D")
238 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
242 std::string strPixelTypeOut(pixelTypeOut);
245 int pixelTypeOutCode; // for the switch case
247 if (strPixelTypeOut == "8S")
251 pixelTypeOutCode = -8;
253 else if (strPixelTypeOut == "8U")
257 pixelTypeOutCode = 8;
259 else if (strPixelTypeOut == "16S")
263 pixelTypeOutCode = -16;
265 else if (strPixelTypeOut == "16U")
269 pixelTypeOutCode = 16;
271 else if (strPixelTypeOut == "32S")
275 pixelTypeOutCode = -32;
277 else if (strPixelTypeOut == "32U")
281 pixelTypeOutCode = 32;
285 std::cout << "Wrong 'pixeltypeout' (" << strPixelTypeOut << ")" << std::endl;
289 std::string strStudyUID;
290 std::string strSerieUID;
292 if (userDefinedStudy)
293 strSerieUID = studyUID;
295 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
297 if (userDefinedSerie)
298 strSerieUID = serieUID;
300 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
304 int singlePlaneDataSize = nX*nY*samplesPerPixel*pixelSizeOut;
305 int dataSizeIn = nX*nY*samplesPerPixel*pixelSize*nZ;
307 uint8_t *pixels = new uint8_t[dataSizeIn];
308 uint8_t *planePixelsOut = new uint8_t[singlePlaneDataSize];
310 Fp->read((char*)pixels, (size_t)dataSizeIn);
312 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
314 ConvertSwapZone(pixelSize, pixels, dataSizeIn);
317 // Copy (and convert) pixels of a single plane
319 switch ( pixelTypeCode )
321 case 8 : CFR(PU8); break;
322 case -8 : CFR(PS8); break;
323 case -16 : CFR(PU16); break;
324 case 16 : CFR(PS16); break;
325 case -32 : CFR(PS32); break;
326 case 32 : CFR(PU32); break;
327 case 33 : CFR(PF32); break;
328 case 64 : CFR(PD64); break;
331 // Create an empty FileHelper
333 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
335 // Get the (empty) image header.
336 GDCM_NAME_SPACE::File *fileToBuild = fileH->GetFile();
338 // 'Study Instance UID'
339 // The user is allowed to create his own Study,
340 // keeping the same 'Study Instance UID' for various images
341 // The user may add images to a 'Manufacturer Study',
342 // adding new Series to an already existing Study
344 fileToBuild->InsertEntryString(strStudyUID,0x0020,0x000d,"UI"); // Study UID
346 // 'Serie Instance UID'
347 // The user is allowed to create his own Series,
348 // keeping the same 'Serie Instance UID' for various images
349 // The user shouldn't add any image to a 'Manufacturer Serie'
350 // but there is no way no to prevent him for doing that
352 fileToBuild->InsertEntryString(strSerieUID,0x0020,0x000e,"UI"); // Serie UID
354 std::ostringstream str;
356 // Set the image size
359 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
362 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
366 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
368 // Set the pixel type
371 str << pixelSizeOut*8;
372 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
375 str << pixelSizeOut*8;
376 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
379 str << ( pixelSizeOut*8 - 1 );
380 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
384 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
386 // If you deal with a Serie of images, as slices of a volume,
387 // it up to you to tell gdcm, for each image, what are the values of :
389 // 0020 0032 DS 3 Image Position (Patient)
390 // 0020 0037 DS 6 Image Orientation (Patient)
393 str << "0.0 \\ 0.0 \\ 0.0";
394 fileToBuild->InsertEntryString(str.str(),0x0020,0x0032, "DS");
397 str << samplesPerPixel;
398 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
400 if (strlen(patientName) != 0)
401 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
405 fileH->SetPhotometricInterpretationToMonochrome1();
407 // Set the image Pixel Data
408 fileH->SetImageData(planePixelsOut,singlePlaneDataSize);
410 // Set the writting mode and write the image
411 fileH->SetWriteModeToRaw();
413 // Write a DICOM Explicit VR file
414 fileH->SetWriteTypeToDcmExplVR();
416 if( !fileH->Write(outputFileName ) )
418 std::cout << "Failed for [" << outputFileName << "]\n"
419 << " File is unwrittable\n";
425 delete[] planePixelsOut;