1 /*=========================================================================
4 Module: $RCSfile: RawToDicomStack.cxx,v $
6 Date: $Date: 2009/01/14 14:07:40 $
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 * 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 i=0;i<nY;i++) \
43 for(int j=0;j<nX;j++) \
44 for(int k=0;k<samplesPerPixel;k++) {\
45 *((t2)planePixelsOut + i*nX +j+k) = *(((t1)pixels)+ nbPlanes*nX*nY + i*nX +j+k);\
49 #define CFR(PPt) switch ( pixelTypeOutCode ) { \
50 case -8 : CRR(PPt,PS8); break; \
51 case 8 : CRR(PPt,PU8); break; \
52 case -16 : CRR(PPt,PS16); break; \
53 case 16 : CRR(PPt,PU16); break; \
54 case -32 : CRR(PPt,PS32); break; \
55 case 32 : CRR(PPt,PU32); break; \
56 case 33 : CRR(PPt,PF32); break; \
57 case 64 : CRR(PPt,PD64); break; \
61 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize);
63 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize)
68 uint16_t *im16 = (uint16_t*)Raw;
69 for( i = 0; i < RawSize / 2; i++ )
71 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
74 else if ( pixelSize == 4 )
79 uint32_t *im32 = (uint32_t*)Raw;
81 for( i = 0; i < RawSize / 4; i++ )
83 low = im32[i] & 0x0000ffff; // 3412
86 im32[i] = ( s32 << 16 ) | high;
92 int main(int argc, char *argv[])
95 " \n RawToDicom : \n ",
96 " Writes a Dicom file from a Raw File ",
97 " usage: RawToDicom filein=inputFileName ",
98 " fileout=outputSkeletonName ",
100 " lines=nb of Lines, ",
101 " [frames = nb of Frames] //defaulted to 1 ",
102 " pixeltype={8U|8S|16U|16S|32U|32S|32F|64D} ",
103 " pixeltypeout={8U|8S|16U|16S|32U|32S} ",
104 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
105 " [samples = {1|3}} //(1:Gray,3:RGB) defaulted to 1",
107 " [studyid = ] [patientname = Patient's name] ",
110 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
111 " studyUID : *aware* user wants to add the serie ",
112 " to an already existing study ",
113 " debug : developper wants to run the program in 'debug mode' ",
117 // ------------ Initialize Arguments Manager ----------------
118 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
120 if (argc == 1 || am->ArgMgrDefined("usage") )
122 am->ArgMgrUsage(usage); // Display 'usage'
127 const char *inputFileName = am->ArgMgrGetString("filein");
128 const char *outputSkeletonFileName = am->ArgMgrGetString("fileout");
130 const char *patientName = am->ArgMgrGetString("patientname", "g^Fantomas");
132 int nX = am->ArgMgrWantInt("rows", usage);
133 int nY = am->ArgMgrWantInt("lines", usage);
134 int nZ = am->ArgMgrGetInt("frames", 1);
135 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
137 int b = am->ArgMgrDefined("b");
138 int l = am->ArgMgrDefined("l");
140 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
142 const char *pixelTypeOut = am->ArgMgrGetString("pixeltypeout", pixelType);
144 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
146 if (am->ArgMgrDefined("debug"))
147 GDCM_NAME_SPACE::Debug::DebugOn();
149 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
150 const char *studyUID;
151 if (userDefinedStudy)
152 studyUID = am->ArgMgrGetString("studyUID");
154 // not described *on purpose* in the Usage !
155 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
156 const char *serieUID;
158 serieUID = am->ArgMgrGetString("serieUID");
160 /* if unused Param we give up */
161 if ( am->ArgMgrPrintUnusedLabels() )
163 am->ArgMgrUsage(usage);
168 delete am; // we don't need Argument Manager any longer
170 // ----------- End Arguments Manager ---------
172 /// \TODO Deal with all the images of a directory
175 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
178 std::cout << "Cannot open file: " << inputFileName;
184 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
186 std::string strPixelType(pixelType);
189 int pixelTypeCode; // for the switch case
191 if (strPixelType == "8S")
197 else if (strPixelType == "8U")
203 else if (strPixelType == "16S")
209 else if (strPixelType == "16U")
215 else if (strPixelType == "32S")
221 else if (strPixelType == "32U")
227 else if (strPixelType == "32F")
234 else if (strPixelType == "64D")
242 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
246 std::string strPixelTypeOut(pixelTypeOut);
249 int pixelTypeOutCode; // for the switch case
251 if (strPixelTypeOut == "8S")
255 pixelTypeOutCode = -8;
257 else if (strPixelTypeOut == "8U")
261 pixelTypeOutCode = 8;
263 else if (strPixelTypeOut == "16S")
267 pixelTypeOutCode = -16;
269 else if (strPixelTypeOut == "16U")
273 pixelTypeOutCode = 16;
275 else if (strPixelTypeOut == "32S")
279 pixelTypeOutCode = -32;
281 else if (strPixelTypeOut == "32U")
285 pixelTypeOutCode = 32;
289 std::cout << "Wrong 'pixeltypeout' (" << strPixelTypeOut << ")" << std::endl;
293 std::string strStudyUID;
294 std::string strSerieUID;
296 if (userDefinedStudy)
297 strSerieUID = studyUID;
299 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
301 if (userDefinedSerie)
302 strSerieUID = serieUID;
304 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
308 int singlePlaneDataSize = nX*nY*samplesPerPixel*pixelSizeOut;
309 int dataSizeIn = nX*nY*samplesPerPixel*pixelSize*nZ;
311 uint8_t *pixels = new uint8_t[dataSizeIn];
312 uint8_t *planePixelsOut = new uint8_t[singlePlaneDataSize];
314 Fp->read((char*)pixels, (size_t)dataSizeIn);
316 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
318 ConvertSwapZone(pixelSize, pixels, dataSizeIn);
321 // iterate on the planes.
323 char outputFileName[200];
325 for(int nbPlanes=0; nbPlanes<nZ; nbPlanes++)
328 sprintf(outputFileName, "%s.%04d.dcm", outputSkeletonFileName,nbPlanes);
330 // Copy (and convert) pixels of a single plane
332 switch ( pixelTypeCode )
334 case 8 : CFR(PU8); break;
335 case -8 : CFR(PS8); break;
336 case -16 : CFR(PU16); break;
337 case 16 : CFR(PS16); break;
338 case -32 : CFR(PS32); break;
339 case 32 : CFR(PU32); break;
340 case 33 : CFR(PF32); break;
341 case 64 : CFR(PD64); break;
344 // Create an empty FileHelper
346 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
348 // Get the (empty) image header.
349 GDCM_NAME_SPACE::File *fileToBuild = fileH->GetFile();
351 // 'Study Instance UID'
352 // The user is allowed to create his own Study,
353 // keeping the same 'Study Instance UID' for various images
354 // The user may add images to a 'Manufacturer Study',
355 // adding new Series to an already existing Study
357 fileToBuild->InsertEntryString(strStudyUID,0x0020,0x000d,"UI"); // Study UID
359 // 'Serie Instance UID'
360 // The user is allowed to create his own Series,
361 // keeping the same 'Serie Instance UID' for various images
362 // The user shouldn't add any image to a 'Manufacturer Serie'
363 // but there is no way no to prevent him for doing that
365 fileToBuild->InsertEntryString(strSerieUID,0x0020,0x000e,"UI"); // Serie UID
367 std::ostringstream str;
369 // Set the image size
372 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
375 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
377 // Set the pixel type
380 str << pixelSizeOut*8;
381 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
384 str << pixelSizeOut*8;
385 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
388 str << ( pixelSizeOut*8 - 1 );
389 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
393 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
395 // If you deal with a Serie of images, as slices of a volume,
396 // it up to you to tell gdcm, for each image, what are the values of :
398 // 0020 0032 DS 3 Image Position (Patient)
399 // 0020 0037 DS 6 Image Orientation (Patient)
402 str << "0.0 \\ 0.0 \\" << nbPlanes;
403 fileToBuild->InsertEntryString(str.str(),0x0020,0x0032, "DS");
406 str << samplesPerPixel;
407 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
409 if (strlen(patientName) != 0)
410 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
414 fileH->SetPhotometricInterpretationToMonochrome1();
416 // Set the image Pixel Data (plane)
417 fileH->SetImageData(planePixelsOut,singlePlaneDataSize);
419 // Set the writting mode and write the image
420 fileH->SetWriteModeToRaw();
422 // Write a DICOM Explicit VR file
423 fileH->SetWriteTypeToDcmExplVR();
425 if( !fileH->Write(outputFileName ) )
427 std::cout << "Failed for [" << outputFileName << "]\n"
428 << " File is unwrittable\n";
433 } // end iterate on the planes
436 delete[] planePixelsOut;