]> Creatis software - gdcm.git/blob - Example/TestWrite.cxx
2005-01-11 Jean-Pierre Roux <jpr@creatis.univ-lyon1.fr>
[gdcm.git] / Example / TestWrite.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWrite.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/11 11:37:13 $
7   Version:   $Revision: 1.14 $
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 "gdcmHeader.h"
19 #include "gdcmFile.h"
20
21 #include <iostream>
22
23 int main(int argc, char* argv[])
24 {  
25    std::string zozo;
26
27    gdcm::Header* e1;
28    gdcm::File  * f1;
29
30    //gdcmDocument * d;  //not used
31    void* imageData;
32    int dataSize;
33
34    if (argc < 3) {
35          std::cerr << "usage: " << std::endl 
36                    << argv[0] << " OriginalFileName writtingMode "
37                 << std::endl 
38                    << "(a : ACR, produces a file named OriginalFileName.ACR"
39                    << " x : DICOM Explicit VR, produces a file named OriginalFileName.XDCM"
40                    << " r : RAW, produces a file named OriginalFileName.RAW"
41                    << " v : explicit VR + computes the video inv image --> OriginalFileName.VDCM"
42                 << std::endl;
43
44          return 0;
45    }
46 /*
47    if (0) {  // Just to keep the code for further use
48       std::cout <<std::endl << "-------- Test gdcmHeader ------" <<std::endl;
49       e1 = new gdcmHeaderHelper(argv[1]);
50       if (!f1->GetHeader()->IsReadable()) {
51          std::cout << "Sorry, not a DICOM / ACR File"  <<std::endl;
52          exit(0);
53       }
54       std::cout << std::endl << "----------------------> after new gdcmHeader"
55                 << std::endl;
56       e1->PrintEntry();
57       std::cout <<std::endl <<"---------------------------------------" 
58                 <<std::endl;
59    }
60 */
61
62    std::cout << std::endl
63              << "--------------------- file :" << argv[1] 
64              << std::endl;
65      
66    std::string toto = argv[1]; 
67    std::string mode = argv[2];
68
69    e1 = new gdcm::Header( toto.c_str() );
70    if (!e1->IsReadable())
71    {
72        std::cerr << "Sorry, not a Readable DICOM / ACR File"  <<std::endl;
73        return 0;
74    }
75   // e1->Print(); 
76    
77    f1 = new gdcm::File(e1);
78 // ---     
79
80    dataSize = f1->GetImageDataSize();
81    std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
82    int nX,nY,nZ,sPP,planarConfig;
83    std::string pixelType, transferSyntaxName;
84    nX=e1->GetXSize();
85    nY=e1->GetYSize();
86    nZ=e1->GetZSize();
87    std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
88
89    pixelType    = e1->GetPixelType();
90    sPP          = e1->GetSamplesPerPixel();
91    planarConfig = e1->GetPlanarConfiguration();
92    
93    std::cout << " pixelType="           << pixelType 
94              << " SampleserPixel="      << sPP
95              << " PlanarConfiguration=" << planarConfig 
96              << " PhotometricInterpretation=" 
97                                 << e1->GetEntry(0x0028,0x0004) 
98              << std::endl;
99
100    int numberOfScalarComponents=e1->GetNumberOfScalarComponents();
101    std::cout << "NumberOfScalarComponents " << numberOfScalarComponents <<std::endl;
102    transferSyntaxName = e1->GetTransferSyntaxName();
103    std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" << std::endl;
104    
105 /*   if (  transferSyntaxName != "Implicit VR - Little Endian"
106       && transferSyntaxName != "Explicit VR - Little Endian"     
107       && transferSyntaxName != "Deflated Explicit VR - Little Endian"      
108       && transferSyntaxName != "Explicit VR - Big Endian"
109       && transferSyntaxName != "Uncompressed ACR-NEMA"     ) {
110       std::cout << std::endl << "==========================================="
111                 << std::endl; 
112       f1->GetPixelReadConverter()->Print();
113       std::cout << std::endl << "==========================================="
114                 << std::endl; 
115    }*/
116    imageData= f1->GetImageData();
117
118    switch (mode[0])
119    {
120    case 'a' :
121             // ecriture d'un fichier ACR 
122             // à partir d'un dcmHeader correct.
123
124       zozo = toto + ".ACR";
125       std::cout << "WriteACR" << std::endl;
126       f1->WriteAcr(zozo);
127       break;
128
129    case 'd' :  // Not document in the 'usage', because the method is knowed to be bugged. 
130
131            // ecriture d'un fichier DICOM Implicit VR 
132            // à partir d'un dcmHeader correct.
133
134       zozo = toto + ".DCM";
135       std::cout << "WriteDCM Implicit VR" << std::endl;
136       f1->WriteDcmImplVR(zozo);
137       break;
138
139    case 'x' :
140               // ecriture d'un fichier DICOM Explicit VR 
141               // à partir d'un dcmHeader correct.
142
143       zozo = toto + ".XDCM";
144       std::cout << "WriteDCM Explicit VR" << std::endl;
145       f1->WriteDcmExplVR(zozo);
146       break;
147
148    case 'r' :
149              //  Ecriture d'un Raw File, a afficher avec 
150              // affim filein= dimx= dimy= nbit= signe=
151
152       zozo = toto + ".RAW";
153       std::cout << "WriteRaw" << std::endl;
154       f1->WriteRawData(zozo);
155       break;
156
157    case 'v' :
158
159      if ( f1->GetHeader()->GetBitsAllocated() == 8)
160      {
161         std::cout << "videoinv for 8 bits" << std::endl;
162         for (int i=0; i<dataSize; i++) 
163         {
164            ((uint8_t*)imageData)[i] += 127;
165         }
166      }
167      else
168      {
169         std::cout << "videoinv for 16 bits" << std::endl;    
170         for (int i=0; i<dataSize/2; i++) 
171         {
172            ((uint16_t*)imageData)[i] += 60000; //32767;
173         }
174      }
175      zozo = toto + ".VDCM";
176      std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
177      f1->WriteDcmExplVR(zozo);
178      break;
179
180    }
181   return 0;
182 }
183