]> Creatis software - gdcm.git/blob - Example/exOverlaysACR.cxx
Fix misstyping
[gdcm.git] / Example / exOverlaysACR.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exOverlaysACR.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/05/19 15:47:20 $
7   Version:   $Revision: 1.3 $
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 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23
24 #include <iostream>
25  #include <stdio.h>
26  
27  // WARNING :
28  // unfinished : DO NOT to be used as is !
29  
30  /*
31  // Example (sorry, we've got no more than this one ...)
32  
33 V 0028|0010[US] [Rows] [256] x(100)
34 V 0028|0011[US] [Columns] [256] x(100)
35 V 0028|0030[DS] [Pixel Spacing] [01.56\1.56]
36 V 0028|0100[US] [Bits Allocated] [16] x(10)
37 V 0028|0101[US] [Bits Stored] [12] x(c)
38 V 0028|0102[US] [High Bit] [11] x(b)
39 V 0028|0103[US] [Pixel Representation] [0] x(0)
40  
41 V 6000|0000[UL] [Group Length] [96] x(60)
42 V 6000|0010[US] [Rows] [256] x(100)
43 V 6000|0011[US] [Columns] [256] x(100)
44 V 6000|0040[CS] [Overlay Type] [R ]
45 V 6000|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
46 V 6000|0100[US] [Overlay Bits Allocated] [16] x(10)
47 V 6000|0102[US] [Overlay Bit Position] [12] x(c)
48 ...
49 ...
50 V 6006|0000[UL] [Group Length] [96] x(60)
51 V 6006|0010[US] [Rows] [256] x(100)
52 V 6006|0011[US] [Columns] [256] x(100)
53 V 6006|0040[CS] [Overlay Type] [R ]
54 V 6006|0050[SS] [Overlay Origin] [23601\8241] x(5c31)
55 V 6006|0100[US] [Overlay Bits Allocated] [16] x(10)
56 V 6006|0102[US] [Overlay Bit Position] [15] x(f)
57  */
58  
59 int main(int argc, char *argv[])
60 {  
61    gdcm::File *f1;
62  
63    //gdcm::Debug::DebugOn();
64
65    std::cout << "------------------------------------------------" << std::endl;
66    std::cout << "Gets the 'Overlays' from a full gdcm-readable ACR-NEMA "
67              << "uncompressed image" << std::endl;
68    std::cout << "Writes them in DicomV3 files named 'gdcmOverlay-xxx.dcm'"
69              << std::endl;
70    std::cout << "(Note :  we just have ONE image : "
71              << "SIEMENS_GBS_III-16-ACR_NEMA_1.acr)"
72              << std::endl;
73    std::cout << "------------------------------------------------" << std::endl;
74
75    std::string fileName;
76    if( argc > 1 )
77       fileName = argv[1];
78    else
79       fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";  
80
81    std::cout << fileName << std::endl;
82 // ============================================================
83 //   Read the input image.
84 // ============================================================
85
86    //std::cout << argv[1] << std::endl;
87
88    f1 = new gdcm::File( );
89
90    f1->SetLoadMode(NO_SEQ | NO_SHADOW);
91    f1->Load( fileName );
92
93    if( gdcm::Debug::GetDebugFlag() )
94    {
95       std::cout << "---------------------------------------------" << std::endl;
96       f1->Print();
97       std::cout << "---------------------------------------------" << std::endl;
98    }
99    if (!f1->IsReadable()) {
100        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
101            << "DICOM / ACR File"
102            <<std::endl;
103       delete f1;
104       return 0;
105    }
106    std::cout << " ... is readable " << std::endl;
107
108 // ============================================================
109 //   Check whether image contains Overlays ACR-NEMA style.
110 // ============================================================
111
112    int bitsAllocated = f1->GetBitsAllocated();
113    if ( bitsAllocated <= 8 )
114    {
115       std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
116       delete f1;
117       return 0;
118    }
119    std::string s1 = f1->GetEntryValue(0x6000, 0x0102);
120    if (s1 == gdcm::GDCM_UNFOUND)
121    {
122       std::cout << " Image doesn't contain any Overlay " << std::endl;
123       delete f1;
124       return 0;
125    }
126    std::cout << " File is read! " << std::endl;
127
128    
129 // ============================================================
130 //   Load the pixels in memory.
131 // ============================================================
132
133    // We don't use a gdcm::FileHelper, since it rubs out 
134    // the 'non image' bits of the pixels...
135
136    int nx = f1->GetXSize();
137    int ny = f1->GetYSize();
138  
139    std::cout << "Dimensions " << ny << "  " <<ny << std::endl;
140
141    gdcm::DocEntry *p = f1->GetDocEntry(f1->GetGrPixel(), f1->GetNumPixel());
142    if (p == 0)
143       std::cout << "Pixels element  not found" << std::endl;
144    else
145       std::cout << "Pixels element FOUND" << std::endl;
146
147    int offset = (int)(p->GetOffset());
148
149    std::cout << "Offset " << offset << std::endl;
150
151    FILE *fp = fopen(fileName.c_str(), "r");
152
153    if (fp == 0)
154    {
155       std::cout << "Unable to open File" << std::endl;
156       delete f1;
157       return 0;
158    }
159    else
160       std::cout << "File open successfully" << std::endl; 
161   
162    fseek(fp, (long) offset,SEEK_SET);      
163    uint16_t *pixels = new uint16_t[nx*ny];
164    size_t lgt = fread(pixels, 1,  nx*ny*sizeof( uint16_t) , fp );   
165
166    if ( lgt != (size_t)nx*ny*sizeof( uint16_t) )
167    {
168        std::cout << "Sorry, Pixels of" << fileName << "  are not "
169                  << "readable. expected length :" << nx*ny 
170                  << "  " << "read length : " << lgt
171                  << std::endl;
172        delete f1;
173        delete pixels;  
174        return 0;
175    }
176    else
177    {
178       std::cout << "Pixels read as expected : length = " << lgt << std::endl;
179    } 
180
181
182 // ============================================================
183 //   Get each overlay Bit into an image
184 // ============================================================
185                                          
186    uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
187    
188    uint16_t currentOvlGroup = 0x6000;
189    std::string strOvlBitPosition;
190    int ovlBitPosition;
191    uint16_t mask;
192    int i = 0;
193    uint16_t overlayLocation;
194    std::ostringstream str;
195    std::string strOverlayLocation;
196    gdcm::File *fileToBuild = 0;
197    gdcm::FileHelper *fh = 0;
198
199       
200 while ( (strOvlBitPosition = f1->GetEntryValue(currentOvlGroup, 0x0102)) 
201           != gdcm::GDCM_UNFOUND )
202 {
203
204       strOverlayLocation = f1->GetEntryValue(currentOvlGroup, 0x0200);
205       if ( strOverlayLocation != gdcm::GDCM_UNFOUND )
206       {
207          overlayLocation = atoi(strOverlayLocation.c_str());
208          if ( overlayLocation != f1->GetGrPixel() )
209          {
210             std::cout << "Big Trouble : Overlays are NOT in the Pixels Group "
211                       << std::hex << "(" << overlayLocation << " vs " 
212                       << f1->GetGrPixel() << std::endl;
213             // Actually, here, we should (try to) read the overlay location
214             // and go on the job.
215             continue;
216          }
217       }
218       ovlBitPosition = atoi(strOvlBitPosition.c_str());
219       mask = 1 << ovlBitPosition; 
220       std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;          
221       for (int j=0; j<nx*ny ; j++)
222       {
223          if( gdcm::Debug::GetDebugFlag() )
224             if (pixels[j] >= 0x1000)// if it contains at least one overlay bit
225                printf("%d : %04x\n",j, pixels[j]);
226
227          if ( (pixels[j] & mask) == 0 )
228             tabPixels[j] = 0;
229          else
230             tabPixels[j] = 128;
231       }
232       if( gdcm::Debug::GetDebugFlag() )
233          std::cout << "About to built empty file"  << std::endl;
234
235       fileToBuild = new gdcm::File();
236
237       if( gdcm::Debug::GetDebugFlag() )
238          std::cout << "Finish to built empty file"  << std::endl;
239
240       str.str("");
241       str << nx;
242       fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
243       str.str("");
244       str << ny;
245       fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
246
247       fileToBuild->InsertValEntry("8",0x0028,0x0100); // Bits Allocated
248       fileToBuild->InsertValEntry("8",0x0028,0x0101); // Bits Stored
249       fileToBuild->InsertValEntry("7",0x0028,0x0102); // High Bit
250       fileToBuild->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
251       fileToBuild->InsertValEntry("1",0x0028,0x0002); // Samples per Pixel
252
253       fileToBuild->InsertValEntry("MONOCHROME2 ",0x0028,0x0004);
254       // Other mandatory fields will be set automatically,
255       // just before Write(), by FileHelper::CheckMandatoryElements()
256
257       if( gdcm::Debug::GetDebugFlag() )
258          std::cout << "-------------About to built FileHelper"  << std::endl;
259
260       fh = new gdcm::FileHelper(fileToBuild);
261
262       if( gdcm::Debug::GetDebugFlag() )
263          std::cout << "-------------Finish to built FileHelper"  << std::endl;
264
265       fh->SetImageData(tabPixels,nx*ny);
266       fh->SetWriteTypeToDcmExplVR();
267
268       str.str("");
269       str<<"gdcmOverlay-"<<i << ".dcm";
270       //   Write the current 'overlay' file
271
272       if( !fh->Write(str.str()) )
273       {
274          std::cout << "Failed\n"
275                    << "File in unwrittable\n";
276          delete fh;
277          if (fileToBuild)
278             delete fileToBuild;
279          delete pixels;
280          delete tabPixels;
281          return 0;
282       }
283       else
284       {
285          std::cout << "File written successfully" << std::endl;
286       }
287       currentOvlGroup += 2;
288       i++;
289    }
290     
291    delete f1;
292    if (f1)
293       delete fh;
294    if (fileToBuild)
295       delete fileToBuild;
296    delete pixels;
297    delete tabPixels;
298    return 0;
299 }
300