]> Creatis software - gdcm.git/blob - Testing/TestCopyRescaleDicom.cxx
methods ReplaceOrCreate renamed as Insert
[gdcm.git] / Testing / TestCopyRescaleDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyRescaleDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/24 16:44:54 $
7   Version:   $Revision: 1.10 $
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 "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::File *originalH = new gdcm::File( filename );
47    gdcm::File *copyH     = new gdcm::File( );
48
49    //First of all copy the header field by field
50
51    //////////////// Step 2:
52    std::cout << "2...";
53    // Copy of the header content
54    gdcm::DocEntry* d = originalH->GetFirstEntry();
55    while(d)
56    {
57       if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
58       {
59          copyH->Insert( 
60                               b->GetBinArea(),
61                               b->GetLength(),
62                               b->GetGroup(), 
63                               b->GetElement(),
64                               b->GetVR() );
65       }
66       else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
67       {   
68           copyH->Insert( 
69                               v->GetValue(),
70                               v->GetGroup(), 
71                               v->GetElement(),
72                               v->GetVR() ); 
73       }
74       else
75       {
76        // We skip pb of SQ recursive exploration
77       }
78
79       d=originalH->GetNextEntry();
80    }
81
82    gdcm::FileHelper *original = new gdcm::FileHelper( originalH );
83    gdcm::FileHelper *copy     = new gdcm::FileHelper( copyH );
84
85    size_t dataSize = original->GetImageDataSize();
86
87    size_t rescaleSize;
88    uint8_t *rescaleImage;
89
90    const std::string & bitsStored    = originalH->GetEntryValue(0x0028,0x0101);
91    if( bitsStored == "16" )
92    {
93       std::cout << "Rescale...";
94       copyH->Insert( "8", 0x0028, 0x0100); // BitsAllocated
95       copyH->Insert( "8", 0x0028, 0x0101); // BitsStored
96       copyH->Insert( "7", 0x0028, 0x0102); // HighBit
97       copyH->Insert( "0", 0x0028, 0x0103); //Pixel Representation
98  
99       // We assume the value were from 0 to uint16_t max
100       rescaleSize = dataSize / 2;
101       rescaleImage = new uint8_t[dataSize];
102
103       uint16_t* imageData16 = (uint16_t*)original->GetImageData();
104       for(unsigned int i=0; i<rescaleSize; i++)
105       {
106          rescaleImage[i] = imageData16[i]/256;
107       }
108    }
109    else
110    {
111       std::cout << "Same...";
112       rescaleSize = dataSize;
113       rescaleImage = new uint8_t[dataSize];
114       memcpy(rescaleImage,original->GetImageData(),dataSize);
115    }
116
117    copy->SetImageData(rescaleImage, rescaleSize);
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       delete[] rescaleImage;
132
133       return 1;
134    }
135
136    delete copy;
137    delete copyH;
138
139    //////////////// Step 4:
140    std::cout << "4...";
141    copy = new gdcm::FileHelper( output );
142
143    //Is the file written still gdcm parsable ?
144    if ( !copy->GetFile()->IsReadable() )
145    { 
146       std::cout << " Failed" << std::endl
147                 << "        " << output << " not readable" << std::endl;
148
149       delete original;
150       delete originalH;
151       delete[] rescaleImage;
152
153       return 1;
154    }
155
156    //////////////// Step 5:
157    std::cout << "5...";
158    size_t    dataSizeWritten = copy->GetImageDataSize();
159    uint8_t* imageDataWritten = copy->GetImageData();
160
161    if (originalH->GetXSize() != copy->GetFile()->GetXSize() ||
162        originalH->GetYSize() != copy->GetFile()->GetYSize() ||
163        originalH->GetZSize() != copy->GetFile()->GetZSize())
164    {
165       std::cout << "Failed" << std::endl
166          << "        X Size differs: "
167          << "X: " << originalH->GetXSize() << " # " 
168                   << copy->GetFile()->GetXSize() << " | "
169          << "Y: " << originalH->GetYSize() << " # " 
170                   << copy->GetFile()->GetYSize() << " | "
171          << "Z: " << originalH->GetZSize() << " # " 
172                   << copy->GetFile()->GetZSize() << std::endl;
173       delete original;
174       delete copy;
175       delete originalH;
176       delete[] rescaleImage;
177
178       return 1;
179    }
180
181    if (rescaleSize != dataSizeWritten)
182    {
183       std::cout << " Failed" << std::endl
184                 << "        Pixel areas lengths differ: "
185                 << dataSize << " # " << dataSizeWritten << std::endl;
186
187       delete original;
188       delete copy;
189       delete originalH;
190       delete[] rescaleImage;
191
192       return 1;
193    }
194
195    if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
196    {
197       (void)res;
198       std::cout << " Failed" << std::endl
199                 << "        Pixel differ (as expanded in memory)." << std::endl;
200
201       delete original;
202       delete copy;
203       delete originalH;
204       delete[] rescaleImage;
205
206       return 1;
207    }
208    std::cout << "OK." << std::endl ;
209
210    delete original;
211    delete copy;
212    delete originalH;
213    delete[] rescaleImage;
214
215    return 0;
216 }
217
218 // Here we load a gdcmFile and then try to create from scratch a copy of it,
219 // copying field by field the dicom image
220
221 int TestCopyRescaleDicom(int argc, char* argv[])
222 {
223    if ( argc == 3 )
224    {
225       // The test is specified a specific filename, use it instead of looping
226       // over all images
227       const std::string input     = argv[1];
228       const std::string reference = argv[2];
229       return CopyRescaleDicom( input, reference );
230    }
231    else if ( argc > 3 || argc == 2 )
232    {
233       std::cout << "   Usage: " << argv[0]
234                 << " (no arguments needed)." << std::endl;
235       std::cout << "or   Usage: " << argv[0]
236                 << " filename.dcm reference.dcm" << std::endl;
237       return 1;
238    }
239    // else other cases:
240
241    std::cout << "   Description (Test::TestCopyDicom): "
242              << std::endl;
243    std::cout << "   For all images in gdcmData (and not blacklisted in "
244                 "Test/CMakeLists.txt)"
245              << std::endl;
246    std::cout << "   apply the following to each filename.xxx: "
247              << std::endl;
248    std::cout << "   step 1: parse the image (as gdcmFile) and call"
249              << " IsReadable(). After that, call GetImageData() and "
250              << "GetImageDataSize() "
251              << std::endl;
252    std::cout << "   step 2: create a copy of the readed file and the new"
253              << " pixel datas are set to the copy"
254              << std::endl;
255    std::cout << "   step 3: write the copy of the image"
256              << std::endl;
257    std::cout << "   step 4: read the copy and call IsReadable()"
258              << std::endl;
259    std::cout << "   step 5: compare (in memory with memcmp) that the two "
260              << "images " << std::endl
261              << "           match (as expanded by gdcm)." << std::endl;
262    std::cout << std::endl;
263
264    int i =0;
265    int retVal = 0;  //by default this is an error
266    while( gdcmDataImages[i] != 0 )
267    {
268       std::string filename = GDCM_DATA_ROOT;
269       filename += "/";  //doh!
270       filename += gdcmDataImages[i];
271
272       std::string output = "output.dcm";
273
274       if( CopyRescaleDicom( filename, output ) != 0 )
275       {
276          retVal++;
277       }
278
279       i++;
280    }
281    return retVal;
282 }
283