Home | History | Annotate | Download | only in skpdiff
      1 /*
      2  * Copyright 2013 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkDiffContext_DEFINED
      9 #define SkDiffContext_DEFINED
     10 
     11 #include "SkString.h"
     12 #include "SkTArray.h"
     13 #include "SkTDArray.h"
     14 
     15 class SkWStream;
     16 class SkImageDiffer;
     17 
     18 /**
     19  * Collects records of diffs and outputs them as JSON.
     20  */
     21 class SkDiffContext {
     22 public:
     23     SkDiffContext();
     24     ~SkDiffContext();
     25 
     26     /**
     27      * Sets the differs to be used in each diff. Already started diffs will not retroactively use
     28      * these.
     29      * @param differs An array of differs to use. The array is copied, but not the differs
     30      *                themselves.
     31      */
     32     void setDiffers(const SkTDArray<SkImageDiffer*>& differs);
     33 
     34     /**
     35      * Compares two directories of images with the given differ
     36      * @param baselinePath The baseline directory's path
     37      * @param testPath     The test directory's path
     38      */
     39     void diffDirectories(const char baselinePath[], const char testPath[]);
     40 
     41     /**
     42      * Compares two sets of images identified by glob style patterns with the given differ
     43      * @param baselinePattern A pattern for baseline files
     44      * @param testPattern     A pattern for test files that matches each file of the baseline file
     45      */
     46     void diffPatterns(const char baselinePattern[], const char testPattern[]);
     47 
     48     /**
     49      * Compares the images at the given paths
     50      * @param baselinePath The baseline file path
     51      * @param testPath     The matching test file path
     52      */
     53     void addDiff(const char* baselinePath, const char* testPath);
     54 
     55     /**
     56      * Output the records of each diff in JSON.
     57      *
     58      * The format of the JSON document is one top level array named "records".
     59      * Each record in the array is an object with both a "baselinePath" and "testPath" string field.
     60      * They also have an array named "diffs" with each element being one diff record for the two
     61      * images indicated in the above field.
     62      * A diff record includes:
     63      *    "differName"       : string name of the diff metric used
     64      *    "result"           : numerical result of the diff
     65      *    "pointsOfInterest" : an array of coordinates (stored as a 2-array of ints) of interesting
     66      *                         points
     67      *
     68      * Here is an example:
     69      *
     70      * {
     71      *     "records": [
     72      *         {
     73      *             "baselinePath": "queue.png",
     74      *             "testPath": "queue.png",
     75      *             "diffs": [
     76      *                 {
     77      *                     "differName": "different_pixels",
     78      *                     "result": 1,
     79      *                     "pointsOfInterest": [
     80      *                         [285,279],
     81      *                     ]
     82      *                 }
     83      *             ]
     84      *         }
     85      *     ]
     86      * }
     87      *
     88      * @param stream   The stream to output the diff to
     89      * @param useJSONP True to adding padding to the JSON output to make it cross-site requestable.
     90      */
     91     void outputRecords(SkWStream& stream, bool useJSONP);
     92 
     93     /**
     94      * Output the records score in csv format.
     95      */
     96     void outputCsv(SkWStream& stream);
     97 
     98 
     99 private:
    100     struct DiffData {
    101         const char* fDiffName;
    102         double fResult;
    103         SkTDArray<SkIPoint> fPointsOfInterest;
    104     };
    105 
    106     struct DiffRecord {
    107         SkString           fBaselinePath;
    108         SkString               fTestPath;
    109         SkTArray<DiffData>        fDiffs;
    110         DiffRecord*                fNext;
    111     };
    112 
    113     // We use linked list for the records so that their pointers remain stable. A resizable array
    114     // might change its pointers, which would make it harder for async diffs to record their
    115     // results.
    116     DiffRecord * fRecords;
    117 
    118     SkImageDiffer** fDiffers;
    119     int fDifferCount;
    120 };
    121 
    122 #endif
    123