]> Creatis software - gdcm.git/blob - Testing/TestCopyRescaleDicom.cxx
ENH: Adding a non-working test again
[gdcm.git] / Testing / TestCopyRescaleDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyRescaleDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/07 16:55:56 $
7   Version:   $Revision: 1.1 $
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 "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
22
23 //Generated file:
24 #include "gdcmDataImages.h"
25
26 bool FileExists(const char* filename);
27
28 bool RemoveFile(const char* source);
29
30 int CopyRescaleDicom(std::string const & filename, 
31                      std::string const & output )
32 {
33    std::cout << "   Testing: " << filename << std::endl;
34    if( FileExists( output.c_str() ) )
35    {
36      // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
37       if( !RemoveFile( output.c_str() ) )
38       {
39          std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
40          return 1;
41       }
42    }
43
44    //////////////// Step 1:
45    std::cout << "      1...";
46    gdcm::Header *originalH = new gdcm::Header( filename );
47    gdcm::Header *copyH     = new gdcm::Header( );
48
49    //First of all copy the header field by field
50
51    // Warning :Accessor gdcmElementSet::GetEntry() should not exist 
52    // It was commented out by Mathieu, that was a *good* idea
53    // (the user does NOT have to know the way we implemented the Header !)
54    // Waiting for a 'clean' solution, I keep the method ...JPRx
55
56
57    //////////////// Step 2:
58    std::cout << "2...";
59    originalH->Initialize();
60    gdcm::DocEntry* d = originalH->GetNextEntry();
61
62    while(d)
63    {
64       if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
65       {   
66           copyH->ReplaceOrCreateByNumber( 
67                               v->GetValue(),
68                               v->GetGroup(), 
69                               v->GetElement(),
70                               v->GetVR() ); 
71       }
72
73       d=originalH->GetNextEntry();
74    }
75
76    gdcm::File *original = new gdcm::File( originalH );
77    gdcm::File *copy     = new gdcm::File( copyH );
78
79    size_t dataSize = original->GetImageDataSize();
80    uint8_t* imageData = original->GetImageData();
81
82    size_t rescaleSize = dataSize / 2;
83 #ifdef VERSION_1
84    uint8_t *rescaleImage = new uint8_t[dataSize];
85 #endif
86
87    //2 case now, if 16 bits input then try to downscale to 8bits just for
88    //fun:
89
90    const std::string & bitsStored    = originalH->GetEntryByNumber(0x0028,0x0101);
91 //   std::cout << "BitsStored:    " << bitsStored << std::endl;
92
93    if( bitsStored == "16" )
94    {
95       uint16_t* imageData16 = (uint16_t*)original->GetImageData();
96       copyH->ReplaceOrCreateByNumber( "8", 0x0028, 0x0100); // BitsAllocated
97       copyH->ReplaceOrCreateByNumber( "8", 0x0028, 0x0101); // BitsStored
98       copyH->ReplaceOrCreateByNumber( "7", 0x0028, 0x0102); // HighBit
99       copyH->ReplaceOrCreateByNumber( "0", 0x0028, 0x0103); //Pixel Representation
100  
101       // We assume the value were from 0 to uint16_t max
102       for(unsigned int i=0; i<rescaleSize; i++)
103       {
104 #ifdef VERSION_1
105          rescaleImage[i] = imageData16[i]/256;
106 #else
107          imageData[i]  = imageData16[i]/256;
108 #endif
109       }
110 #ifdef VERSION_1
111       copy->SetImageData(rescaleImage, rescaleSize);
112 #endif 
113    }
114    else
115    {
116       copy->SetImageData(imageData, dataSize);
117    }
118
119    //////////////// Step 3:
120    std::cout << "3...";
121    copy->SetWriteModeToRGB();
122    if( !copy->WriteDcmExplVR(output) )
123    {
124       std::cout << " Failed" << std::endl
125                 << "        " << output << " not written" << std::endl;
126
127       delete original;
128       delete copy;
129       delete originalH;
130       delete copyH;
131
132       return 1;
133    }
134
135    delete copy;
136    delete copyH;
137
138    //////////////// Step 4:
139    std::cout << "4...";
140    copy = new gdcm::File( output );
141
142    //Is the file written still gdcm parsable ?
143    if ( !copy->GetHeader()->IsReadable() )
144    { 
145       std::cout << " Failed" << std::endl
146                 << "        " << output << " not readable" << std::endl;
147
148       delete original;
149       delete originalH;
150
151       return 1;
152    }
153
154    //////////////// Step 5:
155    std::cout << "5...";
156    size_t    dataSizeWritten = copy->GetImageDataSize();
157    uint8_t* imageDataWritten = copy->GetImageData();
158
159    if (dataSize != dataSizeWritten)
160    {
161       std::cout << " Failed" << std::endl
162                 << "        Pixel areas lengths differ: "
163                 << dataSize << " # " << dataSizeWritten << std::endl;
164
165       delete original;
166       delete copy;
167       delete originalH;
168
169       return 1;
170    }
171
172    if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
173    {
174       (void)res;
175       std::cout << " Failed" << std::endl
176                 << "        Pixel differ (as expanded in memory)." << std::endl;
177
178       delete original;
179       delete copy;
180       delete originalH;
181
182       return 1;
183    }
184    std::cout << "OK." << std::endl ;
185
186    delete original;
187    delete copy;
188    delete originalH;
189 #ifdef VERSION_1
190    delete[] rescaleImage;
191 #endif
192
193    return 0;
194 }
195
196 // Here we load a gdcmFile and then try to create from scratch a copy of it,
197 // copying field by field the dicom image
198
199 int TestCopyRescaleDicom(int argc, char* argv[])
200 {
201    if ( argc == 3 )
202    {
203       // The test is specified a specific filename, use it instead of looping
204       // over all images
205       const std::string input     = argv[1];
206       const std::string reference = argv[2];
207       return CopyRescaleDicom( input, reference );
208    }
209    else if ( argc > 3 || argc == 2 )
210    {
211       std::cout << "   Usage: " << argv[0]
212                 << " (no arguments needed)." << std::endl;
213       std::cout << "or   Usage: " << argv[0]
214                 << " filename.dcm reference.dcm" << std::endl;
215       return 1;
216    }
217    // else other cases:
218
219    std::cout << "   Description (Test::TestCopyDicom): "
220              << std::endl;
221    std::cout << "   For all images in gdcmData (and not blacklisted in "
222                 "Test/CMakeLists.txt)"
223              << std::endl;
224    std::cout << "   apply the following to each filename.xxx: "
225              << std::endl;
226    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
227              << " IsReadable(). After that, call GetImageData() and "
228              << "GetImageDataSize() "
229              << std::endl;
230    std::cout << "   step 2: create a copy of the readed file and the new"
231              << " pixel datas are set to the copy"
232              << std::endl;
233    std::cout << "   step 3: write the copy of the image"
234              << std::endl;
235    std::cout << "   step 4: read the copy and call IsReadable()"
236              << std::endl;
237    std::cout << "   step 5: compare (in memory with memcmp) that the two "
238              << "images " << std::endl
239              << "           match (as expanded by gdcm)." << std::endl;
240    std::cout << std::endl;
241
242    int i =0;
243    int retVal = 0;  //by default this is an error
244    while( gdcmDataImages[i] != 0 )
245    {
246       std::string filename = GDCM_DATA_ROOT;
247       filename += "/";  //doh!
248       filename += gdcmDataImages[i];
249
250       std::string output = "output.dcm";
251
252       if( CopyRescaleDicom( filename, output ) != 0 )
253       {
254          retVal++;
255       }
256
257       i++;
258    }
259    return retVal;
260 }
261