1 /*=========================================================================
4 Module: $RCSfile: RawToDicomStack.cxx,v $
6 Date: $Date: 2008/06/12 13:26:39 $
7 Version: $Revision: 1.1 $
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 #define CRR(t1,t2) { for(int i=0;i<nY;i++) \
41 for(int j=0;j<nX;j++) \
42 for(int k=0;k<samplesPerPixel;k++) \
43 *((t2)planePixels + i*nX +j+k) = *(((t1)pixels)+ nbPlanes*nX*nY + i*nX +j+k);\
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; \
56 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize);
58 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize)
63 uint16_t *im16 = (uint16_t*)Raw;
64 for( i = 0; i < RawSize / 2; i++ )
66 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
69 else if ( pixelSize == 4 )
74 uint32_t *im32 = (uint32_t*)Raw;
76 for( i = 0; i < RawSize / 4; i++ )
78 low = im32[i] & 0x0000ffff; // 3412
81 im32[i] = ( s32 << 16 ) | high;
87 int main(int argc, char *argv[])
90 " \n RawToDicom : \n ",
91 " Writes a Dicom file from a Raw File ",
92 " usage: RawToDicom filein=inputFileName ",
93 " fileout=outputSkeletonName ",
95 " lines=nb of Lines, ",
96 " [frames = nb of Frames] //defaulted to 1 ",
97 " pixeltype={8U|8S|16U|16S|32U|32S} ",
98 " pixeltypeout={8U|8S|16U|16S|32U|32S} ",
99 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
100 " [samples = {1|3}} //(1:Gray,3:RGB) defaulted to 1",
102 " [studyid = ] [patientname = Patient's name] ",
105 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
106 " studyUID : *aware* user wants to add the serie ",
107 " to an already existing study ",
108 " debug : developper wants to run the program in 'debug mode' ",
112 // ------------ Initialize Arguments Manager ----------------
113 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
115 if (argc == 1 || am->ArgMgrDefined("usage") )
117 am->ArgMgrUsage(usage); // Display 'usage'
122 const char *inputFileName = am->ArgMgrGetString("filein");
123 const char *outputSkeletonFileName = am->ArgMgrGetString("fileout");
125 const char *patientName = am->ArgMgrGetString("patientname", "g^Fantomas");
127 int nX = am->ArgMgrWantInt("rows", usage);
128 int nY = am->ArgMgrWantInt("lines", usage);
129 int nZ = am->ArgMgrGetInt("frames", 1);
130 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
132 int b = am->ArgMgrDefined("b");
133 int l = am->ArgMgrDefined("l");
135 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
137 const char *pixelTypeOut = am->ArgMgrGetString("pixeltypeout", pixelType);
139 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
141 if (am->ArgMgrDefined("debug"))
142 GDCM_NAME_SPACE::Debug::DebugOn();
144 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
145 const char *studyUID;
146 if (userDefinedStudy)
147 studyUID = am->ArgMgrGetString("studyUID");
149 // not described *on purpose* in the Usage !
150 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
151 const char *serieUID;
153 serieUID = am->ArgMgrGetString("serieUID");
155 /* if unused Param we give up */
156 if ( am->ArgMgrPrintUnusedLabels() )
158 am->ArgMgrUsage(usage);
163 delete am; // we don't need Argument Manager any longer
165 // ----------- End Arguments Manager ---------
167 /// \TODO Deal with all the images of a directory
170 std::ifstream *Fp = new std::ifstream(inputFileName, std::ios::in | std::ios::binary);
173 std::cout << "Cannot open file: " << inputFileName;
179 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
181 std::string strPixelType(pixelType);
184 int pixelTypeCode; // for the switch case
186 if (strPixelType == "8S")
192 else if (strPixelType == "8U")
198 else if (strPixelType == "16S")
204 else if (strPixelType == "16U")
210 else if (strPixelType == "32S")
216 else if (strPixelType == "32U")
224 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
228 std::string strPixelTypeOut(pixelTypeOut);
231 int pixelTypeOutCode; // for the swirch case
233 if (strPixelTypeOut == "8S")
237 pixelTypeOutCode = -8;
239 else if (strPixelTypeOut == "8U")
243 pixelTypeOutCode = 8;
245 else if (strPixelTypeOut == "16S")
249 pixelTypeOutCode = -16;
251 else if (strPixelTypeOut == "16U")
255 pixelTypeOutCode = 16;
257 else if (strPixelTypeOut == "32S")
261 pixelTypeOutCode = -32;
263 else if (strPixelTypeOut == "32U")
267 pixelTypeOutCode = 32;
271 std::cout << "Wrong 'pixeltypeout' (" << strPixelTypeOut << ")" << std::endl;
275 std::string strStudyUID;
276 std::string strSerieUID;
278 if (userDefinedStudy)
279 strSerieUID = studyUID;
281 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
283 if (userDefinedSerie)
284 strSerieUID = serieUID;
286 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
290 int singlePlaneDataSize = nX*nY*samplesPerPixel*pixelSizeOut;
291 int dataSize = singlePlaneDataSize*nZ;
293 uint8_t *pixels = new uint8_t[dataSize];
294 uint8_t *planePixels = new uint8_t[singlePlaneDataSize];
296 Fp->read((char*)pixels, (size_t)dataSize);
298 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
300 ConvertSwapZone(pixelSize, pixels, dataSize);
303 // iterate on the planes.
305 char outputFileName[200];
307 for(int nbPlanes=0; nbPlanes<nZ; nbPlanes++)
310 sprintf(outputFileName, "%s.%04d.dcm", outputSkeletonFileName,nbPlanes);
312 // Copy (and convert) pixels of a single plane
314 switch ( pixelTypeCode ) {
315 case 8 : CFR(PU8); break;
316 case -8 : CFR(PS8); break;
317 case -16 : CFR(PU16); break;
318 case 16 : CFR(PS16); break;
319 case -32 : CFR(PS32); break;
320 case 32 : CFR(PU32); break;
323 // Create an empty FileHelper
325 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
327 // Get the (empty) image header.
328 GDCM_NAME_SPACE::File *fileToBuild = fileH->GetFile();
330 // 'Study Instance UID'
331 // The user is allowed to create his own Study,
332 // keeping the same 'Study Instance UID' for various images
333 // The user may add images to a 'Manufacturer Study',
334 // adding new Series to an already existing Study
336 fileToBuild->InsertEntryString(strStudyUID,0x0020,0x000d,"UI"); // Study UID
338 // 'Serie Instance UID'
339 // The user is allowed to create his own Series,
340 // keeping the same 'Serie Instance UID' for various images
341 // The user shouldn't add any image to a 'Manufacturer Serie'
342 // but there is no way no to prevent him for doing that
344 fileToBuild->InsertEntryString(strSerieUID,0x0020,0x000e,"UI"); // Serie UID
346 std::ostringstream str;
348 // Set the image size
351 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
354 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
356 // Set the pixel type
360 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
364 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
367 str << ( pixelSize*8 - 1 );
368 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
372 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
374 // If you deal with a Serie of images, as slices of a volume,
375 // it up to you to tell gdcm, for each image, what are the values of :
377 // 0020 0032 DS 3 Image Position (Patient)
378 // 0020 0037 DS 6 Image Orientation (Patient)
381 str << "0.0 \\ 0.0 \\" << nbPlanes;
382 fileToBuild->InsertEntryString(str.str(),0x0020,0x0032, "DS");
385 str << samplesPerPixel;
386 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
388 if (strlen(patientName) != 0)
389 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
393 fileH->SetPhotometricInterpretationToMonochrome1();
395 // Set the image Pixel Data (plane)
396 fileH->SetImageData(planePixels,singlePlaneDataSize);
398 // Set the writting mode and write the image
399 fileH->SetWriteModeToRaw();
401 // Write a DICOM Explicit VR file
402 fileH->SetWriteTypeToDcmExplVR();
404 if( !fileH->Write(outputFileName ) )
406 std::cout << "Failed for [" << outputFileName << "]\n"
407 << " File is unwrittable\n";
412 } // end iterate on the planes
415 delete[] planePixels;