1 /*=========================================================================
4 Module: $RCSfile: exGC.cxx,v $
6 Date: $Date: 2005/07/07 17:31:54 $
7 Version: $Revision: 1.5 $
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.
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.
17 =========================================================================*/
20 #include "gdcmFileHelper.h"
21 #include "gdcmDocument.h"
22 #include "gdcmValEntry.h"
23 #include "gdcmBinEntry.h"
24 #include "gdcmSeqEntry.h"
26 #include <stdlib.h> // for exit
28 typedef struct // Maybe we should add it to gdcm ?
36 // This small application, for a given Creatis user (G.C.)
37 // may be taken as an example
39 int main(int argc, char *argv[])
42 // we need a user friendly way for passign parameters on the command line !
44 std::cout << "------------------------------------------------" << std::endl;
45 std::cout << "Transforms a full gdcm-readable 'color' Dicom image "
46 << " (e.g Palette Color, YBR, 3-Planes RGB) "
47 << " into an 'RGB_Pixels' Dicom Image " << std::endl
48 << " Blacks out any 'grey' pixel (r=g=b) "
49 << " Blacks out any 'dark' pixel (r,g,b < threshold) "
51 std::cout << "------------------------------------------------" << std::endl;
55 std::cerr << "Usage :" << std::endl <<
56 argv[0] << " input_dicom output_dicom threshold background" << std::endl;
60 std::string fileName = argv[1];
61 std::string output = argv[2];
65 threshold = atoi( argv[3] );
69 background = atoi( argv[4] );
71 // ============================================================
72 // Read the input image.
73 // ============================================================
74 // a gdcm::File contains all the Dicom Field but the Pixels Element
76 std::cout << argv[1] << std::endl;
78 gdcm::File *f = new gdcm::File();
79 f->SetLoadMode( 0x00000000);
80 f->SetFileName( fileName );
84 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
89 std::cout << " ... is readable " << std::endl;
92 if (!f->IsMonochrome()) {
93 std::cerr << "Sorry, " << fileName <<" not a 'color' File "
100 // ============================================================
101 // Load the pixels in memory.
102 // ============================================================
104 // We need a gdcm::FileHelper, since we want to load the pixels
105 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
107 // (unit8_t DOESN'T mean it's mandatory for the image to be a 8 bits one)
109 uint8_t *imageData = fh->GetImageData();
111 if ( imageData == 0 )
113 std::cerr << "Sorry, Pixels of" << fileName <<" are not "
114 << " gdcm-readable." << std::endl;
118 // ------ User wants write a new image without shadow groups -------------
119 // ------ without Sequences -------------
122 gdcm::FileHelper *copy = new gdcm::FileHelper( output );
124 gdcm::DocEntry *d = f->GetFirstEntry();
127 // We skip SeqEntries, since user cannot do much with them
128 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
129 // We skip Shadow Groups, since nobody knows what they mean
130 && !( d->GetGroup()%2 ) )
133 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
135 copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
136 b->GetGroup(),b->GetElement(),
139 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
141 copy->GetFile()->InsertValEntry( v->GetValue(),
142 v->GetGroup(),v->GetElement(),
147 // We skip gdcm::SeqEntries
150 d = f->GetNextEntry();
153 int imageSize = fh->GetImageDataSize();
154 // Black up all 'grey' pixels
157 for (i = 0; i<imageSize/3; i++)
159 if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
161 ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
164 ((rgb8_t *)imageData)[i].r = (unsigned char)background;
165 ((rgb8_t *)imageData)[i].g = (unsigned char)background;
166 ((rgb8_t *)imageData)[i].b = (unsigned char)background;
170 std::cout << n << " points put to black (within "
171 << imageSize/3 << ")" << std::endl;
174 for (i = 0; i<imageSize/3; i++)
176 if ( ((rgb8_t *)imageData)[i].r < threshold
178 ((rgb8_t *)imageData)[i].g < threshold
180 ((rgb8_t *)imageData)[i].b < threshold )
183 ((rgb8_t *)imageData)[i].r = (unsigned char)background;
184 ((rgb8_t *)imageData)[i].g = (unsigned char)background;
185 ((rgb8_t *)imageData)[i].b = (unsigned char)background;
189 std::cout << n << " points put to black (within "
190 << imageSize/3 << ")" << std::endl;
191 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
192 // and wants to write it as RGB
193 copy->SetImageData(imageData, imageSize);
194 copy->SetWriteModeToRGB();
196 copy->WriteDcmExplVR( output );