1 /*=========================================================================
4 Module: $RCSfile: PcpdenseToDicom.cxx,v $
6 Date: $Date: 2008/09/19 09:33:16 $
7 Version: $Revision: 1.4 $
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 =========================================================================*/
23 #if defined(__BORLANDC__)
28 #include "gdcmFileHelper.h"
29 #include "gdcmDebug.h"
30 #include "gdcmDirList.h"
32 #include "gdcmArgMgr.h"
36 * Converts the "pcpdense" ".txt" (2008 version) files into 16 bits Dicom Files,
37 * Hope they don't change soon!
40 void MakeDicomImage(unsigned short int *tabVal, int X, int Y, std::string dcmImageName, const char * patientName, int nbFrames,
41 std::string studyUID, std::string serieUID, std::string SerieDescr, int imgNum, bool m );
43 void LoadImage(std::ifstream &from, unsigned short int * );
45 void LoadImageX2(std::ifstream &from, unsigned short int * );
48 int main(int argc, char *argv[])
51 " \n pcpdenseToDicom :\n ",
52 " Converts the '.txt' files into 16 bits Dicom Files, ",
54 " pcpdenseToDicom rootfilename=... ",
55 " (e.g.. : meas_MID380_DENSE_stacked_slices_aif_FID81637)",
56 " numberOfSlices = (default : 3) ",
57 " X2 : multiply x 2 image size ",
58 " m :create multiframe files instead of image stacks ",
59 " [patientname = Patient's name] ",
60 " [verbose] [debug] ",
62 " verbose : user wants to run the program in 'verbose mode' ",
63 " debug : *developer* wants to run the program in 'debug mode' ",
66 // ----- Initialize Arguments Manager ------
68 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
70 if (argc == 1 || am->ArgMgrDefined("usage"))
72 am->ArgMgrUsage(usage); // Display 'usage'
76 // Seems that ArgMgrWantString doesn't work on MacOS
77 if(!am->ArgMgrDefined("rootfilename"))
79 std::cout << "'rootfilename' is mandatory" << std::endl;
83 const char *rootfilename = am->ArgMgrWantString("rootfilename",usage);
84 int numberOfSlices = am->ArgMgrGetInt("numberOfSlices",3);
85 const char *patientName = am->ArgMgrGetString("patientname", "Patient^Name");
87 if (am->ArgMgrDefined("debug"))
88 GDCM_NAME_SPACE::Debug::DebugOn();
90 verbose = ( 0 != am->ArgMgrDefined("verbose") );
91 bool X2 = ( 0 != am->ArgMgrDefined("X2") );
92 bool multiframe = ( 0 != am->ArgMgrDefined("m") );
94 // if unused Param we give up
95 if ( am->ArgMgrPrintUnusedLabels() )
97 am->ArgMgrUsage(usage);
101 delete am; // we don't need Argument Manager any longer
103 // ----- Begin Processing -----
105 std::ostringstream Ecc;
106 std::ostringstream perf;
107 std::ostringstream WashoutTc;
109 std::string strSerieUID;
110 std::string strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
111 std::string dcmImageName, textFileName, patientname,/* studyUID, serieUID, */ serieDescr ;
112 std::string deb(rootfilename);
114 unsigned short int *image;
122 Ecc.str(rootfilename);
123 Ecc << Ecc.str() << "_s0" << "_Ecc.txt";
125 std::ifstream fromEcc( Ecc.str().c_str() );
128 std::cout << "Can't open file [" << Ecc.str() << "]" << std::endl;
138 std::cout << "NX, NY : " << NX << ", " << NY << std::endl;
148 image = new unsigned short int[NX*NY*mult*numberOfSlices];
150 image = new unsigned short int[NX*NY*mult];
156 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
160 for (int i=0; i<numberOfSlices; i++)
162 Ecc.str(rootfilename);
163 Ecc << Ecc.str() << "_s" << i << "_Ecc.txt";
165 std::ifstream fromEcc( Ecc.str().c_str() );
168 std::cout << "Can't open file [" << Ecc.str() << "]" << std::endl;
171 std::cout << "Open file [" << Ecc.str() << "] : OK" << std::endl;
174 LoadImageX2(fromEcc, image);
176 LoadImage(fromEcc, image);
179 dcmImageName = Ecc.str() + ".dcm";
181 MakeDicomImage(image, NX, NY, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i, multiframe );
182 } // end : for (int i=0; i<numberOfSlices
189 for (int i=0; i<numberOfSlices; i++)
191 Ecc.str(rootfilename);
192 Ecc << Ecc.str() << "_s" << i << "_Ecc.txt";
194 std::ifstream fromEcc( Ecc.str().c_str() );
197 std::cout << "Can't open file [" << Ecc.str() << "]" << std::endl;
201 std::cout << "Open file [" << Ecc.str() << "] : OK" << std::endl;
203 LoadImageX2(fromEcc ,&image[NX*NY*i] );
205 LoadImage(fromEcc, &image[NX*NY*i] );
208 } // end : for (int i=0; i<numberOfSlices
210 dcmImageName = deb + "_Ecc.dcm";
211 MakeDicomImage(image, NX, NY, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
213 } // end : if (multiframe)
219 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
222 for (int i=0; i<numberOfSlices; i++)
224 perf.str(rootfilename);
225 perf << perf.str() << "_s" << i << "_perf.txt";
227 std::ifstream fromperf( perf.str().c_str() );
230 std::cout << "Can't open file [" << perf.str() << "]" << std::endl;
233 std::cout << "Open file [" << perf.str() << "] : OK" << std::endl;
236 LoadImageX2(fromperf ,image );
238 LoadImage(fromperf, image );
242 dcmImageName = Ecc.str() + ".dcm";
243 MakeDicomImage(image, NX, NY, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i, multiframe );
245 } // end : for (int i=0; i<numberOfSlices
250 for (int i=0; i<numberOfSlices; i++)
252 perf.str(rootfilename);
253 perf << perf.str() << "_s" << i << "_perf.txt";
255 std::ifstream fromperf( perf.str().c_str() );
258 std::cout << "Can't open file [" << perf.str() << "]" << std::endl;
261 std::cout << "Open file [" << perf.str() << "] : OK" << std::endl;
264 LoadImageX2(fromperf, &image[NX*NY*i]);
266 LoadImage(fromperf, &image[NX*NY*i] );
269 } // end : for (int i=0; i<numberOfSlices
271 dcmImageName = deb + "_perf.dcm";
272 MakeDicomImage(image, NX, NY, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, numberOfSlices, multiframe );
280 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
283 for (int i=0; i<numberOfSlices; i++)
285 WashoutTc.str(rootfilename);
286 WashoutTc << WashoutTc.str() << "_s" << i << "_WashoutTc.txt";
288 std::ifstream fromWashoutTc( WashoutTc.str().c_str() );
289 if ( !fromWashoutTc )
291 std::cout << "Can't open file [" << WashoutTc.str() << "]" << std::endl;
294 std::cout << "Open file [" << WashoutTc.str() << "] : OK" << std::endl;
297 LoadImageX2(fromWashoutTc, &image[NX*NY*i]);
299 LoadImage(fromWashoutTc, &image[NX*NY*i] );
300 fromWashoutTc.close();
302 } // end : for (int i=0; i<numberOfSlices
304 dcmImageName = deb + "_WashoutTc.dcm";
305 MakeDicomImage(image, NX, NY, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, numberOfSlices, multiframe );
311 for (int i1=0; i1<numberOfSlices; i1++)
313 WashoutTc.str(rootfilename);
314 WashoutTc << WashoutTc.str() << "_s" << i1 << "_WashoutTc.txt";
316 std::ifstream fromWashoutTc( WashoutTc.str().c_str() );
317 if ( !fromWashoutTc )
319 std::cout << "Can't open file [" << WashoutTc.str() << "]" << std::endl;
323 std::cout << "Open file [" << WashoutTc.str() << "] : OK" << std::endl;
325 LoadImageX2(fromWashoutTc,image );
327 LoadImage(fromWashoutTc,image );
328 fromWashoutTc.close();
330 dcmImageName = Ecc.str() + ".dcm";
331 MakeDicomImage(image, NX, NY, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i1, multiframe );
332 } // end : for (int i=0; i<numberOfSlices
340 // =====================================================================================================================
342 void LoadImage(std::ifstream &from, unsigned short int *image)
344 // in any file ".txt" :
348 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
363 std::cout << "NX, NY : " << NX << ", " << NY << std::endl;
371 image[i*NX+j] = (unsigned short int)(pixelValue * 1000.);
376 // =====================================================================================================================
378 void LoadImageX2(std::ifstream &from, unsigned short int *image )
380 // in any file ".txt" :
384 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
399 std::cout << "NX, NY : " << NX << ", " << NY << std::endl;
408 image[i*4*NX + j*2] = image[i*4*NX + j*2+1] = image[(i*4+2)*NX + j*2] = image[(i*4+2)*NX + j*2+1] = (unsigned short int)(pixelValue);
413 // =====================================================================================================================================
416 void MakeDicomImage(unsigned short int *tabVal, int X, int Y, std::string dcmImageName, const char * patientName, int nbFrames, std::string studyUID, std::string serieUID, std::string SerieDescr, int imgNum, bool m)
419 // GDCM_NAME_SPACE::Debug::DebugOn();
421 std::ostringstream str;
423 GDCM_NAME_SPACE::File *file;
424 file = GDCM_NAME_SPACE::File::New();
426 // Set the image size
429 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
432 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
434 // Set the pixel type
436 file->InsertEntryString("16",0x0028,0x0100,"US"); // Bits Allocated
439 str << 16; // may be 12 or 16 if componentSize =16
440 file->InsertEntryString("16",0x0028,0x0101,"US"); // Bits Stored
441 file->InsertEntryString("15",0x0028,0x0102,"US"); // High Bit
443 // Set the pixel representation // 0/1 , 0=unsigned
444 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
446 // Set the samples per pixel // 1:Grey level, 3:RGB
447 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
454 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
457 if (strlen(patientName) != 0)
458 file->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
460 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
461 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
463 file->InsertEntryString(SerieDescr,0x0008,0x103e, "LO"); // Series Description
465 // 0020 0037 DS 6 Image Orientation (Patient)
466 file->InsertEntryString("1.0\\0.0\\0.0\\0.0\\1.0\\0.0",0x0020,0x0037, "DS"); //[1\0\0\0\1\0] : Axial (Tant pis!)
468 // 0020 0032 DS 3 Image Position (Patient)
469 char charImagePosition[256];
470 sprintf(charImagePosition,"0.0\\0.0\\%f",(float)imgNum);
471 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
473 // 0020 0x1041 DS 1 Slice Location
474 sprintf(charImagePosition,"%f",float(imgNum));
475 file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
477 // Set Rescale Intercept
480 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
485 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
488 GDCM_NAME_SPACE::FileHelper *fileH;
489 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
490 // cast is just to avoid warnings (*no* conversion is performed)
491 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
493 fileH->SetImageData((uint8_t *)tabVal,X*Y*nbFrames*sizeof(uint16_t));
494 fileH->SetWriteModeToRaw();
495 fileH->SetWriteTypeToDcmExplVR();
497 if( !fileH->Write(dcmImageName))
498 std::cout << "Failed for [" << dcmImageName << "]\n"
499 << " File is unwrittable" << std::endl;
510 // =====================================================================================================================