1 /*=========================================================================
4 Module: $RCSfile: ReWrite.cxx,v $
6 Date: $Date: 2006/03/01 09:51:56 $
7 Version: $Revision: 1.18 $
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=anonymizedFileName ",
34 " [mode=write mode] [noshadow] [noseq][debug] ",
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' ",
44 // ----- Initialize Arguments Manager ------
45 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
47 if (argc == 1 || am->ArgMgrDefined("usage"))
49 am->ArgMgrUsage(usage); // Display 'usage'
53 char *fileName = am->ArgMgrWantString("filein",usage);
54 if ( fileName == NULL )
56 std::cout << "'filein= ...' is mandatory" << std::endl;
61 char *outputFileName = am->ArgMgrWantString("fileout",usage);
62 if ( outputFileName == NULL )
64 std::cout << "'fileout= ...' is mandatory" << std::endl;
69 const char *mode = am->ArgMgrGetString("mode","X");
71 int loadMode = gdcm::LD_ALL;
72 if ( am->ArgMgrDefined("noshadowseq") )
73 loadMode |= gdcm::LD_NOSHADOWSEQ;
76 if ( am->ArgMgrDefined("noshadow") )
77 loadMode |= gdcm::LD_NOSHADOW;
78 if ( am->ArgMgrDefined("noseq") )
79 loadMode |= gdcm::LD_NOSEQ;
82 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
84 if (am->ArgMgrDefined("debug"))
85 gdcm::Debug::DebugOn();
87 // if unused Params we give up
88 if ( am->ArgMgrPrintUnusedLabels() )
90 am->ArgMgrUsage(usage);
95 delete am; // we don't need Argument Manager any longer
97 // ----------- End Arguments Manager ---------
99 gdcm::File *f = gdcm::File::New();
100 f->SetLoadMode( loadMode );
101 f->SetFileName( fileName );
102 bool res = f->Load();
109 if (!f->IsReadable())
111 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
116 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
122 dataSize = fh->GetImageDataSize();
123 imageData = fh->GetImageData(); // somewhat important... can't remember
124 fh->SetWriteModeToRGB();
128 dataSize = fh->GetImageDataRawSize();
129 imageData = fh->GetImageDataRaw();// somewhat important... can't remember
130 fh->SetWriteModeToRaw();
133 if ( imageData == 0 ) // to avoid warning
135 std::cout << "Was unable to read pixels " << std::endl;
137 std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
138 int nX,nY,nZ,sPP,planarConfig;
139 std::string pixelType, transferSyntaxName;
143 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
145 pixelType = f->GetPixelType();
146 sPP = f->GetSamplesPerPixel();
147 planarConfig = f->GetPlanarConfiguration();
149 std::cout << " pixelType=" << pixelType
150 << " SampleserPixel=" << sPP
151 << " PlanarConfiguration=" << planarConfig
152 << " PhotometricInterpretation="
153 << f->GetEntryString(0x0028,0x0004)
156 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
157 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
159 transferSyntaxName = f->GetTransferSyntaxName();
160 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
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);
172 // Writting an ACR file
173 // from a full gdcm readable File
174 std::cout << "WriteACR" << std::endl;
175 fh->WriteAcr(outputFileName);
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);
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);
193 fh->SetWriteTypeToDcmExplVR();
194 fh->Write(outputFileName);
199 // Writting a Raw File,
200 std::cout << "WriteRaw" << std::endl;
201 fh->WriteRawData(outputFileName);
205 // Write a 'Video inverse' version of the file.
206 // *Not* described, on purpose, in the USAGE
209 if ( fh->GetFile()->GetBitsAllocated() == 8)
211 std::cout << "videoinv for 8 bits" << std::endl;
212 for (int i=0; i<dataSize; i++)
214 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
219 std::cout << "videoinv for 16 bits" << std::endl;
220 for (int i=0; i<dataSize/2; i++)
222 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
225 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
226 fh->WriteDcmExplVR(outputFileName);