]> Creatis software - gdcm.git/blob - Testing/TestCopyRescaleDicom.cxx
* Test/ : accelerate tests (it's not very significant accelerations).
[gdcm.git] / Testing / TestCopyRescaleDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyRescaleDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/09 15:06:48 $
7   Version:   $Revision: 1.15 $
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); // Bits Allocated
90       copyF->InsertValEntry( "8", 0x0028, 0x0101); // Bits Stored
91       copyF->InsertValEntry( "7", 0x0028, 0x0102); // High Bit
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       uint16_t *tmpImage = imageData16;
100       uint8_t *tmpRescale = rescaleImage;
101       for(unsigned int i=0; i<rescaleSize; i++)
102       {
103          *tmpRescale = (*tmpImage)>>8;
104          tmpImage++;
105          tmpRescale++;
106       }
107    }
108    else
109    {
110       std::cout << "Same...";
111       rescaleSize = dataSize;
112       rescaleImage = new uint8_t[dataSize];
113       memcpy(rescaleImage,original->GetImageData(),dataSize);
114    }
115
116    copy->SetImageData(rescaleImage, rescaleSize);
117
118    //////////////// Step 3:
119    std::cout << "3...";
120    copy->SetWriteModeToRGB();
121    if( !copy->WriteDcmExplVR(output) )
122    {
123       std::cout << " Failed" << std::endl
124                 << "        " << output << " not written" << std::endl;
125
126       delete original;
127       delete copy;
128       delete originalF;
129       delete copyF;
130       delete[] rescaleImage;
131
132       return 1;
133    }
134
135    delete copy;
136    delete copyF;
137
138    //////////////// Step 4:
139    std::cout << "4...";
140    copy = new gdcm::FileHelper( output );
141
142    //Is the file written still gdcm parsable ?
143    if ( !copy->GetFile()->IsReadable() )
144    { 
145       std::cout << " Failed" << std::endl
146                 << "        " << output << " not readable" << std::endl;
147
148       delete original;
149       delete originalF;
150       delete[] rescaleImage;
151
152       return 1;
153    }
154
155    //////////////// Step 5:
156    std::cout << "5...";
157    size_t    dataSizeWritten = copy->GetImageDataSize();
158    uint8_t *imageDataWritten = copy->GetImageData();
159
160    if (originalF->GetXSize() != copy->GetFile()->GetXSize() ||
161        originalF->GetYSize() != copy->GetFile()->GetYSize() ||
162        originalF->GetZSize() != copy->GetFile()->GetZSize())
163    {
164       std::cout << "Failed" << std::endl
165          << "        X Size differs: "
166          << "X: " << originalF->GetXSize() << " # " 
167                   << copy->GetFile()->GetXSize() << " | "
168          << "Y: " << originalF->GetYSize() << " # " 
169                   << copy->GetFile()->GetYSize() << " | "
170          << "Z: " << originalF->GetZSize() << " # " 
171                   << copy->GetFile()->GetZSize() << std::endl;
172       delete original;
173       delete copy;
174       delete originalF;
175       delete[] rescaleImage;
176
177       return 1;
178    }
179
180    if (rescaleSize != dataSizeWritten)
181    {
182       std::cout << " Failed" << std::endl
183                 << "        Pixel areas lengths differ: "
184                 << dataSize << " # " << dataSizeWritten << std::endl;
185
186       delete original;
187       delete copy;
188       delete originalF;
189       delete[] rescaleImage;
190
191       return 1;
192    }
193
194    if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
195    {
196       (void)res;
197       std::cout << " Failed" << std::endl
198                 << "        Pixel differ (as expanded in memory)." << std::endl;
199
200       delete original;
201       delete copy;
202       delete originalF;
203       delete[] rescaleImage;
204
205       return 1;
206    }
207    std::cout << "OK." << std::endl ;
208
209    delete original;
210    delete copy;
211    delete originalF;
212    delete[] rescaleImage;
213
214    return 0;
215 }
216
217 // Here we load a gdcmFile and then try to create from scratch a copy of it,
218 // copying field by field the dicom image
219
220 int TestCopyRescaleDicom(int argc, char *argv[])
221 {
222    if ( argc == 3 )
223    {
224       // The test is specified a specific filename, use it instead of looping
225       // over all images
226       const std::string input     = argv[1];
227       const std::string reference = argv[2];
228       return CopyRescaleDicom( input, reference );
229    }
230    else if ( argc > 3 || argc == 2 )
231    {
232       std::cout << "   Usage: " << argv[0]
233                 << " (no arguments needed)." << std::endl;
234       std::cout << "or   Usage: " << argv[0]
235                 << " filename.dcm reference.dcm" << std::endl;
236       return 1;
237    }
238    // else other cases:
239
240    std::cout << "   Description (Test::TestCopyDicom): "
241              << std::endl;
242    std::cout << "   For all images in gdcmData (and not blacklisted in "
243                 "Test/CMakeLists.txt)"
244              << std::endl;
245    std::cout << "   apply the following to each filename.xxx: "
246              << std::endl;
247    std::cout << "   step 1: parse the image (as gdcmFile) and call"
248              << " IsReadable(). After that, call GetImageData() and "
249              << "GetImageDataSize() "
250              << std::endl;
251    std::cout << "   step 2: create a copy of the readed file and the new"
252              << " pixel data are set to the copy"
253              << std::endl;
254    std::cout << "   step 3: write the copy of the image"
255              << std::endl;
256    std::cout << "   step 4: read the copy and call IsReadable()"
257              << std::endl;
258    std::cout << "   step 5: compare (in memory with memcmp) that the two "
259              << "images " << std::endl
260              << "           match (as expanded by gdcm)." << std::endl;
261    std::cout << std::endl;
262
263    int i =0;
264    int retVal = 0;  //by default this is an error
265    while( gdcmDataImages[i] != 0 )
266    {
267       std::string filename = GDCM_DATA_ROOT;
268       filename += "/";  //doh!
269       filename += gdcmDataImages[i];
270
271       std::string output = "output.dcm";
272
273       if( CopyRescaleDicom( filename, output ) != 0 )
274       {
275          retVal++;
276       }
277
278       i++;
279    }
280    return retVal;
281 }
282