1 /*=========================================================================
4 Module: $RCSfile: RawToInTagDicom.cxx,v $
6 Date: $Date: 2009/01/22 16:30:39 $
7 Version: $Revision: 1.2 $
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 an old style Bruker Dicom file, InTag compliant, from a Raw File
21 * User has to supply parameters.
25 #include "gdcmFileHelper.h"
26 #include "gdcmDebug.h"
28 #include "gdcmDirList.h"
30 #include "gdcmArgMgr.h"
38 * - explores the given directory
39 * - orders the Raw Files
40 * according to their names
41 * - fills a single level Directory with *all* the files,
42 * converted into a Brucker-like Dicom, Intags compliant
46 typedef unsigned char * PU8;
47 typedef short int * PS16;
48 typedef unsigned short int * PU16;
50 typedef unsigned int * PU32;
52 typedef double * PD64;
54 #define CRR(t1,t2) { for(int l=0;l<nX*nY*/* nZ* */samplesPerPixel;l++) \
55 {*((t2)planePixelsOut + l) = *(((t1)pixels)+ l);}\
58 #define CFR(PPt) switch ( pixelTypeOutCode ) { \
59 case -8 : CRR(PPt,PS8); break; \
60 case 8 : CRR(PPt,PU8); break; \
61 case -16 : CRR(PPt,PS16); break; \
62 case 16 : CRR(PPt,PU16); break; \
63 case -32 : CRR(PPt,PS32); break; \
64 case 32 : CRR(PPt,PU32); break; \
65 case 33 : CRR(PPt,PF32); break; \
66 case 64 : CRR(PPt,PD64); break; \
70 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize);
72 void ConvertSwapZone(int pixelSize, void *Raw, size_t RawSize)
77 uint16_t *im16 = (uint16_t*)Raw;
78 for( i = 0; i < RawSize / 2; i++ )
80 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
83 else if ( pixelSize == 4 )
88 uint32_t *im32 = (uint32_t*)Raw;
90 for( i = 0; i < RawSize / 4; i++ )
92 low = im32[i] & 0x0000ffff; // 3412
95 im32[i] = ( s32 << 16 ) | high;
101 int main(int argc, char *argv[])
104 " \n RawToDicom : \n ",
105 " - explores recursively the given directory, ",
106 " - orders the Raw Files according to their names ",
107 " - fills a single level (*) Directory with *all* the files, ",
108 " converted into a Brucker-like Dicom, InTags compliant ",
109 " usage: RawToDicom dirin=rootDirectoryName ",
110 " dirout=outputDirectoryName ",
112 " lines=nb of Lines, ",
113 " [frames = nb of Frames] //defaulted to 1 ",
114 " pixeltype={8U|8S|16U|16S|32U|32S|32F|64D} ",
115 " pixeltypeout={8U|8S|16U|16S|32U|32S} ",
116 " [{b|l}] b:BigEndian,l:LittleEndian default : l ",
117 " [samples = {1|3}} //(1:Gray,3:RGB) defaulted to 1",
119 " [studyid = ] [patientname = Patient's name] ",
120 " [debug] [verbose] [listonly] ",
122 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
123 " studyUID : *aware* user wants to add the serie ",
124 " to an already existing study ",
125 " debug : developper wants to run the program in 'debug mode' ",
129 // ------------ Initialize Arguments Manager ----------------
130 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
132 if (argc == 1 || am->ArgMgrDefined("usage") )
134 am->ArgMgrUsage(usage); // Display 'usage'
139 const char *dirNamein;
140 dirNamein = am->ArgMgrGetString("dirin",".");
142 const char *dirNameout;
143 dirNameout = am->ArgMgrGetString("dirout",".");
145 const char *patientName = am->ArgMgrGetString("patientname", "g^Fantomas");
147 int nX = am->ArgMgrWantInt("rows", usage);
148 int nY = am->ArgMgrWantInt("lines", usage);
149 int nZ = am->ArgMgrGetInt("frames", 1);
150 int samplesPerPixel = am->ArgMgrGetInt("samples", 1);
152 int b = am->ArgMgrDefined("b");
153 int l = am->ArgMgrDefined("l");
155 char *pixelType = am->ArgMgrWantString("pixeltype", usage);
156 const char *pixelTypeOut = am->ArgMgrGetString("pixeltypeout", pixelType);
158 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
160 if (am->ArgMgrDefined("debug"))
161 GDCM_NAME_SPACE::Debug::DebugOn();
163 int verbose = am->ArgMgrDefined("verbose");
164 int listonly = am->ArgMgrDefined("listonly");
166 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
167 const char *studyUID;
168 if (userDefinedStudy)
169 studyUID = am->ArgMgrGetString("studyUID");
171 // not described *on purpose* in the Usage !
172 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
173 const char *serieUID;
175 serieUID = am->ArgMgrGetString("serieUID");
177 /* if unused Param we give up */
178 if ( am->ArgMgrPrintUnusedLabels() )
180 am->ArgMgrUsage(usage);
185 delete am; // we don't need Argument Manager any longer
187 // ----------- End Arguments Manager ---------
190 // ----- Begin Processing -----
193 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
195 std::string strPixelType(pixelType);
198 int pixelTypeCode; // for the switch case
200 if (strPixelType == "8S")
206 else if (strPixelType == "8U")
212 else if (strPixelType == "16S")
218 else if (strPixelType == "16U")
224 else if (strPixelType == "32S")
230 else if (strPixelType == "32U")
236 else if (strPixelType == "32F")
243 else if (strPixelType == "64D")
251 std::cout << "Wrong 'pixeltype' (" << strPixelType << ")" << std::endl;
255 std::string strPixelTypeOut(pixelTypeOut);
258 int pixelTypeOutCode; // for the switch case
260 if (strPixelTypeOut == "8S")
264 pixelTypeOutCode = -8;
266 else if (strPixelTypeOut == "8U")
270 pixelTypeOutCode = 8;
272 else if (strPixelTypeOut == "16S")
276 pixelTypeOutCode = -16;
278 else if (strPixelTypeOut == "16U")
282 pixelTypeOutCode = 16;
284 else if (strPixelTypeOut == "32S")
288 pixelTypeOutCode = -32;
290 else if (strPixelTypeOut == "32U")
294 pixelTypeOutCode = 32;
298 std::cout << "Wrong 'pixeltypeout' (" << strPixelTypeOut << ")" << std::endl;
304 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNamein) )
306 std::cout << "KO : [" << dirNamein << "] is not a Directory." << std::endl;
312 std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
315 std::string systemCommand;
317 std::cout << "Check for output directory :[" << dirNameout << "]."
319 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // dirout not found
321 std::string strDirNameout(dirNameout); // to please gcc 4
322 systemCommand = "mkdir " +strDirNameout; // create it!
324 std::cout << systemCommand << std::endl;
325 system (systemCommand.c_str());
326 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
328 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
334 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
339 std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
342 std::string strDirNamein(dirNamein);
343 GDCM_NAME_SPACE::DirList dirList(strDirNamein, false); // DON'T get recursively the list of files
344 std::string strDirNameout(dirNameout);
348 std::cout << "------------List of found files ------------" << std::endl;
350 std::cout << std::endl;
354 std::string strStudyUID;
355 std::string strSerieUID;
357 if (userDefinedStudy)
358 strSerieUID = studyUID;
360 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
362 if (userDefinedSerie)
363 strSerieUID = serieUID;
365 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
367 GDCM_NAME_SPACE::DirListType fileNames;
368 fileNames = dirList.GetFilenames();
370 GDCM_NAME_SPACE::File *f;
371 GDCM_NAME_SPACE::FileHelper *fh;
373 std::string outputFileName;
375 int sliceIndex = 1; // IMPORTANT : must start with non zero value
376 int frameIndex = 1; // IMPORTANT : must start with non zero value
377 std::string chSessionIndex("1");
379 // -----------------------------------------------------
380 // Iterate fo ALL the files found in the input directory
381 // -----------------------------------------------------
383 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
384 it != fileNames.end();
389 std::ifstream *Fp = new std::ifstream((*it).c_str(), std::ios::in | std::ios::binary);
392 std::cout << "Cannot open file: [" << *it << "]" << std::endl;
398 std::cout << "Success to open file: [" << *it << "]" << std::endl;
402 int singlePlaneDataSize = nX*nY*samplesPerPixel*pixelSizeOut;
403 int dataSizeIn = nX*nY*samplesPerPixel*pixelSize*nZ;
405 uint8_t *pixels = new uint8_t[dataSizeIn];
406 uint8_t *planePixelsOut = new uint8_t[singlePlaneDataSize];
408 Fp->read((char*)pixels, (size_t)dataSizeIn);
410 if ( pixelSize !=1 && ( (l && bigEndian) || (b && ! bigEndian) ) )
412 ConvertSwapZone(pixelSize, pixels, dataSizeIn);
416 std::cout << "After ConvertSwapZone" << std::endl;
418 // Copy (and convert) pixels of a single plane
420 switch ( pixelTypeCode )
422 case 8 : CFR(PU8); break;
423 case -8 : CFR(PS8); break;
424 case -16 : CFR(PU16); break;
425 case 16 : CFR(PS16); break;
426 case -32 : CFR(PS32); break;
427 case 32 : CFR(PU32); break;
428 case 33 : CFR(PF32); break;
429 case 64 : CFR(PD64); break;
433 std::cout << "After CFR" << std::endl;
435 // Create an empty FileHelper
437 GDCM_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
439 // Get the (empty) image header.
440 GDCM_NAME_SPACE::File *fileToBuild = fileH->GetFile();
442 // 'Study Instance UID'
443 // The user is allowed to create his own Study,
444 // keeping the same 'Study Instance UID' for various images
445 // The user may add images to a 'Manufacturer Study',
446 // adding new Series to an already existing Study
448 fileToBuild->InsertEntryString(strStudyUID,0x0020,0x000d,"UI"); // Study UID
450 // 'Serie Instance UID'
451 // The user is allowed to create his own Series,
452 // keeping the same 'Serie Instance UID' for various images
453 // The user shouldn't add any image to a 'Manufacturer Serie'
454 // but there is no way no to prevent him for doing that
456 fileToBuild->InsertEntryString(strSerieUID,0x0020,0x000e,"UI"); // Serie UID
458 std::ostringstream str;
460 // Set the image size
463 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011, "US"); // Columns
466 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010, "US"); // Rows
469 std::cout << "before debut des choses serieuses2"<< std::endl;
473 // fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
475 // Set the pixel type
478 str << pixelSizeOut*8;
479 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100, "US"); // Bits Allocated
482 str << pixelSizeOut*8;
483 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101, "US"); // Bits Stored
486 str << ( pixelSizeOut*8 - 1 );
487 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102, "US"); // High Bit
491 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
493 // If you deal with a Serie of images, as slices of a volume,
494 // it's up to you to tell gdcm, for each image, what are the values of :
496 // 0020 0032 DS 3 Image Position (Patient)
497 // 0020 0037 DS 6 Image Orientation (Patient)
500 str << "0.0\\0.0\\0.0";
502 // take Frame Index as position (we don't know anything else) NO!
504 fileToBuild->InsertEntryString(str.str(),0x0020,0x0032, "DS");
506 fileToBuild->InsertEntryString("1.0\\0.0\\0.0\\0.0\\1.0\\0.0",0x0020,0x0037, "DS"); //[1\0\0\0\1\0] : Axial
510 str << samplesPerPixel;
511 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
513 if (strlen(patientName) != 0)
514 fileToBuild->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
518 fileH->SetPhotometricInterpretationToMonochrome1();
520 // Special InTag, now.
522 fileH->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
524 // Deal with 0x0021, 0x1020 : 'SLICE INDEX'
525 // will stay to 0, since the stuff deals with single slice directories
526 char chSliceIndex[5];
527 sprintf(chSliceIndex, "%04d", sliceIndex);
528 std::string strChSliceIndex(chSliceIndex);
530 // Deal with 0x0021, 0x1040 : 'FRAME INDEX'
534 frameIndex++; // be ready for next one
536 fileH->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, "IS");
537 fileH->InsertEntryString(str.str(), 0x0021, 0x1040, "IS");
540 /// \TODO Ask user to supply 'Pixel Size' value
546 sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
547 fileH->InsertEntryString(fov, 0x0019, 0x1000, "DS");
550 // We want to create MR-like image !
551 fileH->InsertEntryString("MR", 0x0008, 0x0060, "CS");
552 fileH->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI");
553 fileH->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI");
555 fileH->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
559 // Set the image Pixel Data
560 fileH->SetImageData(planePixelsOut,singlePlaneDataSize);
562 // Set the writting mode and write the image
563 fileH->SetWriteModeToRaw();
567 // Write a DICOM Explicit VR file
568 fileH->SetWriteTypeToDcmExplVR();
571 outputFileName = strDirNameout + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + GDCM_NAME_SPACE::Util::GetName(*it) + "_ForInTag.dcm";
572 if( !fileH->Write(outputFileName ) )
574 std::cout << "Failed for [" << outputFileName << "]\n"
575 << " File is unwrittable\n";
578 // End of : for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin() ...
585 delete[] planePixelsOut;
589 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it