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