]> Creatis software - gdcm.git/blob - Example/ReWrite.cxx
Fix some comments
[gdcm.git] / Example / ReWrite.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ReWrite.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/03/17 14:36:37 $
7   Version:   $Revision: 1.19 $
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=outputFileName             ", 
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    const char *mode = am->ArgMgrGetString("mode","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    // Since we just ReWrite the image, we know no modification 
164    // was performed on the pixels.
165    // We don't want this image appears as a 'Secondary Captured image'
166    fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
167
168    switch (mode[0])
169    {
170       case 'A' :
171       case 'a' :
172       // Writting an ACR file
173       // from a full gdcm readable File
174          std::cout << "WriteACR" << std::endl;
175          fh->WriteAcr(outputFileName);
176          break;
177
178       case 'D' : // Not documented in the 'usage', because the method 
179       case 'd' : //                             is known to be bugged. 
180       // Writting a DICOM Implicit VR file
181       // from a full gdcm readable File
182          std::cout << "WriteDCM Implicit VR" << std::endl;
183          fh->WriteDcmImplVR(outputFileName);
184          break;
185
186       case 'X' :
187       case 'x' :
188       // writting a DICOM Explicit VR 
189       // from a full gdcm readable File
190          std::cout << "WriteDCM Explicit VR" << std::endl;
191          // fh->WriteDcmExplVR(outputFileName);
192          // Try this one :
193          fh->SetWriteTypeToDcmExplVR();
194          fh->Write(outputFileName);
195          break;
196
197       case 'R' :
198       case 'r' :
199       //  Writting a Raw File, 
200          std::cout << "WriteRaw" << std::endl;
201          fh->WriteRawData(outputFileName);
202          break;
203  
204  // Just for fun :
205  // Write a 'Video inverse' version of the file.
206  // *Not* described, on purpose,  in the USAGE  
207       case 'V' :
208       case 'v' :
209          if ( fh->GetFile()->GetBitsAllocated() == 8)
210          {
211             std::cout << "videoinv for 8 bits" << std::endl;
212             for (int i=0; i<dataSize; i++) 
213             {
214                ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
215             }
216          }
217          else
218          {
219             std::cout << "videoinv for 16 bits" << std::endl;    
220             for (int i=0; i<dataSize/2; i++) 
221             {
222                ((uint16_t*)imageData)[i] =  65535 - ((uint16_t*)imageData)[i];
223             }
224          }
225          std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
226          fh->WriteDcmExplVR(outputFileName);
227          break;
228    }
229
230    f->Delete();
231    fh->Delete();
232    return 0;
233 }
234