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