1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/04/18 01:50:54 $
7 Version: $Revision: 1.38 $
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"
25 #include "gdcmDataImages.h"
28 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
29 * /remarks The Test file format is (only in little endian) :
34 * - 2 bytes : scalar size (8,16,32)
35 * - 2 bytes : number of components per pixel (1,2,3)
44 bool IsReadable(void) {return readable;}
45 int GetXSize(void) {return sizeX;};
46 void SetXSize(int size) {sizeX = size;};
47 int GetYSize(void) {return sizeY;};
48 void SetYSize(int size) {sizeY = size;};
49 int GetZSize(void) {return sizeZ;};
50 void SetZSize(int size) {sizeZ = size;};
51 int GetScalarSize(void) {return scalarSize;};
52 void SetScalarSize(int size) {scalarSize = size;};
53 int GetNumberOfComponents(void) {return components;};
54 void SetNumberOfComponents(int size) {components = size;};
56 unsigned long GetDataSize(void) {return GetLineSize()*sizeY*sizeZ;}
57 uint8_t *GetData(void) {return data;}
58 void SetData(const uint8_t *newData);
60 void Load(const std::string &filename);
61 void Write(const std::string &filename);
64 unsigned long GetLineSize(void) {return sizeX*scalarSize*components;}
65 int GetSwapCode(uint32_t tag);
68 void DeleteData(void);
71 bool ReadFileHeader(std::ifstream *fp);
72 bool ReadFileData(std::ifstream *fp);
74 bool WriteFileHeader(std::ofstream *fp);
75 bool WriteFileData(std::ofstream *fp);
77 uint8_t ReadInt8 (std::ifstream *fp)
78 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
79 throw( std::ios::failure );
83 uint16_t ReadInt16(std::ifstream *fp)
84 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
85 throw( std::ios::failure );
89 uint32_t ReadInt32(std::ifstream *fp)
90 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
91 throw( std::ios::failure );
95 void WriteInt8 (std::ofstream *fp,uint8_t value);
96 void WriteInt16(std::ofstream *fp,uint16_t value);
97 void WriteInt32(std::ofstream *fp,uint32_t value);
110 static const unsigned int HEADER_SIZE;
113 const unsigned int TestFile::HEADER_SIZE = 20;
115 TestFile::TestFile(void)
130 TestFile::~TestFile(void)
135 void TestFile::SetData(const uint8_t *newData)
140 memcpy(data,newData,GetDataSize());
143 void TestFile::Load(const std::string &filename)
149 void TestFile::Write(const std::string &filename)
155 int TestFile::GetSwapCode(uint32_t tag)
160 switch(tag&0x000000FF)
182 void TestFile::NewData(void)
185 if( GetDataSize() == 0 )
187 data = new uint8_t[GetDataSize()];
190 void TestFile::DeleteData(void)
197 void TestFile::ReadFile(void)
200 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
210 readable=ReadFileHeader(&fp);
213 std::cout << "Problems when reading Header part" << std::endl;
218 readable=ReadFileData(&fp);
221 std::cout << "Problems when reading datas" << std::endl;
236 bool TestFile::ReadFileHeader(std::ifstream *fp)
238 uint32_t tag = ReadInt32(fp);
239 swapCode = GetSwapCode(tag);
242 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
246 sizeX = ReadInt32(fp); // Size X
247 sizeY = ReadInt32(fp); // Size Y
248 sizeZ = ReadInt32(fp); // Size Z
249 scalarSize = ReadInt16(fp)/8; // bits per scalar
250 components = ReadInt16(fp); // Number of components
255 bool TestFile::ReadFileData(std::ifstream *fp)
265 fp->read((char *)data,GetDataSize());
270 void TestFile::WriteFile(void)
272 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
280 WriteFileHeader(&fp);
286 bool TestFile::WriteFileHeader(std::ofstream *fp)
288 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
289 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
290 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
291 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
292 WriteInt32(fp,sizeX); // Size X
293 WriteInt32(fp,sizeY); // Size Y
294 WriteInt32(fp,sizeZ); // Size Z
295 WriteInt16(fp,scalarSize*8); // bits per scalar
296 WriteInt16(fp,components); // number of components
301 bool TestFile::WriteFileData(std::ofstream *fp)
303 fp->write((char *)data,GetDataSize());
308 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
309 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
310 throw( std::ios::failure )
314 fp->read ((char*)&g, (size_t)1);
315 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
317 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
319 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
324 uint16_t TestFile::ReadInt16(std::ifstream *fp)
325 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
326 throw( std::ios::failure )
330 fp->read ((char*)&g, (size_t)2);
331 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
333 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
335 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
338 #if defined(GDCM_WORDS_BIGENDIAN)
339 g = ( g << 8 | g >> 8 );
344 uint32_t TestFile::ReadInt32(std::ifstream *fp)
345 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
346 throw( std::ios::failure )
350 fp->read ((char*)&g, (size_t)4);
351 #if !(__GNUC__==2 && __GNUC_MINOR__<=96)
353 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
355 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
358 #if defined(GDCM_WORDS_BIGENDIAN)
359 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
360 ( (g>>8) & 0x0000ff00) | (g>>24) );
365 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
367 fp->write((char*)&value, (size_t)1);
370 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
372 #if defined(GDCM_WORDS_BIGENDIAN)
373 value = ( value << 8 | value >> 8 );
375 fp->write((char*)&value, (size_t)2);
378 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
380 #if defined(GDCM_WORDS_BIGENDIAN)
381 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
382 ( (value>>8) & 0x0000ff00) | (value>>24) );
384 fp->write((char*)&value, (size_t)4);
387 int InternalTest(std::string const &filename,
388 std::string const &referenceFileName )
390 std::cout << " Testing: " << filename << std::endl;
395 gdcm::FileHelper *tested = new gdcm::FileHelper( filename );
396 if( !tested->GetFile()->IsReadable() )
398 std::cout << " Failed" << std::endl
399 << " Image not gdcm compatible:"
400 << filename << std::endl;
406 ////// Check for existence of reference baseline dicom file:
409 TestFile *reference = new TestFile();
410 std::ifstream refFile(referenceFileName.c_str(),
411 std::ios::binary|std::ios::in);
414 std::cout << " Failed" << std::endl
415 << " Image not found:"
416 << referenceFileName << std::endl;
417 reference->SetXSize(tested->GetFile()->GetXSize());
418 reference->SetYSize(tested->GetFile()->GetYSize());
419 reference->SetZSize(tested->GetFile()->GetZSize());
420 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
421 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
422 reference->SetData(tested->GetImageData());
423 reference->Write(referenceFileName);
428 reference->Load(referenceFileName);
429 if(!reference->IsReadable())
431 std::cout << " Failed" << std::endl
432 << " Image not Testing compatible:"
433 << filename << std::endl;
440 std::string PixelType = tested->GetFile()->GetPixelType();
442 int testedDataSize = tested->GetImageDataSize();
443 uint8_t *testedImageData = tested->GetImageData();
445 int referenceDataSize = reference->GetDataSize();
446 uint8_t *referenceImageData = reference->GetData();
448 // Test the image size
449 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
450 tested->GetFile()->GetYSize() != reference->GetYSize() ||
451 tested->GetFile()->GetZSize() != reference->GetZSize())
453 std::cout << "Failed" << std::endl
455 << "X: " << tested->GetFile()->GetXSize() << " # "
456 << reference->GetXSize() << " | "
457 << "Y: " << tested->GetFile()->GetYSize() << " # "
458 << reference->GetYSize() << " | "
459 << "Z: " << tested->GetFile()->GetZSize() << " # "
460 << reference->GetZSize() << std::endl;
466 // Test the pixel size
467 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
468 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
470 std::cout << "Failed" << std::endl
471 << " Pixel size differs: " << std::endl
472 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
473 << reference->GetScalarSize() << std::endl
474 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
475 << reference->GetNumberOfComponents() << std::endl;
481 // Test the data size
482 if (testedDataSize != referenceDataSize)
484 std::cout << " Failed" << std::endl
487 <<") areas lengths differ: "
488 << testedDataSize << " # " << referenceDataSize
491 << tested->GetFile()->GetXSize() << ","
492 << tested->GetFile()->GetYSize() << ","
493 << tested->GetFile()->GetZSize() << ")"
500 // Test the data content
501 if (int res = memcmp(testedImageData, referenceImageData,
502 testedDataSize) != 0 )
505 std::cout << " Failed" << std::endl
508 << ") differ (as expanded in memory)."
515 //////////////// Clean up:
519 std::cout << "OK." << std::endl;
524 int TestAllReadCompareDicom(int argc, char *argv[])
528 // The test is specified a specific filename, use it instead of looping
530 const std::string input = argv[1];
531 const std::string reference = argv[2];
532 return InternalTest( input, reference );
534 else if ( argc > 3 || argc == 2 )
536 std::cerr << " Usage: " << argv[0]
537 << " (no arguments needed)." << std::endl;
538 std::cerr << "or Usage: " << argv[0]
539 << " filename.dcm reference.dcm" << std::endl;
544 std::cout << " Description (Test::TestAllReadCompareDicom): "
546 std::cout << " For all images in gdcmData (and not blacklisted in "
547 "Test/CMakeLists.txt)"
549 std::cout << " apply the following to each filename.xxx: "
551 std::cout << " step 1: parse the image (as gdcmFile) and call"
554 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
556 << " (with format DICOM V3, explicit Value"
559 std::cout << " step 3a: when image NOT found on step 2, write "
561 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
563 << " (with format DICOM V3, explicit Value"
566 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
568 << " compare it (in memory with memcmp) with the"
570 << " image we are testing (the one of step 1). "
571 << std::endl << std::endl;
575 while( gdcmDataImages[i] != 0 )
577 ////// Check for existence of reference baseline directory
579 std::string baseLineDir = GDCM_DATA_ROOT;
580 baseLineDir += "/BaselineDicom";
582 if( !gdcm::DirList::IsDirectory(baseLineDir) )
584 std::cerr << " The reference baseline directory " << std::endl
586 << baseLineDir << std::endl
587 << " couldn't be opened."
592 ////// Step 1 (see above description):
593 std::string filename = GDCM_DATA_ROOT;
595 filename += gdcmDataImages[i];
598 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
599 std::string::size_type slash_pos = referenceFileName.rfind( "." );
600 if( slash_pos != std::string::npos )
602 referenceFileName.replace( slash_pos + 1, 3, "tst" );
605 if( InternalTest( filename, referenceFileName ) != 0 )