X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=Example%2FexOverlaysDCM.cxx;h=8efa49ebca0c18e4462ac5c39558f6729e461676;hb=86806f70a22e940f0bb0e7d23a570074ff81973f;hp=5e5cdfaab928ebb2ab77e1a64297dff0132910cd;hpb=4c0fe0142482ede7dd8f5408f9303d78c213435b;p=gdcm.git diff --git a/Example/exOverlaysDCM.cxx b/Example/exOverlaysDCM.cxx index 5e5cdfaa..8efa49eb 100755 --- a/Example/exOverlaysDCM.cxx +++ b/Example/exOverlaysDCM.cxx @@ -1,219 +1,193 @@ -/*========================================================================= - - Program: gdcm - Module: $RCSfile: exOverlaysDCM.cxx,v $ - Language: C++ - Date: $Date: 2005/12/09 10:17:52 $ - Version: $Revision: 1.1 $ - - Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de - l'Image). All rights reserved. See Doc/License.txt or - http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notices for more information. - -=========================================================================*/ #include "gdcmFile.h" #include "gdcmFileHelper.h" #include "gdcmCommon.h" #include "gdcmDebug.h" -#include "gdcmDocEntry.h" #include "gdcmDataEntry.h" +#include "gdcmArgMgr.h" #include -#include // for fseek... FIXME -#include // for atoi - - // WARNING : - // unfinished : DO NOT to be used as is ! - + +// Each BIT of Overlay Data (0x6000,0x3000) corresponds +// to a BYTE of overlay image. +void explodeByte(unsigned char byte, unsigned char* result) +{ + unsigned char mask = 1; + for (int i=0;i<8;i++) + { + if ((byte & mask)==0) + result[i]=0; + else + result[i]=1; + mask<<=1; + } + return; +} + int main(int argc, char *argv[]) -{ - gdcm::File *f; - - //gdcm::Debug::DebugOn(); - - std::cout << "------------------------------------------------" << std::endl; - std::cout << "Gets the 'Overlays' from a full gdcm-readable DCM image " - << "uncompressed image" << std::endl; - std::cout << "Writes them in a DicomV3 file named 'gdcmOverlay.dcm'" - << std::endl; - std::cout << "------------------------------------------------" << std::endl; - - std::string fileName; - if( argc > 1 ) - fileName = argv[1]; - else - fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr"; - - std::cout << fileName << std::endl; -// ============================================================ -// Read the input image. -// ============================================================ - - //std::cout << argv[1] << std::endl; - - f = gdcm::File::New( ); - - f->SetLoadMode(gdcm::LD_ALL); - f->SetFileName( fileName ); - f->AddForceLoadElement(0x6000,0x3000); // Overlay Data - bool res = f->Load(); +{ + START_USAGE(usage) + " \n exOverlaysDCM :\n ", + " Extract an overlay image from a DICOM image ", + " Warning : probably segfaults if no overlay ", + " usage: exOverlaysDCM filein=inputFileName fileout=outputFileName [debug] ", + " debug : developper wants to run the program in 'debug mode' ", + FINISH_USAGE + + // ----- Initialize Arguments Manager ------ + GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv); + + if (argc == 1 || am->ArgMgrDefined("usage")) + { + am->ArgMgrUsage(usage); // Display 'usage' + delete am; + return 0; + } + +/* + char *fileName = am->ArgMgrWantString("filein",usage); + if ( fileName == NULL ) + { + delete am; + return 0; + } +*/ + - if( gdcm::Debug::GetDebugFlag() ) + const char *fileName = am->ArgMgrGetString("filein"); + const char *dirName = am->ArgMgrGetString("dirin"); + + if ( (fileName == 0 && dirName == 0) || + (fileName != 0 && dirName != 0) ) { - std::cout << "---------------------------------------------" << std::endl; - f->Print(); - std::cout << "---------------------------------------------" << std::endl; + std::cerr << std::endl + << "Either 'filein=' or 'dirin=' must be present;" + << std::endl << "Not both" << std::endl; + am->ArgMgrUsage(usage); // Display 'usage' + delete am; + return 1; } - if (!res) { - std::cout << "Sorry, " << fileName <<" not a gdcm-readable " - << "DICOM / ACR File" - <Delete(); + + + char *outputFileName = am->ArgMgrWantString("fileout",usage); + if ( outputFileName == NULL ) + { + delete am; return 0; } + if (am->ArgMgrDefined("debug")) + GDCM_NAME_SPACE::Debug::DebugOn(); + + if (am->ArgMgrDefined("warning")) + GDCM_NAME_SPACE::Debug::WarningOn(); + + // if unused Param we give up + if ( am->ArgMgrPrintUnusedLabels() ) + { + am->ArgMgrUsage(usage); + delete am; + return 0; + } + + delete am; // we don't need Argument Manager any longer + + // ============================================================ + // Read the input file. + // ============================================================ + + GDCM_NAME_SPACE::File *f; + + f = GDCM_NAME_SPACE::File::New( ); + f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL ); + f->SetFileName( fileName ); + f->AddForceLoadElement(0x6000,0x3000); // Overlay Data + int res = f->Load(); + + if ( !res ) + { + std::cerr << "Sorry, " << fileName <<" not a gdcm-readable " + << "DICOM / ACR File" <Delete(); + return 0; + } std::cout << " ... is readable " << std::endl; -// ============================================================ -// Check whether image contains Overlays ACR-NEMA style. -// ============================================================ + // ============================================================ + // Load the Overlays in memory (the first one) + // ============================================================ - int bitsAllocated = f->GetBitsAllocated(); - - gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000); - + GDCM_NAME_SPACE::DataEntry *e = f->GetDataEntry(0x6000, 0x3000); if (e == 0) { std::cout << " Image doesn't contain any Overlay " << std::endl; f->Delete(); return 0; - } - + } std::cout << " File is read! " << std::endl; - -// ============================================================ -// Get usefull info from FileHelper. -// ============================================================ - - uint32_t overlayPixelLength = e->GetLength(); - -/* - if ( overlayPixelLength != (size_t)nx*ny*sizeof( uint16_t) ) + uint8_t *overlay = (uint8_t *)(e->GetBinArea()); + if ( overlay == 0 ) { - std::cout << "Sorry, Pixels of" << fileName << " are not " - << "readable. expected length :" << nx*ny*sizeof( uint16_t) - << " " << "read length : " << overlayPixelLength - << std::endl; + std::cerr << "Sorry, Overlays of" << fileName <<" are not " + << " gdcm-readable." << std::endl; f->Delete(); - delete pixels; return 0; } - else - { - std::cout << "Pixels read as expected : length = " << lgt << std::endl; - } -*/ - uint32_t nx = f->GetXSize(); - uint32_t ny = f->GetYSize(); - uint8_t *overlayPixelArea = (uint8_t *)(e->GetBinArea()); - - // should check overlayPixelLength == nx*ny/8 !) + // ============================================================ + // Image data generation. + // ============================================================ - uint8_t *overlayPixelImage = new uint8_t[nx*ny];// uint8 is enough to hold 1 bit ! - - uint16_t m; - for (unsigned int j=0; j 127)?255:0; - - printf("%04x ", overlayPixelImage[j*8 +k]); - } - std::cout << std::endl; - } - } + unsigned int dimX= f->GetXSize(); + unsigned int dimY= f->GetYSize(); - if( gdcm::Debug::GetDebugFlag() ) - std::cout << "About to built empty file" << std::endl; - - gdcm::File *fileToBuild = gdcm::File::New(); - - if( gdcm::Debug::GetDebugFlag() ) - std::cout << "Finish to built empty file" << std::endl; - - std::ostringstream str; - str.str(""); - str << nx; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns - str.str(""); - str << ny; - fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows - - fileToBuild->InsertEntryString("8",0x0028,0x0100); // Bits Allocated - fileToBuild->InsertEntryString("8",0x0028,0x0101); // Bits Stored - fileToBuild->InsertEntryString("7",0x0028,0x0102); // High Bit - fileToBuild->InsertEntryString("0",0x0028,0x0103); // Pixel Representation - fileToBuild->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel - - fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004); - // Other mandatory fields will be set automatically, - // just before Write(), by FileHelper::CheckMandatoryElements() - - if( gdcm::Debug::GetDebugFlag() ) - std::cout << "-------------About to built FileHelper" << std::endl; - - gdcm::FileHelper *fh = gdcm::FileHelper::New(fileToBuild); - - if( gdcm::Debug::GetDebugFlag() ) - std::cout << "-------------Finish to built FileHelper" << std::endl; - - fh->SetImageData(overlayPixelImage,nx*ny); - fh->SetWriteTypeToDcmExplVR(); - - str.str(""); - str<<"gdcmOverlay" << ".dcm"; - // Write the current 'overlay' file - - if( !fh->Write(str.str()) ) - { - std::cout << "Failed\n" - << "File in unwrittable\n"; - fh->Delete(); - if (fileToBuild) - fileToBuild->Delete(); - delete overlayPixelImage; - - return 0; - } - else - { - std::cout << "File written successfully" << std::endl; - } - - if (f) - fh->Delete(); - if (fileToBuild) - fileToBuild->Delete(); + unsigned int dimXY=dimX*dimY; + std::cout << "DimX : "<< dimX <<" DimY : " << dimY + << " DimXY : " <InsertEntryString(temp,0x0028,0x0011, "US"); // Columns + sprintf(temp,"%d ",dimY); + f2->InsertEntryString(temp,0x0028,0x0010, "US"); // Rows + + f2->InsertEntryString("8",0x0028,0x0100, "US"); // Bits Allocated + f2->InsertEntryString("8",0x0028,0x0101, "US"); // Bits Stored + f2->InsertEntryString("7",0x0028,0x0102, "US"); // High Bit + f2->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation + f2->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel + f2->InsertEntryString("MONOCHROME2 ",0x0028,0x0004, "LO"); + + + // feel free to add any field (Dicom Data Entry) you like, here. + // ... + GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f2); + + fh->SetImageData(outputData,dimXY); + fh->WriteDcmExplVR(outputFileName); + std::cout <<"End WriteOverlayImage" << std::endl; + + delete outputData; f->Delete(); - delete overlayPixelImage; + f2->Delete(); + fh->Delete(); - return 0; }