1 /*=========================================================================
4 Module: $RCSfile: ReWrite.cxx,v $
6 Date: $Date: 2006/04/19 10:23:56 $
7 Version: $Revision: 1.20 $
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.
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.
17 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmDebug.h"
22 #include "gdcmArgMgr.h"
26 int main(int argc, char *argv[])
30 " Re write a full gdcm-readable Dicom image ",
31 " (usefull when the file header is not very straight). ",
33 " usage: ReWrite filein=inputFileName fileout=outputFileName ",
34 " [mode=write mode] [noshadow] [noseq][debug] ",
35 " [rubout=xBegin,xEnd,yBegin,yEnd [ruboutvalue=n (<255)] ] ",
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' ",
45 // ----- Initialize Arguments Manager ------
46 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
48 if (argc == 1 || am->ArgMgrDefined("usage"))
50 am->ArgMgrUsage(usage); // Display 'usage'
54 char *fileName = am->ArgMgrWantString("filein",usage);
55 if ( fileName == NULL )
57 std::cout << "'filein= ...' is mandatory" << std::endl;
62 char *outputFileName = am->ArgMgrWantString("fileout",usage);
63 if ( outputFileName == NULL )
65 std::cout << "'fileout= ...' is mandatory" << std::endl;
70 const char *mode = am->ArgMgrGetString("mode","X");
72 int loadMode = gdcm::LD_ALL;
73 if ( am->ArgMgrDefined("noshadowseq") )
74 loadMode |= gdcm::LD_NOSHADOWSEQ;
77 if ( am->ArgMgrDefined("noshadow") )
78 loadMode |= gdcm::LD_NOSHADOW;
79 if ( am->ArgMgrDefined("noseq") )
80 loadMode |= gdcm::LD_NOSEQ;
83 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
85 if (am->ArgMgrDefined("debug"))
86 gdcm::Debug::DebugOn();
92 if (am->ArgMgrDefined("rubout"))
95 boundVal = am->ArgMgrGetListOfInt("rubout", &nbBound);
99 std::cout << "Illegal number of 'rubout' boundary values (expected : 4, found:"
100 << nbBound << "); 'rubout' ignored" << std::endl;
104 ruboutVal = am->ArgMgrGetInt("ruboutvalue", 0);
109 // if unused Params we give up
110 if ( am->ArgMgrPrintUnusedLabels() )
112 am->ArgMgrUsage(usage);
117 delete am; // we don't need Argument Manager any longer
119 // ----------- End Arguments Manager ---------
121 gdcm::File *f = gdcm::File::New();
122 f->SetLoadMode( loadMode );
123 f->SetFileName( fileName );
124 bool res = f->Load();
131 if (!f->IsReadable())
133 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
138 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
144 dataSize = fh->GetImageDataSize();
145 imageData = fh->GetImageData(); // somewhat important... can't remember
146 fh->SetWriteModeToRGB();
150 dataSize = fh->GetImageDataRawSize();
151 imageData = fh->GetImageDataRaw();// somewhat important... can't remember
152 fh->SetWriteModeToRaw();
155 if ( imageData == 0 ) // to avoid warning
157 std::cout << "Was unable to read pixels " << std::endl;
159 std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
160 int nX,nY,nZ,sPP,planarConfig;
161 std::string pixelType, transferSyntaxName;
165 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
167 pixelType = f->GetPixelType();
168 sPP = f->GetSamplesPerPixel();
169 planarConfig = f->GetPlanarConfiguration();
171 std::cout << " pixelType=" << pixelType
172 << " SampleserPixel=" << sPP
173 << " PlanarConfiguration=" << planarConfig
174 << " PhotometricInterpretation="
175 << f->GetEntryString(0x0028,0x0004)
178 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
179 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
181 transferSyntaxName = f->GetTransferSyntaxName();
182 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
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);
193 if (boundVal[0]<0 || boundVal[0]>nX)
195 std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl;
198 if (boundVal[1]<0 || boundVal[1]>nX)
200 std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl;
203 if (boundVal[0] > boundVal[1])
205 std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl;
208 if (boundVal[2]<0 || boundVal[2]>nY)
210 std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl;
213 if (boundVal[3]<0 || boundVal[3]>nY)
215 std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl;
218 if (boundVal[2] > boundVal[3])
220 std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl;
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];
230 for(int rbl=boundVal[2]; rbl<boundVal[3];rbl++)
232 memset((char *)imageData+offsetToBeginOfRubout, ruboutVal, lengthToRubout);
233 offsetToBeginOfRubout += lineLength;
242 // Writting an ACR file
243 // from a full gdcm readable File
244 std::cout << "WriteACR" << std::endl;
245 fh->WriteAcr(outputFileName);
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);
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);
263 fh->SetWriteTypeToDcmExplVR();
264 fh->Write(outputFileName);
269 // Writting a Raw File,
270 std::cout << "WriteRaw" << std::endl;
271 fh->WriteRawData(outputFileName);
275 // Write a 'Video inverse' version of the file.
276 // *Not* described, on purpose, in the USAGE
279 if ( fh->GetFile()->GetBitsAllocated() == 8)
281 std::cout << "videoinv for 8 bits" << std::endl;
282 for (int i=0; i<dataSize; i++)
284 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
289 std::cout << "videoinv for 16 bits" << std::endl;
290 for (int i=0; i<dataSize/2; i++)
292 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
295 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
296 fh->WriteDcmExplVR(outputFileName);