1 /*=========================================================================
4 Module: $RCSfile: ReWrite.cxx,v $
6 Date: $Date: 2007/08/24 10:48:08 $
7 Version: $Revision: 1.30 $
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 ---------
154 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
155 f->SetLoadMode( loadMode );
157 f->SetFileName( fileName );
158 bool res = f->Load();
165 if (!f->IsReadable())
167 std::cerr << "Sorry, not a Readable DICOM / ACR File" <<std::endl;
172 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
176 int nX,nY,nZ,sPP,planarConfig;
177 std::string pixelType, transferSyntaxName;
182 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
184 pixelType = f->GetPixelType();
185 sPP = f->GetSamplesPerPixel();
186 planarConfig = f->GetPlanarConfiguration();
188 std::cout << " pixelType=" << pixelType
189 << " SampleserPixel=" << sPP
190 << " PlanarConfiguration=" << planarConfig
191 << " PhotometricInterpretation="
192 << f->GetEntryString(0x0028,0x0004)
195 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
196 std::cout << "NumberOfScalarComponents " << numberOfScalarComponents
198 transferSyntaxName = f->GetTransferSyntaxName();
199 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
203 fh->SetPhotometricInterpretationToMonochrome1();
207 dataSize = fh->GetImageDataSize();
208 imageData = fh->GetImageData(); // somewhat important : Loads the Pixels in memory !
209 fh->SetWriteModeToRGB();
213 dataSize = fh->GetImageDataRawSize();
214 imageData = fh->GetImageDataRaw();// somewhat important : Loads the Pixels in memory !
215 fh->SetWriteModeToRaw();
218 if ( imageData == 0 ) // to avoid warning
220 std::cout << "Was unable to read pixels " << std::endl;
222 printf(" dataSize %d imageData %p\n",dataSize, imageData);
224 // Since we just ReWrite the image, we know no modification
225 // was performed on the pixels.
226 // We don't want this image appears as a 'Secondary Captured image'
227 fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
230 /// \todo : think about rubbing out a part of a *multiframe* image!
233 if (boundVal[0]<0 || boundVal[0]>nX)
235 std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl;
238 if (boundVal[1]<0 || boundVal[1]>nX)
240 std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl;
243 if (boundVal[0] > boundVal[1])
245 std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl;
248 if (boundVal[2]<0 || boundVal[2]>nY)
250 std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl;
253 if (boundVal[3]<0 || boundVal[3]>nY)
255 std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl;
258 if (boundVal[2] > boundVal[3])
260 std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl;
265 int pixelLength = f->GetBitsAllocated()/8;
266 int lineLength = nX * sPP * pixelLength;
267 size_t lengthToRubout = (boundVal[1]-boundVal[0])*sPP*pixelLength;
268 int offsetToBeginOfRubout = boundVal[0]*sPP*pixelLength+lineLength*boundVal[2];
270 for(int rbl=boundVal[2]; rbl<boundVal[3];rbl++)
272 memset((char *)imageData+offsetToBeginOfRubout, ruboutVal, lengthToRubout);
273 offsetToBeginOfRubout += lineLength;
279 //------------------------------ Set the Writing mode ---------------------------------
285 // Writting an ACR file
286 // from a full gdcm readable File
287 std::cout << "WriteACR" << std::endl;
288 fh->SetWriteTypeToAcr();
291 case 'D' : // Not documented in the 'usage', because the method
292 case 'd' : // is known to be bugged.
293 // Writting a DICOM Implicit VR file
294 // from a full gdcm readable File
295 std::cout << "WriteDCM Implicit VR" << std::endl;
296 fh->SetWriteTypeToDcmImplVR();
301 // writting a DICOM Explicit VR
302 // from a full gdcm readable File
303 std::cout << "WriteDCM Explicit VR" << std::endl;
304 // fh->WriteDcmExplVR(outputFileName);
306 fh->SetWriteTypeToDcmExplVR();
312 // Writting a Raw File,
313 std::cout << "WriteRaw" << std::endl;
314 fh->WriteRawData(outputFileName);
319 // writting a DICOM Jpeg Lossless
320 // from a full gdcm readable File
321 std::cout << "WriteDCM Jpeg Lossless" << std::endl;
322 fh->SetWriteTypeToJPEG();
326 // writting a DICOM Jpeg 2000
327 // from a full gdcm readable File
328 std::cout << "WriteDCM Jpeg 2000" << std::endl;
329 fh->SetWriteTypeToJPEG2000();
333 // Write a 'Video inverse' version of the file.
334 // *Not* described, on purpose, in the USAGE
337 if ( fh->GetFile()->GetBitsAllocated() == 8)
339 std::cout << "videoinv for 8 bits" << std::endl;
340 for (int i=0; i<dataSize; i++)
342 ((uint8_t*)imageData)[i] = 255 - ((uint8_t*)imageData)[i];
347 std::cout << "videoinv for 16 bits" << std::endl;
348 for (int i=0; i<dataSize/2; i++)
350 ((uint16_t*)imageData)[i] = 65535 - ((uint16_t*)imageData)[i];
353 std::cout << "WriteDCM Explicit VR + VideoInv" << std::endl;
354 fh->SetWriteTypeToDcmExplVR();
360 // user wants to keep only a part of the image (ROI, and/or some frames)
361 // ---------------------------------------------------------------------
362 // (==> this is no longer really 'ReWrite' !)
369 if (roiBoundVal[0]<0 || roiBoundVal[0]>=nX)
371 std::cout << "xBegin out of bounds; 'roi' ignored" << std::endl;
374 if (roiBoundVal[1]<0 || roiBoundVal[1]>=nX)
376 std::cout << "xEnd out of bounds; 'roi' ignored" << std::endl;
379 if (roiBoundVal[0] > roiBoundVal[1])
381 std::cout << "xBegin greater than xEnd; 'roi' ignored" << std::endl;
385 if (roiBoundVal[2]<0 || roiBoundVal[2]>=nY)
387 std::cout << "yBegin out of bounds; 'roi' ignored" << std::endl;
390 if (roiBoundVal[3]<0 || roiBoundVal[3]>=nY)
392 std::cout << "yEnd out of bounds; 'roi' ignored" << std::endl;
395 if (roiBoundVal[2] > roiBoundVal[3])
397 std::cout << "yBegin greater than yEnd; 'roi' ignored" << std::endl;
403 roiBoundVal = new int[4];
405 roiBoundVal[1] = nX-1;
407 roiBoundVal[3] = nY-1;
410 subImDimX = roiBoundVal[1]-roiBoundVal[0]+1;
411 subImDimY = roiBoundVal[3]-roiBoundVal[2]+1;
413 if (roi || beg != -1 || end != -1)
420 std::ostringstream str;
422 // Set the data that will be *actually* written.
424 int pixelSize = fh->GetFile()->GetPixelSize();
425 size_t lgrSubLine = subImDimX* pixelSize * numberOfScalarComponents;
426 size_t lgrSubFrame = subImDimY*lgrSubLine;
428 int lgrSubImage = (end-beg+1) * lgrSubFrame;
430 uint8_t * subImage = new uint8_t[lgrSubImage];
432 uint8_t * srcCopy = (uint8_t *) imageData;
433 uint8_t * destCopy = subImage;
434 int lineSize = nX*pixelSize*numberOfScalarComponents;
435 int frameSize = nY*lineSize;
437 int lineOffset = roiBoundVal[0]*pixelSize * numberOfScalarComponents;
439 for (int frameNb=beg, frameCount=0; frameNb<=end; frameNb++, frameCount++)
441 for (int lineNb=roiBoundVal[2], lineCount=0; lineNb<=roiBoundVal[3]; lineNb++, lineCount++)
443 /// \todo : increment data pointer, don't multiply so much!
444 memcpy( (void *)(destCopy + frameCount*lgrSubFrame + lineCount*lgrSubLine),
445 (void *)(srcCopy + frameNb*frameSize + lineNb*lineSize + lineOffset ),
450 // Set the image size
453 fh->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
457 fh->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
460 fh->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
462 //fh->SetImageData(subImage,lgrSubImage);
463 fh->SetUserData(subImage,lgrSubImage); // ensures the compression (if any)
467 fh->SetUserData(imageData,dataSize); // ensures the compression (if any)
472 //----------------------------------- Write, now! ---------------------------------
474 if (mode[0] != 'R' && mode[0] != 'r')
475 fh->Write(outputFileName);