1 /*=========================================================================
4 Module: $RCSfile: ReWrite.cxx,v $
6 Date: $Date: 2007/07/25 13:15:14 $
7 Version: $Revision: 1.28 $
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"
24 #include <string.h> // for memcpy
27 int main(int argc, char *argv[])
31 " Re write a full gdcm-readable Dicom image ",
32 " (usefull when the file header is not very straight). ",
34 " usage: ReWrite filein=inputFileName fileout=outputFileName ",
35 " [mode=write mode] [monochrome1] [noshadow] [noseq][debug] ",
36 " --> The following line to 'rubout' a burnt-in Patient name ",
37 " [rubout=xBegin,xEnd,yBegin,yEnd [ruboutvalue=n (<255)] ] ",
38 " --> The 2 following lines, to extract a sub image within some frames ",
39 " [ROI=xBegin,xEnd,yBegin,yEnd] ",
40 " [firstframe=beg] [lastframe=end] ",
42 " mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels) ",
43 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
44 " noshadowseq: user doesn't want to load Private Sequences ",
45 " noshadow : user doesn't want to load Private groups (odd number)",
46 " noseq : user doesn't want to load Sequences ",
47 " rgb : user wants to transform LUT (if any) to RGB pixels ",
48 " warning : developper wants to run the program in 'warning mode'",
49 " debug : developper wants to run the program in 'debug mode' ",
52 // ----- Initialize Arguments Manager ------
53 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
55 if (argc == 1 || am->ArgMgrDefined("usage"))
57 am->ArgMgrUsage(usage); // Display 'usage'
61 char *fileName = am->ArgMgrWantString("filein",usage);
62 if ( fileName == NULL )
64 std::cout << "'filein= ...' is mandatory" << std::endl;
69 char *outputFileName = am->ArgMgrWantString("fileout",usage);
70 if ( outputFileName == NULL )
72 std::cout << "'fileout= ...' is mandatory" << std::endl;
77 const char *mode = am->ArgMgrGetString("mode","X");
79 int loadMode = GDCM_NAME_SPACE::LD_ALL;
80 if ( am->ArgMgrDefined("noshadowseq") )
81 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
84 if ( am->ArgMgrDefined("noshadow") )
85 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
86 if ( am->ArgMgrDefined("noseq") )
87 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
90 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
92 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
94 if (am->ArgMgrDefined("debug"))
95 GDCM_NAME_SPACE::Debug::DebugOn();
97 if (am->ArgMgrDefined("warning"))
98 GDCM_NAME_SPACE::Debug::WarningOn();
104 if (am->ArgMgrDefined("rubout"))
107 boundVal = am->ArgMgrGetListOfInt("rubout", &nbBound);
111 std::cout << "Illegal number of 'rubout' boundary values (expected : 4, found:"
112 << nbBound << "); 'rubout' ignored" << std::endl;
116 ruboutVal = am->ArgMgrGetInt("ruboutvalue", 0);
122 if (am->ArgMgrDefined("roi"))
125 roiBoundVal = am->ArgMgrGetListOfInt("roi", &nbRoiBound);
129 std::cout << "Illegal number of 'ROI' boundary values (expected : 4, found:"
130 << nbRoiBound << "); 'ROI' ignored" << std::endl;
137 int beg = am->ArgMgrGetInt("firstFrame",-1);
138 int end = am->ArgMgrGetInt("lastFrame",-1);
140 // if unused Params we give up
141 if ( am->ArgMgrPrintUnusedLabels() )
143 am->ArgMgrUsage(usage);
148 delete am; // we don't need Argument Manager any longer
150 // ----------- End Arguments Manager ---------
152 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
153 f->SetLoadMode( loadMode );
155 f->SetFileName( fileName );
156 bool res = f->Load();
163 if (!f->IsReadable())
165 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
170 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
174 int nX,nY,nZ,sPP,planarConfig;
175 std::string pixelType, transferSyntaxName;
180 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
182 pixelType = f->GetPixelType();
183 sPP = f->GetSamplesPerPixel();
184 planarConfig = f->GetPlanarConfiguration();
186 std::cout << " pixelType=" << pixelType
187 << " SampleserPixel=" << sPP
188 << " PlanarConfiguration=" << planarConfig
189 << " PhotometricInterpretation="
190 << f->GetEntryString(0x0028,0x0004)
193 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
194 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
196 transferSyntaxName = f->GetTransferSyntaxName();
197 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
201 fh->SetPhotometricInterpretationToMonochrome1();
205 dataSize = fh->GetImageDataSize();
206 imageData = fh->GetImageData(); // somewhat important : Loads the Pixels in memory !
207 fh->SetWriteModeToRGB();
211 dataSize = fh->GetImageDataRawSize();
212 imageData = fh->GetImageDataRaw();// somewhat important : Loads the Pixels in memory !
213 fh->SetWriteModeToRaw();
216 if ( imageData == 0 ) // to avoid warning
218 std::cout << "Was unable to read pixels " << std::endl;
220 printf(" dataSize %d imageData %p\n",dataSize, imageData);
222 // Since we just ReWrite the image, we know no modification
223 // was performed on the pixels.
224 // We don't want this image appears as a 'Secondary Captured image'
225 fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
228 /// \todo : think about rubbing out a part of a *multiframe* image!
231 if (boundVal[0]<0 || boundVal[0]>nX)
233 std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl;
236 if (boundVal[1]<0 || boundVal[1]>nX)
238 std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl;
241 if (boundVal[0] > boundVal[1])
243 std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl;
246 if (boundVal[2]<0 || boundVal[2]>nY)
248 std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl;
251 if (boundVal[3]<0 || boundVal[3]>nY)
253 std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl;
256 if (boundVal[2] > boundVal[3])
258 std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl;
263 int pixelLength = f->GetBitsAllocated()/8;
264 int lineLength = nX * sPP * pixelLength;
265 size_t lengthToRubout = (boundVal[1]-boundVal[0])*sPP*pixelLength;
266 int offsetToBeginOfRubout = boundVal[0]*sPP*pixelLength+lineLength*boundVal[2];
268 for(int rbl=boundVal[2]; rbl<boundVal[3];rbl++)
270 memset((char *)imageData+offsetToBeginOfRubout, ruboutVal, lengthToRubout);
271 offsetToBeginOfRubout += lineLength;
277 // user wants to keep only a part of the image (ROI, and/or some frames)
278 // ---------------------------------------------------------------------
279 // (==> this is no longer really 'ReWrite' !)
286 if (roiBoundVal[0]<0 || roiBoundVal[0]>=nX)
288 std::cout << "xBegin out of bounds; 'roi' ignored" << std::endl;
291 if (roiBoundVal[1]<0 || roiBoundVal[1]>=nX)
293 std::cout << "xEnd out of bounds; 'roi' ignored" << std::endl;
296 if (roiBoundVal[0] > roiBoundVal[1])
298 std::cout << "xBegin greater than xEnd; 'roi' ignored" << std::endl;
302 if (roiBoundVal[2]<0 || roiBoundVal[2]>=nY)
304 std::cout << "yBegin out of bounds; 'roi' ignored" << std::endl;
307 if (roiBoundVal[3]<0 || roiBoundVal[3]>=nY)
309 std::cout << "yEnd out of bounds; 'roi' ignored" << std::endl;
312 if (roiBoundVal[2] > roiBoundVal[3])
314 std::cout << "yBegin greater than yEnd; 'roi' ignored" << std::endl;
320 roiBoundVal = new int[4];
322 roiBoundVal[1] = nX-1;
324 roiBoundVal[3] = nY-1;
327 subImDimX = roiBoundVal[1]-roiBoundVal[0]+1;
328 subImDimY = roiBoundVal[3]-roiBoundVal[2]+1;
330 if (roi || beg != -1 || end != -1)
337 std::ostringstream str;
339 // Set the data that will be *actually* written.
341 int pixelSize = fh->GetFile()->GetPixelSize();
342 size_t lgrSubLine = subImDimX* pixelSize * numberOfScalarComponents;
343 size_t lgrSubFrame = subImDimY*lgrSubLine;
345 int lgrSubImage = (end-beg+1) * lgrSubFrame;
347 uint8_t * subImage = new uint8_t[lgrSubImage];
349 uint8_t * srcCopy = (uint8_t *) imageData;
350 uint8_t * destCopy = subImage;
351 int lineSize = nX*pixelSize*numberOfScalarComponents;
352 int frameSize = nY*lineSize;
354 int lineOffset = roiBoundVal[0]*pixelSize * numberOfScalarComponents;
356 for (int frameNb=beg, frameCount=0; frameNb<=end; frameNb++, frameCount++)
358 for (int lineNb=roiBoundVal[2], lineCount=0; lineNb<=roiBoundVal[3]; lineNb++, lineCount++)
360 /// \todo increment data pointer, don't multiply so much!
361 memcpy( (void *)(destCopy + frameCount*lgrSubFrame + lineCount*lgrSubLine),
362 (void *)(srcCopy + frameNb*frameSize + lineNb*lineSize + lineOffset ),
367 // Set the image size
370 fh->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
374 fh->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
377 fh->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
379 fh->SetImageData(subImage,lgrSubImage);
384 //----------------------------------- Write, now! ---------------------------------
390 // Writting an ACR file
391 // from a full gdcm readable File
392 std::cout << "WriteACR" << std::endl;
393 fh->WriteAcr(outputFileName);
396 case 'D' : // Not documented in the 'usage', because the method
397 case 'd' : // is known to be bugged.
398 // Writting a DICOM Implicit VR file
399 // from a full gdcm readable File
400 std::cout << "WriteDCM Implicit VR" << std::endl;
401 fh->WriteDcmImplVR(outputFileName);
406 // writting a DICOM Explicit VR
407 // from a full gdcm readable File
408 std::cout << "WriteDCM Explicit VR" << std::endl;
409 // fh->WriteDcmExplVR(outputFileName);
411 fh->SetWriteTypeToDcmExplVR();
412 fh->Write(outputFileName);
417 // Writting a Raw File,
418 std::cout << "WriteRaw" << std::endl;
419 fh->WriteRawData(outputFileName);
423 // Write a 'Video inverse' version of the file.
424 // *Not* described, on purpose, in the USAGE
427 if ( fh->GetFile()->GetBitsAllocated() == 8)
429 std::cout << "videoinv for 8 bits" << std::endl;
430 for (int i=0; i<dataSize; i++)
432 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
437 std::cout << "videoinv for 16 bits" << std::endl;
438 for (int i=0; i<dataSize/2; i++)
440 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
443 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
444 fh->WriteDcmExplVR(outputFileName);