]> Creatis software - gdcm.git/blob - Testing/TestCopyDicom.cxx
* Test/TestCopyDicom.cxx : avoid compilation warnings.
[gdcm.git] / Testing / TestCopyDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/18 17:06:54 $
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 //Generated file:
25 #include "gdcmDataImages.h"
26
27 #ifndef _WIN32
28 #include <unistd.h> //for access, unlink
29 #else
30 #include <io.h> //for _access on Win32
31 #endif
32
33 // return true if the file exists
34 bool FileExists(const char* filename)
35 {
36 #ifdef _MSC_VER
37 # define access _access
38 #endif
39 #ifndef R_OK
40 # define R_OK 04
41 #endif
42   if ( access(filename, R_OK) != 0 )
43     {
44     return false;
45     }
46   else
47     {
48     return true;
49     }
50 }
51
52 bool RemoveFile(const char* source)
53 {
54 #ifdef _MSC_VER
55 #define _unlink unlink
56 #endif
57   return unlink(source) != 0 ? false : true;
58 }
59
60 // Here we load a gdcmFile and then try to create from scratch a copy of it,
61 // copying field by field the dicom image
62
63 int TestCopyDicom(int , char* [])
64 {
65    int i =0;
66    int retVal = 0;  //by default this is an error
67    while( gdcmDataImages[i] != 0 )
68    {
69       std::string filename = GDCM_DATA_ROOT;
70       filename += "/";  //doh!
71       filename += gdcmDataImages[i];
72       std::cerr << "Filename: " << filename << std::endl;
73
74       std::string output = "../Testing/Temporary/output.dcm";
75
76       if( FileExists( output.c_str() ) )
77       {
78         // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
79          if( !RemoveFile( output.c_str() ) )
80          {
81             std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
82             return 1;
83          }
84       }
85
86       gdcm::File *original = new gdcm::File( filename );
87       gdcm::File *copy = new gdcm::File( output );
88
89       const gdcm::TagDocEntryHT & Ht = original->GetHeader()->GetTagHT();
90
91       size_t dataSize = original->GetImageDataSize();
92       uint8_t* imageData = original->GetImageData();
93       (void)dataSize;  // To use the variable and not have warnings at compil.
94       (void)imageData; // To use the variable and not have warnings at compil.
95
96       //First of all copy the header field by field
97   
98       // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
99       // It was commented out by Mathieu, that was a *good* idea
100       // (the user does NOT have to know the way we implemented the Header !)
101       // Waiting for a 'clean' solution, I keep the method ...JPRx
102
103       gdcm::DocEntry* d;
104
105       for (gdcm::TagDocEntryHT::const_iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
106       {
107          d = tag->second;
108          if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
109          {
110             copy->GetHeader()->ReplaceOrCreateByNumber( 
111                                  b->GetBinArea(),
112                                  b->GetLength(),
113                                  b->GetGroup(), 
114                                  b->GetElement(),
115                                  b->GetVR() );
116          }
117          else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
118          {   
119              copy->GetHeader()->ReplaceOrCreateByNumber( 
120                                  v->GetValue(),
121                                  v->GetGroup(), 
122                                  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
135       // Useless to set the image datas, because it's already made when
136       // copying the corresponding BinEntry that contains the pixel datas
137       //copy->SetImageData(imageData, dataSize);
138       original->GetHeader()->SetImageDataSize(dataSize);
139
140       copy->WriteDcmExplVR( output );
141
142       delete original;
143       delete copy;
144
145       copy = new gdcm::File( output );
146
147       //Is the file written still gdcm parsable ?
148       if ( !copy->GetHeader()->IsReadable() )
149       { 
150          retVal +=1;
151          std::cout << output << " Failed" << std::endl;
152       }
153       i++;
154    }
155    return retVal;
156 }
157