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