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