]> Creatis software - gdcm.git/blob - Example/WriteRead.cxx
End of gdcm/Example kosherization (Load related stuff)
[gdcm.git] / Example / WriteRead.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteRead.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/08 12:02:02 $
7   Version:   $Revision: 1.14 $
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
21 #include <iostream>
22
23 int main(int argc, char *argv[])
24 {  
25    std::string fileNameToWrite;
26
27    gdcm::File *e1;
28    gdcm::File *e2;
29    gdcm::FileHelper *f1;
30    gdcm::FileHelper *f2;
31    uint8_t* imageData, *imageData2;
32    int dataSize, dataSize2;
33      
34    if( argc < 2 )
35     {
36     std::cerr << "Usage " << argv[0] << " image.dcm" << std::endl;
37     return 1;
38     }
39
40    std::string fileName = argv[1];
41
42 // --------------------- we read the input image
43
44    std::cout << argv[1] << std::endl;
45    e1 = new gdcm::File( );
46    e1->SetFileName( fileName );
47    e1->Load();
48    if (!e1->IsReadable()) {
49        std::cerr << "Sorry, " << fileName <<"  not a Readable DICOM / ACR File"
50                  <<std::endl;
51        return 0;
52    }
53    
54    f1 = new gdcm::FileHelper(e1);
55    imageData= f1->GetImageData();
56    dataSize = f1->GetImageDataSize();
57
58 // --------------------- we write it as an Explicit VR DICOM file
59
60       fileNameToWrite = "temp.XDCM";
61       std::cout << "WriteDCM Explicit VR" << std::endl;
62       f1->WriteDcmExplVR(fileNameToWrite);
63
64 // --------------------- we read the written image
65    e2 = new gdcm::File( );
66    e2->SetFileName( fileNameToWrite );
67    e2->Load();
68    if (!e2->IsReadable()) {
69        std::cerr << "Sorry, " << fileNameToWrite << " not a Readable DICOM / ACR File"  
70                  <<std::endl;
71        return 0;
72    }
73    f2 = new gdcm::FileHelper(e2);
74    imageData2= f2->GetImageData();
75    dataSize2 = f2->GetImageDataSize();
76
77 // --------------------- we compare the pixel areas
78
79   if (dataSize != dataSize2) {
80      std::cout << " ----------------------------------------- " 
81           << "Bad shot! Lengthes are different : " 
82           << dataSize << " # " << dataSize2
83           << " for file : " << fileName << std::endl;
84
85      return 0;
86   }
87   if (int res=memcmp(imageData,imageData2,dataSize) !=0) {
88      std::cout << " ----------------------------------------- " 
89           << "Bad shot! Pixels are different : " 
90           << " for file : " << fileName << std::endl;
91      std::cout << "memcmp(imageData,imageData2,dataSize) = " << res << std::endl;
92      return 1;
93   }
94   
95   //If we reach here everything is fine, return 0 then:
96   return 0;
97 }
98