]> Creatis software - gdcm.git/blob - Example/exGrey2RGB.cxx
cc3d5e4b4c1f8cfb881f236e0054c9a3543a819b
[gdcm.git] / Example / exGrey2RGB.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exGrey2RGB.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/19 15:19:25 $
7   Version:   $Revision: 1.3 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmDocument.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmDebug.h"
24
25 #ifndef _WIN32
26 #include <unistd.h> //for access, unlink
27 #else
28 #include <io.h> //for _access
29 #endif
30
31 // return true if the file exists
32 bool FileExists(const char *filename)
33 {
34 #ifdef _MSC_VER
35 # define access _access
36 #endif
37 #ifndef R_OK
38 # define R_OK 04
39 #endif
40   if ( access(filename, R_OK) != 0 )
41     {
42     return false;
43     }
44   else
45     {
46     return true;
47     }
48 }
49
50 bool RemoveFile(const char *source)
51 {
52 #ifdef _MSC_VER
53 #define _unlink unlink
54 #endif
55   return unlink(source) != 0 ? false : true;
56 }
57
58 // Here we load a gdcmFile (8 or 16 Bits) and convert it into a 8 or 16 RGB file
59
60 int main(int argc, char *argv[])
61 {
62    if (argc < 3)
63    {
64       std::cerr << "Usage :" << std::endl << 
65       argv[0] << " input_dicom output_dicom" << std::endl;
66       return 1;
67    }
68
69    //gdcm::Debug::DebugOn();
70
71    std::string filename = argv[1];
72    std::string output   = argv[2];
73
74    if( FileExists( output.c_str() ) )
75    {
76       std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
77       if( !RemoveFile( output.c_str() ) )
78       {
79          std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
80          return 1;
81       }
82    }
83  
84    gdcm::FileHelper *fh = new gdcm::FileHelper( );
85    fh->SetFileName( filename );
86    fh->Load();
87       
88    size_t dataSize    = fh->GetImageDataSize();
89    uint8_t *imageData = fh->GetImageData();
90
91    uint8_t *imageDataRGB = new uint8_t[dataSize*3];
92
93    if (fh->GetFile()->GetEntryValue(0x0028,0x0100) == "8" )
94    {
95       for (unsigned int i=0;i<dataSize;i++)
96       {
97          imageDataRGB[i*3]=imageDataRGB[i*3+1]
98                           =imageDataRGB[i*3+2]
99                           =imageData[i]; 
100       }
101    }
102    else
103    {
104       for (unsigned int i=0;i<dataSize/2;i++)
105       {
106         //std::cout << i << std::endl;
107          ((uint16_t *)imageDataRGB)[i*3]=((uint16_t *)imageDataRGB)[i*3+1]
108                                         =((uint16_t *)imageDataRGB)[i*3+2]
109                                         =((uint16_t *)imageData)[i]; 
110       }
111    }
112    // Samples Per Pixel  
113    fh->GetFile()->InsertValEntry( "3 " ,0x0028,0x0002);
114    // Photometric Interpretation
115    fh->GetFile()->InsertValEntry( "RGB ",0x0028,0x0004 );
116    // Planar Configuration
117    fh->GetFile()->InsertValEntry( "1 ",0x0028,0x0006 );
118
119    // TODO  : free existing PixelData first !
120
121    fh->SetImageData(imageDataRGB, dataSize*3);   
122    fh->WriteDcmExplVR( output );
123
124    return 0;
125 }
126
127