1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/11/09 10:52:37 $
7 Version: $Revision: 1.53 $
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 !
288 // Track BigEndian troubles
289 std::cout << " calarSize : " << GetScalarSize()
290 << " SwapCode:" << GetSwapCode()
293 for (unsigned int i=0; i<GetDataSize()/2; i++)
295 g = ((uint16_t *)Data)[i];
296 g = ( g << 8 | g >> 8 );
297 ((uint16_t *)Data)[i] = g;
302 void TestFile::WriteFile()
304 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
312 WriteFileHeader(&fp);
318 bool TestFile::WriteFileHeader(std::ofstream *fp)
320 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
321 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
322 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
323 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
324 WriteInt32(fp,SizeX); // Size X
325 WriteInt32(fp,SizeY); // Size Y
326 WriteInt32(fp,SizeZ); // Size Z
327 WriteInt16(fp,ScalarSize*8); // bits per scalar
328 WriteInt16(fp,Components); // number of components
333 bool TestFile::WriteFileData(std::ofstream *fp)
335 fp->write((char *)Data,GetDataSize());
340 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
341 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
342 throw( std::ios::failure )
346 fp->read ((char*)&g, (size_t)1);
347 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
349 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
351 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
356 uint16_t TestFile::ReadInt16(std::ifstream *fp)
357 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
358 throw( std::ios::failure )
362 fp->read ((char*)&g, (size_t)2);
363 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
365 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
367 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
370 #if defined(GDCM_WORDS_BIGENDIAN)
371 g = ( g << 8 | g >> 8 );
376 uint32_t TestFile::ReadInt32(std::ifstream *fp)
377 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
378 throw( std::ios::failure )
382 fp->read ((char*)&g, (size_t)4);
383 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
385 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
387 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
390 #if defined(GDCM_WORDS_BIGENDIAN)
391 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
392 ( (g>>8) & 0x0000ff00) | (g>>24) );
397 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
399 fp->write((char*)&value, (size_t)1);
402 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
404 #if defined(GDCM_WORDS_BIGENDIAN)
405 value = ( value << 8 | value >> 8 );
407 fp->write((char*)&value, (size_t)2);
410 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
412 #if defined(GDCM_WORDS_BIGENDIAN)
413 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
414 ( (value>>8) & 0x0000ff00) | (value>>24) );
416 fp->write((char*)&value, (size_t)4);
419 int InternalTest(std::string const &filename,
420 std::string const &referenceFileName )
422 std::cout << " Testing: " << filename << std::endl;
429 gdcm::File *f = gdcm::File::New();
430 f->SetLoadMode ( gdcm::LD_ALL ); // Load everything
431 f->SetFileName( filename );
434 if( !f->IsReadable() )
436 std::cout << " Failed" << std::endl
437 << " Image not gdcm compatible:"
438 << filename << std::endl;
442 gdcm::FileHelper *tested = gdcm::FileHelper::New( f );
445 ////// Check for existence of reference baseline dicom file:
448 TestFile *reference = new TestFile();
449 std::ifstream refFile(referenceFileName.c_str(),
450 std::ios::binary|std::ios::in);
453 std::cout << " Failed" << std::endl
454 << " Image not found:"
455 << referenceFileName << std::endl;
456 reference->SetXSize(tested->GetFile()->GetXSize());
457 reference->SetYSize(tested->GetFile()->GetYSize());
458 reference->SetZSize(tested->GetFile()->GetZSize());
459 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
460 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
461 reference->SetData(tested->GetImageData());
462 reference->Write(referenceFileName);
467 reference->Load(referenceFileName);
468 if(!reference->IsReadable())
470 std::cout << " Failed" << std::endl
471 << " Image not Testing compatible:"
472 << filename << std::endl;
480 std::string PixelType = tested->GetFile()->GetPixelType();
482 int testedDataSize = tested->GetImageDataSize();
483 uint8_t *testedImageData = tested->GetImageData();
485 int referenceDataSize = reference->GetDataSize();
486 uint8_t *referenceImageData = reference->GetData();
488 // Test the image size
489 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
490 tested->GetFile()->GetYSize() != reference->GetYSize() ||
491 tested->GetFile()->GetZSize() != reference->GetZSize())
493 std::cout << "Failed" << std::endl
495 << "X: " << tested->GetFile()->GetXSize() << " # "
496 << reference->GetXSize() << " | "
497 << "Y: " << tested->GetFile()->GetYSize() << " # "
498 << reference->GetYSize() << " | "
499 << "Z: " << tested->GetFile()->GetZSize() << " # "
500 << reference->GetZSize() << std::endl;
507 // Test the pixel size
508 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
509 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
511 std::cout << "Failed" << std::endl
512 << " Pixel size differs: " << std::endl
513 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
514 << reference->GetScalarSize() << std::endl
515 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
516 << reference->GetNumberOfComponents() << std::endl
517 << " Pixel type: " << tested->GetFile()->GetPixelType() << std::endl;
524 // Test the data size
525 if (testedDataSize != referenceDataSize)
527 std::cout << " Failed" << std::endl
530 <<") areas lengths differ: "
531 << testedDataSize << " # " << referenceDataSize
534 << tested->GetFile()->GetXSize() << ","
535 << tested->GetFile()->GetYSize() << ","
536 << tested->GetFile()->GetZSize() << ")"
544 // Test the data content
545 if ( memcmp(testedImageData, referenceImageData,
546 testedDataSize) != 0 )
548 std::string ts = tested->GetFile()->GetTransferSyntax();
550 std::cout << " Failed" << std::endl
553 << ") differ (as expanded in memory)."
556 << gdcm::Global::GetTS()->GetValue(ts) << std::endl;
558 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
559 << " pixels differing (pos : test - ref) :"
563 for(i=0, j=0;i<testedDataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
565 if(testedImageData[i]!=referenceImageData[i])
567 std::cout << std::hex << "(" << i << " : "
568 << std::hex << (int)(testedImageData[i]) << " - "
569 << std::hex << (int)(referenceImageData[i]) << ") "
574 std::cout << std::endl;
582 //////////////// Clean up:
587 std::cout << "OK." << std::endl;
592 int TestAllReadCompareDicom(int argc, char *argv[])
594 // Temporarily added, to track BigEndian troubles
595 gdcm::Debug::WarningOn();
598 gdcm::Debug::DebugOn();
602 // The test is specified a specific filename, use it instead of looping
604 const std::string input = argv[1];
605 const std::string reference = argv[2];
606 return InternalTest( input, reference );
608 else if ( argc > 4 || argc == 2 )
610 std::cerr << " Usage: " << argv[0]
611 << " (no arguments needed)." << std::endl;
612 std::cerr << "or Usage: " << argv[0]
613 << " filename.dcm reference.dcm" << std::endl;
618 std::cout << " Description (Test::TestAllReadCompareDicom): "
620 std::cout << " For all images in gdcmData (and not blacklisted in "
621 "Test/CMakeLists.txt)"
623 std::cout << " apply the following to each filename.xxx: "
625 std::cout << " step 1: parse the image (as gdcmFile) and call"
628 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.tst"
630 << " special internal file format containing the"
632 << " caracteristic of the image and the pixel data "
633 << "(uncompressed). This file is written if it's not found."
635 std::cout << " step 3: compare the DICOM image with the reference image"
637 << " (.tst file). The test is made on the caracteristics"
639 << " of the image and the pixel data"
640 << std::endl << std::endl;
644 while( gdcmDataImages[i] != 0 )
646 ////// Check for existence of reference baseline directory
648 std::string baseLineDir = GDCM_DATA_ROOT;
649 baseLineDir += "/BaselineDicom";
651 if( !gdcm::DirList::IsDirectory(baseLineDir) )
653 std::cerr << " The reference baseline directory " << std::endl
655 << baseLineDir << std::endl
656 << " couldn't be opened."
661 ////// Step 1 (see above description):
662 std::string filename = GDCM_DATA_ROOT;
664 filename += gdcmDataImages[i];
667 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
668 std::string::size_type slash_pos = referenceFileName.rfind( "." );
669 if( slash_pos != std::string::npos )
671 referenceFileName.replace( slash_pos + 1, 3, "tst" );
674 if( InternalTest( filename, referenceFileName ) != 0 )