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