]> Creatis software - gdcm.git/blob - Example/exColorToRGB.cxx
Fix mistypings
[gdcm.git] / Example / exColorToRGB.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exColorToRGB.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:05 $
7   Version:   $Revision: 1.10 $
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
19 #include "gdcmFile.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmDocument.h"
22 #include "gdcmDataEntry.h"
23 #include "gdcmSeqEntry.h"
24
25 #include <stdlib.h> // for exit
26
27 int main(int argc, char *argv[])
28 {
29
30    std::cout << "------------------------------------------------" << std::endl;
31    std::cout << "Tranforms a full gdcm-readable 'color' Dicom image "
32           << " (e.g Palette Color, YBR, 3-Planes RGB) "
33           << " into an 'RGB_Pixels' Dicom Image "
34           << " "            << std::endl;
35
36    if (argc < 3)
37    {
38       std::cerr << "Usage :" << std::endl << 
39       argv[0] << " input_dicom output_dicom" << std::endl;
40       return 1;
41    }
42
43    std::string fileName = argv[1];
44    std::string output   = argv[2];
45
46
47 // ============================================================
48 //   Read the input image.
49 // ============================================================
50    // a GDCM_NAME_SPACE::File contains all the Dicom Field but the Pixels Element
51
52    std::cout << argv[1] << std::endl;
53
54    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
55    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
56    f->SetFileName( fileName );
57    bool res = f->Load();        
58
59    if (!res) 
60    {
61        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
62                  << "DICOM / ACR File"
63                  <<std::endl;
64        f->Delete();
65        return 0;
66    }
67    std::cout << " ... is readable " << std::endl;
68
69 /*
70    if (!f->IsMonochrome()) 
71    {
72        std::cerr << "Sorry, " << fileName <<"  not a 'color' File "
73                  << " "
74                  <<std::endl;
75        f->Delete();
76        return 0;
77    }
78 */
79
80 // ============================================================
81 //   Load the pixels in memory.
82 // ============================================================
83
84    // We need a GDCM_NAME_SPACE::FileHelper, since we want to load the pixels        
85    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
86
87    // uint8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
88    // It's just for prototyping.
89    // Feel free to cast it.
90
91    uint8_t *imageData = fh->GetImageData();
92
93    if ( imageData == 0 )
94    {
95        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
96                  << " gdcm-readable."       << std::endl;
97        f->Delete();
98        return 0;
99    }
100  
101
102    // ------ User wants write a new image without shadow groups -------------
103    // ------                              without Sequences     -------------
104
105  
106    GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
107    copy->SetFileName( output );
108    copy->Load();
109  
110    GDCM_NAME_SPACE::DocEntry *d = f->GetFirstEntry();
111    while(d)
112    {
113       // We skip SeqEntries, since user cannot do much with them
114       if ( !(dynamic_cast<GDCM_NAME_SPACE::SeqEntry*>(d))
115       // We skip Shadow Groups, since nobody knows what they mean
116            && !( d->GetGroup()%2 ) )
117       { 
118
119          if ( GDCM_NAME_SPACE::DataEntry *de = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(d) )
120          {              
121             copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
122                                                  de->GetGroup(),de->GetElement(),
123                                                  de->GetVR() );
124          }
125          else
126          {
127           // We skip GDCM_NAME_SPACE::SeqEntries
128          }
129       }
130       d = f->GetNextEntry();
131    }
132  
133    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
134    // and wants to write it as RGB
135    copy->SetImageData(imageData, fh->GetImageDataSize());
136    copy->SetWriteModeToRGB();
137
138    copy->WriteDcmExplVR( output );
139
140    f->Delete();
141    fh->Delete();
142    copy->Delete();
143
144    exit (0);
145 }
146