WebKitTools/DumpRenderTree/chromium/ImageDiff.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Google Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 // This file input format is based loosely on
       
    32 // WebKitTools/DumpRenderTree/ImageDiff.m
       
    33 
       
    34 // The exact format of this tool's output to stdout is important, to match
       
    35 // what the run-webkit-tests script expects.
       
    36 
       
    37 #include "config.h"
       
    38 
       
    39 #include "gfx/codec/png_codec.h"
       
    40 #include <algorithm>
       
    41 #include <stdio.h>
       
    42 #include <string.h>
       
    43 #include <vector>
       
    44 #include <wtf/OwnArrayPtr.h>
       
    45 #include <wtf/Vector.h>
       
    46 
       
    47 #if OS(WINDOWS)
       
    48 #include <windows.h>
       
    49 #define PATH_MAX MAX_PATH
       
    50 #endif
       
    51 
       
    52 using namespace gfx;
       
    53 using namespace std;
       
    54 
       
    55 // Causes the app to remain open, waiting for pairs of filenames on stdin.
       
    56 // The caller is then responsible for terminating this app.
       
    57 static const char optionPollStdin[] = "--use-stdin";
       
    58 static const char optionGenerateDiff[] = "--diff";
       
    59 
       
    60 // Return codes used by this utility.
       
    61 static const int statusSame = 0;
       
    62 static const int statusDifferent = 1;
       
    63 static const int statusError = 2;
       
    64 
       
    65 // Color codes.
       
    66 static const uint32_t rgbaRed = 0x000000ff;
       
    67 static const uint32_t rgbaAlpha = 0xff000000;
       
    68 
       
    69 class Image {
       
    70 public:
       
    71     Image()
       
    72         : m_width(0)
       
    73         , m_height(0) {}
       
    74 
       
    75     Image(const Image& image)
       
    76         : m_width(image.m_width)
       
    77         , m_height(image.m_height)
       
    78         , m_data(image.m_data) {}
       
    79 
       
    80     bool hasImage() const { return m_width > 0 && m_height > 0; }
       
    81     int width() const { return m_width; }
       
    82     int height() const { return m_height; }
       
    83     const unsigned char* data() const { return &m_data.front(); }
       
    84 
       
    85     // Creates the image from stdin with the given data length. On success, it
       
    86     // will return true. On failure, no other methods should be accessed.
       
    87     bool craeteFromStdin(size_t byteLength)
       
    88     {
       
    89         if (!byteLength)
       
    90             return false;
       
    91 
       
    92         OwnArrayPtr<unsigned char> source(new unsigned char[byteLength]);
       
    93         if (fread(source.get(), 1, byteLength, stdin) != byteLength)
       
    94             return false;
       
    95 
       
    96         if (!PNGCodec::Decode(source.get(), byteLength, PNGCodec::FORMAT_RGBA,
       
    97                               &m_data, &m_width, &m_height)) {
       
    98             clear();
       
    99             return false;
       
   100         }
       
   101         return true;
       
   102     }
       
   103 
       
   104     // Creates the image from the given filename on disk, and returns true on
       
   105     // success.
       
   106     bool createFromFilename(const char* filename)
       
   107     {
       
   108         FILE* f = fopen(filename, "rb");
       
   109         if (!f)
       
   110             return false;
       
   111 
       
   112         vector<unsigned char> compressed;
       
   113         const int bufSize = 1024;
       
   114         unsigned char buf[bufSize];
       
   115         size_t numRead = 0;
       
   116         while ((numRead = fread(buf, 1, bufSize, f)) > 0)
       
   117             std::copy(buf, &buf[numRead], std::back_inserter(compressed));
       
   118 
       
   119         fclose(f);
       
   120 
       
   121         if (!PNGCodec::Decode(&compressed[0], compressed.size(),
       
   122                               PNGCodec::FORMAT_RGBA, &m_data, &m_width, &m_height)) {
       
   123             clear();
       
   124             return false;
       
   125         }
       
   126         return true;
       
   127     }
       
   128 
       
   129     void clear()
       
   130     {
       
   131         m_width = m_height = 0;
       
   132         m_data.clear();
       
   133     }
       
   134 
       
   135     // Returns the RGBA value of the pixel at the given location
       
   136     const uint32_t pixelAt(int x, int y) const
       
   137     {
       
   138         ASSERT(x >= 0 && x < m_width);
       
   139         ASSERT(y >= 0 && y < m_height);
       
   140         return *reinterpret_cast<const uint32_t*>(&(m_data[(y * m_width + x) * 4]));
       
   141     }
       
   142 
       
   143     void setPixelAt(int x, int y, uint32_t color) const
       
   144     {
       
   145         ASSERT(x >= 0 && x < m_width);
       
   146         ASSERT(y >= 0 && y < m_height);
       
   147         void* addr = &const_cast<unsigned char*>(&m_data.front())[(y * m_width + x) * 4];
       
   148         *reinterpret_cast<uint32_t*>(addr) = color;
       
   149     }
       
   150 
       
   151 private:
       
   152     // pixel dimensions of the image
       
   153     int m_width, m_height;
       
   154 
       
   155     vector<unsigned char> m_data;
       
   156 };
       
   157 
       
   158 float percentageDifferent(const Image& baseline, const Image& actual)
       
   159 {
       
   160     int w = min(baseline.width(), actual.width());
       
   161     int h = min(baseline.height(), actual.height());
       
   162 
       
   163     // Compute pixels different in the overlap
       
   164     int pixelsDifferent = 0;
       
   165     for (int y = 0; y < h; y++) {
       
   166         for (int x = 0; x < w; x++) {
       
   167             if (baseline.pixelAt(x, y) != actual.pixelAt(x, y))
       
   168                 pixelsDifferent++;
       
   169         }
       
   170     }
       
   171 
       
   172     // Count pixels that are a difference in size as also being different
       
   173     int maxWidth = max(baseline.width(), actual.width());
       
   174     int maxHeight = max(baseline.height(), actual.height());
       
   175 
       
   176     // ...pixels off the right side, but not including the lower right corner
       
   177     pixelsDifferent += (maxWidth - w) * h;
       
   178 
       
   179     // ...pixels along the bottom, including the lower right corner
       
   180     pixelsDifferent += (maxHeight - h) * maxWidth;
       
   181 
       
   182     // Like the WebKit ImageDiff tool, we define percentage different in terms
       
   183     // of the size of the 'actual' bitmap.
       
   184     float totalPixels = static_cast<float>(actual.width()) * static_cast<float>(actual.height());
       
   185     if (!totalPixels)
       
   186         return 100.0f; // When the bitmap is empty, they are 100% different.
       
   187     return static_cast<float>(pixelsDifferent) / totalPixels * 100;
       
   188 }
       
   189 
       
   190 void printHelp()
       
   191 {
       
   192     fprintf(stderr,
       
   193             "Usage:\n"
       
   194             "  ImageDiff <compare file> <reference file>\n"
       
   195             "    Compares two files on disk, returning 0 when they are the same\n"
       
   196             "  ImageDiff --use-stdin\n"
       
   197             "    Stays open reading pairs of filenames from stdin, comparing them,\n"
       
   198             "    and sending 0 to stdout when they are the same\n"
       
   199             "  ImageDiff --diff <compare file> <reference file> <output file>\n"
       
   200             "    Compares two files on disk, outputs an image that visualizes the"
       
   201             "    difference to <output file>\n");
       
   202     /* For unfinished webkit-like-mode (see below)
       
   203        "\n"
       
   204        "  ImageDiff -s\n"
       
   205        "    Reads stream input from stdin, should be EXACTLY of the format\n"
       
   206        "    \"Content-length: <byte length> <data>Content-length: ...\n"
       
   207        "    it will take as many file pairs as given, and will compare them as\n"
       
   208        "    (cmp_file, reference_file) pairs\n");
       
   209     */
       
   210 }
       
   211 
       
   212 int compareImages(const char* file1, const char* file2)
       
   213 {
       
   214     Image actualImage;
       
   215     Image baselineImage;
       
   216 
       
   217     if (!actualImage.createFromFilename(file1)) {
       
   218         fprintf(stderr, "ImageDiff: Unable to open file \"%s\"\n", file1);
       
   219         return statusError;
       
   220     }
       
   221     if (!baselineImage.createFromFilename(file2)) {
       
   222         fprintf(stderr, "ImageDiff: Unable to open file \"%s\"\n", file2);
       
   223         return statusError;
       
   224     }
       
   225 
       
   226     float percent = percentageDifferent(actualImage, baselineImage);
       
   227     if (percent > 0.0) {
       
   228         // failure: The WebKit version also writes the difference image to
       
   229         // stdout, which seems excessive for our needs.
       
   230         printf("diff: %01.2f%% failed\n", percent);
       
   231         return statusDifferent;
       
   232     }
       
   233 
       
   234     // success
       
   235     printf("diff: %01.2f%% passed\n", percent);
       
   236     return statusSame;
       
   237 
       
   238 }
       
   239 
       
   240 // Untested mode that acts like WebKit's image comparator. I wrote this but
       
   241 // decided it's too complicated. We may use it in the future if it looks useful.
       
   242 int untestedCompareImages()
       
   243 {
       
   244     Image actualImage;
       
   245     Image baselineImage;
       
   246     char buffer[2048];
       
   247     while (fgets(buffer, sizeof(buffer), stdin)) {
       
   248         if (!strncmp("Content-length: ", buffer, 16)) {
       
   249             char* context;
       
   250 #if OS(WINDOWS)
       
   251             strtok_s(buffer, " ", &context);
       
   252             int imageSize = strtol(strtok_s(0, " ", &context), 0, 10);
       
   253 #else
       
   254             strtok_r(buffer, " ", &context);
       
   255             int imageSize = strtol(strtok_r(0, " ", &context), 0, 10);
       
   256 #endif
       
   257 
       
   258             bool success = false;
       
   259             if (imageSize > 0 && !actualImage.hasImage()) {
       
   260                 if (!actualImage.craeteFromStdin(imageSize)) {
       
   261                     fputs("Error, input image can't be decoded.\n", stderr);
       
   262                     return 1;
       
   263                 }
       
   264             } else if (imageSize > 0 && !baselineImage.hasImage()) {
       
   265                 if (!baselineImage.craeteFromStdin(imageSize)) {
       
   266                     fputs("Error, baseline image can't be decoded.\n", stderr);
       
   267                     return 1;
       
   268                 }
       
   269             } else {
       
   270                 fputs("Error, image size must be specified.\n", stderr);
       
   271                 return 1;
       
   272             }
       
   273         }
       
   274 
       
   275         if (actualImage.hasImage() && baselineImage.hasImage()) {
       
   276             float percent = percentageDifferent(actualImage, baselineImage);
       
   277             if (percent > 0.0) {
       
   278                 // failure: The WebKit version also writes the difference image to
       
   279                 // stdout, which seems excessive for our needs.
       
   280                 printf("diff: %01.2f%% failed\n", percent);
       
   281             } else {
       
   282                 // success
       
   283                 printf("diff: %01.2f%% passed\n", percent);
       
   284             }
       
   285             actualImage.clear();
       
   286             baselineImage.clear();
       
   287         }
       
   288         fflush(stdout);
       
   289     }
       
   290     return 0;
       
   291 }
       
   292 
       
   293 bool createImageDiff(const Image& image1, const Image& image2, Image* out)
       
   294 {
       
   295     int w = min(image1.width(), image2.width());
       
   296     int h = min(image1.height(), image2.height());
       
   297     *out = Image(image1);
       
   298     bool same = (image1.width() == image2.width()) && (image1.height() == image2.height());
       
   299 
       
   300     // FIXME: do something with the extra pixels if the image sizes are different.
       
   301     for (int y = 0; y < h; y++) {
       
   302         for (int x = 0; x < w; x++) {
       
   303             uint32_t basePixel = image1.pixelAt(x, y);
       
   304             if (basePixel != image2.pixelAt(x, y)) {
       
   305                 // Set differing pixels red.
       
   306                 out->setPixelAt(x, y, rgbaRed | rgbaAlpha);
       
   307                 same = false;
       
   308             } else {
       
   309                 // Set same pixels as faded.
       
   310                 uint32_t alpha = basePixel & rgbaAlpha;
       
   311                 uint32_t newPixel = basePixel - ((alpha / 2) & rgbaAlpha);
       
   312                 out->setPixelAt(x, y, newPixel);
       
   313             }
       
   314         }
       
   315     }
       
   316 
       
   317     return same;
       
   318 }
       
   319 
       
   320 static bool writeFile(const char* outFile, const unsigned char* data, size_t dataSize)
       
   321 {
       
   322     FILE* file = fopen(outFile, "wb");
       
   323     if (!file) {
       
   324         fprintf(stderr, "ImageDiff: Unable to create file \"%s\"\n", file);
       
   325         return false;
       
   326     }
       
   327     if (dataSize != fwrite(data, 1, dataSize, file)) {
       
   328         fclose(file);
       
   329         fprintf(stderr, "ImageDiff: Unable to write data to file \"%s\"\n", file);
       
   330         return false;
       
   331     }
       
   332     fclose(file);
       
   333     return true;
       
   334 }
       
   335 
       
   336 int diffImages(const char* file1, const char* file2, const char* outFile)
       
   337 {
       
   338     Image actualImage;
       
   339     Image baselineImage;
       
   340 
       
   341     if (!actualImage.createFromFilename(file1)) {
       
   342         fprintf(stderr, "ImageDiff: Unable to open file \"%s\"\n", file1);
       
   343         return statusError;
       
   344     }
       
   345     if (!baselineImage.createFromFilename(file2)) {
       
   346         fprintf(stderr, "ImageDiff: Unable to open file \"%s\"\n", file2);
       
   347         return statusError;
       
   348     }
       
   349 
       
   350     Image diffImage;
       
   351     bool same = createImageDiff(baselineImage, actualImage, &diffImage);
       
   352     if (same)
       
   353         return statusSame;
       
   354 
       
   355     vector<unsigned char> pngData;
       
   356     PNGCodec::Encode(diffImage.data(), PNGCodec::FORMAT_RGBA, diffImage.width(),
       
   357                      diffImage.height(), diffImage.width() * 4, false, &pngData);
       
   358     if (!writeFile(outFile, &pngData.front(), pngData.size()))
       
   359         return statusError;
       
   360     return statusDifferent;
       
   361 }
       
   362 
       
   363 int main(int argc, const char* argv[])
       
   364 {
       
   365     Vector<const char*> values;
       
   366     bool pollStdin = false;
       
   367     bool generateDiff = false;
       
   368     for (int i = 1; i < argc; ++i) {
       
   369         if (!strcmp(argv[i], optionPollStdin))
       
   370             pollStdin = true;
       
   371         else if (!strcmp(argv[i], optionGenerateDiff))
       
   372             generateDiff = true;
       
   373         else
       
   374             values.append(argv[i]);
       
   375     }
       
   376 
       
   377     if (pollStdin) {
       
   378         // Watch stdin for filenames.
       
   379         const size_t bufferSize = PATH_MAX;
       
   380         char stdinBuffer[bufferSize];
       
   381         char firstName[bufferSize];
       
   382         bool haveFirstName = false;
       
   383         while (fgets(stdinBuffer, bufferSize, stdin)) {
       
   384             if (!stdinBuffer[0])
       
   385                 continue;
       
   386 
       
   387             if (haveFirstName) {
       
   388                 // compareImages writes results to stdout unless an error occurred.
       
   389                 if (compareImages(firstName, stdinBuffer) == statusError)
       
   390                     printf("error\n");
       
   391                 fflush(stdout);
       
   392                 haveFirstName = false;
       
   393             } else {
       
   394                 // Save the first filename in another buffer and wait for the second
       
   395                 // filename to arrive via stdin.
       
   396                 strcpy(firstName, stdinBuffer);
       
   397                 haveFirstName = true;
       
   398             }
       
   399         }
       
   400         return 0;
       
   401     }
       
   402 
       
   403     if (generateDiff) {
       
   404         if (values.size() == 3)
       
   405             return diffImages(values[0], values[1], values[2]);
       
   406     } else if (values.size() == 2)
       
   407         return compareImages(argv[1], argv[2]);
       
   408 
       
   409     printHelp();
       
   410     return statusError;
       
   411 }