1 /*=========================================================================
4 Module: $RCSfile: PcpdenseToDicom.cxx,v $
6 Date: $Date: 2011/09/06 16:08:07 $
7 Version: $Revision: 1.9 $
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 * );
46 void RotateImage(unsigned short int *image, unsigned short int *image2, int NX, int NY);
47 void FlipImage (unsigned short int *image, unsigned short int *image2, int NX, int NY);
51 int main(int argc, char *argv[])
54 " \n pcpdenseToDicom :\n ",
55 " Converts the '.txt' files into 16 bits Dicom-like Files, ",
57 " pcpdenseToDicom rootfilename=... ",
58 " (e.g.. : meas_MID380_DENSE_stacked_slices_aif_FID81637)",
59 " numberOfSlices = (default : 3) ",
60 " X2 : multiply x 2 image size ",
61 " m :create multiframe files instead of image stacks ",
62 " [patientname = Patient's name] ",
63 " [verbose] [debug] ",
65 " verbose : user wants to run the program in 'verbose mode' ",
66 " debug : *developer* wants to run the program in 'debug mode' ",
69 // ----- Initialize Arguments Manager ------
71 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
73 if (argc == 1 || am->ArgMgrDefined("usage"))
75 am->ArgMgrUsage(usage); // Display 'usage'
79 // Seems that ArgMgrWantString doesn't work on MacOS
80 if(!am->ArgMgrDefined("rootfilename"))
82 std::cout << "'rootfilename' is mandatory" << std::endl;
86 const char *rootfilename = am->ArgMgrWantString("rootfilename",usage);
87 int numberOfSlices = am->ArgMgrGetInt("numberOfSlices",3);
88 const char *patientName = am->ArgMgrGetString("patientname", "Patient^Name");
90 if (am->ArgMgrDefined("debug"))
91 GDCM_NAME_SPACE::Debug::DebugOn();
93 verbose = ( 0 != am->ArgMgrDefined("verbose") );
94 bool X2 = ( 0 != am->ArgMgrDefined("X2") );
95 bool multiframe = ( 0 != am->ArgMgrDefined("m") );
97 // if unused Param we give up
98 if ( am->ArgMgrPrintUnusedLabels() )
100 am->ArgMgrUsage(usage);
104 delete am; // we don't need Argument Manager any longer
106 // ----- Begin Processing -----
108 std::ostringstream Ecc;
109 std::ostringstream perf;
110 std::ostringstream WashoutTc;
112 std::string strSerieUID;
113 std::string strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
114 std::string dcmImageName, textFileName, patientname, serieDescr;
115 std::string deb(rootfilename);
117 unsigned short int *image;
118 unsigned short int *image2;
126 Ecc.str(rootfilename);
127 Ecc << Ecc.str() << "_s0" << "_Ecc.txt";
129 std::ifstream fromEcc( Ecc.str().c_str() );
132 std::cout << "Can't open file [" << Ecc.str() << "]" << std::endl;
142 std::cout << "NX, NY : " << NX << ", " << NY << std::endl;
152 image = new unsigned short int[NX*NY*mult*numberOfSlices];
153 image2 = new unsigned short int[NX*NY*mult*numberOfSlices];
155 image = new unsigned short int[NX*NY*mult];
156 image2 = new unsigned short int[NX*NY*mult];
161 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
166 for (int i=0; i<numberOfSlices; i++)
168 Ecc.str(rootfilename);
169 Ecc << Ecc.str() << "_s" << i << "_Ecc.txt";
171 std::ifstream fromEcc( Ecc.str().c_str() );
174 std::cout << "Can't open file [" << Ecc.str() << "]" << std::endl;
177 std::cout << "Open file [" << Ecc.str() << "] : OK" << std::endl;
178 dcmImageName = Ecc.str() + ".dcm";
182 LoadImageX2(fromEcc, image);
183 RotateImage(image, image2, NX*2, NY*2);
184 FlipImage(image2, image2, NY*2, NX*2);
185 MakeDicomImage(image, NY*2, NX*2, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i, multiframe );
189 LoadImage(fromEcc, image);
190 RotateImage(image, image2, NX, NY);
191 FlipImage(image2, image2, NY, NX);
192 MakeDicomImage(image2, NY, NX, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i, multiframe );
196 } // end : for (int i=0; i<numberOfSlices
200 for (int i=0; i<numberOfSlices; i++)
202 Ecc.str(rootfilename);
203 Ecc << Ecc.str() << "_s" << i << "_Ecc.txt";
205 std::ifstream fromEcc( Ecc.str().c_str() );
208 std::cout << "Can't open file [" << Ecc.str() << "]" << std::endl;
212 std::cout << "Open file [" << Ecc.str() << "] : OK" << std::endl;
215 LoadImageX2(fromEcc ,&image[NX*NY*4*i] );
216 RotateImage(&image[NX*NY*4*i], &image2[NX*NY*4*i] , NX*2, NY*2);
217 FlipImage(&image2[NX*NY*4*i], &image2[NX*NY*4*i] , NY*2, NX*2);
221 LoadImage(fromEcc, &image[NX*NY*i] );
222 RotateImage(&image[NX*NY*i],&image2[NX*NY*i], NX, NY);
223 FlipImage(&image2[NX*NY*i], &image2[NX*NY*i], NY, NX);
227 } // end : for (int i=0; i<numberOfSlices
229 dcmImageName = deb + "_Ecc.dcm";
231 MakeDicomImage(image2, NY*2, NX*2, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
233 MakeDicomImage(image2, NY, NX, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
234 } // end : if (multiframe)
240 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
245 for (int i=0; i<numberOfSlices; i++)
247 perf.str(rootfilename);
248 perf << perf.str() << "_s" << i << "_perf.txt";
250 std::ifstream fromperf( perf.str().c_str() );
253 std::cout << "Can't open file [" << perf.str() << "]" << std::endl;
256 std::cout << "Open file [" << perf.str() << "] : OK" << std::endl;
259 LoadImageX2(fromperf ,image );
261 LoadImage(fromperf, image );
265 dcmImageName = perf.str() + ".dcm";
268 MakeDicomImage(image, NX*2, NY*2, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i, multiframe );
270 MakeDicomImage(image, NX, NY, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i, multiframe );
272 } // end : for (int i=0; i<numberOfSlices
277 for (int i=0; i<numberOfSlices; i++)
279 perf.str(rootfilename);
280 perf << perf.str() << "_s" << i << "_perf.txt";
282 std::ifstream fromperf( perf.str().c_str() );
285 std::cout << "Can't open file [" << perf.str() << "]" << std::endl;
288 std::cout << "Open file [" << perf.str() << "] : OK" << std::endl;
291 LoadImageX2(fromperf, &image[NX*NY*i*4]);
293 LoadImage(fromperf, &image[NX*NY*i] );
296 } // end : for (int i=0; i<numberOfSlices
298 dcmImageName = deb + "_perf.dcm";
300 MakeDicomImage(image, NX*2, NY*2, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
302 MakeDicomImage(image, NX, NY, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
310 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
312 serieDescr = "WashoutTc";
316 for (int i1=0; i1<numberOfSlices; i1++)
318 WashoutTc.str(rootfilename);
319 WashoutTc << WashoutTc.str() << "_s" << i1 << "_WashoutTc.txt";
321 std::ifstream fromWashoutTc( WashoutTc.str().c_str() );
322 if ( !fromWashoutTc )
324 std::cout << "Can't open file [" << WashoutTc.str() << "]" << std::endl;
328 std::cout << "Open file [" << WashoutTc.str() << "] : OK" << std::endl;
330 LoadImageX2(fromWashoutTc,image );
332 LoadImage(fromWashoutTc,image );
333 fromWashoutTc.close();
335 dcmImageName = WashoutTc.str() + ".dcm";
337 MakeDicomImage(image, NX*2, NY*2, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i1, multiframe );
339 MakeDicomImage(image, NX, NY, dcmImageName, patientName, 1, strStudyUID, strSerieUID, serieDescr, i1, multiframe );
340 } // end : for (int i=0; i<numberOfSlices
344 for (int i=0; i<numberOfSlices; i++)
346 WashoutTc.str(rootfilename);
347 WashoutTc << WashoutTc.str() << "_s" << i << "_WashoutTc.txt";
349 std::ifstream fromWashoutTc( WashoutTc.str().c_str() );
350 if ( !fromWashoutTc )
352 std::cout << "Can't open file [" << WashoutTc.str() << "]" << std::endl;
355 std::cout << "Open file [" << WashoutTc.str() << "] : OK" << std::endl;
358 LoadImageX2(fromWashoutTc, &image[NX*NY*4*i]);
360 LoadImage(fromWashoutTc, &image[NX*NY*i] );
361 fromWashoutTc.close();
363 } // end : for (int i=0; i<numberOfSlices
365 dcmImageName = deb + "_WashoutTc.dcm";
367 MakeDicomImage(image, NX*2, NY*2, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
369 MakeDicomImage(image, NX, NY, dcmImageName, patientName, numberOfSlices, strStudyUID, strSerieUID, serieDescr, 0, multiframe );
376 // =====================================================================================================================
378 void LoadImage(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;
407 image[i*NX+j] = (unsigned short int)(pixelValue * 1000.); // Why do we multiply by 1000? // JPR
413 // =====================================================================================================================
415 void LoadImageX2(std::ifstream &from, unsigned short int *image )
417 // in any file ".txt" :
421 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
436 std::cout << "NX, NY : " << NX << ", " << NY << std::endl;
439 for( k=0;k<NX*NY*4;k++)
449 pixelValue*=1000.; // Why do we multiply by 1000? // JPR
450 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);
455 int lgrLigneNvlleImage = NX+NX;
456 int debLigneNvlleImage = 0;
460 pixelValue*=1000.; // Why do we multiply by 1000? // JPR
461 image[debLigneNvlleImage + j+j] =
462 image[debLigneNvlleImage + j+j +1] =
463 image[debLigneNvlleImage + lgrLigneNvlleImage +j+j] =
464 image[debLigneNvlleImage + lgrLigneNvlleImage + j+j +1] =
465 (unsigned short int)(pixelValue);
467 debLigneNvlleImage += 2*lgrLigneNvlleImage;
470 // =====================================================================================================================================
471 void RotateImage(unsigned short int *image, unsigned short int *image2, int NX, int NY)
474 for( int i=0;i<NY;i++) {
475 for(int j=0;j<NX;j++) {
476 image2[NY*j + i] = image[k];
483 // =====================================================================================================================================
484 void FlipImage(unsigned short int *image, unsigned short int *image2, int NX, int NY)
486 unsigned short int temp;
487 for(int i=0;i<NY/2;i++) {
488 for(int j=0;j<NX;j++) {
489 temp = image[NX*i + j];
490 image2[NX*i + j] = image[NX*(NY-i-1) + j];
491 image2[NX*(NY-i-1) + j] = temp;
497 // =====================================================================================================================================
498 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)
501 std::cout << "========================> in MakeDicomImage : dcmImageName = [" << dcmImageName << "] NX= " << X << " NY= " << Y << std::endl;
502 // GDCM_NAME_SPACE::Debug::DebugOn();
504 std::ostringstream str;
506 GDCM_NAME_SPACE::File *file;
507 file = GDCM_NAME_SPACE::File::New();
509 // Set the image size
512 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
515 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
517 // Set the pixel type
519 file->InsertEntryString("16",0x0028,0x0100,"US"); // Bits Allocated
522 str << 16; // may be 12 or 16 if componentSize =16
523 file->InsertEntryString("16",0x0028,0x0101,"US"); // Bits Stored
524 file->InsertEntryString("15",0x0028,0x0102,"US"); // High Bit
526 // Set the pixel representation // 0/1 , 0=unsigned
527 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
529 // Set the samples per pixel // 1:Grey level, 3:RGB
530 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
537 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
540 if (strlen(patientName) != 0)
541 file->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
543 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
544 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
546 file->InsertEntryString(SerieDescr,0x0008,0x103e, "LO"); // Series Description
548 // 0020 0037 DS 6 Image Orientation (Patient)
549 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!)
551 // 0020 0032 DS 3 Image Position (Patient)
552 char charImagePosition[256];
553 sprintf(charImagePosition,"0.0\\0.0\\%f",(float)imgNum);
554 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
556 // 0020 0x1041 DS 1 Slice Location
557 sprintf(charImagePosition,"%f",float(imgNum));
558 file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
560 // Set Rescale Intercept
563 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
568 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
571 GDCM_NAME_SPACE::FileHelper *fileH;
572 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
573 // cast is just to avoid warnings (*no* conversion is performed)
574 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
576 fileH->SetImageData((uint8_t *)tabVal,X*Y*nbFrames*sizeof(uint16_t));
577 fileH->SetWriteModeToRaw();
578 fileH->SetWriteTypeToDcmExplVR();
580 if( !fileH->Write(dcmImageName))
581 std::cout << "Failed for [" << dcmImageName << "]\n"
582 << " File is unwrittable" << std::endl;
593 // =====================================================================================================================