1 /*=========================================================================
4 Module: $RCSfile: exOverlaysDCM.cxx,v $
6 Date: $Date: 2005/12/09 10:17:52 $
7 Version: $Revision: 1.1 $
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"
23 #include "gdcmDataEntry.h"
26 #include <stdio.h> // for fseek... FIXME
27 #include <stdlib.h> // for atoi
30 // unfinished : DO NOT to be used as is !
33 int main(int argc, char *argv[])
37 //gdcm::Debug::DebugOn();
39 std::cout << "------------------------------------------------" << std::endl;
40 std::cout << "Gets the 'Overlays' from a full gdcm-readable DCM image "
41 << "uncompressed image" << std::endl;
42 std::cout << "Writes them in a DicomV3 file named 'gdcmOverlay.dcm'"
44 std::cout << "------------------------------------------------" << std::endl;
50 fileName = "SIEMENS_GBS_III-16-ACR_NEMA_1.acr";
52 std::cout << fileName << std::endl;
53 // ============================================================
54 // Read the input image.
55 // ============================================================
57 //std::cout << argv[1] << std::endl;
59 f = gdcm::File::New( );
61 f->SetLoadMode(gdcm::LD_ALL);
62 f->SetFileName( fileName );
63 f->AddForceLoadElement(0x6000,0x3000); // Overlay Data
66 if( gdcm::Debug::GetDebugFlag() )
68 std::cout << "---------------------------------------------" << std::endl;
70 std::cout << "---------------------------------------------" << std::endl;
73 std::cout << "Sorry, " << fileName <<" not a gdcm-readable "
79 std::cout << " ... is readable " << std::endl;
81 // ============================================================
82 // Check whether image contains Overlays ACR-NEMA style.
83 // ============================================================
85 int bitsAllocated = f->GetBitsAllocated();
87 gdcm::DataEntry *e = f->GetDataEntry(0x6000, 0x3000);
91 std::cout << " Image doesn't contain any Overlay " << std::endl;
96 std::cout << " File is read! " << std::endl;
98 // ============================================================
99 // Get usefull info from FileHelper.
100 // ============================================================
103 uint32_t overlayPixelLength = e->GetLength();
106 if ( overlayPixelLength != (size_t)nx*ny*sizeof( uint16_t) )
108 std::cout << "Sorry, Pixels of" << fileName << " are not "
109 << "readable. expected length :" << nx*ny*sizeof( uint16_t)
110 << " " << "read length : " << overlayPixelLength
118 std::cout << "Pixels read as expected : length = " << lgt << std::endl;
121 uint32_t nx = f->GetXSize();
122 uint32_t ny = f->GetYSize();
124 uint8_t *overlayPixelArea = (uint8_t *)(e->GetBinArea());
126 // should check overlayPixelLength == nx*ny/8 !)
128 uint8_t *overlayPixelImage = new uint8_t[nx*ny];// uint8 is enough to hold 1 bit !
131 for (unsigned int j=0; j<overlayPixelLength; j++)
133 if (overlayPixelArea[j] != 0)
135 std::cout << "j : " << std::dec << j << " Ox(" << std::hex <<(int)overlayPixelArea[j]
137 for (unsigned int k=0; k<8; k++)
139 m = overlayPixelArea[j]<<k;
140 //printf("m : %04x ", m);
142 //printf(" : %04x \n", m);
143 overlayPixelImage[j*8 +k] = ( m > 127)?255:0;
145 printf("%04x ", overlayPixelImage[j*8 +k]);
147 std::cout << std::endl;
152 if( gdcm::Debug::GetDebugFlag() )
153 std::cout << "About to built empty file" << std::endl;
155 gdcm::File *fileToBuild = gdcm::File::New();
157 if( gdcm::Debug::GetDebugFlag() )
158 std::cout << "Finish to built empty file" << std::endl;
160 std::ostringstream str;
163 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
166 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
168 fileToBuild->InsertEntryString("8",0x0028,0x0100); // Bits Allocated
169 fileToBuild->InsertEntryString("8",0x0028,0x0101); // Bits Stored
170 fileToBuild->InsertEntryString("7",0x0028,0x0102); // High Bit
171 fileToBuild->InsertEntryString("0",0x0028,0x0103); // Pixel Representation
172 fileToBuild->InsertEntryString("1",0x0028,0x0002); // Samples per Pixel
174 fileToBuild->InsertEntryString("MONOCHROME2 ",0x0028,0x0004);
175 // Other mandatory fields will be set automatically,
176 // just before Write(), by FileHelper::CheckMandatoryElements()
178 if( gdcm::Debug::GetDebugFlag() )
179 std::cout << "-------------About to built FileHelper" << std::endl;
181 gdcm::FileHelper *fh = gdcm::FileHelper::New(fileToBuild);
183 if( gdcm::Debug::GetDebugFlag() )
184 std::cout << "-------------Finish to built FileHelper" << std::endl;
186 fh->SetImageData(overlayPixelImage,nx*ny);
187 fh->SetWriteTypeToDcmExplVR();
190 str<<"gdcmOverlay" << ".dcm";
191 // Write the current 'overlay' file
193 if( !fh->Write(str.str()) )
195 std::cout << "Failed\n"
196 << "File in unwrittable\n";
199 fileToBuild->Delete();
200 delete overlayPixelImage;
206 std::cout << "File written successfully" << std::endl;
212 fileToBuild->Delete();
214 delete overlayPixelImage;