1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/11/28 16:29:13 $
7 Version: $Revision: 1.58 $
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 =========================================================================*/
18 #include "gdcmDirList.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmGlobal.h"
23 #include "gdcmDebug.h"
29 #include "gdcmDataImages.h"
33 //--> The .tst files *must* be generated on a Little Endian based computer.
36 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
37 * /remarks The Test file format is (only in little endian) :
42 * - 2 bytes : scalar size (8,16,32) --> ?!? 1 or 2 only in DICOM V3 !
43 * - 2 bytes : number of components per pixel (1,2,3) ---> 1 or 3 only in DICOMV3 !
52 bool IsReadable() {return readable;}
54 int GetXSize() {return SizeX;}
55 int GetYSize() {return SizeY;}
56 int GetZSize() {return SizeZ;}
58 void SetXSize(int size) {SizeX = size;}
59 void SetYSize(int size) {SizeY = size;}
60 void SetZSize(int size) {SizeZ = size;}
62 int GetScalarSize() {return ScalarSize;}
63 void SetScalarSize(int size) {ScalarSize = size;}
65 int GetNumberOfComponents() {return Components;}
66 void SetNumberOfComponents(int size) {Components = size;}
67 int GetSwapCode() {return SwapCode;}
69 unsigned long GetDataSize() {return GetLineSize()*SizeY*SizeZ;}
70 uint8_t *GetData() {return Data;}
71 void SetData(const uint8_t *newData);
73 void Load(const std::string &filename);
74 void Write(const std::string &filename);
77 unsigned long GetLineSize() {return SizeX*ScalarSize*Components;}
78 int ComputeSwapCode(uint32_t tag);
84 bool ReadFileHeader(std::ifstream *fp);
85 bool ReadFileData(std::ifstream *fp);
87 bool WriteFileHeader(std::ofstream *fp);
88 bool WriteFileData(std::ofstream *fp);
90 uint8_t ReadInt8 (std::ifstream *fp)
91 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
92 throw( std::ios::failure );
96 uint16_t ReadInt16(std::ifstream *fp)
97 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
98 throw( std::ios::failure );
102 uint32_t ReadInt32(std::ifstream *fp)
103 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
104 throw( std::ios::failure );
108 void WriteInt8 (std::ofstream *fp,uint8_t value);
109 void WriteInt16(std::ofstream *fp,uint16_t value);
110 void WriteInt32(std::ofstream *fp,uint32_t value);
112 std::string fileName;
123 static const unsigned int HEADER_SIZE;
126 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
127 const unsigned int TestFile::HEADER_SIZE = 20;
144 TestFile::~TestFile()
149 void TestFile::SetData(const uint8_t *newData)
154 memcpy(Data,newData,GetDataSize());
157 void TestFile::Load(const std::string &filename)
163 void TestFile::Write(const std::string &filename)
169 int TestFile::ComputeSwapCode(uint32_t tag)
171 // FIXME : 100 % useless method !
172 // "gdcm" was written on disc byte per byte.
173 // when you fread if, you'll get *allways* "gdcm"
174 // whatever the processor indianess is !
177 //std::cout << std::hex << "0x(" << tag << ")" << std::dec << std::endl;
181 switch(tag&0x000000FF)
203 void TestFile::NewData()
206 if( GetDataSize() == 0 )
208 Data = new uint8_t[GetDataSize()];
211 void TestFile::DeleteData()
218 void TestFile::ReadFile()
221 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
231 readable=ReadFileHeader(&fp);
234 std::cout << "Problems when reading Header part" << std::endl;
239 readable=ReadFileData(&fp);
242 std::cout << "Problems when reading data" << std::endl;
257 bool TestFile::ReadFileHeader(std::ifstream *fp)
259 uint32_t tag = ReadInt32(fp);
260 SwapCode = ComputeSwapCode(tag);
263 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
267 SizeX = ReadInt32(fp); // Size X
268 SizeY = ReadInt32(fp); // Size Y
269 SizeZ = ReadInt32(fp); // Size Z
270 ScalarSize = ReadInt16(fp)/8; // bytes per scalar
271 Components = ReadInt16(fp); // Number of components
276 bool TestFile::ReadFileData(std::ifstream *fp)
285 // Read data Note : .tst images are *always* created
286 // on little endian processor !
287 fp->read((char *)Data,GetDataSize());
289 // Track BigEndian troubles
290 std::cout << " ScalarSize : " << GetScalarSize()
291 << " IsCurrentProcessorBigEndian:"
292 << gdcm::Util::IsCurrentProcessorBigEndian()
295 //if (GetScalarSize() == 1 || GetSwapCode() == 1234)
296 if (GetScalarSize() == 1 || !gdcm::Util::IsCurrentProcessorBigEndian() )
300 // We *know* the .tst files are written in 'Little Endian' format.
301 // We *know* DataSize may be 1 or 2 !
304 std::cout << " Let's swap Pixels" <<std::endl;
306 for (unsigned int i=0; i<GetDataSize()/2; i++)
308 g = ((uint16_t *)Data)[i];
309 g = ( g << 8 | g >> 8 );
310 ((uint16_t *)Data)[i] = g;
315 void TestFile::WriteFile()
317 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
325 WriteFileHeader(&fp);
331 bool TestFile::WriteFileHeader(std::ofstream *fp)
333 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
334 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
335 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
336 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
338 // FIXME : Think of writting an int32, better !
339 // (('g' << 8 + 'd') << 8 + 'c') + 'm'
340 // if you want to use it to check the endianess.
341 // (and upload again *all* the .tst files ...)
342 WriteInt32(fp,SizeX); // Size X
343 WriteInt32(fp,SizeY); // Size Y
344 WriteInt32(fp,SizeZ); // Size Z
345 WriteInt16(fp,ScalarSize*8); // bits per scalar
346 WriteInt16(fp,Components); // number of components
351 bool TestFile::WriteFileData(std::ofstream *fp)
353 fp->write((char *)Data,GetDataSize());
358 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
359 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
360 throw( std::ios::failure )
364 fp->read ((char*)&g, (size_t)1);
365 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
367 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
369 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
374 uint16_t TestFile::ReadInt16(std::ifstream *fp)
375 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
376 throw( std::ios::failure )
380 fp->read ((char*)&g, (size_t)2);
381 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
383 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
385 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
388 #if defined(GDCM_WORDS_BIGENDIAN)
389 g = ( g << 8 | g >> 8 );
394 uint32_t TestFile::ReadInt32(std::ifstream *fp)
395 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
396 throw( std::ios::failure )
400 fp->read ((char*)&g, (size_t)4);
401 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
403 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
405 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
408 #if defined(GDCM_WORDS_BIGENDIAN)
409 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
410 ( (g>>8) & 0x0000ff00) | (g>>24) );
415 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
417 fp->write((char*)&value, (size_t)1);
420 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
422 #if defined(GDCM_WORDS_BIGENDIAN)
423 value = ( value << 8 | value >> 8 );
425 fp->write((char*)&value, (size_t)2);
428 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
430 #if defined(GDCM_WORDS_BIGENDIAN)
431 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
432 ( (value>>8) & 0x0000ff00) | (value>>24) );
434 fp->write((char*)&value, (size_t)4);
437 int InternalTest(std::string const &filename,
438 std::string const &referenceFileName )
440 std::cout << " Testing: " << filename << std::endl;
447 gdcm::File *f = gdcm::File::New();
448 f->SetLoadMode ( gdcm::LD_ALL ); // Load everything
449 f->SetFileName( filename );
452 if( !f->IsReadable() )
454 std::cout << " Failed" << std::endl
455 << " Image not gdcm compatible:"
456 << filename << std::endl;
460 gdcm::FileHelper *tested = gdcm::FileHelper::New( f );
463 ////// Check for existence of reference baseline dicom file:
466 TestFile *reference = new TestFile();
467 std::ifstream refFile(referenceFileName.c_str(),
468 std::ios::binary|std::ios::in);
471 std::cout << " Failed" << std::endl
472 << " Image not found:"
473 << referenceFileName << std::endl;
474 reference->SetXSize(tested->GetFile()->GetXSize());
475 reference->SetYSize(tested->GetFile()->GetYSize());
476 reference->SetZSize(tested->GetFile()->GetZSize());
477 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
478 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
479 reference->SetData(tested->GetImageData());
480 reference->Write(referenceFileName);
485 reference->Load(referenceFileName);
486 if(!reference->IsReadable())
488 std::cout << " Failed" << std::endl
489 << " Image not Testing compatible:"
490 << filename << std::endl;
498 std::string PixelType = tested->GetFile()->GetPixelType();
500 int testedDataSize = tested->GetImageDataSize();
501 uint8_t *testedImageData = tested->GetImageData();
503 int referenceDataSize = reference->GetDataSize();
504 uint8_t *referenceImageData = reference->GetData();
506 // Test the image size
507 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
508 tested->GetFile()->GetYSize() != reference->GetYSize() ||
509 tested->GetFile()->GetZSize() != reference->GetZSize())
511 std::cout << "Failed" << std::endl
513 << "X: " << tested->GetFile()->GetXSize() << " # "
514 << reference->GetXSize() << " | "
515 << "Y: " << tested->GetFile()->GetYSize() << " # "
516 << reference->GetYSize() << " | "
517 << "Z: " << tested->GetFile()->GetZSize() << " # "
518 << reference->GetZSize() << std::endl;
525 // Test the pixel size
526 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
527 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
529 std::cout << "Failed" << std::endl
530 << " Pixel size differs: " << std::endl
531 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
532 << reference->GetScalarSize() << std::endl
533 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
534 << reference->GetNumberOfComponents() << std::endl
535 << " Pixel type: " << tested->GetFile()->GetPixelType() << std::endl;
542 // Test the data size
543 if (testedDataSize != referenceDataSize)
545 std::cout << " Failed" << std::endl
548 <<") areas lengths differ: "
549 << testedDataSize << " # " << referenceDataSize
552 << tested->GetFile()->GetXSize() << ","
553 << tested->GetFile()->GetYSize() << ","
554 << tested->GetFile()->GetZSize() << ")"
562 // Test the data content
563 if ( memcmp(testedImageData, referenceImageData,
564 testedDataSize) != 0 )
566 std::string ts = tested->GetFile()->GetTransferSyntax();
568 std::cout << " Failed" << std::endl
571 << ") differ (as expanded in memory)."
574 << gdcm::Global::GetTS()->GetValue(ts) << std::endl;
576 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
577 << " pixels differing (pos : test - ref) :"
581 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
583 if(testedImageData[i]!=referenceImageData[i])
585 std::cout << std::hex << "(" << i << " : "
586 << std::hex << (int)(testedImageData[i]) << " - "
587 << std::hex << (int)(referenceImageData[i]) << ") "
592 std::cout << std::endl;
600 //////////////// Clean up:
605 std::cout << "OK." << std::endl;
610 int TestAllReadCompareDicom(int argc, char *argv[])
612 // Temporarily added, to track BigEndian troubles
613 gdcm::Debug::WarningOn();
616 gdcm::Debug::DebugOn();
620 // The test is specified a specific filename, use it instead of looping
622 const std::string input = argv[1];
623 const std::string reference = argv[2];
624 return InternalTest( input, reference );
626 else if ( argc > 4 || argc == 2 )
628 std::cerr << " Usage: " << argv[0]
629 << " (no arguments needed)." << std::endl;
630 std::cerr << "or Usage: " << argv[0]
631 << " filename.dcm reference.dcm" << std::endl;
636 std::cout << " Description (Test::TestAllReadCompareDicom): "
638 std::cout << " For all images in gdcmData (and not blacklisted in "
639 "Test/CMakeLists.txt)"
641 std::cout << " apply the following to each filename.xxx: "
643 std::cout << " step 1: parse the image (as gdcmFile) and call"
646 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
648 << " special internal file format containing the"
650 << " caracteristic of the image and the pixel data "
651 << "(uncompressed). This file is written if it's not found."
653 std::cout << " step 3: compare the DICOM image with the reference image"
655 << " (.tst file). The test is made on the caracteristics"
657 << " of the image and the pixel data"
658 << std::endl << std::endl;
662 while( gdcmDataImages[i] != 0 )
664 ////// Check for existence of reference baseline directory
666 std::string baseLineDir = GDCM_DATA_ROOT;
667 baseLineDir += "/BaselineDicom";
669 if( !gdcm::DirList::IsDirectory(baseLineDir) )
671 std::cerr << " The reference baseline directory " << std::endl
673 << baseLineDir << std::endl
674 << " couldn't be opened."
679 //if (gdcmDataImages[i] == "D_CLUNIE_CT2_RLE.dcm")
680 // gdcm::Debug::DebugOn(); // track pb on BigEndian Proc
682 gdcm::Debug::DebugOff();
684 ////// Step 1 (see above description):
685 std::string filename = GDCM_DATA_ROOT;
687 filename += gdcmDataImages[i];
690 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
691 std::string::size_type slash_pos = referenceFileName.rfind( "." );
692 if( slash_pos != std::string::npos )
694 referenceFileName.replace( slash_pos + 1, 3, "tst" );
697 if( InternalTest( filename, referenceFileName ) != 0 )