]> Creatis software - gdcm.git/blob - Example/ToInTag.cxx
Avoid messing written Dicom images when no Study UID is found
[gdcm.git] / Example / ToInTag.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ToInTag.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/06/08 13:41:28 $
7   Version:   $Revision: 1.5 $
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 #include "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmFile.h"
22 #include "gdcmFileHelper.h"
23 #include "gdcmDirList.h"
24 #include "gdcmDebug.h"
25 #include "gdcmArgMgr.h"
26 #include "gdcmUtil.h"
27 #include "gdcmSerieHelper.h"
28
29 #include <iostream>
30
31 /**
32   * \brief   
33   *          - explores recursively the given directory
34   *          - keeps the requested series
35   *          - orders the gdcm-readable found Files
36   *            according to their Patient/Study/Serie/Image characteristics
37   *          - fills a single level Directory with *all* the files,
38   *            converted into a Brucker-like Dicom, Intags compliant
39   */  
40
41 typedef std::map<std::string, gdcm::File*> SortedFiles;
42
43 int main(int argc, char *argv[]) 
44 {
45    START_USAGE(usage)
46    " \n ToInTag :\n                                                           ",
47    " - explores recursively the given directory,                              ",
48    " - keeps the requested series/ drops the unrequested series               ",
49    " - orders the gdcm-readable found Files according to their                ",
50    "           (0x0010, 0x0010) Patient's Name                                ",
51    "           (0x0020, 0x000e) Series Instance UID                           ",
52    "           (0x0020, 0x0032) Image Position (Patient)                      ",
53    "           (0x0018, 0x1060) Trigger Time                                  ",
54    "           (0x0018, 0x1312) In-plane Phase Encoding Direction             ",
55    " - fills a single level (*) Directory with *all* the files,               ",
56    "           converted into a Brucker-like Dicom, InTags compliant          ",
57    "   (*) actually : creates as many directories as Patients                 ",
58    "                  -that shouldn't appear, but being carefull is better ! -",
59    " or                                                                       ",
60    " - fills a tree-like structure of directories as :                        ",
61    "        - Patient                                                         ",
62    "        -- Serie                                                          ",
63    "        --- Position                                                      ",
64    "            Images are (sorted by Trigger Time /                          ",
65    "                     Encoding Direction (Row, Column)                     ",
66    "      use :                                                               ",
67    "           0x0021, 0x1020 : 'SLICE INDEX'                                 ",
68    "           0x0021, 0x1040 : 'FRAME INDEX'                                 ",
69    "           0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)         ",
70    " usage:                                                                   ",
71    " PhilipsToBrucker dirin=rootDirectoryName                                 ",
72    "                  dirout=outputDirectoryName                              ",
73    "                  {  [keep= list of seriesNumber to process]              ",
74    "                   | [drop= list of seriesNumber to ignore] }             ",
75    "                  [taggrid] [skel]                                        ",
76    "                  [input = {ACR|DCM}]                                     ", 
77    "                  [extent=image suffix (.IMA, .NEMA, .DCM, ...)]          ",
78    "                  [listonly] [split]                                      ",
79    "                  [noshadowseq][noshadow][noseq] [verbose] [debug]        ",
80    "                                                                          ",
81    " dirout : will be created if doesn't exist                                ",
82    " keep : if user wants to process a limited number of series               ",
83    "            he gives the list of 'SeriesNumber' (tag 0020|0011)           ",
84    " drop : if user wants to ignore a limited number of series                ",
85    "            he gives the list of 'SeriesNumber' (tag 0020|0011)           ",   
86    "        SeriesNumber are short enough to be human readable                ",
87    "        e.g : 1030,1035,1043                                              ", 
88    " taggrid : user knows all the images are 'grid' -ie : not 'col', not 'raw'",
89    " extent : DO NOT forget the leading '.' !                                 ",
90    " skel: name skeleton eg : patName_1.nema -> skel=patName_                 ",
91    " split: creates a tree-like structure of directories as :                 ",
92    "        - Patient                                                         ",
93    "        -- Serie                                                          ",
94    "        --- Position                                                      ",
95    "            Images are (sorted by Trigger Time /                          ",
96    "                     Encoding Direction (Row, Column)                     ",
97    " noshadowseq: user doesn't want to load Private Sequences                 ",
98    " noshadow : user doesn't want to load Private groups (odd number)         ",
99    " noseq    : user doesn't want to load Sequences                           ",
100    " verbose  : user wants to run the program in 'verbose mode'               ",
101    " debug    : *developer*  wants to run the program in 'debug mode'         ",
102    FINISH_USAGE
103
104    std::cout << "inside ToInTag" << std::endl;
105    
106    // ----- Initialize Arguments Manager ------
107       
108    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
109   
110    if (argc == 1 || am->ArgMgrDefined("usage")) 
111    {
112       am->ArgMgrUsage(usage); // Display 'usage'
113       delete am;
114       return 0;
115    }
116
117    const char *dirNamein;   
118    dirNamein  = am->ArgMgrGetString("dirin","."); 
119
120    const char *dirNameout;   
121    dirNameout  = am->ArgMgrGetString("dirout",".");  
122    
123    int loadMode = gdcm::LD_ALL;
124    if ( am->ArgMgrDefined("noshadowseq") )
125       loadMode |= gdcm::LD_NOSHADOWSEQ;
126    else 
127    {
128    if ( am->ArgMgrDefined("noshadow") )
129          loadMode |= gdcm::LD_NOSHADOW;
130       if ( am->ArgMgrDefined("noseq") )
131          loadMode |= gdcm::LD_NOSEQ;
132    }
133
134    if (am->ArgMgrDefined("debug"))
135       gdcm::Debug::DebugOn();
136       
137    int verbose  = am->ArgMgrDefined("verbose");
138    int split    = am->ArgMgrDefined("split");
139    int listonly = am->ArgMgrDefined("listonly");
140          
141    int nbSeriesToKeep;
142    int *seriesToKeep = am->ArgMgrGetListOfInt("keep", &nbSeriesToKeep);
143    int nbSeriesToDrop;
144    int *seriesToDrop = am->ArgMgrGetListOfInt("drop", &nbSeriesToDrop);
145  
146    if ( nbSeriesToKeep!=0 && nbSeriesToDrop!=0)
147    {
148       std::cout << "KEEP and DROP are mutually exclusive !" << std::endl;
149       delete am;
150       return 0;         
151    }
152    
153    int taggrid = am->ArgMgrDefined("taggrid");
154    
155    int hasSkel = am->ArgMgrDefined("skel");
156    const char *skel;
157    if (hasSkel)
158       skel = am->ArgMgrGetString("skel");   
159       
160    const char *extent  = am->ArgMgrGetString("extent",".DCM");
161    
162    // if unused Param we give up
163    if ( am->ArgMgrPrintUnusedLabels() )
164    { 
165       am->ArgMgrUsage(usage);
166       delete am;
167       return 0;
168    }
169    delete am;  // we don't need Argument Manager any longer
170
171    // ----- Begin Processing -----
172    
173    if ( ! gdcm::DirList::IsDirectory(dirNamein) )
174    {
175       std::cout << "KO : [" << dirNamein << "] is not a Directory." << std::endl;
176       return 0;
177
178    }
179    else
180    {
181       std::cout << "OK : [" << dirNamein << "] is a Directory." << std::endl;
182    }
183
184    std::string systemCommand;
185    
186    std::cout << "Check for output directory :[" << dirNameout << "]."
187              <<std::endl;
188    if ( ! gdcm::DirList::IsDirectory(dirNameout) )    // dirout not found
189    {
190       std::string strDirNameout(dirNameout);          // to please gcc 4
191       systemCommand = "mkdir " +strDirNameout;        // create it!
192       if (verbose)
193          std::cout << systemCommand << std::endl;
194       system (systemCommand.c_str());
195       if ( ! gdcm::DirList::IsDirectory(dirNameout) ) // be sure it worked
196       {
197           std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)" << std::endl;
198       return 0;
199
200       }
201       else
202       {
203         std::cout << "Directory [" << dirNameout << "] created." << std::endl;
204       }
205    }
206    else
207    {
208        std::cout << "Output Directory [" << dirNameout << "] already exists; Used as is." << std::endl;
209    }
210     
211    std::string strDirNamein(dirNamein);
212    gdcm::DirList dirList(strDirNamein, true); // get recursively the list of files
213    
214    if (listonly)
215    {
216       std::cout << "------------List of found files ------------" << std::endl;
217       dirList.Print();
218    }
219    
220    gdcm::DirListType fileNames;
221    fileNames = dirList.GetFilenames();
222    gdcm::SerieHelper *s;              // Needed only to may use SerieHelper::AddSeriesDetail()
223    s = gdcm::SerieHelper::New();
224
225    std::string token = "%%%"; // Hope it's enough!
226 /*       
227    std::cout << "---------------Print Serie--------------" << std::endl; 
228    s->SetDirectory(dirNamein, true); // true : recursive exploration 
229    s->SetUseSeriesDetails(true);  
230    s->AddSeriesDetail(0x0018, 0x1312);   
231    s->Print();
232 */
233   
234    gdcm::File *f;
235    gdcm::FileHelper *fh;
236    std::vector<std::string> tokens;
237    std::vector<std::string> tokensForFileName;
238 /*   
239    std::cout << "---------------Print Unique Series identifiers---------"  
240              << std::endl;     
241    std::string uniqueSeriesIdentifier;
242  
243    for (gdcm::DirListType::iterator it) = fileNames.begin();  
244                                     it != fileNames.end();
245                                   ++it)
246    {
247       std::cout << "File Name : " << *it << std::endl;
248       f = gdcm::File::New();
249       f->SetLoadMode(gdcm::LD_ALL);
250       f->SetFileName( *it );
251       f->Load();
252         
253       uniqueSeriesIdentifier=s->CreateUniqueSeriesIdentifier(f);
254       std::cout << "                           [" <<
255                uniqueSeriesIdentifier  << "]" << std::endl;       
256       f->Delete();
257    }
258 */
259    
260    if (verbose)
261       std::cout << "------------------Print Break levels-----------------" << std::endl;
262
263    std::string userFileIdentifier; 
264    SortedFiles sf;
265
266    s->AddSeriesDetail(0x0010, 0x0010, false); // Patient's Name
267    s->AddSeriesDetail(0x0020, 0x000e, false); // Series Instance UID
268    s->AddSeriesDetail(0x0020, 0x0032, false); // Image Position (Patient)     
269    s->AddSeriesDetail(0x0018, 0x1060, true);  // Trigger Time (true: convert to keep numerical order)
270    s->AddSeriesDetail(0x0018, 0x1312, false); // In-plane Phase Encoding Direction 
271
272    //uint8_t *imageData; // Useless : pixels will not be loaded 
273                          //          (images are overwritten)
274          
275    for (gdcm::DirListType::iterator it = fileNames.begin();  
276                                     it != fileNames.end();
277                                   ++it)
278    {
279       f = gdcm::File::New();
280       f->SetLoadMode(loadMode);
281       f->SetFileName( *it );
282       f->Load();
283       
284
285       std::string strSeriesNumber;
286       int seriesNumber;
287       int j;
288
289       // keep only requested Series      
290       bool keep = false;
291       if (nbSeriesToKeep != 0)
292       {     
293          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
294          seriesNumber = atoi( strSeriesNumber.c_str() );
295          for (j=0;j<nbSeriesToKeep; j++)
296          {
297             if(seriesNumber == seriesToKeep[j])
298             {
299                keep = true;
300                break;
301             }
302          }
303          if ( !keep)
304          {
305             f->Delete();
306             continue;
307          } 
308       }
309       // drop all unrequested Series
310       bool drop = false;
311       if (nbSeriesToDrop != 0)
312       {     
313          strSeriesNumber = f->GetEntryString(0x0020, 0x0011 );
314          seriesNumber = atoi( strSeriesNumber.c_str() );
315          for (j=0;j<nbSeriesToDrop; j++)
316          {
317             if(seriesNumber == seriesToDrop[j])
318             { 
319                drop = true;
320                break;
321             }
322         }
323         if (drop)
324         {
325            f->Delete();
326            continue;
327         }
328       }      
329
330       userFileIdentifier=s->CreateUserDefinedFileIdentifier(f); 
331       tokens.clear();
332       gdcm::Util::Tokenize (userFileIdentifier, tokens, token); 
333    
334       int imageNum; // Within FileName
335       char newName[1024];
336       
337       if ( tokens[3] == "gdcmUnfound")  // sometimes Trigger Time is not found. CreateUserDefinedFileIdentifier is not aware of the pb.
338       {
339          ///this is a trick to build up a lexicographical compliant name :
340          ///     eg : fich001.ima vs fich100.ima as opposed to fich1.ima vs fich100.ima
341          std::string name = gdcm::Util::GetName( *it );
342          if (hasSkel)
343          {
344             gdcm::Util::Tokenize (name, tokensForFileName, skel);
345             imageNum = atoi ( tokensForFileName[0].c_str() );
346             // probabely we could write something much more complicated using C++ !
347             sprintf (newName, "%s%06d%s", skel, imageNum, extent);
348             tokens[3] = newName;
349             tokensForFileName.clear();    
350          }
351          else
352             tokens[3] = name;
353  
354  
355          userFileIdentifier = tokens[0] + token + tokens[1] + token + tokens[2] + token 
356                     + tokens[3] + token + tokens[4] + token;
357       }   
358       if (verbose) 
359          std::cout << "[" << userFileIdentifier  << "]" << std::endl;
360                
361       // storing in a map ensures automatic sorting !      
362       sf[userFileIdentifier] = f;
363    }
364       
365    std::string fullFilename, lastFilename;
366    std::string previousPatientName, currentPatientName;
367    std::string previousSerieInstanceUID, currentSerieInstanceUID;
368    std::string previousImagePosition, currentImagePosition;
369    std::string previousPhaseEncodingDirection, currentPhaseEncodingDirection;
370    std::string previousTriggerTime, currentTriggerTime;
371    
372    std::string currentStudyUID;   
373       
374    std::string writeDir, currentWriteDir;
375    std::string currentPatientWriteDir, currentSerieWriteDir, 
376                currentPositionWriteDir, currentPhaseEncodingDirectionWriteDir;
377
378    std::string fullWriteFilename;
379    std::string strExtent(extent); 
380            
381    writeDir = gdcm::Util::NormalizePath(dirNameout);     
382    SortedFiles::iterator it2;
383  
384    previousPatientName            = "";
385    previousSerieInstanceUID       = "";   
386    previousImagePosition          = "";
387    previousPhaseEncodingDirection = "";
388    previousTriggerTime            = "";
389    
390    int sliceIndex = 0; // Is incremented *at the beginning* of processing
391    int frameIndex = 1;
392    int flag       = 0;
393        
394    gdcm::File *currentFile;
395
396    std::string defaultStudyUID =  gdcm::Util::CreateUniqueUID();
397    std::string defaultSerieUID; 
398      
399    for (it2 = sf.begin() ; it2 != sf.end(); ++it2)
400    {  
401       currentFile = it2->second;
402        
403       fullFilename =  currentFile->GetFileName();
404       lastFilename =  gdcm::Util::GetName( fullFilename ); 
405       std::cout << "Rewrite [" <<lastFilename << "]" << std::endl;
406      
407       tokens.clear();
408       gdcm::Util::Tokenize (it2->first, tokens, token);
409       
410       currentPatientName            = tokens[0];
411       currentSerieInstanceUID       = tokens[1];
412       currentImagePosition          = tokens[2];
413       currentTriggerTime            = tokens[3];
414       currentPhaseEncodingDirection = tokens[4]; 
415
416       if ( currentImagePosition[0] == '-')
417           currentImagePosition[0] = 'M';
418       if ( currentImagePosition[0] == '+')
419           currentImagePosition[0] = 'P'; 
420       
421       if (previousPatientName != currentPatientName)
422       {      
423          if ( currentFile->GetEntryString(0x0020,0x000d) == gdcm::GDCM_UNFOUND)
424          {
425             if (verbose)   
426                std::cout << "--- new  Study UID created" << std::endl;
427             defaultStudyUID =  gdcm::Util::CreateUniqueUID();
428             currentFile->InsertEntryString(defaultStudyUID, 0x0020, 0x000d, "UI" );
429          }
430   
431          previousPatientName = currentPatientName;
432          if (verbose)   
433             std::cout << "==== new Patient  [" << currentPatientName  << "]" << std::endl;
434     
435          previousPatientName            = currentPatientName;
436          previousSerieInstanceUID       = ""; //currentSerieInstanceUID;
437          previousImagePosition          = ""; //currentImagePosition;
438          previousTriggerTime            = "";
439          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
440   
441          currentPatientWriteDir = writeDir + currentPatientName;
442          //if ( ! gdcm::DirList::IsDirectory(currentPatientWriteDir) )
443            {
444               systemCommand   = "mkdir " + currentPatientWriteDir;
445               if (verbose)
446                  std::cout << systemCommand << std::endl;
447               system ( systemCommand.c_str() );
448          }
449       }
450
451       if (previousSerieInstanceUID != currentSerieInstanceUID)
452       {        
453          if (verbose)   
454             std::cout << "==== === new Serie [" << currentSerieInstanceUID << "]"
455                       << std::endl;
456       
457          if ( currentFile->GetEntryString(0x0020,0x000e) == gdcm::GDCM_UNFOUND)
458          {
459             if (verbose)   
460                std::cout << "--- --- new  Serie UID created" << std::endl;
461             defaultSerieUID =  gdcm::Util::CreateUniqueUID();
462             currentFile->InsertEntryString(defaultSerieUID, 0x0020, 0x000e, "UI" );
463          }       
464       
465          if (split)
466          {
467              currentSerieWriteDir  = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR
468                              + currentSerieInstanceUID;
469              systemCommand   = "mkdir " + currentSerieWriteDir;  
470              system (systemCommand.c_str());
471          }
472          previousSerieInstanceUID       = currentSerieInstanceUID;
473          previousImagePosition          = ""; //currentImagePosition;
474          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
475       }
476
477       if (previousImagePosition != currentImagePosition)
478       {
479          frameIndex = 1;
480          flag = 0;        
481          if (verbose)   
482             std::cout << "=== === === new Position [" << currentImagePosition  << "]"
483                       << std::endl;
484          if (split)
485          {
486              currentPositionWriteDir  = currentSerieWriteDir + gdcm::GDCM_FILESEPARATOR
487                              + currentImagePosition;
488              systemCommand   = "mkdir " + currentPositionWriteDir;     
489              system (systemCommand.c_str()); 
490          }
491          previousImagePosition          = currentImagePosition;
492          previousPhaseEncodingDirection = ""; //currentPhaseEncodingDirection;
493          if (split)
494             sliceIndex = 1; // only *one* slice in a given directory
495          else
496             sliceIndex += 1;
497       }      
498
499 // We don't split on Row/Column!
500 /*
501       if (previousPhaseEncodingDirection != currentPhaseEncodingDirection)
502       {        
503          if (verbose)   
504             std::cout << "==== === === === new PhaseEncodingDirection [" 
505                       << currentPhaseEncodingDirection  << "]" << std::endl;
506       
507          if (split)
508          {
509              currentPhaseEncodingDirectionWriteDir  = currentPositionWriteDir 
510                              + gdcm::GDCM_FILESEPARATOR
511                              + currentPhaseEncodingDirection;
512              systemCommand   = "mkdir " + currentPhaseEncodingDirectionWriteDir;     
513              system (systemCommand.c_str());     
514          }      
515     
516          previousPhaseEncodingDirection = currentPhaseEncodingDirection;
517       } 
518 */    
519       
520       if (verbose)
521          std::cout << "--- --- --- --- --- " << (it2->second)->GetFileName() 
522                    << std::endl;
523    
524       if ( gdcm::Debug::GetDebugFlag())
525          std::cout << "--- --- --- --- --- " << it2->first << "  " 
526                    << (it2->second)->GetFileName() << " " 
527                    << gdcm::Util::GetName( fullFilename ) << std::endl;           
528       
529       // Transform the image to be 'Brucker-Like'
530       // ----------------------------------------   
531     
532       // Deal with 0x0019, 0x1000 : 'FOV'
533       int nX = currentFile->GetXSize();
534       int nY = currentFile->GetYSize();
535       float pxSzX = currentFile->GetXSpacing();
536       float pxSzY = currentFile->GetYSpacing();
537       char fov[64];
538       sprintf(fov, "%f\\%f",nX*pxSzX, nY*pxSzY);
539       if (currentFile->IsVRCoherent(0x0019) == 1 )
540          currentFile->InsertEntryString(fov, 0x0019, 0x1000, "  ");
541       else     
542          currentFile->InsertEntryString(fov, 0x0019, 0x1000, "DS");
543
544      
545       // Deal with 0x0020, 0x0012 : 'SESSION INDEX'  (Acquisition Number)
546       std::string chSessionIndex;
547       // CLEANME
548       if (taggrid) 
549          chSessionIndex = "1";
550       else
551       {
552          if (currentPhaseEncodingDirection == "COL" || currentPhaseEncodingDirection == "COL " || currentPhaseEncodingDirection == " COL")
553             chSessionIndex = "1";
554          else if (currentPhaseEncodingDirection == "ROW" || currentPhaseEncodingDirection == "ROW "|| currentPhaseEncodingDirection == " ROW")
555             chSessionIndex = "2"; 
556          else
557          {
558             std::cout << "====================== PhaseEncodingDirection "
559                       << " neither COL nor ROW (?!?) : [ "
560                       << currentPhaseEncodingDirection << "]" << std::endl;
561             chSessionIndex = "1";
562          }
563       }
564        if (currentFile->IsVRCoherent(0x0020) == 1 )     
565          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "  ");
566        else
567          currentFile->InsertEntryString(chSessionIndex, 0x0020, 0x0012, "IS");
568  
569       // Deal with  0x0021, 0x1020 : 'SLICE INDEX'
570       char chSliceIndex[5];
571       sprintf(chSliceIndex, "%04d", sliceIndex);
572       std::string strChSliceIndex(chSliceIndex);
573        
574       // Deal with  0x0021, 0x1040 : 'FRAME INDEX' 
575       char chFrameIndex[5];
576       sprintf(chFrameIndex, "%04d", frameIndex);
577
578       std::string stringVR;       
579       if (currentFile->IsVRCoherent(0x0021) == 1 )
580          stringVR = "  ";
581       else
582         stringVR = "IS";
583   
584       currentFile->InsertEntryString(strChSliceIndex, 0x0021, 0x1020, stringVR);
585       currentFile->InsertEntryString(chFrameIndex,    0x0021, 0x1040, stringVR); 
586  
587  
588       std::string strImagePositionPatient    = currentFile->GetEntryString(0x0020, 0x0032 );
589       if (strImagePositionPatient == gdcm::GDCM_UNFOUND)
590          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0030), 0x0020, 0x0032, "DS" );  
591       
592       std::string strImageOrientationPatient = f->GetEntryString(0x0020, 0x0037 );
593       if (strImageOrientationPatient == gdcm::GDCM_UNFOUND)
594          currentFile->InsertEntryString(currentFile->GetEntryString(0x0020, 0x0035), 0x0020, 0x0037, "DS" );       
595                 
596       if (taggrid)
597          frameIndex++;
598       else     
599       {     
600          if (flag == 0)
601          {       
602             flag = 1;
603          }
604          else
605          {
606             frameIndex++;
607             flag = 0;
608          }
609       } 
610                  
611       if (split)
612       
613          //fullWriteFilename = currentPhaseEncodingDirectionWriteDir + gdcm::GDCM_FILESEPARATOR 
614          //                                + lastFilename + strExtent;      
615          fullWriteFilename = currentPositionWriteDir + gdcm::GDCM_FILESEPARATOR 
616                                          + lastFilename + strExtent; 
617       else
618          fullWriteFilename = currentPatientWriteDir + gdcm::GDCM_FILESEPARATOR 
619                                          + lastFilename + strExtent; 
620       
621       /*           
622       systemCommand  = "cp " + fullFilename + " " + fullWriteFilename;
623       std::cout << systemCommand << std::endl;
624       system (  systemCommand.c_str() );
625       */
626             
627       // Load the pixels in RAM.    
628       
629       fh = gdcm::FileHelper::New(currentFile);     
630       uint8_t *imageData = fh->GetImageDataRaw(); // Don't convert (Gray Pixels + LUT) into (RGB pixels) ?!?
631       fh->SetWriteTypeToDcmExplVR();
632       // We didn't modify pixels -> keep unchanged the following :
633       // 'Media Storage SOP Class UID' (0x0002,0x0002)
634       // 'SOP Class UID'               (0x0008,0x0016)
635       // 'Image Type'                  (0x0008,0x0008)
636       // 'Conversion Type'             (0x0008,0x0064)
637       
638       // Put to Black the burnt-in number.
639       nX = currentFile->GetXSize();
640       nY = currentFile->GetYSize();
641       for(int y=nY-15; y<nY; y++)
642          for(int x=nX/3; x<nX/2+50; x++)
643            imageData[ y*nX*2 + x ] = 0;   
644         
645       fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
646       if (!fh->Write(fullWriteFilename))
647       {
648          std::cout << "Fail to write :[" << fullWriteFilename << "]"
649                    << std::endl;
650       } 
651       fh->Delete();                
652    }
653    return 0;
654  }