1 /*=========================================================================
4 Module: $RCSfile: ReWriteExtended.cxx,v $
6 Date: $Date: 2007/07/13 08:17:20 $
7 Version: $Revision: 1.6 $
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 " warning : developper wants to run the program in 'warning mode'",
46 " debug : developper wants to run the program in 'debug mode' ",
49 // ----- Initialize Arguments Manager ------
50 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
52 if (argc == 1 || am->ArgMgrDefined("usage"))
54 am->ArgMgrUsage(usage); // Display 'usage'
58 char *fileName = am->ArgMgrWantString("filein",usage);
59 if ( fileName == NULL )
61 std::cout << "'filein= ...' is mandatory" << std::endl;
66 char *outputFileName = am->ArgMgrWantString("fileout",usage);
67 if ( outputFileName == NULL )
69 std::cout << "'fileout= ...' is mandatory" << std::endl;
74 const char *mode = am->ArgMgrGetString("mode","X");
76 int filecontent = am->ArgMgrGetInt("filecontent", GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
78 int loadMode = GDCM_NAME_SPACE::LD_ALL;
79 if ( am->ArgMgrDefined("noshadowseq") )
80 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
83 if ( am->ArgMgrDefined("noshadow") )
84 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
85 if ( am->ArgMgrDefined("noseq") )
86 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
89 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
91 if (am->ArgMgrDefined("debug"))
92 GDCM_NAME_SPACE::Debug::DebugOn();
94 if (am->ArgMgrDefined("warning"))
95 GDCM_NAME_SPACE::Debug::WarningOn();
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",-1);
117 int end = am->ArgMgrGetInt("lastFrame",-1);
119 // if unused Params we give up
120 if ( am->ArgMgrPrintUnusedLabels() )
122 am->ArgMgrUsage(usage);
127 delete am; // we don't need Argument Manager any longer
129 // ----------- End Arguments Manager ---------
131 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
132 f->SetLoadMode( loadMode );
133 f->SetFileName( fileName );
134 bool res = f->Load();
141 if (!f->IsReadable())
143 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
148 //std::cout <<std::endl <<" dataSize " << dataSize << std::endl;
149 int nX,nY,nZ,sPP,planarConfig;
150 std::string pixelType, transferSyntaxName;
154 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
156 pixelType = f->GetPixelType();
157 sPP = f->GetSamplesPerPixel();
158 planarConfig = f->GetPlanarConfiguration();
160 std::cout << " pixelType=" << pixelType
161 << " SampleserPixel=" << sPP
162 << " PlanarConfiguration=" << planarConfig
163 << " PhotometricInterpretation="
164 << f->GetEntryString(0x0028,0x0004)
167 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
168 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
170 transferSyntaxName = f->GetTransferSyntaxName();
171 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
174 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
183 std::cout << " " << boundRoiVal[0] << " " << boundRoiVal[1] << " " << boundRoiVal[2] << " " <<
184 boundRoiVal[3] <<std::endl;
185 if (boundRoiVal[0]<0 || boundRoiVal[0]>=nX)
187 std::cout << "xBegin out of bounds; 'roi' ignored" << std::endl;
190 if (boundRoiVal[1]<0 || boundRoiVal[1]>=nX)
192 std::cout << "xEnd out of bounds; 'roi' ignored" << std::endl;
195 if (boundRoiVal[0] > boundRoiVal[1])
197 std::cout << "xBegin greater than xEnd; 'roi' ignored" << std::endl;
201 if (boundRoiVal[2]<0 || boundRoiVal[2]>=nY)
203 std::cout << "yBegin out of bounds; 'roi' ignored" << std::endl;
206 if (boundRoiVal[3]<0 || boundRoiVal[3]>=nY)
208 std::cout << "yEnd out of bounds; 'roi' ignored" << std::endl;
211 if (boundRoiVal[2] > boundRoiVal[3])
213 std::cout << "yBegin greater than yEnd; 'roi' ignored" << std::endl;
221 boundRoiVal = new int[4];
223 boundRoiVal[1] = nX-1;
225 boundRoiVal[3] = nY-1;
228 subImDimX = boundRoiVal[1]-boundRoiVal[0]+1;
229 subImDimY = boundRoiVal[3]-boundRoiVal[2]+1;
231 // =======================================================================
234 dataSize = fh->GetImageDataSize();
235 imageData = fh->GetImageData(); // somewhat important... can't remember
236 fh->SetWriteModeToRGB();
240 dataSize = fh->GetImageDataRawSize();
241 imageData = fh->GetImageDataRaw();// somewhat important... can't remember
242 fh->SetWriteModeToRaw();
245 if ( imageData == 0 ) // to avoid warning
247 std::cout << "Was unable to read pixels " << std::endl;
250 // We trust user. (just an example; *never* trust an user !)
251 fh->SetContentType((GDCM_NAME_SPACE::ImageContentType)filecontent);
253 /// \todo Here, give the detail of operations a 'decent' user should perform,
254 /// according to what *he* wants to do.
256 // an user shouldn't add images to a 'native' serie.
257 // He is allowed to create his own Serie, within a 'native' Study :
258 // if he wants to do so, he has to call GDCM_NAME_SPACE::Util::GetUniqueUID
259 // only once for a given image set, belonging to a single 'user Serie'
261 std::string SerieInstanceUID;
264 case GDCM_NAME_SPACE::USER_OWN_IMAGE :
265 SerieInstanceUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
266 f->SetEntryString(SerieInstanceUID,0x0020,0x000e);
269 case GDCM_NAME_SPACE::FILTERED_IMAGE :
270 /// \todo : to be finished!
273 case GDCM_NAME_SPACE::CREATED_IMAGE :
274 /// \todo : to be finished!
277 case GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE :
278 /// \todo : to be finished!
286 // Writting an ACR file
287 // from a full gdcm readable File
288 std::cout << "WriteACR" << std::endl;
289 fh->WriteAcr(outputFileName);
292 case 'D' : // Not documented in the 'usage', because the method
293 case 'd' : // is known to be bugged.
294 // Writting a DICOM Implicit VR file
295 // from a full gdcm readable File
296 std::cout << "WriteDCM Implicit VR" << std::endl;
297 fh->WriteDcmImplVR(outputFileName);
302 // writting a DICOM Explicit VR
303 // from a full gdcm readable File
304 std::cout << "WriteDCM Explicit VR" << std::endl;
305 // fh->WriteDcmExplVR(outputFileName);
307 fh->SetWriteTypeToDcmExplVR();
308 fh->Write(outputFileName);
313 // Writting a Raw File,
314 std::cout << "WriteRaw" << std::endl;
315 fh->WriteRawData(outputFileName);
319 // Write a 'Video inverse' version of the file.
320 // *Not* described, on purpose, in the USAGE
323 if ( fh->GetFile()->GetBitsAllocated() == 8)
325 std::cout << "videoinv for 8 bits" << std::endl;
326 for (int i=0; i<dataSize; i++)
328 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
333 std::cout << "videoinv for 16 bits" << std::endl;
334 for (int i=0; i<dataSize/2; i++)
336 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
339 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
340 fh->WriteDcmExplVR(outputFileName);