]> Creatis software - gdcm.git/blob - Example/TestWrite.cxx
ENH: Add license to examples since they belong to gdcm
[gdcm.git] / Example / TestWrite.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWrite.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/16 04:26:18 $
7   Version:   $Revision: 1.9 $
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 <iostream>
19 #include <stdio.h>
20 #include "gdcm.h"
21
22 int main(int argc, char* argv[])
23 {  
24    std::string toto;
25    char zozo[200];
26
27
28    gdcm::Header* e1;
29    gdcm::File  * f1;
30
31    //gdcmDocument * d;  //not used
32    void* imageData;
33    int dataSize;
34
35    if (argc < 3) {
36          std::cerr << "usage: " << std::endl 
37                    << argv[0] << " OriginalFileName writtingMode "
38                 << std::endl 
39                    << "(a : ACR, produces a file named OriginalFileName.ACR"
40                    << " x : DICOM Explicit VR, produces a file named OriginalFileName.XDCM"
41                    << " r : RAW, produces a file named OriginalFileName.RAW"
42                    << " v : explicit VR + computes the video inv image --> OriginalFileName.VDCM"
43                 << std::endl;
44
45          return 0;
46    }
47 /*
48    if (0) {  // Just to keep the code for further use
49       std::cout <<std::endl << "-------- Test gdcmHeader ------" <<std::endl;
50       e1 = new gdcmHeaderHelper(argv[1]);
51       if (!f1->GetHeader()->IsReadable()) {
52          std::cout << "Sorry, not a DICOM / ACR File"  <<std::endl;
53          exit(0);
54       }
55       std::cout << std::endl << "----------------------> after new gdcmHeader"
56                 << std::endl;
57       e1->PrintEntry();
58       std::cout <<std::endl <<"---------------------------------------" 
59                 <<std::endl;
60    }
61 */
62
63    std::cout << std::endl
64              << "--------------------- file :" << argv[1] 
65              << std::endl;
66      
67    toto = argv[1]; 
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->GetEntryByNumber(0x0028,0x0004) 
98              << std::endl;
99
100    int numberOfScalarComponents=e1->GetNumberOfScalarComponents();
101    std::cout << "NumberOfScalarComponents " << numberOfScalarComponents <<std::endl;
102    transferSyntaxName = e1->GetTransfertSyntaxName();
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->GetPixelConverter()->Print();
113       std::cout << std::endl << "==========================================="
114                 << std::endl; 
115    }
116    imageData= f1->GetImageData();
117
118    switch (argv[2][0]) {
119    case 'a' :
120             // ecriture d'un fichier ACR 
121             // à partir d'un dcmHeader correct.
122
123       sprintf(zozo, "%s.ACR", toto.c_str());
124       printf ("WriteACR\n");
125       f1->WriteAcr(zozo);
126       break;
127
128    case 'd' :  // Not document in the 'usage', because the method is knowed to be bugged. 
129
130            // ecriture d'un fichier DICOM Implicit VR 
131            // à partir d'un dcmHeader correct.
132
133       sprintf(zozo, "%s.DCM", toto.c_str());
134       printf ("WriteDCM Implicit VR\n");
135       f1->WriteDcmImplVR(zozo);
136       break;
137
138    case 'x' :
139               // ecriture d'un fichier DICOM Explicit VR 
140               // à partir d'un dcmHeader correct.
141
142       sprintf(zozo, "%s.XDCM", toto.c_str());
143       std::cout << "WriteDCM Explicit VR" << std::endl;
144       f1->WriteDcmExplVR(zozo);
145       break;
146
147    case 'r' :
148              //  Ecriture d'un Raw File, a afficher avec 
149              // affim filein= dimx= dimy= nbit= signe=
150
151       sprintf(zozo, "%s.RAW", toto.c_str());
152       std::cout << "WriteRaw" << std::endl;
153       f1->WriteRawData(zozo);
154       break;
155
156    case 'v' :
157
158      if ( f1->GetHeader()->GetBitsAllocated() == 8)
159      {
160         std::cout << "videoinv for 8 bits" << std::endl;
161         for (int i=0; i<dataSize; i++) 
162         {
163            ((uint8_t*)imageData)[i] += 127;
164         }
165      }
166      else
167      {
168         std::cout << "videoinv for 16 bits" << std::endl;    
169         for (int i=0; i<dataSize/2; i++) 
170         {
171            ((uint16_t*)imageData)[i] += 60000; //32767;
172         }
173      }
174      sprintf(zozo, "%s.VDCM", toto.c_str());
175      printf ("WriteDCM Explicit VR + VideoInv\n");
176      f1->WriteDcmExplVR(zozo);
177      break;
178
179    }
180   return 0;
181 }
182