]> Creatis software - gdcm.git/blob - Example/WriteRead.cxx
* src/jpeg/libijg12/jdhuff12.c:
[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
27    e1 = new gdcmHeader(toto, false, true);
28    if (!e1->IsReadable()) {
29        std::cerr << "Sorry, " << toto <<"  not a Readable DICOM / ACR File"
30                  <<std::endl;
31        return 0;
32    }
33    
34    f1 = new gdcmFile(e1);
35    imageData= f1->GetImageData();
36    dataSize = f1->GetImageDataSize();
37
38 // --------------------- we write it as an Explicit VR DICOM file
39
40       sprintf(zozo, "temp.XDCM" );
41       std::cout << "WriteDCM Explicit VR" << std::endl;
42       f1->WriteDcmExplVR(zozo);
43
44 // --------------------- we read the written image
45       
46    e2 = new gdcmHeader(zozo, false, true);
47    if (!e2->IsReadable()) {
48        std::cerr << "Sorry, " << zozo << " not a Readable DICOM / ACR File"  
49                  <<std::endl;
50        return 0;
51    }
52    f2 = new gdcmFile(e2);
53    imageData2= f2->GetImageData();
54    dataSize2 = f2->GetImageDataSize();
55
56 // --------------------- we compare the pixel areas
57
58   if (dataSize != dataSize2) {
59      std::cout << " ----------------------------------------- " 
60           << "Bad shot! Lengthes are different : " 
61           << dataSize << " # " << dataSize2
62           << " for file : " << toto << std::endl;
63
64      return 1;
65   }
66   if (int res=memcmp(imageData,imageData2,dataSize) !=0) {
67      std::cout << " ----------------------------------------- " 
68           << "Bad shot! Pixels are different : " 
69           << " for file : " << toto << std::endl;
70      std::cout << "memcmp(imageData,imageData2,dataSize) = " << res << std::endl;
71      return 1;
72   }
73   
74   //If we reach here everythin is fine, return 0 then:
75   return 0;
76 }
77