]> Creatis software - gdcm.git/blob - Example/exImageLighten.cxx
COMP: Finish patching gdcm for support for bcb6
[gdcm.git] / Example / exImageLighten.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exImageLighten.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/06 15:49:31 $
7   Version:   $Revision: 1.2 $
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 #include <stdlib.h> // for exit
27
28 int main(int argc, char *argv[])
29 {  
30    gdcm::File *f1;
31  
32    std::cout << "-----------------------------------------------" << std::endl;
33    std::cout << "Removes from a full gdcm-readable  Dicom image"  << std::endl;
34    std::cout << " all the 'Shadow groups' and the 'Sequence' entries"
35           << " Warning :  'Compressed images' are uncompressed" 
36           << std::endl
37           << "         :  'YBR images' are converted into 'RGB images'"
38           << std::endl
39           << "         :  'RGB planes' are converted into 'RGB pixels'"
40           << std::endl
41           << "         :  'Palette Color images' are kept 'as is'"
42           << std::endl;
43    std::cout << "-----------------------------------------------" << std::endl;
44
45    if( argc < 3 )
46     {
47        std::cerr << "Usage " << argv[0]  << " Source image.dcm  " 
48                  << " Output image.dcm " << std::endl;
49        return 1;
50     }
51
52    std::string fileName       = argv[1];
53    std::string output         = argv[2];
54
55 // ============================================================
56 //   Read the input image.
57 // ============================================================
58
59    std::cout << argv[1] << std::endl;
60
61    f1 = new gdcm::File( fileName );
62    if (!f1->IsReadable()) {
63        std::cerr << "Sorry, " << fileName <<"  not a gdcm-readable "
64                  << "DICOM / ACR File"
65                  <<std::endl;
66        return 0;
67    }
68    std::cout << " ... is readable " << std::endl;
69
70 // ============================================================
71 //   Read the Pixels
72 //
73 // ============================================================
74  
75    // Pixel Reading must be done here, to be sure 
76    // to load the Palettes Color (if any)
77
78    // First, create a gdcm::FileHelper
79    gdcm::FileHelper *fh1 = new gdcm::FileHelper(f1);
80
81    // Load the pixels, DO NOT transform LUT (if any) into RGB Pixels 
82    uint8_t *imageDataRaw = fh1->GetImageDataRaw();
83    // Get the image data size
84    size_t dataRawSize    = fh1->GetImageDataRawSize();
85
86 // ============================================================
87 //   Create a new gdcm::Filehelper, to hold new image.
88 // ============================================================
89
90    gdcm::FileHelper *copy = new gdcm::FileHelper( output );
91
92 // ============================================================
93 //   Selective copy of the entries (including Pixel Element).
94 // ============================================================
95
96    gdcm::DocEntry *d = f1->GetFirstEntry();
97  
98    d = f1->GetFirstEntry();
99    while(d)
100    {
101       // We skip SeqEntries, since user cannot do much with them
102       if ( !(dynamic_cast<gdcm::SeqEntry*>(d))
103       // We skip Shadow Groups, since nobody knows what they mean
104            && !( d->GetGroup()%2 ) )
105       { 
106
107          if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
108          {              
109             copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
110                                              b->GetGroup(),b->GetElement(),
111                                              b->GetVR() );
112          }
113          else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
114          {   
115              copy->GetFile()->InsertValEntry( v->GetValue(),
116                                               v->GetGroup(),v->GetElement(),
117                                               v->GetVR() ); 
118          }
119          else
120          {
121           // We skip gdcm::SeqEntries
122          }
123       }
124       d = f1->GetNextEntry();
125    }
126    
127    // User wants to keep the Palette Color -if any- 
128    // and write the image as it was
129    copy->SetImageData(imageDataRaw, dataRawSize);
130    copy->SetWriteModeToRaw();
131    copy->WriteDcmExplVR( output );
132
133    std::cout << std::endl
134              << "------------------------------------------------------------"
135              << std::endl;
136    delete f1;
137    delete fh1;
138    delete copy;
139
140    exit (0);
141 }
142