Chromium Code Reviews| Index: experimental/skpdiff/main.cpp |
| diff --git a/experimental/skpdiff/main.cpp b/experimental/skpdiff/main.cpp |
| index 21bf7169d4dfa607bf627724612bb4cc9f85c675..8883d81881f8d203dfc0eebb346a67af16f68b87 100644 |
| --- a/experimental/skpdiff/main.cpp |
| +++ b/experimental/skpdiff/main.cpp |
| @@ -9,16 +9,27 @@ |
| #define __NO_STD_STRING // Uses cl::STRING_CLASS instead of std::string |
| #include <CL/cl.hpp> |
| +#include "SkCommandLineFlags.h" |
| +#include "SkGraphics.h" |
| #include "SkOSFile.h" |
| -#include "SkStream.h" |
| #include "SkString.h" |
| #include "SkTArray.h" |
| #include "SkTDArray.h" |
| + |
|
djsollen
2013/06/26 14:34:02
remove
|
| #include "SkImageDiffer.h" |
| #include "SkCLImageDiffer.h" |
| #include "skpdiff_util.h" |
| +#include "SkForceLinking.h" |
| +__SK_FORCE_IMAGE_DECODER_LINKING; |
| + |
| +// Command line argument definitions go here |
| +DEFINE_bool2(list, l, false, "List out available differs"); |
| +DEFINE_int32(differ, 0, "The index of the differ to use. Uses 0 if not given."); |
|
djsollen
2013/06/26 14:34:02
instead of using an index it will be much clearer
|
| +DEFINE_string2(folders, f, "", "Compare two folders with identical subfile names: <baseline folder> <test folder>"); |
| +DEFINE_string2(patterns, p, "", "Use two patterns to compare images: <baseline> <test>"); |
| + |
| /// A callback for any OpenCL errors |
| CL_CALLBACK void error_notify(const char* errorInfo, const void* privateInfoSize, ::size_t cb, void* userData) { |
| SkDebugf("OpenCL error notify: %s\n", errorInfo); |
| @@ -92,36 +103,127 @@ static void diff_directories(const char baselinePath[], const char testPath[], S |
| } |
| } |
| -static void print_help() |
| -{ |
| - SkDebugf( |
| - "Usage:\n" \ |
| - "skpdiff <baseline directory> <test directory>\n\n" |
| - ); |
| -} |
| -int main(int argc, char** argv) { |
| - if (argc != 3) |
| - { |
| - print_help(); |
| - return 1; |
| +/// Compares two sets of images identified by glob style patterns with the given differ |
| +static void diff_patterns(const char baselinePattern[], const char testPattern[], SkImageDiffer* differ) { |
| + // Get the files in the baseline and test patterns. Because they are in sorted order, it's easy |
| + // to find corresponding images by matching entry indices. |
| + // |
| + SkTArray<SkString> baselineEntries; |
| + if (!glob_files(baselinePattern, &baselineEntries)) { |
| + SkDebugf("Unable to get pattern \"%s\"\n", baselinePattern); |
| + return; |
| + } |
| + |
| + SkTArray<SkString> testEntries; |
| + if (!glob_files(testPattern, &testEntries)) { |
| + SkDebugf("Unable to get pattern \"%s\"\n", testPattern); |
| + return; |
| } |
| + if (baselineEntries.count() != testEntries.count()) { |
| + SkDebugf("Baseline and test patterns do not yield corresponding number of files\n"); |
| + return; |
| + } |
| + |
| + SkTDArray<int> queuedDiffIDs; |
| + for (int entryIndex = 0; entryIndex < baselineEntries.count(); entryIndex++) { |
| + const char* baselineFilename = baselineEntries[entryIndex].c_str(); |
| + const char* testFilename = testEntries [entryIndex].c_str(); |
| + SkDebugf("%s %s\n", baselineFilename, testFilename); |
| + |
| + int diffID = differ->queueDiffOfFile(baselineFilename, testFilename); |
| + if (diffID >= 0) { |
| + queuedDiffIDs.push(diffID); |
| + SkDebugf("Result: %f\n", differ->getResult(diffID)); |
| + } |
| + } |
| +} |
| + |
| + |
| +static bool init_cl_diff(SkImageDiffer* differ) |
| +{ |
| // Setup OpenCL |
| cl::Device device; |
| cl::Context context; |
| if (!init_device_and_context(&device, &context)) { |
| - return 1; |
| + return false; |
| } |
| // Setup our differ of choice |
| - SkCLImageDiffer* differ = SkNEW(SkDifferentPixelsImageDiffer); |
| - if (!differ->init(device(), context())) { |
| + SkCLImageDiffer* clDiffer = (SkCLImageDiffer*)differ; |
| + return clDiffer->init(device(), context()); |
| +} |
| + |
| +// List here every differ |
| +SkDifferentPixelsImageDiffer gDiffPixel; |
| + |
| +/// A null terminated array of pointer to every differ declared above |
| +SkImageDiffer* gDiffers[] = { &gDiffPixel, NULL }; |
| + |
| +/// A parallel array of functions to initialize the above differs |
| +bool (*gDiffInits[])(SkImageDiffer*) = { init_cl_diff, NULL }; |
| + |
| + |
| +int main(int argc, char** argv) { |
| + // Setup command line parsing |
| + SkCommandLineFlags::SetUsage("Compare images using various metrics."); |
| + SkCommandLineFlags::Parse(argc, argv); |
| + |
| + // Needed by various Skia components |
| + SkAutoGraphics ag; |
| + |
| + // Calculate the number of differs, and optionally print them if the user requests it |
| + int numDiffers; |
| + int differIndex; |
| + for (differIndex = 0; NULL != gDiffers[differIndex]; differIndex++) { |
| + if (FLAGS_list) { |
| + SkDebugf("%i) %s", differIndex, gDiffers[differIndex]->getName()); |
| + // The first differ in the list is the default one |
| + if (0 == differIndex) { |
| + SkDebugf(" (default)"); |
| + } |
| + SkDebugf("\n"); |
| + } |
| + } |
| + numDiffers = differIndex; |
| + |
| + |
| + // Don't attempt to initialize the differ if we aren't going to use it |
| + if (FLAGS_folders.isEmpty() && FLAGS_patterns.isEmpty()) { |
| + return 0; |
| + } |
| + |
| + // Check that the chosen differ is real |
| + if (FLAGS_differ < 0 || FLAGS_differ >= numDiffers) { |
| + SkDebugf("The differ index given is invalid.\n"); |
| return 1; |
| } |
| - // Diff our folders |
| - diff_directories(argv[1], argv[2], differ); |
| + // Get the chosen differ and say which one they chose |
| + SkImageDiffer * differ = gDiffers[FLAGS_differ]; |
| + SkDebugf("Using differ \"%s\"\n", differ->getName()); |
| + |
| + // Initialize the differ using the global list of init functions that match the list of differs |
| + gDiffInits[FLAGS_differ](differ); |
| + |
| + // Perform a folder diff if one is requested |
| + if (!FLAGS_folders.isEmpty()) { |
| + if (2 == FLAGS_folders.count()) { |
| + diff_directories(FLAGS_folders[0], FLAGS_folders[1], differ); |
| + } else { |
| + SkDebugf("Folders flag expects two arguments: <baseline folder> <test folder>\n"); |
| + } |
| + } |
| + |
| + // Perform a pattern diff if one is requested |
| + if (!FLAGS_patterns.isEmpty()) { |
| + if (2 == FLAGS_patterns.count()) { |
| + diff_patterns(FLAGS_patterns[0], FLAGS_patterns[1], differ); |
| + } else { |
| + SkDebugf("Patterns flag expects two arguments: <baseline pattern> <test pattern>\n"); |
| + } |
| + } |
| return 0; |
| } |