]> Creatis software - gdcm.git/blob - Example/WriteRead.cxx
Add the 'WriteRead' example
[gdcm.git] / Example / WriteRead.cxx
1 #include <iostream>
2 #include "gdcm.h"
3 #include "gdcmHeader.h"
4 #include "gdcmDocument.h"
5
6 #include <stdio.h>
7
8 int main(int argc, char* argv[])
9 {  
10    std::string toto;
11    char zozo[200];
12
13    gdcmHeader* e1, *e2;
14    gdcmFile  * f1, *f2;
15
16    void* imageData, *imageData2;
17    int dataSize, dataSize2;
18      
19    if( argc < 2 )
20     {
21     std::cerr << "Usage " << argv[0] << " image.dcm" << std::endl;
22     return 1;
23     }
24
25    toto = argv[1];
26
27 // --------------------- we read the input image
28
29
30    e1 = new gdcmHeader(toto, false, true);
31    if (!e1->IsReadable()) {
32        std::cerr << "Sorry, " << toto <<"  not a Readable DICOM / ACR File"
33                  <<std::endl;
34        return 0;
35    }
36    
37    f1 = new gdcmFile(e1);
38    imageData= f1->GetImageData();
39    dataSize = f1->GetImageDataSize();
40
41 // --------------------- we write it as an Explicit VR DICOM file
42
43       sprintf(zozo, "temp.XDCM" );
44       std::cout << "WriteDCM Explicit VR" << std::endl;
45       f1->WriteDcmExplVR(zozo);
46
47 // --------------------- we read the written image
48       
49    e2 = new gdcmHeader(zozo, false, true);
50    if (!e2->IsReadable()) {
51        std::cerr << "Sorry, " << zozo << " not a Readable DICOM / ACR File"  
52                  <<std::endl;
53        return 0;
54    }
55    f2 = new gdcmFile(e2);
56    imageData2= f2->GetImageData();
57    dataSize2 = f2->GetImageDataSize();
58
59 // --------------------- we compare the pixel areas
60
61   if (dataSize != dataSize2) {
62      std::cout << " ----------------------------------------- " 
63           << "Bad shot! Lengthes are different : " 
64           << dataSize << " # " << dataSize2
65           << " for file : " << toto << std::endl;
66
67      return 1;
68   }
69   if (int res=memcmp(imageData,imageData2,dataSize) !=0) {
70      std::cout << " ----------------------------------------- " 
71           << "Bad shot! Pixels are different : " 
72           << " for file : " << toto << std::endl;
73      std::cout << "memcmp(imageData,imageData2,dataSize) = " << res << std::endl;
74      return 1;
75   }
76   
77   //If we reach here everythin is fine, return 0 then:
78   return 0;
79 }
80