|
| 1 | +//===- ImageComparators.h - Image Comparators -------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "Image/Color.h" |
| 13 | +#include "Image/Image.h" |
| 14 | + |
| 15 | +#include "llvm/Support/YAMLTraits.h" |
| 16 | +#include "llvm/Support/raw_ostream.h" |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | + |
| 20 | +#ifndef HLSLTEST_IMAGE_IMAGECOMPARATOR_H |
| 21 | +#define HLSLTEST_IMAGE_IMAGECOMPARATOR_H |
| 22 | + |
| 23 | +namespace hlsltest { |
| 24 | + |
| 25 | +struct CompareCheck { |
| 26 | + enum CheckType { |
| 27 | + None, |
| 28 | + Furthest, |
| 29 | + RMS, |
| 30 | + DiffRMS, |
| 31 | + PixelPercent, |
| 32 | + Intervals, |
| 33 | + } Type = None; |
| 34 | + double Val = 0; |
| 35 | + std::vector<double> Vals = {}; |
| 36 | +}; |
| 37 | + |
| 38 | +class ImageComparatorDistance : public ImageComparatorBase { |
| 39 | + double Furthest = 0.0; |
| 40 | + double RMS = 0.0; |
| 41 | + double DiffRMS = 0.0; |
| 42 | + uint64_t Count = 0; |
| 43 | + uint64_t VisibleDiffs = 0; |
| 44 | + uint64_t Histogram[10] = {0}; |
| 45 | + |
| 46 | + llvm::SmallVector<CompareCheck> Checks; |
| 47 | + |
| 48 | + llvm::SmallString<256> ErrStr; |
| 49 | + |
| 50 | + // 2.3 corresponds to a "just noticeable distance" for the CIE76 |
| 51 | + // difference algorithm. If no pixels are worse than that, there should be |
| 52 | + // no noticeable difference in the image. |
| 53 | + static constexpr double VisibleDiff = 2.3; |
| 54 | + |
| 55 | +public: |
| 56 | + ImageComparatorDistance() { |
| 57 | + Checks.push_back(CompareCheck{CompareCheck::Furthest, VisibleDiff}); |
| 58 | + } |
| 59 | + ImageComparatorDistance(llvm::ArrayRef<CompareCheck> C) : Checks(C) {} |
| 60 | + void processPixel(Color L, Color R) override { |
| 61 | + double Distance = Color::CIE75Distance(L, R); |
| 62 | + if (Distance > VisibleDiff) { |
| 63 | + VisibleDiffs += 1; |
| 64 | + DiffRMS += Distance; |
| 65 | + |
| 66 | + // A difference >10 is a more than 10% off in the L*a*b color space. |
| 67 | + int Idx = static_cast<int>(std::clamp(Distance - VisibleDiff, 0.0, 9.0)); |
| 68 | + Histogram[Idx] += 1; |
| 69 | + } |
| 70 | + |
| 71 | + Furthest = std::max(Furthest, Distance); |
| 72 | + RMS += Distance; |
| 73 | + Count += 1; |
| 74 | + } |
| 75 | + |
| 76 | + void print(llvm::raw_ostream &OS) override { |
| 77 | + double CountDbl = static_cast<double>(Count); |
| 78 | + OS << "RMS Difference: " << RMS << "\n"; |
| 79 | + OS << "Furthest Pixel Difference: " << Furthest << "\n"; |
| 80 | + OS << "Pixels with visible differences: " << VisibleDiffs << " " |
| 81 | + << (static_cast<double>(VisibleDiffs) / CountDbl * 100.0) << "%\n"; |
| 82 | + OS << "RMS Different Pixels Only: " << DiffRMS << "\n"; |
| 83 | + OS << "Total Pixels: " << Count << "\n"; |
| 84 | + OS << "Histogram Data:\n"; |
| 85 | + for (int I = 0; I < 10; ++I) { |
| 86 | + OS << "\t[" << I << "]: " << Histogram[I] << " " |
| 87 | + << (static_cast<double>(Histogram[I]) / CountDbl * 100.0) << "\n"; |
| 88 | + } |
| 89 | + if (!ErrStr.empty()) |
| 90 | + OS << "Error: " << ErrStr << "\n"; |
| 91 | + } |
| 92 | + |
| 93 | + bool evaluateCheck(const CompareCheck &C, llvm::raw_ostream &Err); |
| 94 | + |
| 95 | + bool result() override { |
| 96 | + RMS = sqrt(RMS / static_cast<double>(Count)); |
| 97 | + DiffRMS = sqrt(DiffRMS / static_cast<double>(VisibleDiffs)); |
| 98 | + llvm::raw_svector_ostream Err(ErrStr); |
| 99 | + |
| 100 | + for (const auto &C : Checks) { |
| 101 | + if (!evaluateCheck(C, Err)) |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + return true; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +class ImageComparatorDiffImage : public ImageComparatorBase { |
| 110 | + Image DiffImg; |
| 111 | + float *DiffPtr; |
| 112 | + llvm::StringRef OutputFilename; |
| 113 | + |
| 114 | +public: |
| 115 | + virtual ~ImageComparatorDiffImage() {} |
| 116 | + ImageComparatorDiffImage(uint32_t Height, uint32_t Width, llvm::StringRef OF) |
| 117 | + : DiffImg(Height, Width, 4, 3, true), OutputFilename(OF) { |
| 118 | + DiffPtr = reinterpret_cast<float *>(DiffImg.data()); |
| 119 | + } |
| 120 | + void processPixel(Color L, Color R) override { |
| 121 | + // TODO: I should probably instead use a color distance for this too... |
| 122 | + DiffPtr[0] = abs(L.R - R.R); |
| 123 | + DiffPtr[1] = abs(L.G - R.G); |
| 124 | + DiffPtr[2] = abs(L.B - R.B); |
| 125 | + DiffPtr += 3; |
| 126 | + } |
| 127 | + |
| 128 | + void print(llvm::raw_ostream &) override { |
| 129 | + llvm::consumeError(Image::writePNG(DiffImg, OutputFilename)); |
| 130 | + } |
| 131 | +}; |
| 132 | +} // namespace hlsltest |
| 133 | + |
| 134 | +LLVM_YAML_IS_SEQUENCE_VECTOR(hlsltest::CompareCheck) |
| 135 | + |
| 136 | +namespace llvm { |
| 137 | +namespace yaml { |
| 138 | + |
| 139 | +template <> struct MappingTraits<hlsltest::CompareCheck> { |
| 140 | + static void mapping(IO &I, hlsltest::CompareCheck &C); |
| 141 | +}; |
| 142 | + |
| 143 | +template <> struct ScalarEnumerationTraits<hlsltest::CompareCheck::CheckType> { |
| 144 | + static void enumeration(IO &I, hlsltest::CompareCheck::CheckType &V) { |
| 145 | +#define ENUM_CASE(Val) I.enumCase(V, #Val, hlsltest::CompareCheck::Val) |
| 146 | + ENUM_CASE(Furthest); |
| 147 | + ENUM_CASE(RMS); |
| 148 | + ENUM_CASE(DiffRMS); |
| 149 | + ENUM_CASE(PixelPercent); |
| 150 | + ENUM_CASE(Intervals); |
| 151 | +#undef ENUM_CASE |
| 152 | + } |
| 153 | +}; |
| 154 | +} // namespace yaml |
| 155 | +} // namespace llvm |
| 156 | + |
| 157 | +#endif // HLSLTEST_IMAGE_IMAGECOMPARATOR_H |
0 commit comments