1 /*=========================================================================
4 Module: $RCSfile: ReWriteExtended.cxx,v $
6 Date: $Date: 2006/03/17 14:56:29 $
7 Version: $Revision: 1.2 $
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[])
29 " \n ReWriteExtended :\n",
30 " Re write a full gdcm-readable Dicom image using new features ",
31 " (DO NOT use right now; checking no achieved !). ",
33 " usage: ReWriteExtended filein=inputFileName fileout=outputFileName ",
34 " filecontent = 1 : USER_OWN_IMAGE ",
35 " = 2 : FILTERED_IMAGE ",
36 " = 3 : CREATED_IMAGE ",
37 " = 4 : UNMODIFIED_PIXELS_IMAGE ",
38 " [mode=write mode] [noshadow] [noseq][debug] ",
40 " mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels) ",
41 " noshadowseq: user doesn't want to load Private Sequences ",
42 " noshadow : user doesn't want to load Private groups (odd number)",
43 " noseq : user doesn't want to load Sequences ",
44 " rgb : user wants to transform LUT (if any) to RGB pixels ",
45 " debug : user wants to run the program in 'debug mode' ",
48 // ----- Initialize Arguments Manager ------
49 gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
51 if (argc == 1 || am->ArgMgrDefined("usage"))
53 am->ArgMgrUsage(usage); // Display 'usage'
57 char *fileName = am->ArgMgrWantString("filein",usage);
58 if ( fileName == NULL )
60 std::cout << "'filein= ...' is mandatory" << std::endl;
65 char *outputFileName = am->ArgMgrWantString("fileout",usage);
66 if ( outputFileName == NULL )
68 std::cout << "'fileout= ...' is mandatory" << std::endl;
73 const char *mode = am->ArgMgrGetString("mode","X");
75 int filecontent = am->ArgMgrGetInt("filecontent", gdcm::UNMODIFIED_PIXELS_IMAGE);
77 int loadMode = gdcm::LD_ALL;
78 if ( am->ArgMgrDefined("noshadowseq") )
79 loadMode |= gdcm::LD_NOSHADOWSEQ;
82 if ( am->ArgMgrDefined("noshadow") )
83 loadMode |= gdcm::LD_NOSHADOW;
84 if ( am->ArgMgrDefined("noseq") )
85 loadMode |= gdcm::LD_NOSEQ;
88 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
90 if (am->ArgMgrDefined("debug"))
91 gdcm::Debug::DebugOn();
93 // if unused Params we give up
94 if ( am->ArgMgrPrintUnusedLabels() )
96 am->ArgMgrUsage(usage);
101 delete am; // we don't need Argument Manager any longer
103 // ----------- End Arguments Manager ---------
105 gdcm::File *f = gdcm::File::New();
106 f->SetLoadMode( loadMode );
107 f->SetFileName( fileName );
108 bool res = f->Load();
115 if (!f->IsReadable())
117 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
122 gdcm::FileHelper *fh = gdcm::FileHelper::New(f);
128 dataSize = fh->GetImageDataSize();
129 imageData = fh->GetImageData(); // somewhat important... can't remember
130 fh->SetWriteModeToRGB();
134 dataSize = fh->GetImageDataRawSize();
135 imageData = fh->GetImageDataRaw();// somewhat important... can't remember
136 fh->SetWriteModeToRaw();
139 if ( imageData == 0 ) // to avoid warning
141 std::cout << "Was unable to read pixels " << std::endl;
143 std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
144 int nX,nY,nZ,sPP,planarConfig;
145 std::string pixelType, transferSyntaxName;
149 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
151 pixelType = f->GetPixelType();
152 sPP = f->GetSamplesPerPixel();
153 planarConfig = f->GetPlanarConfiguration();
155 std::cout << " pixelType=" << pixelType
156 << " SampleserPixel=" << sPP
157 << " PlanarConfiguration=" << planarConfig
158 << " PhotometricInterpretation="
159 << f->GetEntryString(0x0028,0x0004)
162 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
163 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
165 transferSyntaxName = f->GetTransferSyntaxName();
166 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
170 // We trust user. (just an example; *never* trust an user !)
171 fh->SetContentType((gdcm::ImageContentType)filecontent);
173 /// \todo Here, give the detail of operations a 'decent' user should perform,
174 /// according to what *he* wants to do.
177 // an user shouldn't add images to a 'native' serie.
178 // He is allowed to create his own Serie, within a 'native' Study :
179 // if he wants to do so, he has to call gdcm::Util::GetUniqueUID
180 // only once for a given image set, belonging to a single 'user Serie'
182 std::string SerieInstanceUID;
185 case gdcm::USER_OWN_IMAGE :
186 SerieInstanceUID = gdcm::Util::CreateUniqueUID();
187 f->SetEntryString(SerieInstanceUID,0x0020,0x000e);
190 case gdcm::FILTERED_IMAGE :
191 /// \todo : to be finished!
194 case gdcm::CREATED_IMAGE :
195 /// \todo : to be finished!
198 case gdcm::UNMODIFIED_PIXELS_IMAGE :
199 /// \todo : to be finished!
207 // Writting an ACR file
208 // from a full gdcm readable File
209 std::cout << "WriteACR" << std::endl;
210 fh->WriteAcr(outputFileName);
213 case 'D' : // Not documented in the 'usage', because the method
214 case 'd' : // is known to be bugged.
215 // Writting a DICOM Implicit VR file
216 // from a full gdcm readable File
217 std::cout << "WriteDCM Implicit VR" << std::endl;
218 fh->WriteDcmImplVR(outputFileName);
223 // writting a DICOM Explicit VR
224 // from a full gdcm readable File
225 std::cout << "WriteDCM Explicit VR" << std::endl;
226 // fh->WriteDcmExplVR(outputFileName);
228 fh->SetWriteTypeToDcmExplVR();
229 fh->Write(outputFileName);
234 // Writting a Raw File,
235 std::cout << "WriteRaw" << std::endl;
236 fh->WriteRawData(outputFileName);
240 // Write a 'Video inverse' version of the file.
241 // *Not* described, on purpose, in the USAGE
244 if ( fh->GetFile()->GetBitsAllocated() == 8)
246 std::cout << "videoinv for 8 bits" << std::endl;
247 for (int i=0; i<dataSize; i++)
249 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
254 std::cout << "videoinv for 16 bits" << std::endl;
255 for (int i=0; i<dataSize/2; i++)
257 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
260 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
261 fh->WriteDcmExplVR(outputFileName);