1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2005/04/11 17:30:27 $
7 Version: $Revision: 1.34 $
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"
26 #include "gdcmDataImages.h"
29 * /brief File Read/Writer specific for the TestAllReadCompareDicom test
30 * /remarks The Test file format is (only in little endian) :
35 * - 2 bytes : scalar size (8,16,32)
36 * - 2 bytes : number of components per pixel (1,2,3)
45 bool IsReadable(void) {return readable;}
46 int GetXSize(void) {return sizeX;};
47 void SetXSize(int size) {sizeX = size;};
48 int GetYSize(void) {return sizeY;};
49 void SetYSize(int size) {sizeY = size;};
50 int GetZSize(void) {return sizeZ;};
51 void SetZSize(int size) {sizeZ = size;};
52 int GetScalarSize(void) {return scalarSize;};
53 void SetScalarSize(int size) {scalarSize = size;};
54 int GetNumberOfComponents(void) {return components;};
55 void SetNumberOfComponents(int size) {components = size;};
57 unsigned long GetDataSize(void) {return GetLineSize()*sizeY*sizeZ;}
58 uint8_t *GetData(void) {return data;}
59 void SetData(const uint8_t *newData);
61 void Load(const std::string &filename);
62 void Write(const std::string &filename);
65 unsigned long GetLineSize(void) {return sizeX*scalarSize*components;}
66 int GetSwapCode(uint32_t tag);
69 void DeleteData(void);
72 bool ReadFileHeader(std::ifstream *fp);
73 bool ReadFileData(std::ifstream *fp);
75 bool WriteFileHeader(std::ofstream *fp);
76 bool WriteFileData(std::ofstream *fp);
78 uint8_t ReadInt8 (std::ifstream *fp)
79 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
80 throw( std::ofstream::failure );
84 uint16_t ReadInt16(std::ifstream *fp)
85 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
86 throw( std::ios::failure );
90 uint32_t ReadInt32(std::ifstream *fp)
91 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
92 throw( std::ios::failure );
96 void WriteInt8 (std::ofstream *fp,uint8_t value);
97 void WriteInt16(std::ofstream *fp,uint16_t value);
98 void WriteInt32(std::ofstream *fp,uint32_t value);
100 std::string fileName;
111 static const unsigned int HEADER_SIZE;
114 const unsigned int TestFile::HEADER_SIZE = 20;
116 TestFile::TestFile(void)
131 TestFile::~TestFile(void)
136 void TestFile::SetData(const uint8_t *newData)
141 memcpy(data,newData,GetDataSize());
144 void TestFile::Load(const std::string &filename)
150 void TestFile::Write(const std::string &filename)
156 int TestFile::GetSwapCode(uint32_t tag)
161 switch(tag&0x000000FF)
183 void TestFile::NewData(void)
186 if( GetDataSize() == 0 )
188 data = new uint8_t[GetDataSize()];
191 void TestFile::DeleteData(void)
198 void TestFile::ReadFile(void)
201 std::ifstream fp(fileName.c_str(),std::ios::in | std::ios::binary);
211 readable=ReadFileHeader(&fp);
214 std::cout << "Problems when reading Header part" << std::endl;
219 readable=ReadFileData(&fp);
222 std::cout << "Problems when reading datas" << std::endl;
237 bool TestFile::ReadFileHeader(std::ifstream *fp)
239 uint32_t tag = ReadInt32(fp);
240 swapCode = GetSwapCode(tag);
243 std::cout << "TestFile: Bad tag - Must be 'gdcm'" << std::endl;
247 sizeX = ReadInt32(fp); // Size X
248 sizeY = ReadInt32(fp); // Size Y
249 sizeZ = ReadInt32(fp); // Size Z
250 scalarSize = ReadInt16(fp)/8; // bits per scalar
251 components = ReadInt16(fp); // Number of components
256 bool TestFile::ReadFileData(std::ifstream *fp)
266 fp->read((char *)data,GetDataSize());
271 void TestFile::WriteFile(void)
273 std::ofstream fp(fileName.c_str(),std::ios::out | std::ios::binary);
281 WriteFileHeader(&fp);
287 bool TestFile::WriteFileHeader(std::ofstream *fp)
289 WriteInt8(fp,'g'); // Bitmap tag - must be 'g'
290 WriteInt8(fp,'d'); // Bitmap tag - must be 'd'
291 WriteInt8(fp,'c'); // Bitmap tag - must be 'c'
292 WriteInt8(fp,'m'); // Bitmap tag - must be 'm'
293 WriteInt32(fp,sizeX); // Size X
294 WriteInt32(fp,sizeY); // Size Y
295 WriteInt32(fp,sizeZ); // Size Z
296 WriteInt16(fp,scalarSize*8); // bits per scalar
297 WriteInt16(fp,components); // number of components
302 bool TestFile::WriteFileData(std::ofstream *fp)
304 fp->write((char *)data,GetDataSize());
309 uint8_t TestFile::ReadInt8 (std::ifstream *fp)
310 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
311 throw( std::ios::failure )
315 fp->read ((char*)&g, (size_t)1);
316 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
318 throw std::ios::failure( "TestFile::ReadInt8() - file error." );
320 throw std::ios::failure( "TestFile::ReadInt8() - EOF." );
325 uint16_t TestFile::ReadInt16(std::ifstream *fp)
326 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
327 throw( std::ios::failure )
331 fp->read ((char*)&g, (size_t)2);
332 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
334 throw std::ios::failure( "TestFile::ReadInt16() - file error." );
336 throw std::ios::failure( "TestFile::ReadInt16() - EOF." );
339 #if defined(GDCM_WORDS_BIGENDIAN)
340 g = ( g << 8 | g >> 8 );
345 uint32_t TestFile::ReadInt32(std::ifstream *fp)
346 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
347 throw( std::ios::failure )
351 fp->read ((char*)&g, (size_t)4);
352 #if !(__GNUC__==2 && __GNUC_MINOR__==96)
354 throw std::ios::failure( "TestFile::ReadInt32() - file error." );
356 throw std::ios::failure( "TestFile::ReadInt32() - EOF." );
359 #if defined(GDCM_WORDS_BIGENDIAN)
360 g = ( (g<<24) | ((g<<8) & 0x00ff0000) |
361 ( (g>>8) & 0x0000ff00) | (g>>24) );
366 void TestFile::WriteInt8 (std::ofstream *fp,uint8_t value)
368 fp->write((char*)&value, (size_t)1);
371 void TestFile::WriteInt16(std::ofstream *fp,uint16_t value)
373 #if defined(GDCM_WORDS_BIGENDIAN)
374 value = ( value << 8 | value >> 8 );
376 fp->write((char*)&value, (size_t)2);
379 void TestFile::WriteInt32(std::ofstream *fp,uint32_t value)
381 #if defined(GDCM_WORDS_BIGENDIAN)
382 value = ( (value<<24) | ((value<<8) & 0x00ff0000) |
383 ( (value>>8) & 0x0000ff00) | (value>>24) );
385 fp->write((char*)&value, (size_t)4);
388 int InternalTest(std::string const &filename,
389 std::string const &referenceFileName )
391 std::cout << " Testing: " << filename << std::endl;
396 gdcm::FileHelper *tested = new gdcm::FileHelper( filename );
397 if( !tested->GetFile()->IsReadable() )
399 std::cout << " Failed" << std::endl
400 << " Image not gdcm compatible:"
401 << filename << std::endl;
407 ////// Check for existence of reference baseline dicom file:
410 TestFile *reference = new TestFile();
411 std::ifstream refFile(referenceFileName.c_str(),
412 std::ios::binary|std::ios::in);
415 std::cout << " Failed" << std::endl
416 << " Image not found:"
417 << referenceFileName << std::endl;
418 reference->SetXSize(tested->GetFile()->GetXSize());
419 reference->SetYSize(tested->GetFile()->GetYSize());
420 reference->SetZSize(tested->GetFile()->GetZSize());
421 reference->SetScalarSize(tested->GetFile()->GetPixelSize());
422 reference->SetNumberOfComponents(tested->GetFile()->GetNumberOfScalarComponents());
423 reference->SetData(tested->GetImageData());
424 reference->Write(referenceFileName);
429 reference->Load(referenceFileName);
430 if(!reference->IsReadable())
432 std::cout << " Failed" << std::endl
433 << " Image not Testing compatible:"
434 << filename << std::endl;
441 std::string PixelType = tested->GetFile()->GetPixelType();
443 int testedDataSize = tested->GetImageDataSize();
444 uint8_t *testedImageData = tested->GetImageData();
446 int referenceDataSize = reference->GetDataSize();
447 uint8_t *referenceImageData = reference->GetData();
449 // Test the image size
450 if (tested->GetFile()->GetXSize() != reference->GetXSize() ||
451 tested->GetFile()->GetYSize() != reference->GetYSize() ||
452 tested->GetFile()->GetZSize() != reference->GetZSize())
454 std::cout << "Failed" << std::endl
456 << "X: " << tested->GetFile()->GetXSize() << " # "
457 << reference->GetXSize() << " | "
458 << "Y: " << tested->GetFile()->GetYSize() << " # "
459 << reference->GetYSize() << " | "
460 << "Z: " << tested->GetFile()->GetZSize() << " # "
461 << reference->GetZSize() << std::endl;
467 // Test the pixel size
468 if (tested->GetFile()->GetPixelSize() != reference->GetScalarSize() ||
469 tested->GetFile()->GetNumberOfScalarComponents() != reference->GetNumberOfComponents())
471 std::cout << "Failed" << std::endl
472 << " Pixel size differs: " << std::endl
473 << " Scalar size: " << tested->GetFile()->GetPixelSize() << " # "
474 << reference->GetScalarSize() << std::endl
475 << " Number of scalar: " << tested->GetFile()->GetNumberOfScalarComponents() << " # "
476 << reference->GetNumberOfComponents() << std::endl;
482 // Test the data size
483 if (testedDataSize != referenceDataSize)
485 std::cout << " Failed" << std::endl
488 <<") areas lengths differ: "
489 << testedDataSize << " # " << referenceDataSize
492 << tested->GetFile()->GetXSize() << ","
493 << tested->GetFile()->GetYSize() << ","
494 << tested->GetFile()->GetZSize() << ")"
501 // Test the data content
502 if (int res = memcmp(testedImageData, referenceImageData,
503 testedDataSize) != 0 )
506 std::cout << " Failed" << std::endl
509 << ") differ (as expanded in memory)."
516 //////////////// Clean up:
520 std::cout << "OK." << std::endl;
525 int TestAllReadCompareDicom(int argc, char *argv[])
529 // The test is specified a specific filename, use it instead of looping
531 const std::string input = argv[1];
532 const std::string reference = argv[2];
533 return InternalTest( input, reference );
535 else if ( argc > 3 || argc == 2 )
537 std::cerr << " Usage: " << argv[0]
538 << " (no arguments needed)." << std::endl;
539 std::cerr << "or Usage: " << argv[0]
540 << " filename.dcm reference.dcm" << std::endl;
545 std::cout << " Description (Test::TestAllReadCompareDicom): "
547 std::cout << " For all images in gdcmData (and not blacklisted in "
548 "Test/CMakeLists.txt)"
550 std::cout << " apply the following to each filename.xxx: "
552 std::cout << " step 1: parse the image (as gdcmFile) and call"
555 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
557 << " (with format DICOM V3, explicit Value"
560 std::cout << " step 3a: when image NOT found on step 2, write "
562 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
564 << " (with format DICOM V3, explicit Value"
567 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
569 << " compare it (in memory with memcmp) with the"
571 << " image we are testing (the one of step 1). "
572 << std::endl << std::endl;
576 while( gdcmDataImages[i] != 0 )
578 ////// Check for existence of reference baseline directory
580 std::string baseLineDir = GDCM_DATA_ROOT;
581 baseLineDir += "/BaselineDicom/";
583 if( !gdcm::DirList::IsDirectory(baseLineDir) )
585 std::cerr << " The reference baseline directory " << std::endl
587 << baseLineDir << std::endl
588 << " couldn't be opened."
593 ////// Step 1 (see above description):
594 std::string filename = GDCM_DATA_ROOT;
596 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 )