Changeset 284764 in webkit
- Timestamp:
- Oct 24, 2021, 11:22:05 AM (5 years ago)
- Location:
- trunk/Tools
- Files:
-
- 4 edited
-
ChangeLog (modified) (1 diff)
-
ImageDiff/ImageDiff.cpp (modified) (8 diffs)
-
ImageDiff/PlatformImage.cpp (modified) (4 diffs)
-
ImageDiff/PlatformImage.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Tools/ChangeLog
r284762 r284764 1 2021-10-23 Simon Fraser <simon.fraser@apple.com> 2 3 Add an ImageDiff option to print out WPT-style pixel differences 4 https://bugs.webkit.org/show_bug.cgi?id=232212 5 6 Reviewed by Darin Adler. 7 8 When passed `--difference`, ImageDiff will compute and print out the "maxDifference=;totalPixels=" 9 values documented at https://web-platform-tests.org/writing-tests/reftests.html. With these values, 10 there is no built-in tolerance as there is with the legacy difference computation. 11 12 Rename variables related to the legacy computation. 13 14 * ImageDiff/ImageDiff.cpp: 15 (processImages): 16 (main): 17 * ImageDiff/PlatformImage.cpp: 18 (ImageDiff::PlatformImage::difference): 19 * ImageDiff/PlatformImage.h: 20 1 21 2021-10-24 Simon Fraser <simon.fraser@apple.com> 2 22 -
trunk/Tools/ImageDiff/ImageDiff.cpp
r284762 r284764 1 1 /* 2 * Copyright (C) 2021 Apple Inc. All rights reserved. 2 3 * Copyright (C) 2017 Igalia S.L. 3 4 * Copyright (C) 2005, 2007, 2015 Apple Inc. All rights reserved. … … 49 50 #endif 50 51 51 static int processImages(std::unique_ptr<PlatformImage>&& actualImage, std::unique_ptr<PlatformImage>&& baselineImage, float tolerance )52 static int processImages(std::unique_ptr<PlatformImage>&& actualImage, std::unique_ptr<PlatformImage>&& baselineImage, float tolerance, bool printDifference) 52 53 { 53 54 if (!actualImage->isCompatible(*baselineImage)) { … … 63 64 } 64 65 65 float difference = 100.0f; 66 auto diffImage = actualImage->difference(*baselineImage, difference); 67 if (difference <= tolerance) 68 difference = 0.0f; 66 PlatformImage::Difference differenceData = { 100, 0, 0 }; 67 auto diffImage = actualImage->difference(*baselineImage, differenceData); 68 float legacyDifference = differenceData.percentageDifference; 69 if (legacyDifference <= tolerance) 70 legacyDifference = 0.0f; 69 71 else { 70 difference = roundf(difference * 100.0f) / 100.0f;71 difference = std::max<float>(difference, 0.01f); // round to 2 decimal places72 } 73 74 if ( difference > 0.0f) {72 legacyDifference = roundf(legacyDifference * 100.0f) / 100.0f; 73 legacyDifference = std::max<float>(legacyDifference, 0.01f); // round to 2 decimal places 74 } 75 76 if (legacyDifference > 0.0f) { 75 77 if (diffImage) 76 78 diffImage->writeAsPNGToStdout(); 77 fprintf(stdout, "diff: %01.2f%% failed\n", difference);79 fprintf(stdout, "diff: %01.2f%% failed\n", legacyDifference); 78 80 } else 79 fprintf(stdout, "diff: %01.2f%% passed\n", difference); 81 fprintf(stdout, "diff: %01.2f%% passed\n", legacyDifference); 82 83 if (printDifference) 84 fprintf(stdout, "maxDifference=%u; totalPixels=%lu\n", differenceData.maxDifference, differenceData.totalPixels); 80 85 81 86 return EXIT_SUCCESS; … … 91 96 float tolerance = 0.0f; 92 97 bool verbose = false; 98 bool printDifference = false; 93 99 94 100 for (int i = 1; i < argc; ++i) { … … 112 118 } 113 119 120 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--difference")) { 121 printDifference = true; 122 continue; 123 } 124 114 125 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { 115 126 fprintf(stdout, 116 "usage: ImageDiff [-h] [- t TOLERANCE] [-v] ([actualImage baselineImage] | <stdin>)\n" \127 "usage: ImageDiff [-h] [-v] [-d] [-t TOLERANCE] ([actualImage baselineImage] | <stdin>)\n" \ 117 128 "\n" \ 118 129 "Reads two PNG-encoded images and compares them. If two file path arguments are supplied, \n" \ … … 123 134 " -h, --help show this help message and exit\n" \ 124 135 " -v, --verbose print diagnostic information to stderr\n" \ 136 " -d, --difference print WPT-style maxDifference and totalPixels data\n" \ 125 137 " -t, --tolerance TOLERANCE\n" \ 126 138 " compare the images with the given tolerance\n" … … 159 171 fprintf(stderr, "Comparing files actual: %s and baseline: %s\n", file1Path, file2Path); 160 172 161 return processImages(std::move(actualImage), std::move(baselineImage), tolerance );173 return processImages(std::move(actualImage), std::move(baselineImage), tolerance, printDifference); 162 174 } 163 175 } … … 207 219 if (verbose) 208 220 fprintf(stderr, "ImageDiff: processing images\n"); 209 auto result = processImages(std::exchange(actualImage, { }), std::exchange(baselineImage, { }), tolerance );221 auto result = processImages(std::exchange(actualImage, { }), std::exchange(baselineImage, { }), tolerance, printDifference); 210 222 if (result != EXIT_SUCCESS) 211 223 return result; -
trunk/Tools/ImageDiff/PlatformImage.cpp
r216576 r284764 1 1 /* 2 * Copyright (C) 2005, 2007 , 2015Apple Inc. All rights reserved.2 * Copyright (C) 2005, 2007-2021 Apple Inc. All rights reserved. 3 3 * Copyright (C) 2005 Ben La Monica <ben.lamonica@gmail.com>. All rights reserved. 4 4 * Copyright (C) 2011 Brent Fulgham. All rights reserved. … … 41 41 } 42 42 43 std::unique_ptr<PlatformImage> PlatformImage::difference(const PlatformImage& other, float& percentageDifference)43 std::unique_ptr<PlatformImage> PlatformImage::difference(const PlatformImage& other, Difference& difference) 44 44 { 45 45 size_t width = this->width(); … … 48 48 // Compare the content of the 2 bitmaps 49 49 void* diffBuffer = malloc(width * height); 50 float count = 0.0f; 51 float sum = 0.0f; 52 float maxDistance = 0.0f; 50 size_t pixelCountWithSignificantDifference = 0; 51 float legacyDistanceSum = 0.0f; 52 float legacyDistanceMax = 0.0f; 53 53 54 unsigned char* basePixel = this->pixels(); 54 55 unsigned char* pixel = other.pixels(); 55 56 unsigned char* diffPixel = reinterpret_cast<unsigned char*>(diffBuffer); 57 56 58 for (size_t y = 0; y < height; ++y) { 57 59 for (size_t x = 0; x < width; ++x) { 58 float red = (pixel[0] - basePixel[0]) / std::max<float>(255 - basePixel[0], basePixel[0]);60 float red = (pixel[0] - basePixel[0]) / std::max<float>(255 - basePixel[0], basePixel[0]); 59 61 float green = (pixel[1] - basePixel[1]) / std::max<float>(255 - basePixel[1], basePixel[1]); 60 float blue = (pixel[2] - basePixel[2]) / std::max<float>(255 - basePixel[2], basePixel[2]);62 float blue = (pixel[2] - basePixel[2]) / std::max<float>(255 - basePixel[2], basePixel[2]); 61 63 float alpha = (pixel[3] - basePixel[3]) / std::max<float>(255 - basePixel[3], basePixel[3]); 62 float distance = sqrtf(red * red + green * green + blue * blue + alpha * alpha) / 2.0f;64 float legacyDistance = sqrtf(red * red + green * green + blue * blue + alpha * alpha) / 2.0f; 63 65 64 *diffPixel++ = static_cast<unsigned char>(distance * 255.0f); 66 *diffPixel++ = static_cast<unsigned char>(legacyDistance * 255.0f); 67 68 // WPT-style difference code. 69 if (legacyDistance) { 70 ++difference.totalPixels; 71 unsigned redDiff = std::abs(pixel[0] - basePixel[0]); 72 unsigned greenDiff = std::abs(pixel[1] - basePixel[1]); 73 unsigned blueDiff = std::abs(pixel[2] - basePixel[2]); 74 unsigned maxDiff = std::max({ redDiff, greenDiff, blueDiff }); 75 difference.maxDifference = std::max(difference.maxDifference, maxDiff); 76 } 65 77 66 if (distance >= 1.0f / 255.0f) {67 count += 1.0f;68 sum += distance;69 if (distance > maxDistance)70 maxDistance = distance;78 // Legacy difference code. Note there is some built-in tolerance here. 79 if (legacyDistance >= 1.0f / 255.0f) { 80 ++pixelCountWithSignificantDifference; 81 legacyDistanceSum += legacyDistance; 82 legacyDistanceMax = std::max(legacyDistanceMax, legacyDistance); 71 83 } 72 84 … … 76 88 } 77 89 78 // Compute the difference as a percentage combining both the number of different pixels and their difference amount i.e. the average distance over the entire image 79 if ( count > 0.0f)80 percentageDifference = 100.0f * sum / (height * width);90 // Compute the difference as a percentage combining both the number of different pixels and their difference amount i.e. the average distance over the entire image. 91 if (pixelCountWithSignificantDifference) 92 difference.percentageDifference = 100.0f * legacyDistanceSum / (height * width); 81 93 else 82 percentageDifference = 0.0f;94 difference.percentageDifference = 0.0f; 83 95 84 if (!p ercentageDifference) {96 if (!pixelCountWithSignificantDifference) { 85 97 free(diffBuffer); 86 98 return nullptr; 87 99 } 88 100 89 // Generate a normalized diff image if there is any difference 90 if ( maxDistance < 1.0f) {101 // Generate a normalized diff image if there is any difference. 102 if (pixelCountWithSignificantDifference) { 91 103 diffPixel = reinterpret_cast<unsigned char*>(diffBuffer); 92 104 for (size_t p = 0; p < height * width; ++p) 93 diffPixel[p] /= maxDistance;105 diffPixel[p] /= legacyDistanceMax; 94 106 } 95 107 -
trunk/Tools/ImageDiff/PlatformImage.h
r284762 r284764 52 52 size_t rowBytes() const; 53 53 bool hasAlpha() const; 54 54 55 unsigned char* pixels() const; 55 56 bool isCompatible(const PlatformImage&) const; 56 std::unique_ptr<PlatformImage> difference(const PlatformImage&, float& percentageDifference); 57 58 struct Difference { 59 float percentageDifference { 0 }; // Legacy different measure. 60 61 // WPT-style difference: https://web-platform-tests.org/writing-tests/reftests.html. 62 unsigned maxDifference { 0 }; 63 size_t totalPixels { 0 }; 64 }; 65 std::unique_ptr<PlatformImage> difference(const PlatformImage&, Difference&); 66 57 67 void writeAsPNGToStdout(); 58 68
Note:
See TracChangeset
for help on using the changeset viewer.