1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/11/02 09:39:04 $
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 =========================================================================*/
18 #include "gdcmDirList.h"
20 #include "gdcmFileHelper.h"
21 #include "gdcmGlobal.h"
23 #include "gdcmDebug.h"
28 #include "gdcmDataImages.h"
32 //--> The .tst files *must* be generated on a Little Endian based computer.
35 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
36 * /remarks The Test file format is (only in little endian) :
41 * - 2 bytes : scalar size (8,16,32) --> ?!? 1 or 2 only in DICOM V3 !
42 * - 2 bytes : number of components per pixel (1,2,3) ---> 1 or 3 only in DICOMV3 !
51 bool IsReadable() {return readable;}
53 int GetXSize() {return SizeX;}
54 int GetYSize() {return SizeY;}
55 int GetZSize() {return SizeZ;}
57 void SetXSize(int size) {SizeX = size;}
58 void SetYSize(int size) {SizeY = size;}
59 void SetZSize(int size) {SizeZ = size;}
61 int GetScalarSize() {return ScalarSize;}
62 void SetScalarSize(int size) {ScalarSize = size;}
64 int GetNumberOfComponents() {return Components;}
65 void SetNumberOfComponents(int size) {Components = size;}
66 int GetSwapCode() {return SwapCode;}
68 unsigned long GetDataSize() {return GetLineSize()*SizeY*SizeZ;}
69 uint8_t *GetData() {return Data;}
70 void SetData(const uint8_t *newData);
72 void Load(const std::string &filename);
73 void Write(const std::string &filename);
76 unsigned long GetLineSize() {return SizeX*ScalarSize*Components;}
77 int ComputeSwapCode(uint32_t tag);
83 bool ReadFileHeader(std::ifstream *fp);
84 bool ReadFileData(std::ifstream *fp);
86 bool WriteFileHeader(std::ofstream *fp);
87 bool WriteFileData(std::ofstream *fp);
89 uint8_t ReadInt8 (std::ifstream *fp)
90 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
91 throw( std::ios::failure );
95 uint16_t ReadInt16(std::ifstream *fp)
96 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
97 throw( std::ios::failure );
101 uint32_t ReadInt32(std::ifstream *fp)
102 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
103 throw( std::ios::failure );
107 void WriteInt8 (std::ofstream *fp,uint8_t value);
108 void WriteInt16(std::ofstream *fp,uint16_t value);
109 void WriteInt32(std::ofstream *fp,uint32_t value);
111 std::string fileName;
122 static const unsigned int HEADER_SIZE;
125 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
126 const unsigned int TestFile::HEADER_SIZE = 20;
143 TestFile::~TestFile()
148 void TestFile::SetData(const uint8_t *newData)
153 memcpy(Data,newData,GetDataSize());
156 void TestFile::Load(const std::string &filename)
162 void TestFile::Write(const std::string &filename)
168 int TestFile::ComputeSwapCode(uint32_t tag)
173 switch(tag&0x000000FF)
195 void TestFile::NewData()
198 if( GetDataSize() == 0 )
200 Data = new uint8_t[GetDataSize()];
203 void TestFile::DeleteData()
210 void TestFile::ReadFile()
213 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
223 readable=ReadFileHeader(&fp);
226 std::cout << "Problems when reading Header part" << std::endl;
231 readable=ReadFileData(&fp);
234 std::cout << "Problems when reading data" << std::endl;
249 bool TestFile::ReadFileHeader(std::ifstream *fp)
251 uint32_t tag = ReadInt32(fp);
252 SwapCode = ComputeSwapCode(tag);
255 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
259 SizeX = ReadInt32(fp); // Size X
260 SizeY = ReadInt32(fp); // Size Y
261 SizeZ = ReadInt32(fp); // Size Z
262 ScalarSize = ReadInt16(fp)/8; // bits per scalar
263 Components = ReadInt16(fp); // Number of components
268 bool TestFile::ReadFileData(std::ifstream *fp)
278 fp->read((char *)Data,GetDataSize());
280 if (GetScalarSize() == 1 || GetSwapCode() == 1234)
284 // We *know* the .tst files are written in 'Little Endian' format.
285 // We *know* DataSize may be 1 or 2 !
287 for (int i=0; i<GetDataSize()/2; i++)
289 g = ((uint16_t *)Data)[i];
290 g = ( g << 8 | g >> 8 );
291 ((uint16_t *)Data)[i] = g;
299 void TestFile::WriteFile()
301 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
309 WriteFileHeader(&fp);
315 bool TestFile::WriteFileHeader(std::ofstream *fp)
317 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
318 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
319 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
320 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
321 WriteInt32(fp,SizeX); // Size X
322 WriteInt32(fp,SizeY); // Size Y
323 WriteInt32(fp,SizeZ); // Size Z
324 WriteInt16(fp,ScalarSize*8); // bits per scalar
325 WriteInt16(fp,Components); // number of components
330 bool TestFile::WriteFileData(std::ofstream *fp)
332 fp->write((char *)Data,GetDataSize());
337 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
338 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
339 throw( std::ios::failure )
343 fp->read ((char*)&g, (size_t)1);
344 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
346 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
348 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
353 uint16_t TestFile::ReadInt16(std::ifstream *fp)
354 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
355 throw( std::ios::failure )
359 fp->read ((char*)&g, (size_t)2);
360 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
362 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
364 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
367 #if defined(GDCM_WORDS_BIGENDIAN)
368 g = ( g << 8 | g >> 8 );
373 uint32_t TestFile::ReadInt32(std::ifstream *fp)
374 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
375 throw( std::ios::failure )
379 fp->read ((char*)&g, (size_t)4);
380 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
382 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
384 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
387 #if defined(GDCM_WORDS_BIGENDIAN)
388 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
389 ( (g>>8) & 0x0000ff00) | (g>>24) );
394 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
396 fp->write((char*)&value, (size_t)1);
399 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
401 #if defined(GDCM_WORDS_BIGENDIAN)
402 value = ( value << 8 | value >> 8 );
404 fp->write((char*)&value, (size_t)2);
407 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
409 #if defined(GDCM_WORDS_BIGENDIAN)
410 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
411 ( (value>>8) & 0x0000ff00) | (value>>24) );
413 fp->write((char*)&value, (size_t)4);
416 int InternalTest(std::string const &filename,
417 std::string const &referenceFileName )
419 std::cout << " Testing: " << filename << std::endl;
426 gdcm::File *f = gdcm::File::New();
427 f->SetLoadMode ( gdcm::LD_ALL ); // Load everything
428 f->SetFileName( filename );
431 if( !f->IsReadable() )
433 std::cout << " Failed" << std::endl
434 << " Image not gdcm compatible:"
435 << filename << std::endl;
439 gdcm::FileHelper *tested = gdcm::FileHelper::New( f );
442 ////// Check for existence of reference baseline dicom file:
445 TestFile *reference = new TestFile();
446 std::ifstream refFile(referenceFileName.c_str(),
447 std::ios::binary|std::ios::in);
450 std::cout << " Failed" << std::endl
451 << " Image not found:"
452 << referenceFileName << std::endl;
453 reference->SetXSize(tested->GetFile()->GetXSize());
454 reference->SetYSize(tested->GetFile()->GetYSize());
455 reference->SetZSize(tested->GetFile()->GetZSize());
456 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
457 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
458 reference->SetData(tested->GetImageData());
459 reference->Write(referenceFileName);
464 reference->Load(referenceFileName);
465 if(!reference->IsReadable())
467 std::cout << " Failed" << std::endl
468 << " Image not Testing compatible:"
469 << filename << std::endl;
477 std::string PixelType = tested->GetFile()->GetPixelType();
479 int testedDataSize = tested->GetImageDataSize();
480 uint8_t *testedImageData = tested->GetImageData();
482 int referenceDataSize = reference->GetDataSize();
483 uint8_t *referenceImageData = reference->GetData();
485 // Test the image size
486 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
487 tested->GetFile()->GetYSize() != reference->GetYSize() ||
488 tested->GetFile()->GetZSize() != reference->GetZSize())
490 std::cout << "Failed" << std::endl
492 << "X: " << tested->GetFile()->GetXSize() << " # "
493 << reference->GetXSize() << " | "
494 << "Y: " << tested->GetFile()->GetYSize() << " # "
495 << reference->GetYSize() << " | "
496 << "Z: " << tested->GetFile()->GetZSize() << " # "
497 << reference->GetZSize() << std::endl;
504 // Test the pixel size
505 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
506 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
508 std::cout << "Failed" << std::endl
509 << " Pixel size differs: " << std::endl
510 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
511 << reference->GetScalarSize() << std::endl
512 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
513 << reference->GetNumberOfComponents() << std::endl
514 << " Pixel type: " << tested->GetFile()->GetPixelType() << std::endl;
521 // Test the data size
522 if (testedDataSize != referenceDataSize)
524 std::cout << " Failed" << std::endl
527 <<") areas lengths differ: "
528 << testedDataSize << " # " << referenceDataSize
531 << tested->GetFile()->GetXSize() << ","
532 << tested->GetFile()->GetYSize() << ","
533 << tested->GetFile()->GetZSize() << ")"
541 // Test the data content
542 if ( memcmp(testedImageData, referenceImageData,
543 testedDataSize) != 0 )
545 std::string ts = tested->GetFile()->GetTransferSyntax();
547 std::cout << " Failed" << std::endl
550 << ") differ (as expanded in memory)."
553 << gdcm::Global::GetTS()->GetValue(ts) << std::endl;
555 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
556 << " pixels differing (pos : test - ref) :"
560 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
562 if(testedImageData[i]!=referenceImageData[i])
564 std::cout << std::hex << "(" << i << " : "
565 << std::hex << (int)(testedImageData[i]) << " - "
566 << std::hex << (int)(referenceImageData[i]) << ") "
571 std::cout << std::endl;
579 //////////////// Clean up:
584 std::cout << "OK." << std::endl;
589 int TestAllReadCompareDicom(int argc, char *argv[])
592 gdcm::Debug::DebugOn();
596 // The test is specified a specific filename, use it instead of looping
598 const std::string input = argv[1];
599 const std::string reference = argv[2];
600 return InternalTest( input, reference );
602 else if ( argc > 4 || argc == 2 )
604 std::cerr << " Usage: " << argv[0]
605 << " (no arguments needed)." << std::endl;
606 std::cerr << "or Usage: " << argv[0]
607 << " filename.dcm reference.dcm" << std::endl;
612 std::cout << " Description (Test::TestAllReadCompareDicom): "
614 std::cout << " For all images in gdcmData (and not blacklisted in "
615 "Test/CMakeLists.txt)"
617 std::cout << " apply the following to each filename.xxx: "
619 std::cout << " step 1: parse the image (as gdcmFile) and call"
622 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
624 << " special internal file format containing the"
626 << " caracteristic of the image and the pixel data "
627 << "(uncompressed). This file is written if it's not found."
629 std::cout << " step 3: compare the DICOM image with the reference image"
631 << " (.tst file). The test is made on the caracteristics"
633 << " of the image and the pixel data"
634 << std::endl << std::endl;
638 while( gdcmDataImages[i] != 0 )
640 ////// Check for existence of reference baseline directory
642 std::string baseLineDir = GDCM_DATA_ROOT;
643 baseLineDir += "/BaselineDicom";
645 if( !gdcm::DirList::IsDirectory(baseLineDir) )
647 std::cerr << " The reference baseline directory " << std::endl
649 << baseLineDir << std::endl
650 << " couldn't be opened."
655 ////// Step 1 (see above description):
656 std::string filename = GDCM_DATA_ROOT;
658 filename += gdcmDataImages[i];
661 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
662 std::string::size_type slash_pos = referenceFileName.rfind( "." );
663 if( slash_pos != std::string::npos )
665 referenceFileName.replace( slash_pos + 1, 3, "tst" );
668 if( InternalTest( filename, referenceFileName ) != 0 )