]> Creatis software - gdcm.git/blob - Example/TestCopyDicom.cxx
* Minor coding-style clean up
[gdcm.git] / Example / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:43 $
7   Version:   $Revision: 1.31 $
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 "gdcmDataEntry.h"
22
23 #ifndef _WIN32
24 #include <unistd.h> //for access, unlink
25 #else
26 #include <io.h> //for _access
27 #endif
28
29 // return true if the file exists
30 bool FileExists(const char *filename)
31 {
32 #ifdef _MSC_VER
33 # define access _access
34 #endif
35 #ifndef R_OK
36 # define R_OK 04
37 #endif
38   if ( access(filename, R_OK) != 0 )
39     {
40     return false;
41     }
42   else
43     {
44     return true;
45     }
46 }
47
48 bool RemoveFile(const char *source)
49 {
50 #ifdef _MSC_VER
51 #define _unlink unlink
52 #endif
53   return unlink(source) != 0 ? false : true;
54 }
55
56 // Here we load a gdcmFile and then try to create from scratch a copy of it,
57 // copying field by field the dicom image
58
59 int main(int argc, char *argv[])
60 {
61    if (argc < 3)
62    {
63       std::cerr << "Usage :" << std::endl << 
64       argv[0] << " input_dicom output_dicom" << std::endl;
65       return 1;
66    }
67
68 // don't modify identation in order to let this source xdiffable with ../Test
69
70       std::string filename = argv[1];
71       std::string output = argv[2];
72
73       if( FileExists( output.c_str() ) )
74       {
75          std::cerr << "Don't try to cheat, I am removing the file anyway" 
76                    << std::endl;
77          if( !RemoveFile( output.c_str() ) )
78          {
79             std::cerr << "Ouch, the file exist, but I cannot remove it" 
80                       << std::endl;
81             return 1;
82          }
83       }
84       gdcm::File *fileOr = new gdcm::File();
85       fileOr->SetFileName( filename );
86       fileOr->Load();
87       gdcm::FileHelper *original = new gdcm::FileHelper( fileOr );
88    
89       std::cout << "--- Original ----------------------" << std::endl;
90       //original->GetFile()->Print();
91    
92       gdcm::FileHelper *copy = new gdcm::FileHelper( );
93       copy->SetFileName( output );
94       copy->Load();
95
96       //size_t dataSize;
97       uint8_t *imageData;
98       //dataSize = original->GetImageDataSize();// just an accesor :useless here
99       
100       imageData = original->GetImageData(); // VERY important : 
101                                       // brings pixels into memory !
102       //(void)imageData; // not enough to avoid warning with icc compiler
103       //(void)dataSize; //  not enough to avoid warning on 'Golgot'
104   
105       std::cout << imageData << std::endl; // to avoid warning ?
106
107       //First of all copy the header field by field
108
109       gdcm::DocEntry *d = original->GetFile()->GetFirstEntry();
110       while(d)
111       {
112          if ( gdcm::DataEntry *de = dynamic_cast<gdcm::DataEntry *>(d) )
113          {              
114             copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
115                                                  de->GetGroup(),de->GetElement(),
116                                                  de->GetVR() );
117          }
118          else
119          {
120           // We skip pb of SQ recursive exploration
121           std::cout << "Skipped Sequence " 
122                     << "------------- " << d->GetVR() << " "<< std::hex
123                     << d->GetGroup() << "," << d->GetElement()
124                     << std::endl;    
125          }
126
127          d=original->GetFile()->GetNextEntry();
128       }
129
130       //copy->GetImageData();
131       //copy->SetImageData(imageData, dataSize);
132
133       std::cout << "--- Copy ----------------------" << std::endl;
134       std::cout <<std::endl << "DO NOT care about Offset"  
135                 <<std::endl << std::endl;; 
136       copy->GetFile()->Print();
137       std::cout << "--- ---- ----------------------" << std::endl;
138    
139       copy->WriteDcmExplVR( output );
140       
141
142       delete fileOr;   // File
143       delete original; // FileHelper
144       delete copy;     // FileHelper
145       return 0;
146 }
147
148
149