1 /*=========================================================================
4 Module: $RCSfile: exGC.cxx,v $
6 Date: $Date: 2007/05/23 14:18:05 $
7 Version: $Revision: 1.11 $
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 "gdcmDataEntry.h"
23 #include "gdcmSeqEntry.h"
25 #include <stdlib.h> // for exit
27 typedef struct // Maybe we should add it to gdcm ?
35 // This small application, for a given Creatis user (G.C.)
36 // may be taken as an example
38 int main(int argc, char *argv[])
41 // we need a user friendly way for passign parameters on the command line !
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) "
50 std::cout << "------------------------------------------------" << std::endl;
54 std::cerr << "Usage :" << std::endl <<
55 argv[0] << " input_dicom output_dicom threshold background" << std::endl;
59 std::string fileName = argv[1];
60 std::string output = argv[2];
64 threshold = atoi( argv[3] );
68 background = atoi( argv[4] );
70 // ============================================================
71 // Read the input image.
72 // ============================================================
73 // a GDCM_NAME_SPACE::File contains all the Dicom Field but the Pixels Element
75 std::cout << argv[1] << std::endl;
77 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
78 f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
79 f->SetFileName( fileName );
83 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_NAME_SPACE::FileHelper, since we want to load the pixels
105 GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(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;
120 // ------ User wants write a new image without shadow groups -------------
121 // ------ without Sequences -------------
124 GDCM_NAME_SPACE::FileHelper *copy = GDCM_NAME_SPACE::FileHelper::New( );
125 copy->SetFileName( output );
128 GDCM_NAME_SPACE::DocEntry *d = f->GetFirstEntry();
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 ) )
137 if ( GDCM_NAME_SPACE::DataEntry *de = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(d) )
139 copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
140 de->GetGroup(),de->GetElement(),
145 // We skip GDCM_NAME_SPACE::SeqEntries
148 d = f->GetNextEntry();
151 int imageSize = fh->GetImageDataSize();
152 // Black up all 'grey' pixels
155 for (i = 0; i<imageSize/3; i++)
157 if ( ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].g
159 ((rgb8_t *)imageData)[i].r == ((rgb8_t *)imageData)[i].b )
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;
168 std::cout << n << " points put to black (within "
169 << imageSize/3 << ")" << std::endl;
172 for (i = 0; i<imageSize/3; i++)
174 if ( ((rgb8_t *)imageData)[i].r < threshold
176 ((rgb8_t *)imageData)[i].g < threshold
178 ((rgb8_t *)imageData)[i].b < threshold )
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;
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();
194 copy->WriteDcmExplVR( output );