1 /*=========================================================================
4 Module: $RCSfile: exOverlaysACR.cxx,v $
6 Date: $Date: 2005/07/07 17:31:54 $
7 Version: $Revision: 1.4 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
28 // unfinished : DO NOT to be used as is !
31 // Example (sorry, we've got no more than this one ...)
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)
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)
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)
59 int main(int argc, char *argv[])
63 //gdcm::Debug::DebugOn();
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'"
70 std::cout << "(Note : we just have ONE image : "
71 << "SIEMENS_GBS_III-16-ACR_NEMA_1.acr)"
73 std::cout << "------------------------------------------------" << std::endl;
79 fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";
81 std::cout << fileName << std::endl;
82 // ============================================================
83 // Read the input image.
84 // ============================================================
86 //std::cout << argv[1] << std::endl;
88 f = new gdcm::File( );
90 f->SetLoadMode(NO_SEQ | NO_SHADOW);
91 f->SetFileName( fileName );
94 if( gdcm::Debug::GetDebugFlag() )
96 std::cout << "---------------------------------------------" << std::endl;
98 std::cout << "---------------------------------------------" << std::endl;
101 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
102 << "DICOM / ACR File"
107 std::cout << " ... is readable " << std::endl;
109 // ============================================================
110 // Check whether image contains Overlays ACR-NEMA style.
111 // ============================================================
113 int bitsAllocated = f->GetBitsAllocated();
114 if ( bitsAllocated <= 8 )
116 std::cout << " 8 bits pixel image cannot contain Overlays " << std::endl;
120 std::string s1 = f->GetEntryValue(0x6000, 0x0102);
121 if (s1 == gdcm::GDCM_UNFOUND)
123 std::cout << " Image doesn't contain any Overlay " << std::endl;
127 std::cout << " File is read! " << std::endl;
130 // ============================================================
131 // Load the pixels in memory.
132 // ============================================================
134 // We don't use a gdcm::FileHelper, since it rubs out
135 // the 'non image' bits of the pixels...
137 int nx = f->GetXSize();
138 int ny = f->GetYSize();
140 std::cout << "Dimensions " << ny << " " <<ny << std::endl;
142 gdcm::DocEntry *p = f->GetDocEntry(f->GetGrPixel(), f->GetNumPixel());
144 std::cout << "Pixels element not found" << std::endl;
146 std::cout << "Pixels element FOUND" << std::endl;
148 int offset = (int)(p->GetOffset());
150 std::cout << "Offset " << offset << std::endl;
152 FILE *fp = fopen(fileName.c_str(), "r");
156 std::cout << "Unable to open File" << std::endl;
161 std::cout << "File open successfully" << std::endl;
163 fseek(fp, (long) offset,SEEK_SET);
164 uint16_t *pixels = new uint16_t[nx*ny];
165 size_t lgt = fread(pixels, 1, nx*ny*sizeof( uint16_t) , fp );
167 if ( lgt != (size_t)nx*ny*sizeof( uint16_t) )
169 std::cout << "Sorry, Pixels of" << fileName << " are not "
170 << "readable. expected length :" << nx*ny
171 << " " << "read length : " << lgt
179 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
183 // ============================================================
184 // Get each overlay Bit into an image
185 // ============================================================
187 uint8_t *tabPixels = new uint8_t[nx*ny]; // uint8 is enought to hold 1 bit !
189 uint16_t currentOvlGroup = 0x6000;
190 std::string strOvlBitPosition;
194 uint16_t overlayLocation;
195 std::ostringstream str;
196 std::string strOverlayLocation;
197 gdcm::File *fileToBuild = 0;
198 gdcm::FileHelper *fh = 0;
201 while ( (strOvlBitPosition = f->GetEntryValue(currentOvlGroup, 0x0102))
202 != gdcm::GDCM_UNFOUND )
205 strOverlayLocation = f->GetEntryValue(currentOvlGroup, 0x0200);
206 if ( strOverlayLocation != gdcm::GDCM_UNFOUND )
208 overlayLocation = atoi(strOverlayLocation.c_str());
209 if ( overlayLocation != f->GetGrPixel() )
211 std::cout << "Big Trouble : Overlays are NOT in the Pixels Group "
212 << std::hex << "(" << overlayLocation << " vs "
213 << f->GetGrPixel() << std::endl;
214 // Actually, here, we should (try to) read the overlay location
215 // and go on the job.
219 ovlBitPosition = atoi(strOvlBitPosition.c_str());
220 mask = 1 << ovlBitPosition;
221 std::cout << "Mask :[" <<std::hex << mask << "]" << std::endl;
222 for (int j=0; j<nx*ny ; j++)
224 if( gdcm::Debug::GetDebugFlag() )
225 if (pixels[j] >= 0x1000)// if it contains at least one overlay bit
226 printf("%d : %04x\n",j, pixels[j]);
228 if ( (pixels[j] & mask) == 0 )
233 if( gdcm::Debug::GetDebugFlag() )
234 std::cout << "About to built empty file" << std::endl;
236 fileToBuild = new gdcm::File();
238 if( gdcm::Debug::GetDebugFlag() )
239 std::cout << "Finish to built empty file" << std::endl;
243 fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
246 fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
248 fileToBuild->InsertValEntry("8",0x0028,0x0100); // Bits Allocated
249 fileToBuild->InsertValEntry("8",0x0028,0x0101); // Bits Stored
250 fileToBuild->InsertValEntry("7",0x0028,0x0102); // High Bit
251 fileToBuild->InsertValEntry("0",0x0028,0x0103); // Pixel Representation
252 fileToBuild->InsertValEntry("1",0x0028,0x0002); // Samples per Pixel
254 fileToBuild->InsertValEntry("MONOCHROME2 ",0x0028,0x0004);
255 // Other mandatory fields will be set automatically,
256 // just before Write(), by FileHelper::CheckMandatoryElements()
258 if( gdcm::Debug::GetDebugFlag() )
259 std::cout << "-------------About to built FileHelper" << std::endl;
261 fh = new gdcm::FileHelper(fileToBuild);
263 if( gdcm::Debug::GetDebugFlag() )
264 std::cout << "-------------Finish to built FileHelper" << std::endl;
266 fh->SetImageData(tabPixels,nx*ny);
267 fh->SetWriteTypeToDcmExplVR();
270 str<<"gdcmOverlay-"<<i << ".dcm";
271 // Write the current 'overlay' file
273 if( !fh->Write(str.str()) )
275 std::cout << "Failed\n"
276 << "File in unwrittable\n";
286 std::cout << "File written successfully" << std::endl;
288 currentOvlGroup += 2;