1
/*=========================================================================
3
* Copyright Insight Software Consortium
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0.txt
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
*=========================================================================*/
18
#ifndef __ImageCompare_h
19
#define __ImageCompare_h
20
#include <SimpleITKTestHarness.h>
21
#include <sitkImage.h>
25
typedef ImageCompare Self;
28
// Compare the image to the named baseline image slice by slice, display the difference in the center slice
29
// If the baseline does not exist, fail, and write a baseline image in the output directory
30
bool compare ( const itk::simple::Image& image, std::string testGroup, std::string tag );
32
float testImages( const itk::simple::Image& testImage,
33
const itk::simple::Image& baselineImage,
35
const std::string &baselineImageFilename );
38
// Return the message from the previous image comparison.
39
std::string getMessage() { return mMessage; }
40
void setTolerance ( double t ) { mTolerance = t; }
41
double getTolerance() { return mTolerance; }
43
static void NormalizeAndSave ( const itk::simple::Image &image, const std::string &filename );
49
#define IMAGECOMPARE( x, tag ) \
51
ImageCompare imageCompare; \
52
EXPECT_TRUE( imageCompare.compare( x, "", tag ) ) << imageCompare.getMessage(); \
54
#define IMAGECOMPAREWITHTOLERANCE( x, tag, tolerance ) \
56
ImageCompare imageCompare; \
57
imageCompare.setTolerance(tolerance); \
58
EXPECT_TRUE( imageCompare.compare( x, "", tag ) ) << imageCompare.getMessage(); \
61
#define IMAGECOMPAREWITHHASH( hash, hashType, image, message ) \
63
std::string actualHashValue = itk::simple::Hash( image, itk::simple::HashImageFilter::hashType ); \
64
EXPECT_EQ ( hash, actualHashValue ) << message; \
65
if ( hash != actualHashValue ) \
67
std::string TestImageFilename = dataFinder.GetOutputFile ( actualHashValue + ".png"); \
69
ImageCompare::NormalizeAndSave( image, TestImageFilename ); \
71
std::cout << "<DartMeasurementFile name=\"TestImage\" type=\"image/png\">"; \
72
std::cout << TestImageFilename << "</DartMeasurementFile>" << std::endl; \
73
} catch (std::exception &e) { \
74
std::cerr << "Unexpected error while writing image:" << e.what() << std::endl; \
76
std::cerr << "Unknow Error while writing image for measurement" << std::endl; \
79
itk::simple::Image timage = image; \
80
if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt8) \
82
timage = itk::simple::Cast( timage, itk::simple::sitkUInt8); \
84
else if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt16) \
86
timage = itk::simple::Cast( timage, itk::simple::sitkUInt16); \
88
else if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt32) \
90
timage = itk::simple::Cast( timage, itk::simple::sitkUInt32); \
92
else if (timage.GetPixelIDValue() == itk::simple::sitkLabelUInt64) \
94
timage = itk::simple::Cast( timage, itk::simple::sitkUInt64); \
96
itk::simple::WriteImage ( timage, dataFinder.GetOutputFile ( actualHashValue + ".nrrd" ) ); \
97
std::cout << "Saved actual image as " << dataFinder.GetOutputFile ( actualHashValue + ".nrrd" ) << std::endl; \
101
#define IMAGECOMPARE_WITH_TESTCASE( x, testCase, tag ) \
103
ImageCompare imageCompare; \
104
EXPECT_TRUE( imageCompare.compare( x, testCase, tag ) ) << imageCompare.getMessage(); \
106
#define IMAGECOMPAREWITHTOLERANCE_WITH_TESTCASE( x, testCase, tag, tolerance ) \
108
ImageCompare imageCompare; \
109
imageCompare.setTolerance(tolerance); \
110
EXPECT_TRUE( imageCompare.compare( x, testCase, tag ) ) << imageCompare.getMessage(); \