From 031dab1266be5850ffa788ebe4fdc3fe2b6c2511 Mon Sep 17 00:00:00 2001 From: jpr Date: Wed, 15 Jun 2005 10:06:36 +0000 Subject: [PATCH] Add a user supplied example 'Grey to RGB' that converts a 8/16 Bits 'grey level' image (Samples per Pixel = 1) into an 'RGB image' (Samples per Pixel = 3) Result may be usefull for performing some checks --- Example/CMakeLists.txt | 1 + Example/exGrey2RGB.cxx | 125 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 Example/exGrey2RGB.cxx diff --git a/Example/CMakeLists.txt b/Example/CMakeLists.txt index b03bbe76..814cdb46 100644 --- a/Example/CMakeLists.txt +++ b/Example/CMakeLists.txt @@ -12,6 +12,7 @@ SET(EXAMPLE_SOURCES exAnonymize exAnonymizeNoLoad # without loading the Pixel Data exColorToRGB + exGrey2RGB exGC exImageLighten exOverlaysACR diff --git a/Example/exGrey2RGB.cxx b/Example/exGrey2RGB.cxx new file mode 100644 index 00000000..b096ad50 --- /dev/null +++ b/Example/exGrey2RGB.cxx @@ -0,0 +1,125 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: exGrey2RGB.cxx,v $ + Language: C++ + Date: $Date: 2005/06/15 10:06:36 $ + 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 "gdcmDocument.h" +#include "gdcmValEntry.h" +#include "gdcmBinEntry.h" +#include "gdcmDebug.h" + +#ifndef _WIN32 +#include //for access, unlink +#else +#include //for _access +#endif + +// return true if the file exists +bool FileExists(const char *filename) +{ +#ifdef _MSC_VER +# define access _access +#endif +#ifndef R_OK +# define R_OK 04 +#endif + if ( access(filename, R_OK) != 0 ) + { + return false; + } + else + { + return true; + } +} + +bool RemoveFile(const char *source) +{ +#ifdef _MSC_VER +#define _unlink unlink +#endif + return unlink(source) != 0 ? false : true; +} + +// Here we load a gdcmFile (8 or 16 Bits) and convert it into a 8 or 16 RGB file + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + std::cerr << "Usage :" << std::endl << + argv[0] << " input_dicom output_dicom" << std::endl; + return 1; + } + + //gdcm::Debug::DebugOn(); + + std::string filename = argv[1]; + std::string output = argv[2]; + + if( FileExists( output.c_str() ) ) + { + std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl; + if( !RemoveFile( output.c_str() ) ) + { + std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl; + return 1; + } + } + + gdcm::FileHelper *fh = new gdcm::FileHelper( filename ); + + size_t dataSize = fh->GetImageDataSize(); + uint8_t *imageData = fh->GetImageData(); + + uint8_t *imageDataRGB = new uint8_t[dataSize*3]; + + if (fh->GetFile()->GetEntryValue(0x0028,0x0100) == "8" ) + { + for (unsigned int i=0;iGetFile()->InsertValEntry( "3 " ,0x0028,0x0002); + // Photometric Interpretation + fh->GetFile()->InsertValEntry( "RGB ",0x0028,0x0004 ); + // Planar Configuration + fh->GetFile()->InsertValEntry( "1 ",0x0028,0x0006 ); + + // TODO : free existing PixelData first ! + + fh->SetImageData(imageDataRGB, dataSize*3); + fh->WriteDcmExplVR( output ); + + return 0; +} + + -- 2.45.1