1 /*=========================================================================
4 Module: $RCSfile: exGrey2RGB.cxx,v $
6 Date: $Date: 2005/06/15 10:06:36 $
7 Version: $Revision: 1.1 $
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 "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmDebug.h"
26 #include <unistd.h> //for access, unlink
28 #include <io.h> //for _access
31 // return true if the file exists
32 bool FileExists(const char *filename)
35 # define access _access
40 if ( access(filename, R_OK) != 0 )
50 bool RemoveFile(const char *source)
53 #define _unlink unlink
55 return unlink(source) != 0 ? false : true;
58 // Here we load a gdcmFile (8 or 16 Bits) and convert it into a 8 or 16 RGB file
60 int main(int argc, char *argv[])
64 std::cerr << "Usage :" << std::endl <<
65 argv[0] << " input_dicom output_dicom" << std::endl;
69 //gdcm::Debug::DebugOn();
71 std::string filename = argv[1];
72 std::string output = argv[2];
74 if( FileExists( output.c_str() ) )
76 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
77 if( !RemoveFile( output.c_str() ) )
79 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
84 gdcm::FileHelper *fh = new gdcm::FileHelper( 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()->GetEntryValue(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()->InsertValEntry( "3 " ,0x0028,0x0002);
112 // Photometric Interpretation
113 fh->GetFile()->InsertValEntry( "RGB ",0x0028,0x0004 );
114 // Planar Configuration
115 fh->GetFile()->InsertValEntry( "1 ",0x0028,0x0006 );
117 // TODO : free existing PixelData first !
119 fh->SetImageData(imageDataRGB, dataSize*3);
120 fh->WriteDcmExplVR( output );