X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=Example%2FReWrite.cxx;h=ac672f06f28e53daebbd247d1d60cc1977404830;hb=24394a63b18810c791af1c913e1a6fc712d3d3a0;hp=1cabef1b08662ea4395dac370a2a6ad77a2d505f;hpb=48f7ce7aa61778477992107caf15e8960359e66b;p=gdcm.git diff --git a/Example/ReWrite.cxx b/Example/ReWrite.cxx index 1cabef1b..ac672f06 100644 --- a/Example/ReWrite.cxx +++ b/Example/ReWrite.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: ReWrite.cxx,v $ Language: C++ - Date: $Date: 2006/01/26 15:52:43 $ - Version: $Revision: 1.17 $ + Date: $Date: 2007/03/06 10:19:00 $ + Version: $Revision: 1.21 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -21,6 +21,7 @@ #include "gdcmArgMgr.h" +#include // for memcpy #include int main(int argc, char *argv[]) @@ -30,15 +31,20 @@ int main(int argc, char *argv[]) " Re write a full gdcm-readable Dicom image ", " (usefull when the file header is not very straight). ", " ", - " usage: ReWrite filein=inputFileName fileout=anonymizedFileName ", - " [mode=write mode] [noshadow] [noseq][debug] ", + " usage: ReWrite filein=inputFileName fileout=outputFileName ", + " [mode=write mode] [noshadow] [noseq][debug] ", + " --> The following line to 'rubout' a burnt-in Patient name ", + " [rubout=xBegin,xEnd,yBegin,yEnd [ruboutvalue=n (<255)] ] ", + " --> The 2 following lines, to extract a sub image withon some frames ", + " [ROI=xBegin,xEnd,yBegin,yEnd] ", + " [firstframe=beg] [lastframe=end] ", " ", " mode = a (ACR), x (Explicit VR Dicom), r (RAW : only pixels) ", " noshadowseq: user doesn't want to load Private Sequences ", " noshadow : user doesn't want to load Private groups (odd number)", " noseq : user doesn't want to load Sequences ", " rgb : user wants to tranform LUT (if any) to RGB pixels ", - " debug : user wants to run the program in 'debug mode' ", + " debug : developper wants to run the program in 'debug mode' ", FINISH_USAGE // ----- Initialize Arguments Manager ------ @@ -83,6 +89,46 @@ int main(int argc, char *argv[]) if (am->ArgMgrDefined("debug")) gdcm::Debug::DebugOn(); + + bool fail = false; + int *boundVal; + int ruboutVal; + bool rubout = false; + if (am->ArgMgrDefined("rubout")) + { + int nbBound; + boundVal = am->ArgMgrGetListOfInt("rubout", &nbBound); + + if (nbBound !=4) + { + std::cout << "Illegal number of 'rubout' boundary values (expected : 4, found:" + << nbBound << "); 'rubout' ignored" << std::endl; + fail = true; + } + + ruboutVal = am->ArgMgrGetInt("ruboutvalue", 0); + rubout = true; + } + + int *roiBoundVal; + bool roi = false; + if (am->ArgMgrDefined("roi")) + { + int nbRoiBound; + roiBoundVal = am->ArgMgrGetListOfInt("roi", &nbRoiBound); + + if (nbRoiBound !=4) + { + std::cout << "Illegal number of 'ROI' boundary values (expected : 4, found:" + << nbRoiBound << "); 'ROI' ignored" << std::endl; + fail = true; + } + else + roi = true; + } + + int beg = am->ArgMgrGetInt("firstFrame",-1); + int end = am->ArgMgrGetInt("lastFrame",-1); // if unused Params we give up if ( am->ArgMgrPrintUnusedLabels() ) @@ -105,41 +151,25 @@ int main(int argc, char *argv[]) f->Delete(); return 0; } - + if (!f->IsReadable()) { std::cerr << "Sorry, not a Readable DICOM / ACR File" <Delete(); return 0; } + gdcm::FileHelper *fh = gdcm::FileHelper::New(f); void *imageData; int dataSize; - - if (rgb) - { - dataSize = fh->GetImageDataSize(); - imageData = fh->GetImageData(); // somewhat important... can't remember - fh->SetWriteModeToRGB(); - } - else - { - dataSize = fh->GetImageDataRawSize(); - imageData = fh->GetImageDataRaw();// somewhat important... can't remember - fh->SetWriteModeToRaw(); - } - - if ( imageData == 0 ) // to avoid warning - { - std::cout << "Was unable to read pixels " << std::endl; - } - std::cout <GetXSize(); nY=f->GetYSize(); nZ=f->GetZSize(); + std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl; pixelType = f->GetPixelType(); @@ -159,11 +189,190 @@ int main(int argc, char *argv[]) transferSyntaxName = f->GetTransferSyntaxName(); std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]" << std::endl; + + + if (rgb) + { + dataSize = fh->GetImageDataSize(); + imageData = fh->GetImageData(); // somewhat important : Loads the Pixels in memory ! + fh->SetWriteModeToRGB(); + } + else + { + dataSize = fh->GetImageDataRawSize(); + imageData = fh->GetImageDataRaw();// somewhat important : Loads the Pixels in memory ! + fh->SetWriteModeToRaw(); + } + + if ( imageData == 0 ) // to avoid warning + { + std::cout << "Was unable to read pixels " << std::endl; + } + printf(" dataSize %d imageData %p\n",dataSize, imageData); // Since we just ReWrite the image, we know no modification // was performed on the pixels. // We don't want this image appears as a 'Secondary Captured image' - fh->SetKeepMediaStorageSOPClassUID(true); + fh->SetContentType(gdcm::UNMODIFIED_PIXELS_IMAGE); + + + /// \todo : think about rubbing out a part of a *multiframe* image! + if (rubout) + { + if (boundVal[0]<0 || boundVal[0]>nX) + { + std::cout << "xBegin out of bounds; 'rubout' ignored" << std::endl; + fail = true; + } + if (boundVal[1]<0 || boundVal[1]>nX) + { + std::cout << "xEnd out of bounds; 'rubout' ignored" << std::endl; + fail = true; + } + if (boundVal[0] > boundVal[1]) + { + std::cout << "xBegin greater than xEnd; 'rubout' ignored" << std::endl; + fail = true; + } + if (boundVal[2]<0 || boundVal[2]>nY) + { + std::cout << "yBegin out of bounds; 'rubout' ignored" << std::endl; + fail = true; + } + if (boundVal[3]<0 || boundVal[3]>nY) + { + std::cout << "yEnd out of bounds; 'rubout' ignored" << std::endl; + fail = true; + } + if (boundVal[2] > boundVal[3]) + { + std::cout << "yBegin greater than yEnd; 'rubout' ignored" << std::endl; + fail = true; + } + if (!fail) + { + int pixelLength = f->GetBitsAllocated()/8; + int lineLength = nX * sPP * pixelLength; + size_t lengthToRubout = (boundVal[1]-boundVal[0])*sPP*pixelLength; + int offsetToBeginOfRubout = boundVal[0]*sPP*pixelLength+lineLength*boundVal[2]; + + for(int rbl=boundVal[2]; rbl this is no longer really 'ReWrite' !) + + int subImDimX = nX; + int subImDimY = nY; + + if (roi) + { + if (roiBoundVal[0]<0 || roiBoundVal[0]>=nX) + { + std::cout << "xBegin out of bounds; 'roi' ignored" << std::endl; + fail = true; + } + if (roiBoundVal[1]<0 || roiBoundVal[1]>=nX) + { + std::cout << "xEnd out of bounds; 'roi' ignored" << std::endl; + fail = true; + } + if (roiBoundVal[0] > roiBoundVal[1]) + { + std::cout << "xBegin greater than xEnd; 'roi' ignored" << std::endl; + fail = true; + } + + if (roiBoundVal[2]<0 || roiBoundVal[2]>=nY) + { + std::cout << "yBegin out of bounds; 'roi' ignored" << std::endl; + fail = true; + } + if (roiBoundVal[3]<0 || roiBoundVal[3]>=nY) + { + std::cout << "yEnd out of bounds; 'roi' ignored" << std::endl; + fail = true; + } + if (roiBoundVal[2] > roiBoundVal[3]) + { + std::cout << "yBegin greater than yEnd; 'roi' ignored" << std::endl; + fail = true; + } + } + else + { + roiBoundVal = new int(4); + roiBoundVal[0] = 0; + roiBoundVal[1] = nX-1; + roiBoundVal[2] = 0; + roiBoundVal[3] = nY-1; + } + + subImDimX = roiBoundVal[1]-roiBoundVal[0]+1; + subImDimY = roiBoundVal[3]-roiBoundVal[2]+1; + + if (roi || beg != -1 || end != -1) + { + if (beg == -1) + beg = 0; + if (end == -1) + end = nZ; + + std::ostringstream str; + + // Set the data that will be *actually* written. + + int pixelSize = fh->GetFile()->GetPixelSize(); + size_t lgrSubLine = subImDimX* pixelSize * numberOfScalarComponents; + size_t lgrSubFrame = subImDimY*lgrSubLine; + ; + int lgrSubImage = (end-beg+1) * lgrSubFrame; + + uint8_t * subImage = new uint8_t[lgrSubImage]; + + uint8_t * srcCopy = (uint8_t *) imageData; + uint8_t * destCopy = subImage; + int lineSize = nX*pixelSize*numberOfScalarComponents; + int frameSize = nY*lineSize; + + int lineOffset = roiBoundVal[0]*pixelSize * numberOfScalarComponents; + + for (unsigned int frameNb=beg, frameCount=0; frameNb<=end; frameNb++, frameCount++) + { + for (unsigned int lineNb=roiBoundVal[2], lineCount=0; lineNb<=roiBoundVal[3]; lineNb++, lineCount++) + { + /// \todo increment data pointer, don't multiply so much! + memcpy( (void *)(destCopy + frameCount*lgrSubFrame + lineCount*lgrSubLine), + (void *)(srcCopy + frameNb*frameSize + lineNb*lineSize + lineOffset ), + lgrSubLine); + } + } + + // Set the image size + str.str(""); + str << subImDimX ; + fh->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns + + str.str(""); + str << subImDimY; + fh->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows + str.str(""); + str << end-beg+1; + fh->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames + + fh->SetImageData(subImage,lgrSubImage); + + } + + +//----------------------------------- Write, now! --------------------------------- switch (mode[0]) {