]> Creatis software - gdcm.git/blob - Example/TestReadWriteReadCompare.cxx
First stage of name normalisation : gdcm::File replace by gdcm::FileHelper
[gdcm.git] / Example / TestReadWriteReadCompare.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/20 16:16:58 $
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 "gdcmHeader.h"
19 #include "gdcmFileHelper.h"
20
21 //Generated file:
22 #include "gdcmDataImages.h"
23
24 int main(int argc, char* argv[]) 
25 {
26    if (argc<2)
27    {
28       std::cerr << "Test::TestReadWriteReadCompare: Usage: " << argv[0]
29                 << " fileToCheck.dcm " << std::endl;
30    }
31    
32    std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
33    std::cout << "   For all images in gdcmData (and not blacklisted in "
34                 "Test/CMakeLists.txt)" << std::endl;
35    std::cout << "   apply the following multistep test: " << std::endl;
36    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
37              << " IsReadable(). " << std::endl;
38    std::cout << "   step 2: write the corresponding image in DICOM V3 "
39              << "with explicit" << std::endl
40              << "           Value Representation in temporary file "
41              << "TestReadWriteReadCompare.dcm." << std::endl;
42    std::cout << "   step 3: read the image written on step2 and call "
43              << " IsReadable(). " << std::endl;
44    std::cout << "   step 4: compare (in memory with memcmp) that the two "
45              << "images " << std::endl
46              << "           match (as expanded by gdcm)." << std::endl;
47
48    //int i = 0;
49    //while( gdcmDataImages[i] != 0 ) 
50    {
51       std::string filename = GDCM_DATA_ROOT;
52       filename += "/";
53       //filename += gdcmDataImages[i++];
54      filename +=argv[1];
55    
56       std::cout << "   Testing: " << filename << std::endl;
57
58       //////////////// Step 1 (see above description): 
59
60       gdcm::Header *header = new gdcm::Header( filename );
61       if( !header->IsReadable() )
62       {
63          std::cerr << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
64                    << filename << std::endl;
65          delete header;
66          return 1;
67       }
68       std::cout << "           step 1 ...";
69
70       //////////////// Step 2:
71
72       gdcm::FileHelper *file = new gdcm::FileHelper( header );
73       int dataSize    = file->GetImageDataSize();
74       uint8_t* imageData = file->GetImageData(); //EXTREMELY IMPORTANT
75              // Sure, it is : It's up to the user to decide if he wants to
76              // GetImageData or if he wants to GetImageDataRaw
77              // (even if we do it by setting a flag, he will have to decide) 
78
79       /// \todo Following line commented out because gdcmFile::SetImageData() is
80       /// brain dead: it sets ImageDataSize to its argument and PixelRead to a.
81       /// Later on, when writing gdcmFile::WriteBase() 
82       /// and because PixelRead == 1 we call
83       ///    PixelElement->SetLength( ImageDataSizeRaw );
84       /// where we use ImageDataSizeRAW instead of ImageDataSize !
85       /// But when the original image made the transformation LUT -> RGB, 
86       /// ImageDataSizeRaw is the third of ImageDataSize, and there is no
87       /// reason (since we called gdcmFile::SetImageData) to use the Raw image
88       /// size... This "bug" in gdcmFile made that we had to black list
89       /// images 8BitsUncompressedColor.dcm, OT-PAL-8-face.dcm and 
90       /// US-PAL-8-10x-echo.dcm...
91       /// In conclusion fix gdcmFile, and then uncomment the following line.
92       
93       // --> I did. ctest doesn't break. But ... is it enought to say it's OK ?
94       
95       file->SetImageData(imageData, dataSize);
96       
97       file->WriteDcmExplVR( "TestReadWriteReadCompare.dcm" );
98       std::cout << "2...";
99     
100       //////////////// Step 3:
101
102       gdcm::FileHelper *reread = new gdcm::FileHelper( "TestReadWriteReadCompare.dcm" );
103       if( !reread->GetHeader()->IsReadable() )
104       {
105         std::cerr << "Test::TestReadWriteReadCompare: Could not reread image "
106                   << "written:" << filename << std::endl;
107         delete header;
108         delete file;
109         delete reread;
110         return 1;
111       }
112       std::cout << "3...";
113       // For the next step:
114       int    dataSizeWritten = reread->GetImageDataSize();
115       uint8_t *imageDataWritten = reread->GetImageData();
116
117       //////////////// Step 4:
118  
119       if (dataSize != dataSizeWritten)
120       {
121          std::cout << std::endl
122             << "        Pixel areas lengths differ: "
123             << dataSize << " # " << dataSizeWritten << std::endl;
124          delete header;
125          delete file;
126          delete reread;
127          return 1;
128       }
129
130       if (int res = memcmp(imageData, imageDataWritten, dataSize) !=0)
131       {
132          (void)res;
133          std::cout << std::endl
134             << "        Pixel differ (as expanded in memory)." << std::endl;
135          delete header;
136          delete file;
137          delete reread;
138          return 1;
139       }
140       std::cout << "4...OK." << std::endl ;
141
142       //////////////// Clean up:
143       delete header;
144       delete file;
145       delete reread;
146    }
147
148   return 0;
149 }