]> Creatis software - gdcm.git/blob - Example/Dense2007ToDicom.cxx
Fix mistypings
[gdcm.git] / Example / Dense2007ToDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Dense2007ToDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/31 15:05:07 $
7   Version:   $Revision: 1.9 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include <fstream>
20 #include <iostream>
21 //#include <values.h>
22
23 #if defined(__BORLANDC__)
24 #include <ctype.h>
25 #endif
26
27 #include "gdcmFile.h"
28 #include "gdcmFileHelper.h"
29 #include "gdcmDebug.h"
30 #include "gdcmDirList.h"
31 #include "gdcmUtil.h"
32 #include "gdcmArgMgr.h"
33
34 /**
35   * \brief   
36   *  Converts the "Dense" ".txt" (2007 version)  files into 16 bits Dicom Files,
37   * Hope they don't change soon!
38   */  
39
40 void LoadPeakStrain(std::ifstream &from, std::string imageName, const char * patientname, std::string studyUID);
41 void LoadStrain(std::ifstream &from, std::string imageName, const char * patientname, bool createMultiFrame, std::string studyUID);
42 void MakeDicomImage(float *tabVal, float *X, float *Y, float *Z, int NP, std::string dcmImageName,
43                     const char * patientname, int nbFrames, std::string studyUID, std::string serieUID);
44
45 bool verbose;
46
47 int main(int argc, char *argv[])
48 {
49    START_USAGE(usage)
50    " \n Dense2007ToDicom :\n                                                  ",
51    "        Converts the '.txt' files into 16 bits Dicom Files,               ",
52    " usage:                                                                   ",
53    " Dense2007ToDicom strain=...strain.txt  peak_strain=...peak_strain.txt    ",
54    "                 [patientname = Patient's name]                           ",
55    "                 [m]ultiframe                                             ",
56    "                 [verbose] [debug]                                        ",
57    "                                                                          ",
58    " verbose  : user wants to run the program in 'verbose mode'               ",
59    " debug    : *developer*  wants to run the program in 'debug mode'         ",
60    FINISH_USAGE
61
62    // ----- Initialize Arguments Manager ------
63       
64    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
65   
66    if (argc == 1 || am->ArgMgrDefined("usage")) 
67    {
68       am->ArgMgrUsage(usage); // Display 'usage'
69       delete am;
70       return 0;
71    }
72    // Seems that ArgMgrWantString doesn't work on MacOS   
73    if(!am->ArgMgrDefined("strain"))
74    {
75       std::cout << "strain is mandatory" << std::endl;
76       exit(0);   
77    }
78    if(!am->ArgMgrDefined("peak_strain"))
79    {
80       std::cout << "peak_strain is mandatory" << std::endl;
81       exit(0);   
82    }
83       
84    const char *strain      = am->ArgMgrWantString("strain",usage);
85    const char *peak_strain = am->ArgMgrWantString("peak_strain",usage);
86
87    const char *patientName = am->ArgMgrGetString("patientname", "Patient^Name");
88    
89    bool createMultiFrame = (am->ArgMgrDefined("m") != 0);
90          
91    if (am->ArgMgrDefined("debug"))
92       GDCM_NAME_SPACE::Debug::DebugOn();
93
94    verbose  =  ( 0 != am->ArgMgrDefined("verbose") );     
95
96    // if unused Param we give up
97    if ( am->ArgMgrPrintUnusedLabels() )
98    { 
99       am->ArgMgrUsage(usage);
100       delete am;
101       return 0;
102    }
103    delete am;  // we don't need Argument Manager any longer
104
105    // ----- Begin Processing -----
106
107    std::ifstream fromPeakStrain( peak_strain );             
108    if ( !fromPeakStrain )
109    {
110       std::cout << "Can't open file [" << peak_strain << "]" << std::endl;
111       exit(0);
112    }
113
114    std::ifstream fromStrain( strain );      
115    if ( !fromStrain )
116    {
117       std::cout << "Can't open file [" << strain << "]" << std::endl;
118       exit(0);
119    }
120      
121    std::string strStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
122        
123    std::cout << "Success in open file [" << peak_strain << "]" << std::endl;
124    LoadPeakStrain(fromPeakStrain, GDCM_NAME_SPACE::Util::GetName(peak_strain), patientName,strStudyUID);
125    fromPeakStrain.close();  
126
127    std::cout << "Success in open file [" << strain << "]" << std::endl;
128    LoadStrain(fromStrain, GDCM_NAME_SPACE::Util::GetName(strain), patientName, createMultiFrame, strStudyUID);
129    fromStrain.close();      
130    return 1;            
131 }
132
133 // =====================================================================================================================
134
135 void LoadPeakStrain(std::ifstream &from, std::string textFileName, const char * patientname,std::string studyUID)
136 {
137 // in sax_base_slice0_peak_strain.txt :
138
139 /*
140 Number of material points (NP) = 181
141 Origin of (readout, phase enc, slice sel) coordinates in 3D =  87.3243 3.19392
142 88.2381
143 Readout direction in 3D =  -0.162314 -0.0771294 -0.983720
144 Phase Enc. direction in 3D =  -0.540606 -0.827052 0.154046
145 Slice select direction in 3D =  0.825469 -0.556809 -0.0925458
146 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
147 followed by their peak Ecc strain, an array of NP elements,
148 followed by their peak Err strain, an array of NP elements,
149 followed by their peak E11 strain, an array of NP elements,
150 followed by their Peak E22 strain, an array of NP elements,
151       42.0000      10.0000     0.000000
152       ...
153     -0.154905   -0.0840482    -0.157350    -0.221403    -0.168118    -0.131331
154     -0.153781    -0.148481    -0.166602    -0.232858    -0.222650    -0.213712
155     ...
156 */  
157
158    if (!from)
159       return;
160
161    std::string str1;
162    int NP;
163
164    //Number of material points (NP) = 181   
165     from >> str1;
166     from >> str1;
167     from >> str1;
168     from >> str1;
169     from >> str1;
170     from >> str1;
171     from >> NP;
172
173     std::cout << "NP : " << NP << std::endl; 
174
175    //Origin of (readout, phase enc, slice sel) coordinates in 3D =  87.3243 3.19392 88.2381
176      from >> str1;
177      from >> str1;
178      from >> str1;
179      from >> str1;
180      from >> str1;
181      from >> str1;
182      from >> str1;
183      from >> str1;
184      from >> str1;
185      from >> str1;
186      from >> str1;
187
188      float readout,  phase_enc, slice_sel;
189      from >> readout;
190      from >> phase_enc;
191      from >> slice_sel;
192      std::cout << " readout " << readout << " phase_enc " << phase_enc << " slice_sel " << slice_sel << std::endl;
193
194     // Readout direction in 3D =  -0.162314 -0.0771294 -0.983720
195
196     from >> str1;
197     from >> str1;
198     from >> str1;
199     from >> str1;
200     from >> str1;
201
202     float readoutX, readoutY, readoutZ;
203     from >> readoutX;
204     from >> readoutY;       
205     from >> readoutZ;
206     std::cout << " readoutX " << readoutX <<  " readoutY " << readoutY <<  " readoutZ " << readoutZ << std::endl;
207
208 // Phase Enc. direction in 3D =  -0.540606 -0.827052 0.154046
209
210      from >> str1;
211      from >> str1;
212      from >> str1;
213      from >> str1;
214      from >> str1;
215      from >> str1;
216
217     float phase_encX, phase_encY, phase_encZ;
218     from >> phase_encX;
219     from >> phase_encY;       
220     from >> phase_encZ;
221     std::cout << " phase_encX " << phase_encX <<  " phase_encY " << phase_encY <<  " phase_encZ " << phase_encZ << std::endl; 
222
223 // Slice select direction in 3D =  0.825469 -0.556809 -0.0925458
224      from >> str1;
225      from >> str1;
226      from >> str1;
227      from >> str1;
228      from >> str1;
229      from >> str1;
230
231     float slice_selX, slice_selY, slice_selZ;
232     from >> slice_selX;
233     from >> slice_selY;       
234     from >> slice_selZ;
235     std::cout << " slice_selX " << slice_selX <<  " slice_selY " << slice_selY <<  " slice_selZ " << slice_selZ << std::endl; 
236
237
238 // Skip 5 lines :
239 /*
240 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
241 followed by their peak Ecc strain, an array of NP elements,
242 followed by their peak Err strain, an array of NP elements,
243 followed by their peak E11 strain, an array of NP elements,
244 followed by their Peak E22 strain, an array of NP elements,
245 */
246
247 std::cout << "------------start skipping 1 line---------------- " << std::endl;
248    std::getline(from, str1);
249    std::cout << "[" << str1 << "]" << std::endl;
250 std::cout << "------------start skipping 1 line---------------- " << std::endl;
251    std::getline(from, str1);
252    std::cout << "[" << str1 << "]" << std::endl;
253 std::cout << "------------start skipping 1 line---------------- " << std::endl;
254    std::getline(from, str1);
255    std::cout << "[" << str1 << "]" << std::endl;
256 std::cout << "------------start skipping 1 line---------------- " << std::endl;
257    std::getline(from, str1);
258    std::cout << "[" << str1 << "]" << std::endl;
259 std::cout << "------------start skipping 1 line---------------- " << std::endl;
260    std::getline(from, str1);
261    std::cout << "[" << str1 << "]" << std::endl;
262 std::cout << "------------start skipping 1 line---------------- " << std::endl;
263    std::getline(from, str1);
264    std::cout << "[" << str1 << "]" << std::endl;
265 std::cout << "------------stop skipping ---------------- " << std::endl;
266
267    float *X = new float[NP];
268    float *Y = new float[NP];   
269    float *Z = new float[NP];
270
271    char c;
272    int i;   
273    for (i=0; i<NP; i++) {
274
275       from >> X[i];
276       for (;;) {
277         if (!from.get(c))
278           break;
279         if (!isspace(c)) {
280           from.putback(c);
281           break;
282         }
283      }
284
285       from >> Y[i];  
286       for (;;) {
287         if (!from.get(c))
288           break;
289         if (!isspace(c)) {
290           from.putback(c);
291           break;
292         }
293      }     
294       from >> Z[i];
295
296    } // end for i<NP
297
298    std::string dcmImageName;    
299    std::string serieUID;
300
301    std::cout << "--------------- Ecc_strain ------------------" << std::endl;
302    float *ecc_strain = new float[NP];
303    for (i=0; i<NP; i++) {
304       from >> ecc_strain[i]; 
305      // if (verbose)
306      //    std::cout <<  ecc_strain[i] <<  std::endl;
307    }
308 //followed by their peak Ecc strain, an array of NP elements,
309    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
310    dcmImageName = textFileName + "_peak_Ecc_strain.dcm";
311    MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);   
312    delete []ecc_strain;
313
314    std::cout << "--------------- Err_strain ------------------" << std::endl;
315    float *err_strain = new float[NP];
316    for (i=0; i<NP; i++) {
317       from >> err_strain[i]; 
318       //if (verbose)
319       //   std::cout <<  err_strain[i] <<  std::endl;
320    }
321 //followed by their peak Err strain, an array of NP elements,
322    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
323    dcmImageName = textFileName + "_peak_Err_strain.dcm";
324    MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
325    delete []err_strain;
326
327    std::cout << "--------------- E11_strain ------------------" << std::endl;
328    float *e11_strain = new float[NP];
329    for (i=0; i<NP; i++) {
330       from >> e11_strain[i]; 
331       //if (verbose)
332       //   std::cout <<  e11_strain[i] <<  std::endl;
333    }
334 //followed by their peak E11 strain, an array of NP elements,
335    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
336    dcmImageName = textFileName + "_peak_E11_strain.dcm";
337    MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
338    delete []e11_strain;          
339
340
341    std::cout << "--------------- E22_strain ------------------" << std::endl;
342    float *e22_strain = new float[NP];
343    for (i=0; i<NP; i++) {
344       from >> e22_strain[i]; 
345       //if (verbose)
346       //   std::cout <<  e22_strain[i] <<  std::endl;
347    }
348 //followed by their Peak E22 strain, an array of NP elements,
349    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
350    dcmImageName = textFileName + "_peak_E22_strain.dcm";
351    MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);         
352    delete []e22_strain;
353      
354 }
355
356 // =====================================================================================================================
357
358 void LoadStrain(std::ifstream &from, std::string textFileName, const char * patientname, bool createMultiFrame, std::string studyUID)
359 {
360
361 // in sax_base_slice0_strain.txt :
362 /*
363 Number of cine frames = 18
364 Temporal resolution = 32.0000 ms
365 First frame starts at 48.0000 ms
366 Number of material points (NP) = 181
367 Origin of (readout, phase enc, slice sel) coordinates in 3D =  87.324341 3.193918 88.238113 
368 Readout direction in 3D =  -0.162314 -0.0771294 -0.983720
369 Phase Enc. direction in 3D =  -0.540606 -0.827052 0.154046
370 Slice select direction in 3D =  0.825469 -0.556809 -0.0925458
371 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
372 followed by their Ecc strain, an array of dimensions(NP, number of cine frames),
373 followed by their Err strain, an array of dimensions(NP, number of cine frames),
374 followed by their E11 strain, an array of dimensions(NP, number of cine frames),
375 followed by their E22 strain, an array of dimensions(NP, number of cine frames),
376 Note that RV Err, E11 and E22 strains are not calculated due to the small thickness.
377       42.0000      10.0000     0.000000
378       44.0000      10.0000     0.000000
379       ...
380    -0.0622793   -0.0840482    -0.157350    -0.196722    -0.105844    -0.131331
381     -0.153781  -0.00940573   -0.0542236    -0.100403   -0.0369671   -0.0696840      
382 */ 
383
384    if (!from)
385       return;
386
387    std::string str1;
388    int NP;    // Number of Points
389    int NCF;   // Number of cine frames
390    float TR;  // Temporal resolution
391    float FFS; // First frame starts
392
393    // Number of cine frames = 18
394     from >> str1;
395     from >> str1;
396     from >> str1;
397     from >> str1;
398     from >> str1;
399     from >> NCF;
400
401    // Temporal resolution = 32.0000 ms
402     from >> str1;
403     from >> str1;
404     from >> str1;
405     from >> TR;
406     from >> str1;
407
408    // First frame starts at 48.0000 ms
409     from >> str1;
410     from >> str1;
411     from >> str1;
412     from >> str1;
413     from >> FFS;
414     from >> str1;        
415
416    //Number of material points (NP) = 181   
417     from >> str1;
418     from >> str1;
419     from >> str1;
420     from >> str1;
421     from >> str1;
422     from >> str1;
423     from >> NP;
424
425     std::cout << "NP : " << NP << std::endl; 
426
427    //Origin of (readout, phase enc, slice sel) coordinates in 3D = 87.324341 3.193918 88.238113 
428      from >> str1;
429      from >> str1;
430      from >> str1;
431      from >> str1;
432      from >> str1;
433      from >> str1;
434      from >> str1;
435      from >> str1;
436      from >> str1;
437      from >> str1;
438      from >> str1;
439
440      float readout,  phase_enc, slice_sel;
441      from >> readout;
442      from >> phase_enc;
443      from >> slice_sel;
444      std::cout << " readout " << readout << " phase_enc " << phase_enc << " slice_sel " << slice_sel << std::endl;
445
446     // Readout direction in 3D =  -0.162314 -0.0771294 -0.983720
447
448     from >> str1;
449     from >> str1;
450     from >> str1;
451     from >> str1;
452     from >> str1;
453
454     float readoutX, readoutY, readoutZ;
455     from >> readoutX;
456     from >> readoutY;       
457     from >> readoutZ;
458     std::cout << " readoutX " << readoutX <<  " readoutY " << readoutY <<  " readoutZ " << readoutZ << std::endl;
459
460 // Phase Enc. direction in 3D =  -0.540606 -0.827052 0.154046
461
462      from >> str1;
463      from >> str1;
464      from >> str1;
465      from >> str1;
466      from >> str1;
467      from >> str1;
468
469     float phase_encX, phase_encY, phase_encZ;
470     from >> phase_encX;
471     from >> phase_encY;       
472     from >> phase_encZ;
473     std::cout << " phase_encX " << phase_encX <<  " phase_encY " << phase_encY <<  " phase_encZ " << phase_encZ << std::endl; 
474
475 // Slice select direction in 3D =  0.825469 -0.556809 -0.0925458
476      from >> str1;
477      from >> str1;
478      from >> str1;
479      from >> str1;
480      from >> str1;
481      from >> str1;
482      
483     float slice_selX, slice_selY, slice_selZ;
484     from >> slice_selX;
485     from >> slice_selY;       
486     from >> slice_selZ;
487     std::cout << " slice_selX " << slice_selX <<  " slice_selY " << slice_selY <<  " slice_selZ " << slice_selZ << std::endl; 
488
489
490
491 // Skip 6 lines :
492 /*
493 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
494 followed by their Ecc strain, an array of dimensions(NP, number of cine frames),
495 followed by their Err strain, an array of dimensions(NP, number of cine frames),
496 followed by their E11 strain, an array of dimensions(NP, number of cine frames),
497 followed by their E22 strain, an array of dimensions(NP, number of cine frames),
498 Note that RV Err, E11 and E22 strains are not calculated due to the small thickness.
499 */
500 std::cout << "------------start skipping 1 line---------------- " << std::endl;
501    std::getline(from, str1);
502    std::cout << "[" << str1 << "]" << std::endl;
503 std::cout << "------------start skipping 1 line---------------- " << std::endl;
504    std::getline(from, str1);
505    std::cout << "[" << str1 << "]" << std::endl;
506 std::cout << "------------start skipping 1 line---------------- " << std::endl;
507    std::getline(from, str1);
508    std::cout << "[" << str1 << "]" << std::endl;
509 std::cout << "------------start skipping 1 line---------------- " << std::endl;
510    std::getline(from, str1);
511    std::cout << "[" << str1 << "]" << std::endl;
512 std::cout << "------------start skipping 1 line---------------- " << std::endl;
513    std::getline(from, str1);
514    std::cout << "[" << str1 << "]" << std::endl;
515 std::cout << "------------start skipping 1 line---------------- " << std::endl;
516    std::getline(from, str1);
517    std::cout << "[" << str1 << "]" << std::endl;
518 std::cout << "------------stop skipping ---------------- " << std::endl;
519    std::getline(from, str1);
520    std::cout << "[" << str1 << "]" << std::endl;
521 std::cout << "------------stop skipping ---------------- " << std::endl;
522   
523    float *X = new float[NP];
524    float *Y = new float[NP];   
525    float *Z = new float[NP];
526       
527    char c;
528    int i;   
529    for (i=0; i<NP; i++) {
530    
531       from >> X[i];
532       for (;;) {
533         if (!from.get(c))
534           break;
535         if (!isspace(c)) {
536           from.putback(c);
537           break;
538         }
539      }  
540       from >> Y[i];  
541       for (;;) {
542         if (!from.get(c))
543           break;
544         if (!isspace(c)) {
545           from.putback(c);
546           break;
547         }
548      }        
549       from >> Z[i];              
550    }
551
552 char frame[10];
553 std::string dcmImageName;    
554 std::string serieUID;
555
556 std::cout << "=======================================================================================" << createMultiFrame << std::endl;
557 if(!createMultiFrame) {     // One image per file here (single frame)
558
559 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
560 float *ecc_strain = new float[NP];
561 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
562 {
563 sprintf(frame, "_%d", nbr_of_frames);
564
565    std::cout << "--------------- Ecc_strain ------------------" << std::endl;
566    for (i=0; i<NP; i++) {
567        from >> ecc_strain[i];
568        if (verbose)
569        std::cout <<  ecc_strain[i] <<  std::endl;
570    }
571 //followed by their Ecc strain, an array of NP elements,
572    dcmImageName = textFileName + frame + "_Ecc_strain.dcm";
573    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
574    MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);      
575 }// end for   nbr_of_frames
576 delete []ecc_strain;
577
578 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
579 float *err_strain = new float[NP];
580 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
581 {
582 sprintf(frame, "_%d", nbr_of_frames);
583    std::cout << "--------------- Err_strain ------------------" << std::endl;
584    for (i=0; i<NP; i++) {
585        from >> err_strain[i]; 
586        if (verbose)
587        std::cout <<  err_strain[i] <<  std::endl;
588    }
589 //followed by their  Err strain, an array of NP elements,
590    dcmImageName = textFileName + frame + "_Err_strain.dcm";
591    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
592    MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
593 }// end for   nbr_of_frames
594 delete []err_strain;
595  
596 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
597 float *e11_strain = new float[NP];
598 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
599 {
600 sprintf(frame, "_%d", nbr_of_frames);   
601    std::cout << "--------------- E11_strain ------------------" << std::endl;
602    for (i=0; i<NP; i++) {
603        from >> e11_strain[i]; 
604        if (verbose)
605        std::cout <<  e11_strain[i] <<  std::endl;
606    }  
607 //followed by their E11 strain, an array of NP elements,
608    dcmImageName = textFileName + frame + "_E11_strain.dcm";
609    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
610    MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
611 }// end for   nbr_of_frames   
612 delete []e11_strain;
613
614 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
615 float *e22_strain = new float[NP];
616 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
617 {
618 sprintf(frame, "_%d", nbr_of_frames); 
619    std::cout << "--------------- E22_strain ------------------" << std::endl;
620    for (i=0; i<NP; i++) {
621        from >> e22_strain[i]; 
622        if (verbose)
623        std::cout <<  e22_strain[i] <<  std::endl;
624    }   
625 //followed by their E22 strain, an array of NP elements,
626    dcmImageName = textFileName + frame + "_E22_strain.dcm";
627    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
628    MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);   
629 } // end for   nbr_of_frames
630 delete [] e22_strain;
631  
632 } // end of single frame
633
634
635 else                      // generate Multiframe files
636 {
637
638
639 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
640 float *ecc_strain = new float[NP*NCF];
641    std::cout << "--------------- Ecc_strain ------------------" << std::endl;
642    for (i=0; i<NP*NCF; i++) {
643        from >> ecc_strain[i];
644        if (verbose)
645        std::cout <<  ecc_strain[i] <<  std::endl;
646    }
647 //followed by their Ecc strain, an array of NP elements,
648    dcmImageName = textFileName + "_Ecc_strain.dcm";
649    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
650    MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
651 delete []ecc_strain;
652
653
654
655 float *err_strain = new float[NP*NCF];
656    std::cout << "--------------- Err_strain ------------------" << std::endl;
657    for (i=0; i<NP*NCF; i++) {
658        from >> err_strain[i];
659        if (verbose)
660        std::cout <<  err_strain[i] <<  std::endl;
661    }
662 //followed by their Ecc strain, an array of NP elements,
663    dcmImageName = textFileName + "_Err_strain.dcm";
664    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
665    MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
666 delete []err_strain;
667
668
669
670 float *e11_strain = new float[NP*NCF];
671    std::cout << "--------------- E11_strain ------------------" << std::endl;
672    for (i=0; i<NP*NCF; i++) {
673        from >> e11_strain[i];
674        if (verbose)
675        std::cout <<  e11_strain[i] <<  std::endl;
676    }
677 //followed by their Ecc strain, an array of NP elements,
678    dcmImageName = textFileName + "_E11_strain.dcm";
679    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
680    MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
681 delete []e11_strain;
682
683
684
685 float *e22_strain = new float[NP*NCF];
686    std::cout << "--------------- E22_strain ------------------" << std::endl;
687    for (i=0; i<NP*NCF; i++) {
688        from >> e22_strain[i];
689        if (verbose)
690        std::cout <<  e22_strain[i] <<  std::endl;
691    }
692 //followed by their Ecc strain, an array of NP elements,
693    dcmImageName = textFileName + "_E22_strain.dcm";
694    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
695    MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
696 delete []e22_strain;
697
698 }    // end of Multiframe    
699 }
700
701
702 // =====================================================================================================================
703     
704
705 void MakeDicomImage(float *tabVal, float *X, float *Y, float *Z, int NP, std::string dcmImageName, const char * patientName, int nbFrames, std::string studyUID, std::string serieUID)
706 {
707
708 std::cout << "=============================================================================="
709           << "enter MakeDicomImage [" << dcmImageName << "] [" << patientName << "]" << std::endl;
710    float minX = 99999., minY = 99999., minZ = 99999.;
711    float maxX = 0., maxY = 0., maxZ = 0.;
712    int i;
713    
714    for (i=0; i<NP; i++) {
715       // std::cout << X[i] << " " << Y[i] << " " << Z[i] <<  std::endl;
716       if(maxX < X[i])
717          maxX = X[i];
718       if(maxY < Y[i])
719          maxY = Y[i];
720       if(maxZ < Z[i])
721          maxZ = Z[i];
722  
723       if(minX > X[i])
724          minX = X[i];
725       if(minY > Y[i])
726          minY = Y[i];
727       if(minZ > Z[i])
728          minZ = Z[i];
729    }   
730    std::cout << "Min X,Y,Z " << minX << " " << minY << " " << minZ <<  std::endl;
731    std::cout << "Max X,Y,Z " << maxX << " " << maxY << " " << maxZ <<  std::endl;
732    std::cout << "Size X,Y,Z " << maxX-minX << " " << maxY-minY << " " << maxZ-minZ <<  std::endl;      
733
734    int lgrFrame = int(maxX*4.)*int(maxY*4.);
735    uint16_t *img = new uint16_t[lgrFrame*nbFrames ];
736
737    // Set whole image to 0 
738    for(int i3=0; i3<lgrFrame*nbFrames; i3++)
739       img[i3] = 0;
740        
741 for(int i4=0; i4<nbFrames; i4++)
742    for(int i2=0; i2<NP; i2++) {   
743       int ordX = int(X[i2]*4.-30);
744       int ordY = int(maxY*4.) - int(Y[i2]*4.)+30;      
745       img[ lgrFrame*i4 + ordX   +  ordY   * int(maxX*4.) ] = int(tabVal[i2 + NP*i4]*100);
746
747       // Try to round up, just to see.   
748        for(int iii=ordY-3; iii<ordY+4; iii++) 
749           for(int jjj=ordX-3; jjj<ordX+4; jjj++) 
750              img[  lgrFrame*i4 + jjj  +  iii   * int(maxX*4.) ] = int(tabVal[i2 + NP*i4]*100);            
751    }
752
753  std::cout << "===========sortie recup points" << std::endl; 
754  // GDCM_NAME_SPACE::Debug::DebugOn();
755   
756    std::ostringstream str;
757
758    GDCM_NAME_SPACE::File *file;
759    file = GDCM_NAME_SPACE::File::New();       
760       
761   // Set the image size
762    str.str(""); 
763    str << (int)(maxX*4.);
764    file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
765    str.str("");
766    str << (int)(maxY*4.);
767    file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
768
769   // Set the pixel type
770   //      16; //8, 16, 32
771    file->InsertEntryString("16",0x0028,0x0100,"US"); // Bits Allocated
772    str.str("");
773    str << 16; // may be 12 or 16 if componentSize =16
774    file->InsertEntryString("16",0x0028,0x0101,"US"); // Bits Stored
775    file->InsertEntryString("15",0x0028,0x0102,"US"); // High Bit
776
777   // Set the pixel representation // 0/1 , 0=unsigned
778    file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
779   // Set the samples per pixel // 1:Grey level, 3:RGB
780    file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
781
782    if (nbFrames != 1)
783    {
784       str.str("");
785       str << nbFrames;
786       file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames  
787    }
788   
789    if (strlen(patientName) != 0)
790       file->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
791
792    file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
793    file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
794      
795    int pos = 0;  // get the usefull part of the name
796 /*  
797    for(i=0, pos=0; pos<dcmImageName.size()-4; pos++, i++) {
798      if( dcmImageName[i]=='.' &&dcmImageName[i+1]=='t' && dcmImageName[i+2]=='x' && dcmImageName[i+3]=='t'  
799        && dcmImageName[i+3]=='_') {
800        pos+=5;
801        break;
802      }
803    }
804 */  
805    file->InsertEntryString(&(dcmImageName.c_str()[pos]),0x0008,0x103e, "LO");  // Series Description   
806 /*
807   // Set Rescale Intercept
808         str.str("");
809         str << div;  
810         file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
811
812   // Set Rescale Slope
813         str.str("");
814         str << mini;  
815         file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
816 */
817     
818    GDCM_NAME_SPACE::FileHelper *fileH;
819    fileH = GDCM_NAME_SPACE::FileHelper::New(file);
820    // cast is just to avoid warnings (*no* conversion)
821    //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
822    
823    fileH->SetImageData((uint8_t *)img,int(maxX*4.)*int(maxY*4.)*nbFrames*sizeof(uint16_t));
824    fileH->SetWriteModeToRaw(); 
825    fileH->SetWriteTypeToDcmExplVR();
826         
827    if( !fileH->Write(dcmImageName))
828       std::cout << "Failed for [" << dcmImageName << "]\n"
829                 << "           File is unwrittable" << std::endl;
830
831    //file->Print();
832            
833    delete img;
834    file->Delete();
835    fileH->Delete();  
836 }