1 /*=========================================================================
4 Module: $RCSfile: Dense2007ToDicom.cxx,v $
6 Date: $Date: 2007/07/10 11:00:50 $
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 =========================================================================*/
24 #include "gdcmFileHelper.h"
25 #include "gdcmDebug.h"
26 #include "gdcmDirList.h"
28 #include "gdcmArgMgr.h"
32 * Converts the "Dense" ".txt" (2007 version) files into 16 bits Dicom Files,
33 * Hope they don't change soon!
37 void LoadPeakStrain(std::ifstream &from, std::string imageName);
38 void LoadStrain(std::ifstream &from, std::string imageName);
39 void MakeDicomImage(float *tabVal, float *X, float *Y, float *Z, int NP, std::string dcmImageName);
42 int main(int argc, char *argv[])
45 " \n Dense2007ToDicom :\n ",
46 " Converts the '.txt' files into 16 bits Dicom Files, ",
48 " Dense2007ToDicom strain=...strain.txt peak_strain=...peak_strain.txt ",
49 " [verbose] [debug] ",
51 " verbose : user wants to run the program in 'verbose mode' ",
52 " debug : *developer* wants to run the program in 'debug mode' ",
55 // ----- Initialize Arguments Manager ------
57 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
59 if (argc == 1 || am->ArgMgrDefined("usage"))
61 am->ArgMgrUsage(usage); // Display 'usage'
66 const char *strain = am->ArgMgrWantString("strain",usage);
67 const char *peak_strain = am->ArgMgrWantString("peak_strain",usage);
69 if (am->ArgMgrDefined("debug"))
70 GDCM_NAME_SPACE::Debug::DebugOn();
72 int verbose = am->ArgMgrDefined("verbose");
75 // if unused Param we give up
76 if ( am->ArgMgrPrintUnusedLabels() )
78 am->ArgMgrUsage(usage);
82 delete am; // we don't need Argument Manager any longer
84 // ----- Begin Processing -----
87 std::ifstream fromPeakStrain( peak_strain );
88 if ( !fromPeakStrain )
90 std::cout << "Can't open file" << peak_strain << std::endl;
94 std::ifstream fromStrain( strain );
97 std::cout << "Can't open file" << strain << std::endl;
101 std::cout << "Success in open file" << peak_strain << std::endl;
102 LoadPeakStrain(fromPeakStrain, peak_strain);
103 fromPeakStrain.close();
105 std::cout << "Success in open file" << strain << std::endl;
106 LoadStrain(fromStrain, strain);
111 // =====================================================================================================================
113 void LoadPeakStrain(std::ifstream &from, std::string textFileName)
115 // in sax_base_slice0_peak_strain.txt :
118 Number of material points (NP) = 181
119 Origin of (readout, phase enc, slice sel) coordinates in 3D = 87.3243 3.19392
121 Readout direction in 3D = -0.162314 -0.0771294 -0.983720
122 Phase Enc. direction in 3D = -0.540606 -0.827052 0.154046
123 Slice select direction in 3D = 0.825469 -0.556809 -0.0925458
124 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
125 followed by their peak Ecc strain, an array of NP elements,
126 followed by their peak Err strain, an array of NP elements,
127 followed by their peak E11 strain, an array of NP elements,
128 followed by their Peak E22 strain, an array of NP elements,
129 42.0000 10.0000 0.000000
131 -0.154905 -0.0840482 -0.157350 -0.221403 -0.168118 -0.131331
132 -0.153781 -0.148481 -0.166602 -0.232858 -0.222650 -0.213712
142 //Number of material points (NP) = 181
151 std::cout << "NP : " << NP << std::endl;
153 //Origin of (readout, phase enc, slice sel) coordinates in 3D = 87.3243 3.19392 88.2381
166 float readout, phase_enc, slice_sel;
170 std::cout << " readout " << readout << " phase_enc " << phase_enc << " slice_sel " << slice_sel << std::endl;
172 // Readout direction in 3D = -0.162314 -0.0771294 -0.983720
180 float readoutX, readoutY, readoutZ;
184 std::cout << " readoutX " << readoutX << " readoutY " << readoutY << " readoutZ " << readoutZ << std::endl;
186 // Phase Enc. direction in 3D = -0.540606 -0.827052 0.154046
195 float phase_encX, phase_encY, phase_encZ;
199 std::cout << " phase_encX " << phase_encX << " phase_encY " << phase_encY << " phase_encZ " << phase_encZ << std::endl;
201 // Slice select direction in 3D = 0.825469 -0.556809 -0.0925458
209 float slice_selX, slice_selY, slice_selZ;
213 std::cout << " slice_selX " << slice_selX << " slice_selY " << slice_selY << " slice_selZ " << slice_selZ << std::endl;
219 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
220 followed by their peak Ecc strain, an array of NP elements,
221 followed by their peak Err strain, an array of NP elements,
222 followed by their peak E11 strain, an array of NP elements,
223 followed by their Peak E22 strain, an array of NP elements,
226 std::cout << "------------start skipping 1 line---------------- " << std::endl;
227 std::getline(from, str1);
228 std::cout << "[" << str1 << "]" << std::endl;
229 std::cout << "------------start skipping 1 line---------------- " << std::endl;
230 std::getline(from, str1);
231 std::cout << "[" << str1 << "]" << std::endl;
232 std::cout << "------------start skipping 1 line---------------- " << std::endl;
233 std::getline(from, str1);
234 std::cout << "[" << str1 << "]" << std::endl;
235 std::cout << "------------start skipping 1 line---------------- " << std::endl;
236 std::getline(from, str1);
237 std::cout << "[" << str1 << "]" << std::endl;
238 std::cout << "------------start skipping 1 line---------------- " << std::endl;
239 std::getline(from, str1);
240 std::cout << "[" << str1 << "]" << std::endl;
241 std::cout << "------------start skipping 1 line---------------- " << std::endl;
242 std::getline(from, str1);
243 std::cout << "[" << str1 << "]" << std::endl;
244 std::cout << "------------stop skipping ---------------- " << std::endl;
246 float *X = new float[NP];
247 float *Y = new float[NP];
248 float *Z = new float[NP];
252 for (i=0; i<NP; i++) {
274 std::cout << "--------------- Ecc_strain ------------------" << std::endl;
275 float *ecc_strain = new float[NP];
276 for (i=0; i<NP; i++) {
277 from >> ecc_strain[i];
278 std::cout << ecc_strain[i] << std::endl;
281 std::cout << "--------------- Err_strain ------------------" << std::endl;
282 float *err_strain = new float[NP];
283 for (i=0; i<NP; i++) {
284 from >> err_strain[i];
285 std::cout << err_strain[i] << std::endl;
288 std::cout << "--------------- E11_strain ------------------" << std::endl;
289 float *e11_strain = new float[NP];
290 for (i=0; i<NP; i++) {
291 from >> e11_strain[i];
292 std::cout << e11_strain[i] << std::endl;
295 std::cout << "--------------- E22_strain ------------------" << std::endl;
296 float *e22_strain = new float[NP];
297 for (i=0; i<NP; i++) {
298 from >> e22_strain[i];
299 std::cout << e22_strain[i] << std::endl;
302 std::string dcmImageName;
304 //followed by their peak Ecc strain, an array of NP elements,
305 dcmImageName = textFileName + "_peak_Ecc_strain.dcm";
306 MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName);
308 //followed by their peak Err strain, an array of NP elements,
309 dcmImageName = textFileName + "_peak_Err_strain.dcm";
310 MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName);
312 //followed by their peak E11 strain, an array of NP elements,
313 dcmImageName = textFileName + "_peak_E11_strain.dcm";
314 MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName);
316 //followed by their Peak E22 strain, an array of NP elements,
317 dcmImageName = textFileName + "_peak_E22_strain.dcm";
318 MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName);
321 // =====================================================================================================================
323 void LoadStrain(std::ifstream &from, std::string textFileName)
326 // in sax_base_slice0_strain.txt :
328 Number of cine frames = 18
329 Temporal resolution = 32.0000 ms
330 First frame starts at 48.0000 ms
331 Number of material points (NP) = 181
332 Origin of (readout, phase enc, slice sel) coordinates in 3D = 87.324341 3.193918 88.238113
333 Readout direction in 3D = -0.162314 -0.0771294 -0.983720
334 Phase Enc. direction in 3D = -0.540606 -0.827052 0.154046
335 Slice select direction in 3D = 0.825469 -0.556809 -0.0925458
336 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
337 followed by their Ecc strain, an array of dimensions(NP, number of cine frames),
338 followed by their Err strain, an array of dimensions(NP, number of cine frames),
339 followed by their E11 strain, an array of dimensions(NP, number of cine frames),
340 followed by their E22 strain, an array of dimensions(NP, number of cine frames),
341 Note that RV Err, E11 and E22 strains are not calculated due to the small thickness.
342 42.0000 10.0000 0.000000
343 44.0000 10.0000 0.000000
345 -0.0622793 -0.0840482 -0.157350 -0.196722 -0.105844 -0.131331
346 -0.153781 -0.00940573 -0.0542236 -0.100403 -0.0369671 -0.0696840
353 int NP; // Number of Pints
354 int NCF; // Number of cine frames
355 float TR; // Temporal resolution
356 float FFS; // First frame starts
358 // Number of cine frames = 18
366 // Temporal resolution = 32.0000 ms
373 // First frame starts at 48.0000 ms
381 //Number of material points (NP) = 181
390 std::cout << "NP : " << NP << std::endl;
392 //Origin of (readout, phase enc, slice sel) coordinates in 3D = 87.324341 3.193918 88.238113
405 float readout, phase_enc, slice_sel;
409 std::cout << " readout " << readout << " phase_enc " << phase_enc << " slice_sel " << slice_sel << std::endl;
411 // Readout direction in 3D = -0.162314 -0.0771294 -0.983720
419 float readoutX, readoutY, readoutZ;
423 std::cout << " readoutX " << readoutX << " readoutY " << readoutY << " readoutZ " << readoutZ << std::endl;
425 // Phase Enc. direction in 3D = -0.540606 -0.827052 0.154046
434 float phase_encX, phase_encY, phase_encZ;
438 std::cout << " phase_encX " << phase_encX << " phase_encY " << phase_encY << " phase_encZ " << phase_encZ << std::endl;
440 // Slice select direction in 3D = 0.825469 -0.556809 -0.0925458
448 float slice_selX, slice_selY, slice_selZ;
452 std::cout << " slice_selX " << slice_selX << " slice_selY " << slice_selY << " slice_selZ " << slice_selZ << std::endl;
458 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
459 followed by their Ecc strain, an array of dimensions(NP, number of cine frames),
460 followed by their Err strain, an array of dimensions(NP, number of cine frames),
461 followed by their E11 strain, an array of dimensions(NP, number of cine frames),
462 followed by their E22 strain, an array of dimensions(NP, number of cine frames),
463 Note that RV Err, E11 and E22 strains are not calculated due to the small thickness.
465 std::cout << "------------start skipping 1 line---------------- " << std::endl;
466 std::getline(from, str1);
467 std::cout << "[" << str1 << "]" << std::endl;
468 std::cout << "------------start skipping 1 line---------------- " << std::endl;
469 std::getline(from, str1);
470 std::cout << "[" << str1 << "]" << std::endl;
471 std::cout << "------------start skipping 1 line---------------- " << std::endl;
472 std::getline(from, str1);
473 std::cout << "[" << str1 << "]" << std::endl;
474 std::cout << "------------start skipping 1 line---------------- " << std::endl;
475 std::getline(from, str1);
476 std::cout << "[" << str1 << "]" << std::endl;
477 std::cout << "------------start skipping 1 line---------------- " << std::endl;
478 std::getline(from, str1);
479 std::cout << "[" << str1 << "]" << std::endl;
480 std::cout << "------------start skipping 1 line---------------- " << std::endl;
481 std::getline(from, str1);
482 std::cout << "[" << str1 << "]" << std::endl;
483 std::cout << "------------stop skipping ---------------- " << std::endl;
484 std::getline(from, str1);
485 std::cout << "[" << str1 << "]" << std::endl;
486 std::cout << "------------stop skipping ---------------- " << std::endl;
488 float *X = new float[NP];
489 float *Y = new float[NP];
490 float *Z = new float[NP];
494 for (i=0; i<NP; i++) {
517 std::cout << "--------------- Ecc_strain ------------------" << std::endl;
518 float *ecc_strain = new float[NP];
519 for (i=0; i<NP; i++) {
520 from >> ecc_strain[i];
521 std::cout << ecc_strain[i] << std::endl;
524 std::cout << "--------------- Err_strain ------------------" << std::endl;
525 float *err_strain = new float[NP];
526 for (i=0; i<NP; i++) {
527 from >> err_strain[i];
528 std::cout << err_strain[i] << std::endl;
531 std::cout << "--------------- E11_strain ------------------" << std::endl;
532 float *e11_strain = new float[NP];
533 for (i=0; i<NP; i++) {
534 from >> e11_strain[i];
535 std::cout << e11_strain[i] << std::endl;
538 std::cout << "--------------- E22_strain ------------------" << std::endl;
539 float *e22_strain = new float[NP];
540 for (i=0; i<NP; i++) {
541 from >> e22_strain[i];
542 std::cout << e22_strain[i] << std::endl;
545 std::string dcmImageName;
547 //followed by their Ecc strain, an array of NP elements,
548 dcmImageName = textFileName + "_Ecc_strain.dcm";
549 MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName);
551 //followed by their Err strain, an array of NP elements,
552 dcmImageName = textFileName + "_Err_strain.dcm";
553 MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName);
555 //followed by their E11 strain, an array of NP elements,
556 dcmImageName = textFileName + "_E11_strain.dcm";
557 MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName);
559 //followed by their E22 strain, an array of NP elements,
560 dcmImageName = textFileName + "_E22_strain.dcm";
561 MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName);
566 // =====================================================================================================================
569 void MakeDicomImage(float *tabVal, float *X, float *Y, float *Z, int NP, std::string dcmImageName)
571 float minX = 999., minY = 999., minZ = 999.;
572 float maxX = 0., maxY = 0., maxZ = 0.;
574 for (i=0; i<NP; i++) {
575 // std::cout << X[i] << " " << Y[i] << " " << Z[i] << std::endl;
591 std::cout << "Min X,Y,Z " << minX << " " << minY << " " << minZ << std::endl;
592 std::cout << "Max X,Y,Z " << maxX << " " << maxY << " " << maxZ << std::endl;
593 std::cout << "Size X,Y,Z " << maxX-minX << " " << maxY-minY << " " << maxZ-minZ << std::endl;
596 uint16_t *img = new uint16_t[int(maxX*maxY)];
598 for(int i3=0;i3<int(maxX*maxY);i3++)
601 for(int i2=0; i2<NP; i2++) {
602 img[ int(maxX -(X[i2]-1))+ int((maxY -Y[i2])* maxX) ] = int(tabVal[i2]*100);
605 // GDCM_NAME_SPACE::Debug::DebugOn();
607 std::ostringstream str;
608 GDCM_NAME_SPACE::File *file;
609 file = GDCM_NAME_SPACE::File::New();
611 // Set the image size
614 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
617 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
618 // Set the pixel type
620 file->InsertEntryString("16",0x0028,0x0100,"US"); // Bits Allocated
622 str << 16; // may be 12 or 16 if componentSize =16
623 file->InsertEntryString("16",0x0028,0x0101,"US"); // Bits Stored
624 file->InsertEntryString("15",0x0028,0x0102,"US"); // High Bit
626 // Set the pixel representation // 0/1 , 0=unsigned
627 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
628 // Set the samples per pixel // 1:Grey level, 3:RGB
629 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
632 // Set Rescale Intercept
635 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
640 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
643 GDCM_NAME_SPACE::FileHelper *fileH;
644 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
645 // cast is just to avoid warnings (*no* conversion)
646 fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t));
647 fileH->SetWriteModeToRaw();
648 fileH->SetWriteTypeToDcmExplVR();
650 if( !fileH->Write(dcmImageName))
651 std::cout << "Failed for [" << dcmImageName << "]\n"
652 << " File is unwrittable" << std::endl;