]> Creatis software - gdcm.git/blob - Example/ReWrite.cxx
* Some classes inherit now from gdcm::RefCounter
[gdcm.git] / Example / ReWrite.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ReWrite.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/25 14:52:27 $
7   Version:   $Revision: 1.15 $
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 #include "gdcmDebug.h"
21
22 #include "gdcmArgMgr.h"
23
24 #include <iostream>
25
26 int main(int argc, char *argv[])
27 {
28    START_USAGE(usage)
29    " \n ReWrite :\n",
30    " Re write a full gdcm-readable Dicom image                              ",
31    "     (usefull when the file header is not very straight).               ",
32    "                                                                        ",
33    " usage: ReWrite filein=inputFileName fileout=anonymizedFileName         ", 
34    "       [mode=write mode] [noshadow] [noseq][debug]                      ", 
35    "                                                                        ",
36    "        mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels)    ",
37    "        noshadowseq: user doesn't want to load Private Sequences        ",
38    "        noshadow : user doesn't want to load Private groups (odd number)",
39    "        noseq    : user doesn't want to load Sequences                  ",
40    "        rgb      : user wants to tranform LUT (if any) to RGB pixels    ",
41    "        debug    : user wants to run the program in 'debug mode'        ",
42    FINISH_USAGE
43
44    // ----- Initialize Arguments Manager ------   
45    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
46   
47    if (argc == 1 || am->ArgMgrDefined("usage")) 
48    {
49       am->ArgMgrUsage(usage); // Display 'usage'
50       delete am;
51       return 0;
52    }
53    char *fileName = am->ArgMgrWantString("filein",usage);
54    if ( fileName == NULL )
55    {
56       std::cout << "'filein= ...' is mandatory" << std::endl;
57       delete am;
58       return 0;
59    }
60
61    char *outputFileName = am->ArgMgrWantString("fileout",usage);
62    if ( outputFileName == NULL )
63    {
64       std::cout << "'fileout= ...' is mandatory" << std::endl;
65       delete am;
66       return 0;
67    }
68
69    char *mode = am->ArgMgrGetString("mode",(char *)"X");
70
71    int loadMode = gdcm::LD_ALL;
72    if ( am->ArgMgrDefined("noshadowseq") )
73       loadMode |= gdcm::LD_NOSHADOWSEQ;
74    else 
75    {
76    if ( am->ArgMgrDefined("noshadow") )
77          loadMode |= gdcm::LD_NOSHADOW;
78       if ( am->ArgMgrDefined("noseq") )
79          loadMode |= gdcm::LD_NOSEQ;
80    }
81
82    bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
83
84    if (am->ArgMgrDefined("debug"))
85       gdcm::Debug::DebugOn();
86  
87    // if unused Params we give up
88    if ( am->ArgMgrPrintUnusedLabels() )
89    { 
90       am->ArgMgrUsage(usage);
91       delete am;
92       return 0;
93    }
94
95    delete am;  // we don't need Argument Manager any longer
96
97    // ----------- End Arguments Manager ---------
98
99    gdcm::File *f = gdcm::File::New();
100    f->SetLoadMode( loadMode );
101    f->SetFileName( fileName );
102    bool res = f->Load();  
103    if ( !res )
104    {
105       f->Delete();
106       return 0;
107    }
108   
109    if (!f->IsReadable())
110    {
111        std::cerr << "Sorry, not a Readable DICOM / ACR File"  <<std::endl;
112        f->Delete();
113        return 0;
114    }
115    
116    gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
117    void *imageData; 
118    int dataSize;
119   
120    if (rgb)
121    {
122       dataSize  = fh->GetImageDataSize();
123       imageData = fh->GetImageData(); // somewhat important... can't remember
124       fh->SetWriteModeToRGB();
125    }
126    else
127    {
128       dataSize  = fh->GetImageDataRawSize();
129       imageData = fh->GetImageDataRaw();// somewhat important... can't remember
130       fh->SetWriteModeToRaw();
131    }
132
133    if ( imageData == 0 ) // to avoid warning
134    {
135       std::cout << "Was unable to read pixels " << std::endl;
136    }
137    std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
138    int nX,nY,nZ,sPP,planarConfig;
139    std::string pixelType, transferSyntaxName;
140    nX=f->GetXSize();
141    nY=f->GetYSize();
142    nZ=f->GetZSize();
143    std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
144
145    pixelType    = f->GetPixelType();
146    sPP          = f->GetSamplesPerPixel();
147    planarConfig = f->GetPlanarConfiguration();
148    
149    std::cout << " pixelType="           << pixelType 
150              << " SampleserPixel="      << sPP
151              << " PlanarConfiguration=" << planarConfig 
152              << " PhotometricInterpretation=" 
153              << f->GetEntryString(0x0028,0x0004) 
154              << std::endl;
155
156    int numberOfScalarComponents=f->GetNumberOfScalarComponents();
157    std::cout << "NumberOfScalarComponents " << numberOfScalarComponents 
158              <<std::endl;
159    transferSyntaxName = f->GetTransferSyntaxName();
160    std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
161              << std::endl;
162
163    switch (mode[0])
164    {
165       case 'A' :
166       case 'a' :
167       // Writting an ACR file
168       // from a full gdcm readable File
169          std::cout << "WriteACR" << std::endl;
170          fh->WriteAcr(outputFileName);
171          break;
172
173       case 'D' : // Not documented in the 'usage', because the method 
174       case 'd' : //                             is known to be bugged. 
175       // Writting a DICOM Implicit VR file
176       // from a full gdcm readable File
177          std::cout << "WriteDCM Implicit VR" << std::endl;
178          fh->WriteDcmImplVR(outputFileName);
179          break;
180
181       case 'X' :
182       case 'x' :
183       // writting a DICOM Explicit VR 
184       // from a full gdcm readable File
185          std::cout << "WriteDCM Explicit VR" << std::endl;
186          // fh->WriteDcmExplVR(outputFileName);
187          // Try this one :
188          fh->SetWriteTypeToDcmExplVR();
189          fh->Write(outputFileName);
190          break;
191
192       case 'R' :
193       case 'r' :
194       //  Writting a Raw File, 
195          std::cout << "WriteRaw" << std::endl;
196          fh->WriteRawData(outputFileName);
197          break;
198  
199  // Just for fun :
200  // Write a 'Video inverse' version of the file.
201  // *Not* described, on purpose,  in the USAGE  
202       case 'V' :
203       case 'v' :
204          if ( fh->GetFile()->GetBitsAllocated() == 8)
205          {
206             std::cout << "videoinv for 8 bits" << std::endl;
207             for (int i=0; i<dataSize; i++) 
208             {
209                ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
210             }
211          }
212          else
213          {
214             std::cout << "videoinv for 16 bits" << std::endl;    
215             for (int i=0; i<dataSize/2; i++) 
216             {
217                ((uint16_t*)imageData)[i] =  65535 - ((uint16_t*)imageData)[i];
218             }
219          }
220          std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
221          fh->WriteDcmExplVR(outputFileName);
222          break;
223    }
224
225    f->Delete();
226    fh->Delete();
227    return 0;
228 }
229