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