1 /*=========================================================================
4 Module: $RCSfile: ReWrite.cxx,v $
6 Date: $Date: 2007/08/29 15:58:19 $
7 Version: $Revision: 1.31 $
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 " j (jpeg lossless), 2 (jpeg2000) ",
44 " monochrome1 = user wants MONOCHROME1 photom. interp. (0=white) ",
45 " noshadowseq: user doesn't want to load Private Sequences ",
46 " noshadow : user doesn't want to load Private groups (odd number)",
47 " noseq : user doesn't want to load Sequences ",
48 " rgb : user wants to transform LUT (if any) to RGB pixels ",
49 " warning : developper wants to run the program in 'warning mode'",
50 " debug : developper wants to run the program in 'debug mode' ",
53 // ----- Initialize Arguments Manager ------
55 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
57 if (argc == 1 || am->ArgMgrDefined("usage"))
59 am->ArgMgrUsage(usage); // Display 'usage'
63 char *fileName = am->ArgMgrWantString("filein",usage);
64 if ( fileName == NULL )
66 std::cout << "'filein= ...' is mandatory" << std::endl;
71 char *outputFileName = am->ArgMgrWantString("fileout",usage);
72 if ( outputFileName == NULL )
74 std::cout << "'fileout= ...' is mandatory" << std::endl;
79 const char *mode = am->ArgMgrGetString("mode","X");
81 int loadMode = GDCM_NAME_SPACE::LD_ALL;
82 if ( am->ArgMgrDefined("noshadowseq") )
83 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
86 if ( am->ArgMgrDefined("noshadow") )
87 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
88 if ( am->ArgMgrDefined("noseq") )
89 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
92 bool rgb = ( 0 != am->ArgMgrDefined("RGB") );
94 bool monochrome1 = ( 0 != am->ArgMgrDefined("monochrome1") );
96 if (am->ArgMgrDefined("debug"))
97 GDCM_NAME_SPACE::Debug::DebugOn();
99 if (am->ArgMgrDefined("warning"))
100 GDCM_NAME_SPACE::Debug::WarningOn();
106 if (am->ArgMgrDefined("rubout"))
109 boundVal = am->ArgMgrGetListOfInt("rubout", &nbBound);
113 std::cout << "Illegal number of 'rubout' boundary values (expected : 4, found:"
114 << nbBound << "); 'rubout' ignored" << std::endl;
118 ruboutVal = am->ArgMgrGetInt("ruboutvalue", 0);
124 if (am->ArgMgrDefined("roi"))
127 roiBoundVal = am->ArgMgrGetListOfInt("roi", &nbRoiBound);
131 std::cout << "Illegal number of 'ROI' boundary values (expected : 4, found:"
132 << nbRoiBound << "); 'ROI' ignored" << std::endl;
139 int beg = am->ArgMgrGetInt("firstFrame",-1);
140 int end = am->ArgMgrGetInt("lastFrame",-1);
142 // if unused Params we give up
143 if ( am->ArgMgrPrintUnusedLabels() )
145 am->ArgMgrUsage(usage);
150 delete am; // we don't need Argument Manager any longer
152 // ----------- End Arguments Manager ---------
153 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
155 f->SetMaxSizeLoadEntry(0x7fff);
157 f->SetLoadMode( loadMode );
159 f->SetFileName( fileName );
160 bool res = f->Load();
167 if (!f->IsReadable())
169 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
174 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
178 int nX,nY,nZ,sPP,planarConfig;
179 std::string pixelType, transferSyntaxName;
184 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
186 pixelType = f->GetPixelType();
187 sPP = f->GetSamplesPerPixel();
188 planarConfig = f->GetPlanarConfiguration();
190 std::cout << " pixelType=" << pixelType
191 << " SampleserPixel=" << sPP
192 << " PlanarConfiguration=" << planarConfig
193 << " PhotometricInterpretation="
194 << f->GetEntryString(0x0028,0x0004)
197 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
198 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
200 transferSyntaxName = f->GetTransferSyntaxName();
201 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
205 fh->SetPhotometricInterpretationToMonochrome1();
209 dataSize = fh->GetImageDataSize();
210 imageData = fh->GetImageData(); // somewhat important : Loads the Pixels in memory !
211 fh->SetWriteModeToRGB();
215 dataSize = fh->GetImageDataRawSize();
216 imageData = fh->GetImageDataRaw();// somewhat important : Loads the Pixels in memory !
217 fh->SetWriteModeToRaw();
220 if ( imageData == 0 ) // to avoid warning
222 std::cout << "Was unable to read pixels " << std::endl;
224 printf(" dataSize %d imageData %p\n",dataSize, imageData);
226 // Since we just ReWrite the image, we know no modification
227 // was performed on the pixels.
228 // We don't want this image appears as a 'Secondary Captured image'
229 fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
232 /// \todo : think about rubbing out a part of a *multiframe* image!
235 if (boundVal[0]<0 || boundVal[0]>nX)
237 std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl;
240 if (boundVal[1]<0 || boundVal[1]>nX)
242 std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl;
245 if (boundVal[0] > boundVal[1])
247 std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl;
250 if (boundVal[2]<0 || boundVal[2]>nY)
252 std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl;
255 if (boundVal[3]<0 || boundVal[3]>nY)
257 std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl;
260 if (boundVal[2] > boundVal[3])
262 std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl;
267 int pixelLength = f->GetBitsAllocated()/8;
268 int lineLength = nX * sPP * pixelLength;
269 size_t lengthToRubout = (boundVal[1]-boundVal[0])*sPP*pixelLength;
270 int offsetToBeginOfRubout = boundVal[0]*sPP*pixelLength+lineLength*boundVal[2];
272 for(int rbl=boundVal[2]; rbl<boundVal[3];rbl++)
274 memset((char *)imageData+offsetToBeginOfRubout, ruboutVal, lengthToRubout);
275 offsetToBeginOfRubout += lineLength;
281 //------------------------------ Set the Writing mode ---------------------------------
287 // Writting an ACR file
288 // from a full gdcm readable File
289 std::cout << "WriteACR" << std::endl;
290 fh->SetWriteTypeToAcr();
293 case 'D' : // Not documented in the 'usage', because the method
294 case 'd' : // is known to be bugged.
295 // Writting a DICOM Implicit VR file
296 // from a full gdcm readable File
297 std::cout << "WriteDCM Implicit VR" << std::endl;
298 fh->SetWriteTypeToDcmImplVR();
303 // writting a DICOM Explicit VR
304 // from a full gdcm readable File
305 std::cout << "WriteDCM Explicit VR" << std::endl;
306 // fh->WriteDcmExplVR(outputFileName);
308 fh->SetWriteTypeToDcmExplVR();
314 // Writting a Raw File,
315 std::cout << "WriteRaw" << std::endl;
316 fh->WriteRawData(outputFileName);
321 // writting a DICOM Jpeg Lossless
322 // from a full gdcm readable File
323 std::cout << "WriteDCM Jpeg Lossless" << std::endl;
324 fh->SetWriteTypeToJPEG();
328 // writting a DICOM Jpeg 2000
329 // from a full gdcm readable File
330 std::cout << "WriteDCM Jpeg 2000" << std::endl;
331 fh->SetWriteTypeToJPEG2000();
335 // Write a 'Video inverse' version of the file.
336 // *Not* described, on purpose, in the USAGE
339 if ( fh->GetFile()->GetBitsAllocated() == 8)
341 std::cout << "videoinv for 8 bits" << std::endl;
342 for (int i=0; i<dataSize; i++)
344 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
349 std::cout << "videoinv for 16 bits" << std::endl;
350 for (int i=0; i<dataSize/2; i++)
352 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
355 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
356 fh->SetWriteTypeToDcmExplVR();
362 // user wants to keep only a part of the image (ROI, and/or some frames)
363 // ---------------------------------------------------------------------
364 // (==> this is no longer really 'ReWrite' !)
371 if (roiBoundVal[0]<0 || roiBoundVal[0]>=nX)
373 std::cout << "xBegin out of bounds; 'roi' ignored" << std::endl;
376 if (roiBoundVal[1]<0 || roiBoundVal[1]>=nX)
378 std::cout << "xEnd out of bounds; 'roi' ignored" << std::endl;
381 if (roiBoundVal[0] > roiBoundVal[1])
383 std::cout << "xBegin greater than xEnd; 'roi' ignored" << std::endl;
387 if (roiBoundVal[2]<0 || roiBoundVal[2]>=nY)
389 std::cout << "yBegin out of bounds; 'roi' ignored" << std::endl;
392 if (roiBoundVal[3]<0 || roiBoundVal[3]>=nY)
394 std::cout << "yEnd out of bounds; 'roi' ignored" << std::endl;
397 if (roiBoundVal[2] > roiBoundVal[3])
399 std::cout << "yBegin greater than yEnd; 'roi' ignored" << std::endl;
405 roiBoundVal = new int[4];
407 roiBoundVal[1] = nX-1;
409 roiBoundVal[3] = nY-1;
412 subImDimX = roiBoundVal[1]-roiBoundVal[0]+1;
413 subImDimY = roiBoundVal[3]-roiBoundVal[2]+1;
415 if (roi || beg != -1 || end != -1)
422 std::ostringstream str;
424 // Set the data that will be *actually* written.
426 int pixelSize = fh->GetFile()->GetPixelSize();
427 size_t lgrSubLine = subImDimX* pixelSize * numberOfScalarComponents;
428 size_t lgrSubFrame = subImDimY*lgrSubLine;
430 int lgrSubImage = (end-beg+1) * lgrSubFrame;
432 uint8_t * subImage = new uint8_t[lgrSubImage];
434 uint8_t * srcCopy = (uint8_t *) imageData;
435 uint8_t * destCopy = subImage;
436 int lineSize = nX*pixelSize*numberOfScalarComponents;
437 int frameSize = nY*lineSize;
439 int lineOffset = roiBoundVal[0]*pixelSize * numberOfScalarComponents;
441 for (int frameNb=beg, frameCount=0; frameNb<=end; frameNb++, frameCount++)
443 for (int lineNb=roiBoundVal[2], lineCount=0; lineNb<=roiBoundVal[3]; lineNb++, lineCount++)
445 /// \todo : increment data pointer, don't multiply so much!
446 memcpy( (void *)(destCopy + frameCount*lgrSubFrame + lineCount*lgrSubLine),
447 (void *)(srcCopy + frameNb*frameSize + lineNb*lineSize + lineOffset ),
452 // Set the image size
455 fh->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
459 fh->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
462 fh->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
464 //fh->SetImageData(subImage,lgrSubImage);
465 fh->SetUserData(subImage,lgrSubImage); // ensures the compression (if any)
469 fh->SetUserData(imageData,dataSize); // ensures the compression (if any)
474 //----------------------------------- Write, now! ---------------------------------
476 if (mode[0] != 'R' && mode[0] != 'r')
477 fh->Write(outputFileName);