]> Creatis software - gdcm.git/blob - Example/ReWrite.cxx
ReWrite now allows user to 'rubout' a part of the image
[gdcm.git] / Example / ReWrite.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: ReWrite.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/04/19 10:23:56 $
7   Version:   $Revision: 1.20 $
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    "       [rubout=xBegin,xEnd,yBegin,yEnd [ruboutvalue=n (<255)] ]         ",
36    "                                                                        ",
37    "        mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels)    ",
38    "        noshadowseq: user doesn't want to load Private Sequences        ",
39    "        noshadow : user doesn't want to load Private groups (odd number)",
40    "        noseq    : user doesn't want to load Sequences                  ",
41    "        rgb      : user wants to tranform LUT (if any) to RGB pixels    ",
42    "        debug    : user wants to run the program in 'debug mode'        ",
43    FINISH_USAGE
44
45    // ----- Initialize Arguments Manager ------   
46    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
47   
48    if (argc == 1 || am->ArgMgrDefined("usage")) 
49    {
50       am->ArgMgrUsage(usage); // Display 'usage'
51       delete am;
52       return 0;
53    }
54    char *fileName = am->ArgMgrWantString("filein",usage);
55    if ( fileName == NULL )
56    {
57       std::cout << "'filein= ...' is mandatory" << std::endl;
58       delete am;
59       return 0;
60    }
61
62    char *outputFileName = am->ArgMgrWantString("fileout",usage);
63    if ( outputFileName == NULL )
64    {
65       std::cout << "'fileout= ...' is mandatory" << std::endl;
66       delete am;
67       return 0;
68    }
69
70    const char *mode = am->ArgMgrGetString("mode","X");
71
72    int loadMode = gdcm::LD_ALL;
73    if ( am->ArgMgrDefined("noshadowseq") )
74       loadMode |= gdcm::LD_NOSHADOWSEQ;
75    else 
76    {
77    if ( am->ArgMgrDefined("noshadow") )
78          loadMode |= gdcm::LD_NOSHADOW;
79       if ( am->ArgMgrDefined("noseq") )
80          loadMode |= gdcm::LD_NOSEQ;
81    }
82
83    bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
84
85    if (am->ArgMgrDefined("debug"))
86       gdcm::Debug::DebugOn();
87       
88    bool fail = false;
89    int *boundVal;
90    int ruboutVal;
91    bool rubout = false; 
92    if (am->ArgMgrDefined("rubout"))
93    {
94       int nbBound;
95       boundVal = am->ArgMgrGetListOfInt("rubout", &nbBound);
96
97       if (nbBound !=4)
98       {
99          std::cout << "Illegal number of 'rubout' boundary values (expected : 4, found:" 
100                    << nbBound << "); 'rubout' ignored" << std::endl;
101          fail = true;
102       }
103             
104       ruboutVal = am->ArgMgrGetInt("ruboutvalue", 0);
105       rubout = true;   
106    }
107
108  
109    // if unused Params we give up
110    if ( am->ArgMgrPrintUnusedLabels() )
111    { 
112       am->ArgMgrUsage(usage);
113       delete am;
114       return 0;
115    }
116
117    delete am;  // we don't need Argument Manager any longer
118
119    // ----------- End Arguments Manager ---------
120
121    gdcm::File *f = gdcm::File::New();
122    f->SetLoadMode( loadMode );
123    f->SetFileName( fileName );
124    bool res = f->Load();  
125    if ( !res )
126    {
127       f->Delete();
128       return 0;
129    }
130   
131    if (!f->IsReadable())
132    {
133        std::cerr << "Sorry, not a Readable DICOM / ACR File"  <<std::endl;
134        f->Delete();
135        return 0;
136    }
137    
138    gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
139    void *imageData; 
140    int dataSize;
141   
142    if (rgb)
143    {
144       dataSize  = fh->GetImageDataSize();
145       imageData = fh->GetImageData(); // somewhat important... can't remember
146       fh->SetWriteModeToRGB();
147    }
148    else
149    {
150       dataSize  = fh->GetImageDataRawSize();
151       imageData = fh->GetImageDataRaw();// somewhat important... can't remember
152       fh->SetWriteModeToRaw();
153    }
154
155    if ( imageData == 0 ) // to avoid warning
156    {
157       std::cout << "Was unable to read pixels " << std::endl;
158    }
159    std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
160    int nX,nY,nZ,sPP,planarConfig;
161    std::string pixelType, transferSyntaxName;
162    nX=f->GetXSize();
163    nY=f->GetYSize();
164    nZ=f->GetZSize();
165    std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
166
167    pixelType    = f->GetPixelType();
168    sPP          = f->GetSamplesPerPixel();
169    planarConfig = f->GetPlanarConfiguration();
170    
171    std::cout << " pixelType="           << pixelType 
172              << " SampleserPixel="      << sPP
173              << " PlanarConfiguration=" << planarConfig 
174              << " PhotometricInterpretation=" 
175              << f->GetEntryString(0x0028,0x0004) 
176              << std::endl;
177
178    int numberOfScalarComponents=f->GetNumberOfScalarComponents();
179    std::cout << "NumberOfScalarComponents " << numberOfScalarComponents 
180              <<std::endl;
181    transferSyntaxName = f->GetTransferSyntaxName();
182    std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" 
183              << std::endl;
184
185    // Since we just ReWrite the image, we know no modification 
186    // was performed on the pixels.
187    // We don't want this image appears as a 'Secondary Captured image'
188    fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE);
189    
190
191    if (rubout)
192    {     
193       if (boundVal[0]<0 || boundVal[0]>nX)
194       { 
195          std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl;
196          fail = true;      
197       }
198       if (boundVal[1]<0 || boundVal[1]>nX)
199       { 
200          std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl;
201          fail = true;      
202       }
203       if (boundVal[0] > boundVal[1])
204       { 
205          std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl;
206          fail = true;      
207       }       
208       if (boundVal[2]<0 || boundVal[2]>nY)
209       { 
210          std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl;
211          fail = true;      
212       }
213       if (boundVal[3]<0 || boundVal[3]>nY)
214       { 
215          std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl;
216          fail = true;      
217       }
218       if (boundVal[2] > boundVal[3])
219       { 
220          std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl;
221          fail = true;      
222       }  
223       if (!fail)
224       {
225          int pixelLength = f->GetBitsAllocated()/8;
226          int lineLength = nX * sPP * pixelLength;
227          size_t lengthToRubout = (boundVal[1]-boundVal[0])*sPP*pixelLength;
228          int offsetToBeginOfRubout = boundVal[0]*sPP*pixelLength+lineLength*boundVal[2];
229       
230          for(int rbl=boundVal[2]; rbl<boundVal[3];rbl++)
231          {
232             memset((char *)imageData+offsetToBeginOfRubout, ruboutVal, lengthToRubout);
233             offsetToBeginOfRubout += lineLength; 
234          }
235       }   
236    } 
237
238    switch (mode[0])
239    {
240       case 'A' :
241       case 'a' :
242       // Writting an ACR file
243       // from a full gdcm readable File
244          std::cout << "WriteACR" << std::endl;
245          fh->WriteAcr(outputFileName);
246          break;
247
248       case 'D' : // Not documented in the 'usage', because the method 
249       case 'd' : //                             is known to be bugged. 
250       // Writting a DICOM Implicit VR file
251       // from a full gdcm readable File
252          std::cout << "WriteDCM Implicit VR" << std::endl;
253          fh->WriteDcmImplVR(outputFileName);
254          break;
255
256       case 'X' :
257       case 'x' :
258       // writting a DICOM Explicit VR 
259       // from a full gdcm readable File
260          std::cout << "WriteDCM Explicit VR" << std::endl;
261          // fh->WriteDcmExplVR(outputFileName);
262          // Try this one :
263          fh->SetWriteTypeToDcmExplVR();
264          fh->Write(outputFileName);
265          break;
266
267       case 'R' :
268       case 'r' :
269       //  Writting a Raw File, 
270          std::cout << "WriteRaw" << std::endl;
271          fh->WriteRawData(outputFileName);
272          break;
273  
274  // Just for fun :
275  // Write a 'Video inverse' version of the file.
276  // *Not* described, on purpose,  in the USAGE  
277       case 'V' :
278       case 'v' :
279          if ( fh->GetFile()->GetBitsAllocated() == 8)
280          {
281             std::cout << "videoinv for 8 bits" << std::endl;
282             for (int i=0; i<dataSize; i++) 
283             {
284                ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
285             }
286          }
287          else
288          {
289             std::cout << "videoinv for 16 bits" << std::endl;    
290             for (int i=0; i<dataSize/2; i++) 
291             {
292                ((uint16_t*)imageData)[i] =  65535 - ((uint16_t*)imageData)[i];
293             }
294          }
295          std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
296          fh->WriteDcmExplVR(outputFileName);
297          break;
298    }
299
300    f->Delete();
301    fh->Delete();
302    return 0;
303 }
304