]> Creatis software - gdcm.git/blob - Testing/TestCopyRescaleDicom.cxx
To see at a glance what are the most time consuming images
[gdcm.git] / Testing / TestCopyRescaleDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyRescaleDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/05/20 15:50:27 $
7   Version:   $Revision: 1.19 $
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 #include <time.h>
24 #include <sys/times.h>
25 #include <iomanip> // for std::ios::left, ...
26
27 //Generated file:
28 #include "gdcmDataImages.h"
29
30 #ifndef _WIN32
31 #include <unistd.h> //for access, unlink
32 #else
33 #include <io.h> //for _access on Win32
34 #endif
35
36 bool FileExists(const char *filename)
37 {
38 #ifdef _MSC_VER
39 # define access _access
40 #endif
41 #ifndef R_OK
42 # define R_OK 04
43 #endif
44   if ( access(filename, R_OK) != 0 )
45     {
46     return false;
47     }
48   else
49     {
50     return true;
51     }
52 }
53
54 bool RemoveFile(const char *source)
55 {
56 #ifdef _MSC_VER
57 #define _unlink unlink
58 #endif
59   return unlink(source) != 0 ? false : true;
60 }
61
62 int CopyRescaleDicom(std::string const &filename, 
63                      std::string const &output )
64 {
65    std::cout << "   Testing: " << filename << std::endl;
66    if( FileExists( output.c_str() ) )
67    {
68      // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
69       if( !RemoveFile( output.c_str() ) )
70       {
71          std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
72          return 1;
73       }
74    }
75
76    //////////////// Step 1:
77    std::cout << "      1...";
78    gdcm::File originalF = gdcm::File( filename );
79    gdcm::File copyF     = gdcm::File( );
80
81    //First of all copy the file, field by field
82
83    //////////////// Step 2:
84    std::cout << "2...";
85    // Copy of the file content
86    gdcm::DocEntry *d = originalF.GetFirstEntry();
87    while(d)
88    {
89       if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
90       {
91          copyF.InsertBinEntry( b->GetBinArea(),b->GetLength(),
92                                b->GetGroup(),b->GetElement(),
93                                b->GetVR() );
94       }
95       else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
96       {   
97           copyF.InsertValEntry( v->GetValue(),
98                                 v->GetGroup(),v->GetElement(),
99                                 v->GetVR() ); 
100       }
101       else
102       {
103        // We skip pb of SQ recursive exploration
104       }
105
106       d = originalF.GetNextEntry();
107    }
108
109    gdcm::FileHelper original = gdcm::FileHelper( &originalF );
110    gdcm::FileHelper copy     = gdcm::FileHelper( &copyF );
111
112    size_t dataSize = original.GetImageDataSize();
113
114    size_t rescaleSize;
115    uint8_t *rescaleImage;
116
117    const std::string &bitsStored = originalF.GetEntryValue(0x0028,0x0101);
118    if( bitsStored == "16" )
119    {
120       std::cout << "Rescale...";
121       copyF.InsertValEntry( "8", 0x0028, 0x0100); // Bits Allocated
122       copyF.InsertValEntry( "8", 0x0028, 0x0101); // Bits Stored
123       copyF.InsertValEntry( "7", 0x0028, 0x0102); // High Bit
124       copyF.InsertValEntry( "0", 0x0028, 0x0103); // Pixel Representation
125
126       // We assume the value were from 0 to uint16_t max
127       rescaleSize = dataSize / 2;
128       rescaleImage = new uint8_t[dataSize];
129
130       uint16_t *imageData16 = (uint16_t*)original.GetImageData();
131       uint16_t *tmpImage = imageData16;
132       uint8_t *tmpRescale = rescaleImage;
133       for(unsigned int i=0; i<rescaleSize; i++)
134       {
135          *tmpRescale = (uint8_t)( (*tmpImage)>>8 );
136          tmpImage++;
137          tmpRescale++;
138       }
139    }
140    else
141    {
142       std::cout << "Same...";
143       rescaleSize = dataSize;
144       rescaleImage = new uint8_t[dataSize];
145       memcpy(rescaleImage,original.GetImageData(),dataSize);
146    }
147
148    copy.SetImageData(rescaleImage, rescaleSize);
149
150    //////////////// Step 3:
151    std::cout << "3...";
152    copy.SetWriteModeToRGB();
153    if( !copy.WriteDcmExplVR(output) )
154    {
155       std::cout << " Failed" << std::endl
156                 << "        " << output << " not written" << std::endl;
157
158       delete[] rescaleImage;
159
160       return 1;
161    }
162
163    //////////////// Step 4:
164    std::cout << "4...";
165    gdcm::FileHelper copy2 = gdcm::FileHelper( output );
166
167    //Is the file written still gdcm parsable ?
168    if ( !copy2.GetFile()->IsReadable() )
169    { 
170       std::cout << " Failed" << std::endl
171                 << "        " << output << " not readable" << std::endl;
172
173       delete[] rescaleImage;
174
175       return 1;
176    }
177
178    //////////////// Step 5:
179    std::cout << "5...";
180    size_t    dataSizeWritten = copy2.GetImageDataSize();
181    uint8_t *imageDataWritten = copy2.GetImageData();
182
183    if (originalF.GetXSize() != copy2.GetFile()->GetXSize() ||
184        originalF.GetYSize() != copy2.GetFile()->GetYSize() ||
185        originalF.GetZSize() != copy2.GetFile()->GetZSize())
186    {
187       std::cout << "Failed" << std::endl
188          << "        X Size differs: "
189          << "X: " << originalF.GetXSize() << " # " 
190                   << copy2.GetFile()->GetXSize() << " | "
191          << "Y: " << originalF.GetYSize() << " # " 
192                   << copy2.GetFile()->GetYSize() << " | "
193          << "Z: " << originalF.GetZSize() << " # " 
194                   << copy2.GetFile()->GetZSize() << std::endl;
195       delete[] rescaleImage;
196
197       return 1;
198    }
199
200    if (rescaleSize != dataSizeWritten)
201    {
202       std::cout << " Failed" << std::endl
203                 << "        Pixel areas lengths differ: "
204                 << dataSize << " # " << dataSizeWritten << std::endl;
205
206       delete[] rescaleImage;
207
208       return 1;
209    }
210
211    if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
212    {
213       (void)res;
214       std::cout << " Failed" << std::endl
215                 << "        Pixel differ (as expanded in memory)." << std::endl;
216
217       delete[] rescaleImage;
218
219       return 1;
220    }
221    std::cout << "OK." << std::endl ;
222
223    delete[] rescaleImage;
224
225    return 0;
226 }
227
228 // Here we load a gdcmFile and then try to create from scratch a copy of it,
229 // copying field by field the dicom image
230
231 int TestCopyRescaleDicom(int argc, char *argv[])
232 {
233    if ( argc == 3 )
234    {
235       // The test is specified a specific filename, use it instead of looping
236       // over all images
237       const std::string input     = argv[1];
238       const std::string reference = argv[2];
239       return CopyRescaleDicom( input, reference );
240    }
241    else if ( argc > 3 || argc == 2 )
242    {
243       std::cout << "   Usage: " << argv[0]
244                 << " (no arguments needed)." << std::endl;
245       std::cout << "or   Usage: " << argv[0]
246                 << " filename.dcm reference.dcm" << std::endl;
247       return 1;
248    }
249    // else other cases:
250
251    std::cout << "   Description (Test::TestCopyDicom): "
252              << std::endl;
253    std::cout << "   For all images in gdcmData (and not blacklisted in "
254                 "Test/CMakeLists.txt)"
255              << std::endl;
256    std::cout << "   apply the following to each filename.xxx: "
257              << std::endl;
258    std::cout << "   step 1: parse the image (as gdcmFile) and call"
259              << " IsReadable(). After that, call GetImageData() and "
260              << "GetImageDataSize() "
261              << std::endl;
262    std::cout << "   step 2: create a copy of the readed file and the new"
263              << " pixel data are set to the copy"
264              << std::endl;
265    std::cout << "   step 3: write the copy of the image"
266              << std::endl;
267    std::cout << "   step 4: read the copy and call IsReadable()"
268              << std::endl;
269    std::cout << "   step 5: compare (in memory with memcmp) that the two "
270              << "images " << std::endl
271              << "           match (as expanded by gdcm)." << std::endl;
272    std::cout << std::endl;
273
274
275    clock_t r1,r2, r3,r4;
276    struct tms tms1,tms2, tms3,tms4;
277
278    r3 = times(&tms3);
279    int i =0;
280    int retVal = 0;  //by default this is an error
281    while( gdcmDataImages[i] != 0 )
282    {
283       std::string filename = GDCM_DATA_ROOT;
284       filename += "/";  //doh!
285       filename += gdcmDataImages[i];
286
287       std::string output = "output.dcm";
288
289       r1 = times(&tms1);
290       if( CopyRescaleDicom( filename, output ) != 0 )
291       {
292          retVal++;
293       }
294       r2 = times(&tms2);
295
296       std::cout 
297         << std::setw(150-strlen(gdcmDataImages[i]))
298         << gdcmDataImages[i] << " user time: " 
299         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
300         << " system time: "
301         << (long) ((tms2.tms_stime)  - (tms1.tms_stime)) 
302         << "\t elapsed time: " << r2 - r1
303         << std::endl;
304
305       i++;
306    }
307    r4 = times(&tms4);
308
309    std::cout 
310         << std::setw(150-strlen("Gross Total")) << " --> "
311         << "Gross Total" << " user time: " 
312         << (long) ((tms4.tms_utime)  - (tms3.tms_utime))
313         << " system time: "
314         << (long) ((tms4.tms_stime)  - (tms3.tms_stime)) 
315         << "\t elapsed time: " << (long) (r4 - r3)
316         << std::endl;
317    return retVal;
318 }
319