| 1 | /* |
| 2 | * Copyright (C) 2005, 2007, 2015 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2005 Ben La Monica <ben.lamonica@gmail.com>. All rights reserved. |
| 4 | * Copyright (C) 2011 Brent Fulgham. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY |
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR |
| 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "PlatformImage.h" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <cstdlib> |
| 32 | |
| 33 | namespace ImageDiff { |
| 34 | |
| 35 | bool PlatformImage::isCompatible(const PlatformImage& other) const |
| 36 | { |
| 37 | return width() == other.width() |
| 38 | && height() == other.height() |
| 39 | && rowBytes() == other.rowBytes() |
| 40 | && hasAlpha() == other.hasAlpha(); |
| 41 | } |
| 42 | |
| 43 | std::unique_ptr<PlatformImage> PlatformImage::difference(const PlatformImage& other, float& percentageDifference) |
| 44 | { |
| 45 | size_t width = this->width(); |
| 46 | size_t height = this->height(); |
| 47 | |
| 48 | // Compare the content of the 2 bitmaps |
| 49 | void* diffBuffer = malloc(width * height); |
| 50 | float count = 0.0f; |
| 51 | float sum = 0.0f; |
| 52 | float maxDistance = 0.0f; |
| 53 | unsigned char* basePixel = this->pixels(); |
| 54 | unsigned char* pixel = other.pixels(); |
| 55 | unsigned char* diffPixel = reinterpret_cast<unsigned char*>(diffBuffer); |
| 56 | for (size_t y = 0; y < height; ++y) { |
| 57 | for (size_t x = 0; x < width; ++x) { |
| 58 | float red = (pixel[0] - basePixel[0]) / std::max<float>(255 - basePixel[0], basePixel[0]); |
| 59 | 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]); |
| 61 | 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; |
| 63 | |
| 64 | *diffPixel++ = static_cast<unsigned char>(distance * 255.0f); |
| 65 | |
| 66 | if (distance >= 1.0f / 255.0f) { |
| 67 | count += 1.0f; |
| 68 | sum += distance; |
| 69 | if (distance > maxDistance) |
| 70 | maxDistance = distance; |
| 71 | } |
| 72 | |
| 73 | basePixel += 4; |
| 74 | pixel += 4; |
| 75 | } |
| 76 | } |
| 77 | |
| 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); |
| 81 | else |
| 82 | percentageDifference = 0.0f; |
| 83 | |
| 84 | if (!percentageDifference) { |
| 85 | free(diffBuffer); |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
| 89 | // Generate a normalized diff image if there is any difference |
| 90 | if (maxDistance < 1.0f) { |
| 91 | diffPixel = reinterpret_cast<unsigned char*>(diffBuffer); |
| 92 | for (size_t p = 0; p < height * width; ++p) |
| 93 | diffPixel[p] /= maxDistance; |
| 94 | } |
| 95 | |
| 96 | return PlatformImage::createFromDiffData(diffBuffer, width, height); |
| 97 | } |
| 98 | |
| 99 | } // namespace ImageDiff |
| 100 | |