pqImageComparison.cxx 5.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * Copyright 2004 Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the
 * U.S. Government. Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that this Notice and any
 * statement of authorship are reproduced on all copies.
 */

#include "pqImageComparison.h"

#include <vtkWindowToImageFilter.h>
#include <vtkBMPWriter.h>
#include <vtkTIFFWriter.h>
#include <vtkPNMWriter.h>
#include <vtkPNGWriter.h>
#include <vtkJPEGWriter.h>
#include <vtkErrorCode.h>
#include <vtkSmartPointer.h>
#include <vtkPNGReader.h>
#include <vtkImageDifference.h>
#include <vtkImageShiftScale.h>
#include <vtkRenderWindow.h>

#include <QDir>
#include <QFileInfo>

template<typename WriterT>
bool saveImage(vtkWindowToImageFilter* Capture, const QFileInfo& File)
{
  WriterT* const writer = WriterT::New();
  writer->SetInput(Capture->GetOutput());
  writer->SetFileName(File.filePath().toAscii().data());
  writer->Write();
  const bool result = writer->GetErrorCode() == vtkErrorCode::NoError;
  writer->Delete();
  
  return result;
}

41
bool pqImageComparison::SaveScreenshot(vtkRenderWindow* RenderWindow, const QString& File)
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{
  vtkWindowToImageFilter* const capture = vtkWindowToImageFilter::New();
  capture->SetInput(RenderWindow);
  capture->Update();

  bool success = false;
  
  const QFileInfo file(File);
  if(file.completeSuffix() == "bmp")
    success = saveImage<vtkBMPWriter>(capture, file);
  else if(file.completeSuffix() == "tif")
    success = saveImage<vtkTIFFWriter>(capture, file);
  else if(file.completeSuffix() == "ppm")
    success = saveImage<vtkPNMWriter>(capture, file);
  else if(file.completeSuffix() == "png")
    success = saveImage<vtkPNGWriter>(capture, file);
  else if(file.completeSuffix() == "jpg")
    success = saveImage<vtkJPEGWriter>(capture, file);
    
  capture->Delete();
  
  return success;
}

66
bool pqImageComparison::CompareImage(vtkRenderWindow* RenderWindow, const QString& ReferenceImage, double Threshold, ostream& Output, const QString& TempDirectory)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{
  // Verify the reference image exists
  if(!QFileInfo(ReferenceImage).exists())
    {
    Output << "<DartMeasurement name=\"ImageNotFound\" type=\"text/string\">";
    Output << ReferenceImage.toAscii().data();
    Output << "</DartMeasurement>" << endl;

    return false;
    }

  // Load the reference image
  vtkSmartPointer<vtkPNGReader> reference_image = vtkSmartPointer<vtkPNGReader>::New();
  reference_image->SetFileName(ReferenceImage.toAscii().data()); 
  reference_image->Update();
  
  // Get a screenshot
  vtkSmartPointer<vtkWindowToImageFilter> screenshot = vtkSmartPointer<vtkWindowToImageFilter>::New();
  screenshot->SetInput(RenderWindow);
  screenshot->Update();

  // Compute the difference between the reference image and the screenshot
  vtkSmartPointer<vtkImageDifference> difference = vtkSmartPointer<vtkImageDifference>::New();
  difference->SetInput(screenshot->GetOutput());
  difference->SetImage(reference_image->GetOutput());
  difference->Update();

  Output << "<DartMeasurement name=\"ImageError\" type=\"numeric/double\">";
  Output << difference->GetThresholdedError();
  Output << "</DartMeasurement>" << endl;

  Output << "<DartMeasurement name=\"ImageThreshold\" type=\"numeric/double\">";
  Output << Threshold;
  Output << "</DartMeasurement>" << endl;

  // If the difference didn't exceed the threshold, we're done
  if(difference->GetThresholdedError() <= Threshold)
    return true;

  // Write the reference image to a file
  const QString reference_file = QDir(TempDirectory).filePath(QFileInfo(ReferenceImage).baseName() + ".reference.png");
  vtkSmartPointer<vtkPNGWriter> reference_writer = vtkSmartPointer<vtkPNGWriter>::New();
  reference_writer->SetInput(reference_image->GetOutput());
  reference_writer->SetFileName(reference_file.toAscii().data());
  reference_writer->Write();

  // Write the screenshot to a file
  const QString screenshot_file = QDir(TempDirectory).filePath(QFileInfo(ReferenceImage).baseName() + ".screenshot.png");
  vtkSmartPointer<vtkPNGWriter> screenshot_writer = vtkSmartPointer<vtkPNGWriter>::New();
  screenshot_writer->SetInput(screenshot->GetOutput());
  screenshot_writer->SetFileName(screenshot_file.toAscii().data());
  screenshot_writer->Write();

  // Write the difference to a file, increasing the contrast to make discrepancies stand out
  vtkSmartPointer<vtkImageShiftScale> scale_image = vtkSmartPointer<vtkImageShiftScale>::New();
  scale_image->SetShift(0);
  scale_image->SetScale(10);
  scale_image->SetInput(difference->GetOutput());
  
  const QString difference_file = QDir(TempDirectory).filePath(QFileInfo(ReferenceImage).baseName() + ".difference.png");
  vtkSmartPointer<vtkPNGWriter> difference_writer = vtkSmartPointer<vtkPNGWriter>::New();
  difference_writer->SetInput(scale_image->GetOutput());
  difference_writer->SetFileName(difference_file.toAscii().data());
  difference_writer->Write();

  Output << "<DartMeasurementFile name=\"ValidImage\" type=\"image/jpeg\">";
  Output << reference_file.toAscii().data();
  Output << "</DartMeasurementFile>" << endl;
 
  Output << "<DartMeasurementFile name=\"TestImage\" type=\"image/jpeg\">";
  Output << screenshot_file.toAscii().data();
  Output << "</DartMeasurementFile>" << endl;
  
  Output << "<DartMeasurementFile name=\"DifferenceImage\" type=\"image/jpeg\">";
  Output << difference_file.toAscii().data();
  Output << "</DartMeasurementFile>" << endl;

  return false;
}