1 /*=========================================================================
4 Module: $RCSfile: exGrey2RGB.cxx,v $
6 Date: $Date: 2007/05/23 14:18:05 $
7 Version: $Revision: 1.6 $
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 "gdcmDocument.h"
21 #include "gdcmDebug.h"
24 #include <unistd.h> //for access, unlink
26 #include <io.h> //for _access
29 // return true if the file exists
30 bool FileExists(const char *filename)
33 # define access _access
38 if ( access(filename, R_OK) != 0 )
48 bool RemoveFile(const char *source)
51 #define _unlink unlink
53 return unlink(source) != 0 ? false : true;
56 // Here we load a gdcmFile (8 or 16 Bits) and convert it into a 8 or 16 RGB file
58 int main(int argc, char *argv[])
62 std::cerr << "Usage :" << std::endl <<
63 argv[0] << " input_dicom output_dicom" << std::endl;
67 //GDCM_NAME_SPACE::Debug::DebugOn();
69 std::string filename = argv[1];
70 std::string output = argv[2];
72 if( FileExists( output.c_str() ) )
74 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
75 if( !RemoveFile( output.c_str() ) )
77 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
82 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New( );
83 fh->SetFileName( filename );
86 size_t dataSize = fh->GetImageDataSize();
87 uint8_t *imageData = fh->GetImageData();
89 uint8_t *imageDataRGB = new uint8_t[dataSize*3];
91 if (fh->GetFile()->GetEntryString(0x0028,0x0100) == "8" )
93 for (unsigned int i=0;i<dataSize;i++)
95 imageDataRGB[i*3]=imageDataRGB[i*3+1]
102 for (unsigned int i=0;i<dataSize/2;i++)
104 //std::cout << i << std::endl;
105 ((uint16_t *)imageDataRGB)[i*3]=((uint16_t *)imageDataRGB)[i*3+1]
106 =((uint16_t *)imageDataRGB)[i*3+2]
107 =((uint16_t *)imageData)[i];
111 fh->GetFile()->InsertEntryString( "3 " ,0x0028,0x0002);
112 // Photometric Interpretation
113 fh->GetFile()->InsertEntryString( "RGB ",0x0028,0x0004 );
114 // Planar Configuration
115 fh->GetFile()->InsertEntryString( "1 ",0x0028,0x0006 );
117 // TODO : free existing PixelData first !
119 fh->SetImageData(imageDataRGB, dataSize*3);
120 fh->WriteDcmExplVR( output );