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