1 /*=========================================================================
4 Module: $RCSfile: PrintFile.cxx,v $
6 Date: $Date: 2005/08/30 15:13:05 $
7 Version: $Revision: 1.50 $
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 "gdcmDocument.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmSQItem.h"
22 #include "gdcmBinEntry.h"
24 #include "gdcmFileHelper.h"
25 #include "gdcmDebug.h"
26 #include "gdcmDirList.h"
28 #include "gdcmArgMgr.h"
33 void ShowLutData(gdcm::File *f);
35 void ShowLutData(gdcm::File *f)
37 // Nothing is written yet to get LUT Data user friendly
38 // The following is to be moved into a PixelReadConvert method
39 // Let here, waiting for a clever idea on the way to do it.
41 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
44 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
47 std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
48 if ( /*lutDescriptor == GDCM_UNFOUND*/ 0 )
50 //gdcmWarningMacro( "LUT Descriptor is missing" );
51 std::cout << "LUT Descriptor is missing" << std::endl;
54 int length; // LUT length in Bytes
55 int deb; // Subscript of the first Lut Value
56 int nbits; // Lut item size (in Bits)
58 int nbRead; // nb of items in LUT descriptor (must be = 3)
60 nbRead = sscanf( lutDescriptor.c_str(),
62 &length, &deb, &nbits );
63 std::cout << "length " << length
69 //gdcmWarningMacro( "Wrong LUT descriptor" );
70 std::cout << "Wrong LUT descriptor" << std::endl;
72 //LUT Data (CTX dependent)
73 gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
76 int BitsAllocated = f->GetBitsAllocated();
77 if ( BitsAllocated <= 8 )
80 if ( ( nbits == 16 ) && ( BitsAllocated == 8 ) )
82 // when LUT item size is different than pixel size
83 mult = 2; // high byte must be = low byte
87 // See PS 3.3-2003 C.11.1.1.2 p 619
90 uint8_t *lut = b->GetBinArea();
91 for( int i=0; i < length; ++i )
93 std::cout << i+deb << " : \t"
94 << (int) (lut[i*mult + 1]) << std::endl;
99 uint16_t *lut = (uint16_t *)(b->GetBinArea());
100 for( int i=0; i < length; ++i )
102 std::cout << i+deb << " : \t"
103 << (int) (((uint16_t *)lut)[i])
109 std::cout << "No LUT Data BinEntry (0x0028,0x3006) found?!? "
113 std::cout << "No First SQ Item within (0x0028,0x3000) ?!? "
117 std::cout << "No LUT Data SeqEntry (0x0028,0x3000) found "
121 int main(int argc, char *argv[])
125 " \n PrintFile : \n ",
126 " Display the header of a ACR-NEMA/PAPYRUS/DICOM File ",
127 " usage: PrintFile {filein=inputFileName|dirin=inputDirectoryName}[level=n]",
128 " [forceload=listOfElementsToForceLoad] ",
129 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
130 " level = 0,1,2 : depending on the amount of details user wants to see",
131 " listOfElementsToForceLOad : group-elem,g2-e2,... (in hexa, no space)",
132 " of Elements to load whatever their length ",
133 " noshadowseq: user doesn't want to load Private Sequences ",
134 " noshadow : user doesn't want to load Private groups (odd number)",
135 " noseq : user doesn't want to load Sequences ",
136 " debug : user wants to run the program in 'debug mode' ",
137 " showlut :user wants to display the Palette Color (as an int array)",
140 // Initialize Arguments Manager
141 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
143 if (argc == 1 || am->ArgMgrDefined("usage") )
145 am->ArgMgrUsage(usage); // Display 'usage'
150 char *fileName = am->ArgMgrGetString("filein",(char *)0);
151 char *dirName = am->ArgMgrGetString("dirin",(char *)0);
153 if ( (fileName == 0 && dirName == 0)
155 (fileName != 0 && dirName != 0) )
157 std::cout <<std::endl
158 << "Either 'filein=' or 'dirin=' must be present;"
159 << std::endl << "Not both" << std::endl;
160 am->ArgMgrUsage(usage); // Display 'usage'
165 if (am->ArgMgrDefined("debug"))
166 gdcm::Debug::DebugOn();
168 int loadMode = gdcm::LD_ALL;
169 if ( am->ArgMgrDefined("noshadowseq") )
170 loadMode |= gdcm::LD_NOSHADOWSEQ;
173 if ( am->ArgMgrDefined("noshadow") )
174 loadMode |= gdcm::LD_NOSHADOW;
175 if ( am->ArgMgrDefined("noseq") )
176 loadMode |= gdcm::LD_NOSEQ;
179 int level = am->ArgMgrGetInt("level", 2);
182 uint16_t *elemsToForceLoad
183 = am->ArgMgrGetXInt16Enum("forceload", &forceLoadNb);
185 bool showlut = ( 0 != am->ArgMgrDefined("SHOWLUT") );
187 /* if unused Param we give up */
188 if ( am->ArgMgrPrintUnusedLabels() )
190 am->ArgMgrUsage(usage);
195 delete am; // we don't need Argument Manager any longer
197 // ----------- End Arguments Manager ---------
199 if ( fileName != 0 ) // ====== Deal with a single file ======
201 // gdcm::File::IsReadable() is no usable here, because we deal with
202 // any kind of gdcm-Parsable *document*
203 // not only gdcm::File (as opposed to gdcm::DicomDir)
205 gdcm::File *f = new gdcm::File();
206 f->SetLoadMode(loadMode);
207 f->SetFileName( fileName );
209 for (int ri=0; ri<forceLoadNb; ri++)
211 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
212 (uint32_t)elemsToForceLoad[2*ri+1] );
215 bool res = f->Load();
219 std::cout << "Cannot process file [" << fileName << "]" << std::endl;
220 std::cout << "Either it doesn't exist, or it's read protected "
222 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
224 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
230 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
231 fh->SetPrintLevel( level );
235 std::cout << "\n\n" << std::endl;
237 std::cout <<std::endl;
238 std::cout <<" dataSize " << fh->GetImageDataSize() << std::endl;
239 std::cout <<" dataSizeRaw " << fh->GetImageDataRawSize() << std::endl;
241 int nX,nY,nZ,sPP,planarConfig;
242 std::string pixelType;
246 std::cout << " DIMX=" << nX << " DIMY=" << nY << " DIMZ=" << nZ << std::endl;
248 pixelType = f->GetPixelType();
249 sPP = f->GetSamplesPerPixel();
250 planarConfig = f->GetPlanarConfiguration();
252 std::cout << " pixelType= [" << pixelType
253 << "] SamplesPerPixel= [" << sPP
254 << "] PlanarConfiguration= [" << planarConfig
256 << " PhotometricInterpretation= ["
257 << f->GetEntryValue(0x0028,0x0004)
260 int numberOfScalarComponents=f->GetNumberOfScalarComponents();
261 std::cout << " NumberOfScalarComponents = " << numberOfScalarComponents
263 << " LUT = " << (f->HasLUT() ? "TRUE" : "FALSE")
266 if ( f->GetEntryValue(0x0002,0x0010) == gdcm::GDCM_NOTLOADED )
268 std::cout << "Transfer Syntax not loaded. " << std::endl
269 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
274 std::string transferSyntaxName = f->GetTransferSyntaxName();
275 std::cout << " TransferSyntaxName= [" << transferSyntaxName << "]"
277 std::cout << " SwapCode= " << f->GetSwapCode() << std::endl;
279 //std::cout << "\n\n" << std::endl;
280 //std::cout << "X spacing " << f->GetXSpacing() << std::endl;
281 //std::cout << "Y spacing " << f->GetYSpacing() << std::endl;
282 //std::cout << "Z spacing " << f->GetZSpacing() << std::endl;
284 // Display the LUT as an int array (for debugging purpose)
285 if ( f->HasLUT() && showlut )
287 uint8_t* lutrgba = fh->GetLutRGBA();
290 std::cout << "Lut RGBA (Palette Color) not built " << std::endl;
292 // Nothing is written yet to get LUT Data user friendly
293 // The following is to be moved into a PixelRedaConvert method
295 gdcm::SeqEntry *modLutSeq = f->GetSeqEntry(0x0028,0x3000);
298 gdcm::SQItem *sqi= modLutSeq->GetFirstSQItem();
301 std::string lutDescriptor = sqi->GetEntryValue(0x0028,0x3002);
302 int length; // LUT length in Bytes
303 int deb; // Subscript of the first Lut Value
304 int nbits; // Lut item size (in Bits)
305 int nbRead; // nb of items in LUT descriptor (must be = 3)
307 nbRead = sscanf( lutDescriptor.c_str(),
309 &length, &deb, &nbits );
312 //gdcmWarningMacro( "Wrong LUT descriptor" );
313 std::cout << "Wrong LUT descriptor" << std::endl;
315 gdcm::BinEntry *b = sqi->GetBinEntry(0x0028,0x3006);
318 if ( b->GetLength() != 0 )
320 std::cout << "---------------------------------------"
321 << " We should never reach this point "
323 //LoadEntryBinArea(b); //LUT Data (CTX dependent)
329 std::cout << "No LUT Data (0x0028,0x3000) found " << std::endl;
333 if ( fh->GetLutItemSize() == 8 )
335 for (int i=0;i<fh->GetLutItemNumber();i++)
336 std::cout << i << " : \t"
337 << (int)(lutrgba[i*4]) << " "
338 << (int)(lutrgba[i*4+1]) << " "
339 << (int)(lutrgba[i*4+2]) << std::endl;
341 else // LutItemSize assumed to be = 16
343 uint16_t* lutrgba16 = (uint16_t*)lutrgba;
344 for (int i=0;i<fh->GetLutItemNumber();i++)
345 std::cout << i << " : \t"
346 << (int)(lutrgba16[i*4]) << " "
347 << (int)(lutrgba16[i*4+1]) << " "
348 << (int)(lutrgba16[i*4+2]) << std::endl;
354 std::cout << "Try LUT Data "<< std::endl;
359 std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
361 std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
362 std::cout<<std::flush;
367 else // ====== Deal with a Directory ======
369 std::cout << "dirName [" << dirName << "]" << std::endl;
370 gdcm::DirList dirList(dirName,1); // gets recursively the file list
371 gdcm::DirListType fileList = dirList.GetFilenames();
374 for( gdcm::DirListType::iterator it = fileList.begin();
375 it != fileList.end();
378 f = new gdcm::File();
379 f->SetLoadMode(loadMode);
380 f->SetFileName( it->c_str() );
382 for (int ri=0; ri<forceLoadNb; ri++)
384 printf("%04x,%04x\n",elemsToForceLoad[2*ri], elemsToForceLoad[2*ri+1]);
385 f->AddForceLoadElement((uint32_t)elemsToForceLoad[2*ri],
386 (uint32_t)elemsToForceLoad[2*ri+1]);
392 std::cout << "Cannot process file [" << it->c_str() << "]"
394 std::cout << "Either it doesn't exist, or it's read protected "
396 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
398 std::cout << "use 'PrintFile filein=... debug' to try to guess the pb"
404 gdcm::FileHelper *fh = new gdcm::FileHelper(f);
405 fh->SetPrintLevel( level );
410 std::cout <<std::endl<<it->c_str()<<" is Readable"<<std::endl;
412 std::cout <<std::endl<<it->c_str()<<" is NOT Readable"<<std::endl;
413 std::cout << "\n\n" << std::endl;
417 std::cout<<std::flush;