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