]> Creatis software - gdcm.git/blob - Example/TestCopyDicom.cxx
f44754df43536380cc070e53b7aee2f8f7f03820
[gdcm.git] / Example / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/17 10:41:59 $
7   Version:   $Revision: 1.30 $
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 "gdcmFile.h"
19 #include "gdcmFileHelper.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" 
77                    << std::endl;
78          if( !RemoveFile( output.c_str() ) )
79          {
80             std::cerr << "Ouch, the file exist, but I cannot remove it" 
81                       << std::endl;
82             return 1;
83          }
84       }
85       gdcm::File *fileOr = new gdcm::File();
86       fileOr->SetFileName( filename );
87       fileOr->Load();
88       gdcm::FileHelper *original = new gdcm::FileHelper( fileOr );
89    
90       std::cout << "--- Original ----------------------" << std::endl;
91       //original->GetFile()->Print();
92    
93       gdcm::FileHelper *copy = new gdcm::FileHelper( );
94       copy->SetFileName( output );
95       copy->Load();
96
97       //size_t dataSize;
98       uint8_t *imageData;
99       //dataSize = original->GetImageDataSize();// just an accesor :useless here
100       
101       imageData = original->GetImageData(); // VERY important : 
102                                       // brings pixels into memory !
103       //(void)imageData; // not enough to avoid warning with icc compiler
104       //(void)dataSize; //  not enough to avoid warning on 'Golgot'
105   
106       std::cout << imageData << std::endl; // to avoid warning ?
107
108       //First of all copy the header field by field
109
110       gdcm::DocEntry *d = original->GetFile()->GetFirstEntry();
111       while(d)
112       {
113          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
114          {              
115             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
116                                              b->GetGroup(),b->GetElement(),
117                                              b->GetVR() );
118          }
119          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
120          {   
121             copy->GetFile()->InsertValEntry( v->GetValue(),
122                                              v->GetGroup(),v->GetElement(),
123                                              v->GetVR() ); 
124          }
125          else
126          {
127           // We skip pb of SQ recursive exploration
128           std::cout << "Skipped Sequence " 
129                     << "------------- " << d->GetVR() << " "<< std::hex
130                     << d->GetGroup() << "," << d->GetElement()
131                     << std::endl;    
132          }
133
134          d=original->GetFile()->GetNextEntry();
135       }
136
137       //copy->GetImageData();
138       //copy->SetImageData(imageData, dataSize);
139
140       std::cout << "--- Copy ----------------------" << std::endl;
141       std::cout <<std::endl << "DO NOT care about Offset"  
142                 <<std::endl << std::endl;; 
143       copy->GetFile()->Print();
144       std::cout << "--- ---- ----------------------" << std::endl;
145    
146       copy->WriteDcmExplVR( output );
147       
148
149       delete fileOr;   // File
150       delete original; // FileHelper
151       delete copy;     // FileHelper
152       return 0;
153 }
154
155
156