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