1 /*=========================================================================
4 Module: $RCSfile: exColorToRGB.cxx,v $
6 Date: $Date: 2005/10/18 08:35:43 $
7 Version: $Revision: 1.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.
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 int main(int argc, char *argv[])
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 "
38 std::cerr << "Usage :" << std::endl <<
39 argv[0] << " input_dicom output_dicom" << std::endl;
43 std::string fileName = argv[1];
44 std::string output = argv[2];
47 // ============================================================
48 // Read the input image.
49 // ============================================================
50 // a gdcm::File contains all the Dicom Field but the Pixels Element
52 std::cout << argv[1] << std::endl;
54 gdcm::File *f = new gdcm::File();
55 f->SetLoadMode( gdcm::LD_ALL);
56 f->SetFileName( fileName );
60 std::cerr << "Sorry, " << fileName <<" not a gdcm-readable "
65 std::cout << " ... is readable " << std::endl;
68 if (!f->IsMonochrome()) {
69 std::cerr << "Sorry, " << fileName <<" not a 'color' File "
76 // ============================================================
77 // Load the pixels in memory.
78 // ============================================================
80 // We need a gdcm::FileHelper, since we want to load the pixels
81 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
83 // uint8_t DOESN'T mean it's mandatory for the image to be a 8 bits one !
84 // It's just for prototyping.
85 // Feel free to cast it.
87 uint8_t *imageData = fh->GetImageData();
91 std::cerr << "Sorry, Pixels of" << fileName <<" are not "
92 << " gdcm-readable." << std::endl;
97 // ------ User wants write a new image without shadow groups -------------
98 // ------ without Sequences -------------
101 gdcm::FileHelper *copy = new gdcm::FileHelper( );
102 copy->SetFileName( output );
105 gdcm::DocEntry *d = f->GetFirstEntry();
108 // We skip SeqEntries, since user cannot do much with them
109 if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
110 // We skip Shadow Groups, since nobody knows what they mean
111 && !( d->GetGroup()%2 ) )
114 if ( gdcm::DataEntry *de = dynamic_cast<gdcm::DataEntry *>(d) )
116 copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
117 de->GetGroup(),de->GetElement(),
122 // We skip gdcm::SeqEntries
125 d = f->GetNextEntry();
128 // User knows the image is a 'color' one -RGB, YBR, Palette Color-
129 // and wants to write it as RGB
130 copy->SetImageData(imageData, fh->GetImageDataSize());
131 copy->SetWriteModeToRGB();
133 copy->WriteDcmExplVR( output );