]> Creatis software - gdcm.git/blob - Example/Dense2007ToDicom.cxx
bf76dcee397034af2c3af41e4541c28281753330
[gdcm.git] / Example / Dense2007ToDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Dense2007ToDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/03/28 15:36:57 $
7   Version:   $Revision: 1.7 $
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");
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, peak_strain, patientName,strStudyUID);
125    fromPeakStrain.close();  
126
127    std::cout << "Success in open file [" << strain << "]" << std::endl;
128    LoadStrain(fromStrain, 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::cout << "--------------- Ecc_strain ------------------" << std::endl;
299    float *ecc_strain = new float[NP];
300    for (i=0; i<NP; i++) {
301       from >> ecc_strain[i]; 
302       if (verbose)
303          std::cout <<  ecc_strain[i] <<  std::endl;
304    }
305    delete []ecc_strain;
306
307    std::cout << "--------------- Err_strain ------------------" << std::endl;
308    float *err_strain = new float[NP];
309    for (i=0; i<NP; i++) {
310       from >> err_strain[i]; 
311       if (verbose)
312          std::cout <<  err_strain[i] <<  std::endl;
313    }
314    delete []err_strain;
315
316    std::cout << "--------------- E11_strain ------------------" << std::endl;
317    float *e11_strain = new float[NP];
318    for (i=0; i<NP; i++) {
319       from >> e11_strain[i]; 
320       if (verbose)
321          std::cout <<  e11_strain[i] <<  std::endl;
322    }  
323    delete []e11_strain;
324
325    std::cout << "--------------- E22_strain ------------------" << std::endl;
326    float *e22_strain = new float[NP];
327    for (i=0; i<NP; i++) {
328       from >> e22_strain[i]; 
329       if (verbose)
330          std::cout <<  e22_strain[i] <<  std::endl;
331    }    
332    delete []e22_strain;
333
334    std::string dcmImageName;    
335    std::string serieUID;
336    
337 //followed by their peak Ecc strain, an array of NP elements,
338    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
339    dcmImageName = textFileName + "_peak_Ecc_strain.dcm";
340    MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
341
342 //followed by their peak Err strain, an array of NP elements,
343    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
344    dcmImageName = textFileName + "_peak_Err_strain.dcm";
345    MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
346
347 //followed by their peak E11 strain, an array of NP elements,
348    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
349    dcmImageName = textFileName + "_peak_E11_strain.dcm";
350    MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
351
352 //followed by their Peak E22 strain, an array of NP elements,
353    serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
354    dcmImageName = textFileName + "_peak_E22_strain.dcm";
355    MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);         
356 }
357
358 // =====================================================================================================================
359
360 void LoadStrain(std::ifstream &from, std::string textFileName, const char * patientname, bool createMultiFrame, std::string studyUID)
361 {
362
363 // in sax_base_slice0_strain.txt :
364 /*
365 Number of cine frames = 18
366 Temporal resolution = 32.0000 ms
367 First frame starts at 48.0000 ms
368 Number of material points (NP) = 181
369 Origin of (readout, phase enc, slice sel) coordinates in 3D =  87.324341 3.193918 88.238113 
370 Readout direction in 3D =  -0.162314 -0.0771294 -0.983720
371 Phase Enc. direction in 3D =  -0.540606 -0.827052 0.154046
372 Slice select direction in 3D =  0.825469 -0.556809 -0.0925458
373 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
374 followed by their Ecc strain, an array of dimensions(NP, number of cine frames),
375 followed by their Err strain, an array of dimensions(NP, number of cine frames),
376 followed by their E11 strain, an array of dimensions(NP, number of cine frames),
377 followed by their E22 strain, an array of dimensions(NP, number of cine frames),
378 Note that RV Err, E11 and E22 strains are not calculated due to the small thickness.
379       42.0000      10.0000     0.000000
380       44.0000      10.0000     0.000000
381       ...
382    -0.0622793   -0.0840482    -0.157350    -0.196722    -0.105844    -0.131331
383     -0.153781  -0.00940573   -0.0542236    -0.100403   -0.0369671   -0.0696840      
384 */ 
385
386    if (!from)
387       return;
388
389    std::string str1;
390    int NP;    // Number of Points
391    int NCF;   // Number of cine frames
392    float TR;  // Temporal resolution
393    float FFS; // First frame starts
394
395    // Number of cine frames = 18
396     from >> str1;
397     from >> str1;
398     from >> str1;
399     from >> str1;
400     from >> str1;
401     from >> NCF;
402
403    // Temporal resolution = 32.0000 ms
404     from >> str1;
405     from >> str1;
406     from >> str1;
407     from >> TR;
408     from >> str1;
409
410    // First frame starts at 48.0000 ms
411     from >> str1;
412     from >> str1;
413     from >> str1;
414     from >> str1;
415     from >> FFS;
416     from >> str1;        
417
418    //Number of material points (NP) = 181   
419     from >> str1;
420     from >> str1;
421     from >> str1;
422     from >> str1;
423     from >> str1;
424     from >> str1;
425     from >> NP;
426
427     std::cout << "NP : " << NP << std::endl; 
428
429    //Origin of (readout, phase enc, slice sel) coordinates in 3D = 87.324341 3.193918 88.238113 
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      from >> str1;
440      from >> str1;
441
442      float readout,  phase_enc, slice_sel;
443      from >> readout;
444      from >> phase_enc;
445      from >> slice_sel;
446      std::cout << " readout " << readout << " phase_enc " << phase_enc << " slice_sel " << slice_sel << std::endl;
447
448     // Readout direction in 3D =  -0.162314 -0.0771294 -0.983720
449
450     from >> str1;
451     from >> str1;
452     from >> str1;
453     from >> str1;
454     from >> str1;
455
456     float readoutX, readoutY, readoutZ;
457     from >> readoutX;
458     from >> readoutY;       
459     from >> readoutZ;
460     std::cout << " readoutX " << readoutX <<  " readoutY " << readoutY <<  " readoutZ " << readoutZ << std::endl;
461
462 // Phase Enc. direction in 3D =  -0.540606 -0.827052 0.154046
463
464      from >> str1;
465      from >> str1;
466      from >> str1;
467      from >> str1;
468      from >> str1;
469      from >> str1;
470
471     float phase_encX, phase_encY, phase_encZ;
472     from >> phase_encX;
473     from >> phase_encY;       
474     from >> phase_encZ;
475     std::cout << " phase_encX " << phase_encX <<  " phase_encY " << phase_encY <<  " phase_encZ " << phase_encZ << std::endl; 
476
477 // Slice select direction in 3D =  0.825469 -0.556809 -0.0925458
478      from >> str1;
479      from >> str1;
480      from >> str1;
481      from >> str1;
482      from >> str1;
483      from >> str1;
484      
485     float slice_selX, slice_selY, slice_selZ;
486     from >> slice_selX;
487     from >> slice_selY;       
488     from >> slice_selZ;
489     std::cout << " slice_selX " << slice_selX <<  " slice_selY " << slice_selY <<  " slice_selZ " << slice_selZ << std::endl; 
490
491
492
493 // Skip 6 lines :
494 /*
495 The following are the (readout, phase enc, slice sel) coordinates (mm) of the grid points for which strains are calculated,
496 followed by their Ecc strain, an array of dimensions(NP, number of cine frames),
497 followed by their Err strain, an array of dimensions(NP, number of cine frames),
498 followed by their E11 strain, an array of dimensions(NP, number of cine frames),
499 followed by their E22 strain, an array of dimensions(NP, number of cine frames),
500 Note that RV Err, E11 and E22 strains are not calculated due to the small thickness.
501 */
502 std::cout << "------------start skipping 1 line---------------- " << std::endl;
503    std::getline(from, str1);
504    std::cout << "[" << str1 << "]" << std::endl;
505 std::cout << "------------start skipping 1 line---------------- " << std::endl;
506    std::getline(from, str1);
507    std::cout << "[" << str1 << "]" << std::endl;
508 std::cout << "------------start skipping 1 line---------------- " << std::endl;
509    std::getline(from, str1);
510    std::cout << "[" << str1 << "]" << std::endl;
511 std::cout << "------------start skipping 1 line---------------- " << std::endl;
512    std::getline(from, str1);
513    std::cout << "[" << str1 << "]" << std::endl;
514 std::cout << "------------start skipping 1 line---------------- " << std::endl;
515    std::getline(from, str1);
516    std::cout << "[" << str1 << "]" << std::endl;
517 std::cout << "------------start skipping 1 line---------------- " << std::endl;
518    std::getline(from, str1);
519    std::cout << "[" << str1 << "]" << std::endl;
520 std::cout << "------------stop skipping ---------------- " << std::endl;
521    std::getline(from, str1);
522    std::cout << "[" << str1 << "]" << std::endl;
523 std::cout << "------------stop skipping ---------------- " << std::endl;
524   
525    float *X = new float[NP];
526    float *Y = new float[NP];   
527    float *Z = new float[NP];
528       
529    char c;
530    int i;   
531    for (i=0; i<NP; i++) {
532    
533       from >> X[i];
534       for (;;) {
535         if (!from.get(c))
536           break;
537         if (!isspace(c)) {
538           from.putback(c);
539           break;
540         }
541      }  
542       from >> Y[i];  
543       for (;;) {
544         if (!from.get(c))
545           break;
546         if (!isspace(c)) {
547           from.putback(c);
548           break;
549         }
550      }        
551       from >> Z[i];              
552    }
553
554 char frame[10];
555 std::string dcmImageName;    
556 std::string serieUID;
557
558 std::cout << "=======================================================================================" << createMultiFrame << std::endl;
559 if(!createMultiFrame) {     // One image per file here (single frame)
560
561 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
562 float *ecc_strain = new float[NP];
563 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
564 {
565 sprintf(frame, "_%d", nbr_of_frames);
566
567    std::cout << "--------------- Ecc_strain ------------------" << std::endl;
568    for (i=0; i<NP; i++) {
569        from >> ecc_strain[i];
570        if (verbose)
571        std::cout <<  ecc_strain[i] <<  std::endl;
572    }
573 //followed by their Ecc strain, an array of NP elements,
574    dcmImageName = textFileName + frame + "_Ecc_strain.dcm";
575    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
576    MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);      
577 }// end for   nbr_of_frames
578 delete []ecc_strain;
579
580 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
581 float *err_strain = new float[NP];
582 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
583 {
584 sprintf(frame, "_%d", nbr_of_frames);
585    std::cout << "--------------- Err_strain ------------------" << std::endl;
586    for (i=0; i<NP; i++) {
587        from >> err_strain[i]; 
588        if (verbose)
589        std::cout <<  err_strain[i] <<  std::endl;
590    }
591 //followed by their  Err strain, an array of NP elements,
592    dcmImageName = textFileName + frame + "_Err_strain.dcm";
593    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
594    MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
595 }// end for   nbr_of_frames
596 delete []err_strain;
597  
598 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
599 float *e11_strain = new float[NP];
600 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
601 {
602 sprintf(frame, "_%d", nbr_of_frames);   
603    std::cout << "--------------- E11_strain ------------------" << std::endl;
604    for (i=0; i<NP; i++) {
605        from >> e11_strain[i]; 
606        if (verbose)
607        std::cout <<  e11_strain[i] <<  std::endl;
608    }  
609 //followed by their E11 strain, an array of NP elements,
610    dcmImageName = textFileName + frame + "_E11_strain.dcm";
611    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
612    MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);
613 }// end for   nbr_of_frames   
614 delete []e11_strain;
615
616 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
617 float *e22_strain = new float[NP];
618 for (int nbr_of_frames=0; nbr_of_frames < NCF;  nbr_of_frames++)
619 {
620 sprintf(frame, "_%d", nbr_of_frames); 
621    std::cout << "--------------- E22_strain ------------------" << std::endl;
622    for (i=0; i<NP; i++) {
623        from >> e22_strain[i]; 
624        if (verbose)
625        std::cout <<  e22_strain[i] <<  std::endl;
626    }   
627 //followed by their E22 strain, an array of NP elements,
628    dcmImageName = textFileName + frame + "_E22_strain.dcm";
629    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
630    MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName, patientname, 1, studyUID, serieUID);   
631 } // end for   nbr_of_frames
632 delete [] e22_strain;
633  
634 } // end of single frame
635
636
637 else                      // generate Multiframe files
638 {
639
640
641 serieUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
642 float *ecc_strain = new float[NP*NCF];
643    std::cout << "--------------- Ecc_strain ------------------" << std::endl;
644    for (i=0; i<NP*NCF; i++) {
645        from >> ecc_strain[i];
646        if (verbose)
647        std::cout <<  ecc_strain[i] <<  std::endl;
648    }
649 //followed by their Ecc strain, an array of NP elements,
650    dcmImageName = textFileName + "_Ecc_strain.dcm";
651    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
652    MakeDicomImage(ecc_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
653 delete []ecc_strain;
654
655
656
657 float *err_strain = new float[NP*NCF];
658    std::cout << "--------------- Err_strain ------------------" << std::endl;
659    for (i=0; i<NP*NCF; i++) {
660        from >> err_strain[i];
661        if (verbose)
662        std::cout <<  err_strain[i] <<  std::endl;
663    }
664 //followed by their Ecc strain, an array of NP elements,
665    dcmImageName = textFileName + "_Err_strain.dcm";
666    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
667    MakeDicomImage(err_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
668 delete []err_strain;
669
670
671
672 float *e11_strain = new float[NP*NCF];
673    std::cout << "--------------- E11_strain ------------------" << std::endl;
674    for (i=0; i<NP*NCF; i++) {
675        from >> e11_strain[i];
676        if (verbose)
677        std::cout <<  e11_strain[i] <<  std::endl;
678    }
679 //followed by their Ecc strain, an array of NP elements,
680    dcmImageName = textFileName + "_E11_strain.dcm";
681    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
682    MakeDicomImage(e11_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
683 delete []e11_strain;
684
685
686
687 float *e22_strain = new float[NP*NCF];
688    std::cout << "--------------- E22_strain ------------------" << std::endl;
689    for (i=0; i<NP*NCF; i++) {
690        from >> e22_strain[i];
691        if (verbose)
692        std::cout <<  e22_strain[i] <<  std::endl;
693    }
694 //followed by their Ecc strain, an array of NP elements,
695    dcmImageName = textFileName + "_E22_strain.dcm";
696    std::cout << "Try to make image :[" << dcmImageName << "]" << std::endl;
697    MakeDicomImage(e22_strain, X, Y, Z, NP, dcmImageName, patientname, NCF, studyUID, serieUID);      
698 delete []e22_strain;
699
700 }    // end of Multiframe    
701 }
702
703
704 // =====================================================================================================================
705     
706
707 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)
708 {
709
710 std::cout << "=============================================================================="
711           << "enter MakeDicomImage [" << dcmImageName << "] [" << patientName << "]" << std::endl;
712    float minX = 99999., minY = 99999., minZ = 99999.;
713    float maxX = 0., maxY = 0., maxZ = 0.;
714    int i;
715    
716    for (i=0; i<NP; i++) {
717       // std::cout << X[i] << " " << Y[i] << " " << Z[i] <<  std::endl;
718       if(maxX < X[i])
719          maxX = X[i];
720       if(maxY < Y[i])
721          maxY = Y[i];
722       if(maxZ < Z[i])
723          maxZ = Z[i];
724  
725       if(minX > X[i])
726          minX = X[i];
727       if(minY > Y[i])
728          minY = Y[i];
729       if(minZ > Z[i])
730          minZ = Z[i];
731    }   
732    std::cout << "Min X,Y,Z " << minX << " " << minY << " " << minZ <<  std::endl;
733    std::cout << "Max X,Y,Z " << maxX << " " << maxY << " " << maxZ <<  std::endl;
734    std::cout << "Size X,Y,Z " << maxX-minX << " " << maxY-minY << " " << maxZ-minZ <<  std::endl;      
735
736    int lgrFrame = int(maxX*4.)*int(maxY*4.);
737    uint16_t *img = new uint16_t[lgrFrame*nbFrames ];
738
739    // Set whole image to 0 
740    for(int i3=0; i3<lgrFrame*nbFrames; i3++)
741       img[i3] = 0;
742        
743 for(int i4=0; i4<nbFrames; i4++)
744    for(int i2=0; i2<NP; i2++) {   
745       int ordX = int(X[i2]*4.-30);
746       int ordY = int(maxY*4.) - int(Y[i2]*4.)+30;      
747       img[ lgrFrame*i4 + ordX   +  ordY   * int(maxX*4.) ] = int(tabVal[i2 + NP*i4]*100);
748
749       // Try to round up, just to see.   
750        for(int iii=ordY-3; iii<ordY+4; iii++) 
751           for(int jjj=ordX-3; jjj<ordX+4; jjj++) 
752              img[  lgrFrame*i4 + jjj  +  iii   * int(maxX*4.) ] = int(tabVal[i2 + NP*i4]*100);            
753    }
754
755  std::cout << "===========sortie recup points" << std::endl; 
756  // GDCM_NAME_SPACE::Debug::DebugOn();
757   
758    std::ostringstream str;
759
760    GDCM_NAME_SPACE::File *file;
761    file = GDCM_NAME_SPACE::File::New();       
762       
763   // Set the image size
764    str.str(""); 
765    str << (int)(maxX*4.);
766    file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
767    str.str("");
768    str << (int)(maxY*4.);
769    file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
770
771   // Set the pixel type
772   //      16; //8, 16, 32
773    file->InsertEntryString("16",0x0028,0x0100,"US"); // Bits Allocated
774    str.str("");
775    str << 16; // may be 12 or 16 if componentSize =16
776    file->InsertEntryString("16",0x0028,0x0101,"US"); // Bits Stored
777    file->InsertEntryString("15",0x0028,0x0102,"US"); // High Bit
778
779   // Set the pixel representation // 0/1 , 0=unsigned
780    file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
781   // Set the samples per pixel // 1:Grey level, 3:RGB
782    file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
783
784    if (nbFrames != 1)
785    {
786       str.str("");
787       str << nbFrames;
788       file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames  
789    }
790   
791    if (strlen(patientName) != 0)
792       file->InsertEntryString(patientName,0x0010,0x0010, "PN"); // Patient's Name
793
794    file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
795    file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
796      
797    int pos = 0;  // get the usefull part of the name
798 /*  
799    for(i=0, pos=0; pos<dcmImageName.size()-4; pos++, i++) {
800      if( dcmImageName[i]=='.' &&dcmImageName[i+1]=='t' && dcmImageName[i+2]=='x' && dcmImageName[i+3]=='t'  
801        && dcmImageName[i+3]=='_') {
802        pos+=5;
803        break;
804      }
805    }
806 */  
807    file->InsertEntryString(&(dcmImageName.c_str()[pos]),0x0008,0x103e, "LO");  // Series Description   
808 /*
809   // Set Rescale Intercept
810         str.str("");
811         str << div;  
812         file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
813
814   // Set Rescale Slope
815         str.str("");
816         str << mini;  
817         file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
818 */
819     
820    GDCM_NAME_SPACE::FileHelper *fileH;
821    fileH = GDCM_NAME_SPACE::FileHelper::New(file);
822    // cast is just to avoid warnings (*no* conversion)
823    //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
824    
825    fileH->SetImageData((uint8_t *)img,int(maxX*4.)*int(maxY*4.)*nbFrames*sizeof(uint16_t));
826    fileH->SetWriteModeToRaw(); 
827    fileH->SetWriteTypeToDcmExplVR();
828         
829    if( !fileH->Write(dcmImageName))
830       std::cout << "Failed for [" << dcmImageName << "]\n"
831                 << "           File is unwrittable" << std::endl;
832
833    //file->Print();
834            
835    delete img;
836    file->Delete();
837    fileH->Delete();  
838 }