1 /*=========================================================================
4 Module: $RCSfile: ReWriteExtended.cxx,v $
6 Date: $Date: 2007/05/23 14:18:04 $
7 Version: $Revision: 1.5 $
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 : developper wants to run the program in 'debug mode' ",
48 // ----- Initialize Arguments Manager ------
49 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::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_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
77 int loadMode = GDCM_NAME_SPACE::LD_ALL;
78 if ( am->ArgMgrDefined("noshadowseq") )
79 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
82 if ( am->ArgMgrDefined("noshadow") )
83 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
84 if ( am->ArgMgrDefined("noseq") )
85 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
88 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
90 if (am->ArgMgrDefined("debug"))
91 GDCM_NAME_SPACE::Debug::DebugOn();
96 // =======================================================================
101 if (am->ArgMgrDefined("roi"))
104 boundRoiVal = am->ArgMgrGetListOfInt("roi", &nbRoiBound);
108 std::cout << "Illegal number of 'ROI' boundary values (expected : 4, found:"
109 << nbRoiBound << "); 'ROI' ignored" << std::endl;
116 int beg = am->ArgMgrGetInt("firstFrame",0);
117 int end = am->ArgMgrGetInt("lastFrame",0);
118 // =======================================================================
121 // if unused Params we give up
122 if ( am->ArgMgrPrintUnusedLabels() )
124 am->ArgMgrUsage(usage);
129 delete am; // we don't need Argument Manager any longer
131 // ----------- End Arguments Manager ---------
133 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
134 f->SetLoadMode( loadMode );
135 f->SetFileName( fileName );
136 bool res = f->Load();
143 if (!f->IsReadable())
145 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
151 //std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
152 int nX,nY,nZ,sPP,planarConfig;
153 std::string pixelType, transferSyntaxName;
157 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
159 pixelType = f->GetPixelType();
160 sPP = f->GetSamplesPerPixel();
161 planarConfig = f->GetPlanarConfiguration();
163 std::cout << " pixelType=" << pixelType
164 << " SampleserPixel=" << sPP
165 << " PlanarConfiguration=" << planarConfig
166 << " PhotometricInterpretation="
167 << f->GetEntryString(0x0028,0x0004)
170 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
171 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
173 transferSyntaxName = f->GetTransferSyntaxName();
174 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
179 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
184 // =======================================================================
191 std::cout << " " << boundRoiVal[0] << " " << boundRoiVal[1] << " " << boundRoiVal[2] << " " <<
192 boundRoiVal[3] <<std::endl;
193 if (boundRoiVal[0]<0 || boundRoiVal[0]>=nX)
195 std::cout << "xBegin out of bounds; 'roi' ignored" << std::endl;
198 if (boundRoiVal[1]<0 || boundRoiVal[1]>=nX)
200 std::cout << "xEnd out of bounds; 'roi' ignored" << std::endl;
203 if (boundRoiVal[0] > boundRoiVal[1])
205 std::cout << "xBegin greater than xEnd; 'roi' ignored" << std::endl;
209 if (boundRoiVal[2]<0 || boundRoiVal[2]>=nY)
211 std::cout << "yBegin out of bounds; 'roi' ignored" << std::endl;
214 if (boundRoiVal[3]<0 || boundRoiVal[3]>=nY)
216 std::cout << "yEnd out of bounds; 'roi' ignored" << std::endl;
219 if (boundRoiVal[2] > boundRoiVal[3])
221 std::cout << "yBegin greater than yEnd; 'roi' ignored" << std::endl;
229 boundRoiVal = new int[4];
231 boundRoiVal[1] = nX-1;
233 boundRoiVal[3] = nY-1;
236 subImDimX = boundRoiVal[1]-boundRoiVal[0]+1;
237 subImDimY = boundRoiVal[3]-boundRoiVal[2]+1;
239 // =======================================================================
242 dataSize = fh->GetImageDataSize();
243 imageData = fh->GetImageData(); // somewhat important... can't remember
244 fh->SetWriteModeToRGB();
248 dataSize = fh->GetImageDataRawSize();
249 imageData = fh->GetImageDataRaw();// somewhat important... can't remember
250 fh->SetWriteModeToRaw();
253 if ( imageData == 0 ) // to avoid warning
255 std::cout << "Was unable to read pixels " << std::endl;
259 // We trust user. (just an example; *never* trust an user !)
260 fh->SetContentType((GDCM_NAME_SPACE::ImageContentType)filecontent);
262 /// \todo Here, give the detail of operations a 'decent' user should perform,
263 /// according to what *he* wants to do.
266 // an user shouldn't add images to a 'native' serie.
267 // He is allowed to create his own Serie, within a 'native' Study :
268 // if he wants to do so, he has to call GDCM_NAME_SPACE::Util::GetUniqueUID
269 // only once for a given image set, belonging to a single 'user Serie'
271 std::string SerieInstanceUID;
274 case GDCM_NAME_SPACE::USER_OWN_IMAGE :
275 SerieInstanceUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
276 f->SetEntryString(SerieInstanceUID,0x0020,0x000e);
279 case GDCM_NAME_SPACE::FILTERED_IMAGE :
280 /// \todo : to be finished!
283 case GDCM_NAME_SPACE::CREATED_IMAGE :
284 /// \todo : to be finished!
287 case GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE :
288 /// \todo : to be finished!
296 // Writting an ACR file
297 // from a full gdcm readable File
298 std::cout << "WriteACR" << std::endl;
299 fh->WriteAcr(outputFileName);
302 case 'D' : // Not documented in the 'usage', because the method
303 case 'd' : // is known to be bugged.
304 // Writting a DICOM Implicit VR file
305 // from a full gdcm readable File
306 std::cout << "WriteDCM Implicit VR" << std::endl;
307 fh->WriteDcmImplVR(outputFileName);
312 // writting a DICOM Explicit VR
313 // from a full gdcm readable File
314 std::cout << "WriteDCM Explicit VR" << std::endl;
315 // fh->WriteDcmExplVR(outputFileName);
317 fh->SetWriteTypeToDcmExplVR();
318 fh->Write(outputFileName);
323 // Writting a Raw File,
324 std::cout << "WriteRaw" << std::endl;
325 fh->WriteRawData(outputFileName);
329 // Write a 'Video inverse' version of the file.
330 // *Not* described, on purpose, in the USAGE
333 if ( fh->GetFile()->GetBitsAllocated() == 8)
335 std::cout << "videoinv for 8 bits" << std::endl;
336 for (int i=0; i<dataSize; i++)
338 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
343 std::cout << "videoinv for 16 bits" << std::endl;
344 for (int i=0; i<dataSize/2; i++)
346 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
349 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
350 fh->WriteDcmExplVR(outputFileName);