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