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