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