]> Creatis software - gdcm.git/blob - Example/WriteRead.cxx
ENH: apply dos2unix
[gdcm.git] / Example / WriteRead.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteRead.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:05 $
7   Version:   $Revision: 1.16 $
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_NAME_SPACE::File *e1;
28    GDCM_NAME_SPACE::File *e2;
29    GDCM_NAME_SPACE::FileHelper *f1;
30    GDCM_NAME_SPACE::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 = GDCM_NAME_SPACE::File::New( );
46    e1->SetFileName( fileName );
47    e1->Load();
48    if (!e1->IsReadable()) 
49    {
50       std::cerr << "Sorry, " << fileName <<"  not a Readable DICOM / ACR File"
51                 <<std::endl;
52       e1->Delete();
53       return 1;
54    }
55    
56    f1 = GDCM_NAME_SPACE::FileHelper::New(e1);
57    imageData= f1->GetImageData();
58    dataSize = f1->GetImageDataSize();
59
60 // --------------------- we write it as an Explicit VR DICOM file
61
62    fileNameToWrite = "temp.XDCM";
63    std::cout << "WriteDCM Explicit VR" << std::endl;
64    f1->WriteDcmExplVR(fileNameToWrite);
65
66 // --------------------- we read the written image
67    e2 = GDCM_NAME_SPACE::File::New( );
68    e2->SetFileName( fileNameToWrite );
69    e2->Load();
70    if (!e2->IsReadable()) 
71    {
72       std::cerr << "Sorry, " << fileNameToWrite << " not a Readable DICOM / ACR File"  
73                <<std::endl;
74       e1->Delete();
75       e2->Delete();
76       f1->Delete();
77       return 1;
78    }
79    f2 = GDCM_NAME_SPACE::FileHelper::New(e2);
80    imageData2= f2->GetImageData();
81    dataSize2 = f2->GetImageDataSize();
82
83 // --------------------- we compare the pixel areas
84
85    if (dataSize != dataSize2) 
86    {
87       std::cout << " ----------------------------------------- " 
88                 << "Bad shot! Lengthes are different : " 
89                 << dataSize << " # " << dataSize2
90                 << " for file : " << fileName << std::endl;
91
92       e1->Delete();
93       e2->Delete();
94       f1->Delete();
95       f2->Delete();
96       return 1;
97    }
98    if (int res=memcmp(imageData,imageData2,dataSize) !=0) 
99    {
100       std::cout << " ----------------------------------------- " 
101                 << "Bad shot! Pixels are different : " 
102                 << " for file : " << fileName << std::endl;
103       std::cout << "memcmp(imageData,imageData2,dataSize) = " << res << std::endl;
104       e1->Delete();
105       e2->Delete();
106       f1->Delete();
107       f2->Delete();
108       return 1;
109    }
110   
111    e1->Delete();
112    e2->Delete();
113    f1->Delete();
114    f2->Delete();
115    return 0;
116 }
117