]> Creatis software - gdcm.git/blob - Testing/TestCopyRescaleDicom.cxx
Stage 2 of names normalization :
[gdcm.git] / Testing / TestCopyRescaleDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestCopyRescaleDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/21 11:40:54 $
7   Version:   $Revision: 1.8 $
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 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::File *originalH = new gdcm::File( filename );
47    gdcm::File *copyH     = new gdcm::File( );
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 File !)
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    gdcm::DocEntry* d = originalH->GetFirstEntry();
61    while(d)
62    {
63       if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
64       {
65          copyH->ReplaceOrCreate( 
66                               b->GetBinArea(),
67                               b->GetLength(),
68                               b->GetGroup(), 
69                               b->GetElement(),
70                               b->GetVR() );
71       }
72       else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
73       {   
74           copyH->ReplaceOrCreate( 
75                               v->GetValue(),
76                               v->GetGroup(), 
77                               v->GetElement(),
78                               v->GetVR() ); 
79       }
80       else
81       {
82        // We skip pb of SQ recursive exploration
83       }
84
85       d=originalH->GetNextEntry();
86    }
87
88    gdcm::FileHelper *original = new gdcm::FileHelper( originalH );
89    gdcm::FileHelper *copy     = new gdcm::FileHelper( copyH );
90
91    size_t dataSize = original->GetImageDataSize();
92
93    size_t rescaleSize;
94    uint8_t *rescaleImage;
95
96    const std::string & bitsStored    = originalH->GetEntry(0x0028,0x0101);
97    if( bitsStored == "16" )
98    {
99       std::cout << "Rescale...";
100       copyH->ReplaceOrCreate( "8", 0x0028, 0x0100); // BitsAllocated
101       copyH->ReplaceOrCreate( "8", 0x0028, 0x0101); // BitsStored
102       copyH->ReplaceOrCreate( "7", 0x0028, 0x0102); // HighBit
103       copyH->ReplaceOrCreate( "0", 0x0028, 0x0103); //Pixel Representation
104  
105       // We assume the value were from 0 to uint16_t max
106       rescaleSize = dataSize / 2;
107       rescaleImage = new uint8_t[dataSize];
108
109       uint16_t* imageData16 = (uint16_t*)original->GetImageData();
110       for(unsigned int i=0; i<rescaleSize; i++)
111       {
112          rescaleImage[i] = imageData16[i]/256;
113       }
114    }
115    else
116    {
117       std::cout << "Same...";
118       rescaleSize = dataSize;
119       rescaleImage = new uint8_t[dataSize];
120       memcpy(rescaleImage,original->GetImageData(),dataSize);
121    }
122
123    copy->SetImageData(rescaleImage, rescaleSize);
124
125    //////////////// Step 3:
126    std::cout << "3...";
127    copy->SetWriteModeToRGB();
128    if( !copy->WriteDcmExplVR(output) )
129    {
130       std::cout << " Failed" << std::endl
131                 << "        " << output << " not written" << std::endl;
132
133       delete original;
134       delete copy;
135       delete originalH;
136       delete copyH;
137       delete[] rescaleImage;
138
139       return 1;
140    }
141
142    delete copy;
143    delete copyH;
144
145    //////////////// Step 4:
146    std::cout << "4...";
147    copy = new gdcm::FileHelper( output );
148
149    //Is the file written still gdcm parsable ?
150    if ( !copy->GetFile()->IsReadable() )
151    { 
152       std::cout << " Failed" << std::endl
153                 << "        " << output << " not readable" << std::endl;
154
155       delete original;
156       delete originalH;
157       delete[] rescaleImage;
158
159       return 1;
160    }
161
162    //////////////// Step 5:
163    std::cout << "5...";
164    size_t    dataSizeWritten = copy->GetImageDataSize();
165    uint8_t* imageDataWritten = copy->GetImageData();
166
167    if (originalH->GetXSize() != copy->GetFile()->GetXSize() ||
168        originalH->GetYSize() != copy->GetFile()->GetYSize() ||
169        originalH->GetZSize() != copy->GetFile()->GetZSize())
170    {
171       std::cout << "Failed" << std::endl
172          << "        X Size differs: "
173          << "X: " << originalH->GetXSize() << " # " 
174                   << copy->GetFile()->GetXSize() << " | "
175          << "Y: " << originalH->GetYSize() << " # " 
176                   << copy->GetFile()->GetYSize() << " | "
177          << "Z: " << originalH->GetZSize() << " # " 
178                   << copy->GetFile()->GetZSize() << std::endl;
179       delete original;
180       delete copy;
181       delete originalH;
182       delete[] rescaleImage;
183
184       return 1;
185    }
186
187    if (rescaleSize != dataSizeWritten)
188    {
189       std::cout << " Failed" << std::endl
190                 << "        Pixel areas lengths differ: "
191                 << dataSize << " # " << dataSizeWritten << std::endl;
192
193       delete original;
194       delete copy;
195       delete originalH;
196       delete[] rescaleImage;
197
198       return 1;
199    }
200
201    if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
202    {
203       (void)res;
204       std::cout << " Failed" << std::endl
205                 << "        Pixel differ (as expanded in memory)." << std::endl;
206
207       delete original;
208       delete copy;
209       delete originalH;
210       delete[] rescaleImage;
211
212       return 1;
213    }
214    std::cout << "OK." << std::endl ;
215
216    delete original;
217    delete copy;
218    delete originalH;
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 datas 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    int i =0;
271    int retVal = 0;  //by default this is an error
272    while( gdcmDataImages[i] != 0 )
273    {
274       std::string filename = GDCM_DATA_ROOT;
275       filename += "/";  //doh!
276       filename += gdcmDataImages[i];
277
278       std::string output = "output.dcm";
279
280       if( CopyRescaleDicom( filename, output ) != 0 )
281       {
282          retVal++;
283       }
284
285       i++;
286    }
287    return retVal;
288 }
289