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