]> Creatis software - gdcm.git/blob - Example/WriteRead.cxx
Normalization
[gdcm.git] / Example / WriteRead.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: WriteRead.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/02 10:06:32 $
7   Version:   $Revision: 1.13 $
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
46    e1 = new gdcm::File( fileName );
47    if (!e1->IsReadable()) {
48        std::cerr << "Sorry, " << fileName <<"  not a Readable DICOM / ACR File"
49                  <<std::endl;
50        return 0;
51    }
52    
53    f1 = new gdcm::FileHelper(e1);
54    imageData= f1->GetImageData();
55    dataSize = f1->GetImageDataSize();
56
57 // --------------------- we write it as an Explicit VR DICOM file
58
59       fileNameToWrite = "temp.XDCM";
60       std::cout << "WriteDCM Explicit VR" << std::endl;
61       f1->WriteDcmExplVR(fileNameToWrite);
62
63 // --------------------- we read the written image
64       
65    e2 = new gdcm::File( fileNameToWrite );
66    if (!e2->IsReadable()) {
67        std::cerr << "Sorry, " << fileNameToWrite << " not a Readable DICOM / ACR File"  
68                  <<std::endl;
69        return 0;
70    }
71    f2 = new gdcm::FileHelper(e2);
72    imageData2= f2->GetImageData();
73    dataSize2 = f2->GetImageDataSize();
74
75 // --------------------- we compare the pixel areas
76
77   if (dataSize != dataSize2) {
78      std::cout << " ----------------------------------------- " 
79           << "Bad shot! Lengthes are different : " 
80           << dataSize << " # " << dataSize2
81           << " for file : " << fileName << std::endl;
82
83      return 0;
84   }
85   if (int res=memcmp(imageData,imageData2,dataSize) !=0) {
86      std::cout << " ----------------------------------------- " 
87           << "Bad shot! Pixels are different : " 
88           << " for file : " << fileName << std::endl;
89      std::cout << "memcmp(imageData,imageData2,dataSize) = " << res << std::endl;
90      return 1;
91   }
92   
93   //If we reach here everything is fine, return 0 then:
94   return 0;
95 }
96