]> Creatis software - gdcm.git/blob - Example/TestCopyDicom.cxx
First stage of name normalisation : gdcm::File replace by gdcm::FileHelper
[gdcm.git] / Example / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/20 16:16:58 $
7   Version:   $Revision: 1.18 $
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 "gdcmHeader.h"
19 #include "gdcmFile.h"
20 #include "gdcmDocument.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23
24 #ifndef _WIN32
25 #include <unistd.h> //for access, unlink
26 #else
27 #include <io.h> //for _access
28 #endif
29
30 // return true if the file exists
31 bool FileExists(const char* filename)
32 {
33 #ifdef _MSC_VER
34 # define access _access
35 #endif
36 #ifndef R_OK
37 # define R_OK 04
38 #endif
39   if ( access(filename, R_OK) != 0 )
40     {
41     return false;
42     }
43   else
44     {
45     return true;
46     }
47 }
48
49 bool RemoveFile(const char* source)
50 {
51 #ifdef _MSC_VER
52 #define _unlink unlink
53 #endif
54   return unlink(source) != 0 ? false : true;
55 }
56
57 // Here we load a gdcmFile and then try to create from scratch a copy of it,
58 // copying field by field the dicom image
59
60 int main(int argc, char* argv[])
61 {
62    if (argc < 3)
63    {
64       std::cerr << "Usage :" << std::endl << 
65       argv[0] << " input_dicom output_dicom" << std::endl;
66       return 1;
67    }
68
69 // don't modify identation in order to let this source xdiffable with ../Test
70
71       std::string filename = argv[1];
72       std::string output = argv[2];
73
74       if( FileExists( output.c_str() ) )
75       {
76          std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
77          if( !RemoveFile( output.c_str() ) )
78          {
79             std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
80             return 1;
81          }
82       }
83       gdcm::FileHelper *original = new gdcm::FileHelper( filename );
84    
85       std::cout << "--- Original ----------------------" << std::endl;
86       //original->GetHeader()->Print();
87    
88       gdcm::FileHelper *copy = new gdcm::FileHelper( output );
89
90       size_t dataSize = original->GetImageDataSize();
91       uint8_t* imageData = original->GetImageData();
92       (void)imageData;
93       (void)dataSize;
94   
95       //First of all copy the header field by field
96   
97       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
98       // It was commented out by Mathieu, that was a *good* idea
99       // (the user does NOT have to know the way we implemented the Header !)
100       // Waiting for a 'clean' solution, I keep the method ...JPRx
101
102       gdcm::DocEntry* d=original->GetHeader()->GetFirstEntry();
103       while(d)
104       {
105          if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
106          {              
107             copy->GetHeader()->ReplaceOrCreate( 
108                                  b->GetBinArea(),
109                                  b->GetLength(),
110                                  b->GetGroup(), 
111                                  b->GetElement(),
112                                  b->GetVR() );
113          }
114          else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
115          {   
116             copy->GetHeader()->ReplaceOrCreate( 
117                                  v->GetValue(),
118                                  v->GetGroup(), 
119                                  v->GetElement(),
120                                  v->GetVR() ); 
121          }
122          else
123          {
124           // We skip pb of SQ recursive exploration
125           //std::cout << "Skipped Sequence " 
126           //          << "------------- " << d->GetVR() << " "<< std::hex
127           //          << d->GetGroup() << " " << d->GetElement()
128           //  << std::endl;    
129          }
130
131          d=original->GetHeader()->GetNextEntry();
132       }
133
134       //copy->GetImageData();
135       //copy->SetImageData(imageData, dataSize);
136
137       std::cout << "--- Copy ----------------------" << std::endl;
138       std::cout <<std::endl << "DO NOT care about Offset"  <<std::endl<<std::endl;; 
139       copy->GetHeader()->Print();
140       std::cout << "--- ---- ----------------------" << std::endl;
141    
142       copy->WriteDcmExplVR( output );
143
144       return 0;
145 }
146
147
148