]> Creatis software - gdcm.git/blob - Example/exGC.cxx
Should avoid some warning from Borland Compiler
[gdcm.git] / Example / exGC.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exGC.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/04/20 11:25:35 $
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
19 #include "gdcmFile.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmDocument.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmBinEntry.h"
24 #include "gdcmSeqEntry.h"
25
26 typedef struct  // Maybe we should add it to gdcm ?
27 {
28    uint8_t r;
29    uint8_t g;
30    uint8_t b;
31 } rgb8_t;
32
33
34 // This small application, for a given Creatis user (G.C.)
35 // may be taken as an example
36
37 int main(int argc, char *argv[])
38 {
39
40    // we need a user friendly way for passign parameters on the command line !
41
42    std::cout << "------------------------------------------------" << std::endl;
43    std::cout << "Transforms a full gdcm-readable 'color' Dicom image "
44           << " (e.g Palette Color, YBR, 3-Planes RGB) "
45           << " into an 'RGB_Pixels' Dicom Image " << std::endl
46           << " Blacks out any 'grey' pixel (r=g=b) "
47           << " Blacks out any 'dark' pixel (r,g,b < threshold) " 
48           << " "            << std::endl;
49    std::cout << "------------------------------------------------" << std::endl;
50
51    if (argc < 3)
52    {
53       std::cerr << "Usage :" << std::endl << 
54       argv[0] << " input_dicom output_dicom threshold background" << std::endl;
55       return 1;
56    }
57
58    std::string fileName = argv[1];
59    std::string output   = argv[2];
60
61    int threshold = 0;
62    if (argc > 3)
63       threshold        = atoi( argv[3] );
64   
65    int background = 0;
66    if (argc > 4)
67       background        = atoi( argv[4] );
68
69 // ============================================================
70 //   Read the input image.
71 // ============================================================
72    // a gdcm::File contains all the Dicom Field but the Pixels Element
73
74    gdcm::File *f1= new gdcm::File( fileName );
75
76
77    std::cout << argv[1] << std::endl;
78
79    f1 = new gdcm::File( fileName );
80    if (!f1->IsReadable()) {
81        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
82                  << "DICOM / ACR File"
83                  <<std::endl;
84        return 0;
85    }
86    std::cout << " ... is readable " << std::endl;
87
88 /*
89    if (!f1->IsMonochrome()) {
90        std::cerr << "Sorry, " << fileName <<"  not a 'color' File "
91            << " "
92                  <<std::endl;
93        return 0;
94    }
95 */
96
97 // ============================================================
98 //   Load the pixels in memory.
99 // ============================================================
100
101    // We need a gdcm::FileHelper, since we want to load the pixels        
102    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
103
104    // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one) 
105
106    uint8_t *imageData = fh1->GetImageData();
107
108    if ( imageData == 0 )
109    {
110        std::cerr << "Sorry, Pixels of" << fileName <<"  are not "
111                  << " gdcm-readable."  << std::endl;
112        return 0;
113    }
114
115    // ------ User wants write a new image without shadow groups -------------
116    // ------                              without Sequences     -------------
117
118  
119    gdcm::FileHelper *copy = new gdcm::FileHelper( output );
120  
121    gdcm::DocEntry *d = f1->GetFirstEntry();
122    while(d)
123    {
124       // We skip SeqEntries, since user cannot do much with them
125       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
126       // We skip Shadow Groups, since nobody knows what they mean
127            && !( d->GetGroup()%2 ) )
128       { 
129
130          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
131          {              
132             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
133                                              b->GetGroup(),b->GetElement(),
134                                              b->GetVR() );
135          }
136          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
137          {   
138              copy->GetFile()->InsertValEntry( v->GetValue(),
139                                               v->GetGroup(),v->GetElement(),
140                                               v->GetVR() ); 
141          }
142          else
143          {
144           // We skip gdcm::SeqEntries
145          }
146       }
147       d = f1->GetNextEntry();
148    }
149
150    int imageSize = fh1->GetImageDataSize();
151 // Black up all 'grey' pixels
152    int i;
153    int n = 0;
154    for (i = 0; i<imageSize/3; i++)
155    {
156       if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
157          &&
158            ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
159       {
160          n++;
161          ((rgb8_t *)imageData)[i].r = (unsigned char)background;
162          ((rgb8_t *)imageData)[i].g = (unsigned char)background;
163          ((rgb8_t *)imageData)[i].b = (unsigned char)background;
164       }
165    }
166    
167     std::cout << n << " points put to black (within " 
168               << imageSize/3 << ")" << std::endl;
169
170    n = 0;
171    for (i = 0; i<imageSize/3; i++)
172    {
173    if ( ((rgb8_t *)imageData)[i].r < threshold
174      &&
175         ((rgb8_t *)imageData)[i].g < threshold
176      &&
177         ((rgb8_t *)imageData)[i].b < threshold )
178       {
179          n++;
180         ((rgb8_t *)imageData)[i].r = (unsigned char)background;
181         ((rgb8_t *)imageData)[i].g = (unsigned char)background;
182         ((rgb8_t *)imageData)[i].b = (unsigned char)background;  
183       }
184    }
185    
186    std::cout << n << " points put to black (within " 
187              << imageSize/3 << ")" << std::endl; 
188    // User knows the image is a 'color' one -RGB, YBR, Palette Color-
189    // and wants to write it as RGB
190    copy->SetImageData(imageData, imageSize);
191    copy->SetWriteModeToRGB();
192
193    copy->WriteDcmExplVR( output );
194
195    delete f1;
196    delete fh1;
197    delete copy;
198
199    exit (0);
200 }