bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #include "skdiff.h" |
| 8 | #include "skdiff_utils.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkTemplates.h" |
| 15 | #include "SkTypes.h" |
| 16 | |
| 17 | bool are_buffers_equal(SkData* skdata1, SkData* skdata2) { |
| 18 | if ((NULL == skdata1) || (NULL == skdata2)) { |
| 19 | return false; |
| 20 | } |
| 21 | if (skdata1->size() != skdata2->size()) { |
| 22 | return false; |
| 23 | } |
| 24 | return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size())); |
| 25 | } |
| 26 | |
| 27 | SkData* read_file(const char* file_path) { |
| 28 | SkFILEStream fileStream(file_path); |
| 29 | if (!fileStream.isValid()) { |
| 30 | SkDebugf("WARNING: could not open file <%s> for reading\n", file_path); |
| 31 | return NULL; |
| 32 | } |
| 33 | size_t bytesInFile = fileStream.getLength(); |
| 34 | size_t bytesLeftToRead = bytesInFile; |
| 35 | |
| 36 | void* bufferStart = sk_malloc_throw(bytesInFile); |
| 37 | char* bufferPointer = (char*)bufferStart; |
| 38 | while (bytesLeftToRead > 0) { |
| 39 | size_t bytesReadThisTime = fileStream.read(bufferPointer, bytesLeftToRead); |
| 40 | if (0 == bytesReadThisTime) { |
| 41 | SkDebugf("WARNING: error reading from <%s>\n", file_path); |
| 42 | sk_free(bufferStart); |
| 43 | return NULL; |
| 44 | } |
| 45 | bytesLeftToRead -= bytesReadThisTime; |
| 46 | bufferPointer += bytesReadThisTime; |
| 47 | } |
| 48 | return SkData::NewFromMalloc(bufferStart, bytesInFile); |
| 49 | } |
| 50 | |
| 51 | bool get_bitmap(SkData* fileBits, DiffResource& resource, SkImageDecoder::Mode mode) { |
| 52 | SkMemoryStream stream(fileBits->data(), fileBits->size()); |
| 53 | |
| 54 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 55 | if (NULL == codec) { |
| 56 | SkDebugf("ERROR: no codec found for <%s>\n", resource.fFullPath.c_str()); |
| 57 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // In debug, the DLL will automatically be unloaded when this is deleted, |
| 62 | // but that shouldn't be a problem in release mode. |
| 63 | SkAutoTDelete<SkImageDecoder> ad(codec); |
| 64 | |
| 65 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 66 | if (!codec->decode(&stream, &resource.fBitmap, kN32_SkColorType, mode)) { |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 67 | SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str()); |
| 68 | resource.fStatus = DiffResource::kCouldNotDecode_Status; |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | resource.fStatus = DiffResource::kDecoded_Status; |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /** Thanks to PNG, we need to force all pixels 100% opaque. */ |
| 77 | static void force_all_opaque(const SkBitmap& bitmap) { |
| 78 | SkAutoLockPixels lock(bitmap); |
| 79 | for (int y = 0; y < bitmap.height(); y++) { |
| 80 | for (int x = 0; x < bitmap.width(); x++) { |
| 81 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool write_bitmap(const SkString& path, const SkBitmap& bitmap) { |
| 87 | SkBitmap copy; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 88 | bitmap.copyTo(©, kN32_SkColorType); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 89 | force_all_opaque(copy); |
| 90 | return SkImageEncoder::EncodeFile(path.c_str(), copy, |
| 91 | SkImageEncoder::kPNG_Type, 100); |
| 92 | } |
| 93 | |
| 94 | /// Return a copy of the "input" string, within which we have replaced all instances |
| 95 | /// of oldSubstring with newSubstring. |
| 96 | /// |
| 97 | /// TODO: If we like this, we should move it into the core SkString implementation, |
| 98 | /// adding more checks and ample test cases, and paying more attention to efficiency. |
| 99 | static SkString replace_all(const SkString &input, |
| 100 | const char oldSubstring[], const char newSubstring[]) { |
| 101 | SkString output; |
| 102 | const char *input_cstr = input.c_str(); |
| 103 | const char *first_char = input_cstr; |
| 104 | const char *match_char; |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 105 | size_t oldSubstringLen = strlen(oldSubstring); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 106 | while (NULL != (match_char = strstr(first_char, oldSubstring))) { |
| 107 | output.append(first_char, (match_char - first_char)); |
| 108 | output.append(newSubstring); |
| 109 | first_char = match_char + oldSubstringLen; |
| 110 | } |
| 111 | output.append(first_char); |
| 112 | return output; |
| 113 | } |
| 114 | |
| 115 | static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) { |
| 116 | SkString diffName (filename); |
| 117 | const char* cstring = diffName.c_str(); |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 118 | size_t dotOffset = strrchr(cstring, '.') - cstring; |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 119 | diffName.remove(dotOffset, diffName.size() - dotOffset); |
| 120 | diffName.append(suffix); |
| 121 | |
| 122 | // In case we recursed into subdirectories, replace slashes with something else |
| 123 | // so the diffs will all be written into a single flat directory. |
| 124 | diffName = replace_all(diffName, PATH_DIV_STR, "_"); |
| 125 | return diffName; |
| 126 | } |
| 127 | |
| 128 | SkString filename_to_diff_filename(const SkString& filename) { |
| 129 | return filename_to_derived_filename(filename, "-diff.png"); |
| 130 | } |
| 131 | |
| 132 | SkString filename_to_white_filename(const SkString& filename) { |
| 133 | return filename_to_derived_filename(filename, "-white.png"); |
| 134 | } |
| 135 | |
| 136 | void create_and_write_diff_image(DiffRecord* drp, |
| 137 | DiffMetricProc dmp, |
| 138 | const int colorThreshold, |
| 139 | const SkString& outputDir, |
| 140 | const SkString& filename) { |
| 141 | const int w = drp->fBase.fBitmap.width(); |
| 142 | const int h = drp->fBase.fBitmap.height(); |
| 143 | |
| 144 | if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) { |
| 145 | drp->fResult = DiffRecord::kDifferentSizes_Result; |
| 146 | } else { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 147 | drp->fDifference.fBitmap.allocN32Pixels(w, h); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 148 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 149 | drp->fWhite.fBitmap.allocN32Pixels(w, h); |
bungeman@google.com | e3c8ddf | 2012-12-05 20:13:12 +0000 | [diff] [blame] | 150 | |
| 151 | SkASSERT(DiffRecord::kUnknown_Result == drp->fResult); |
| 152 | compute_diff(drp, dmp, colorThreshold); |
| 153 | SkASSERT(DiffRecord::kUnknown_Result != drp->fResult); |
| 154 | } |
| 155 | |
| 156 | if (outputDir.isEmpty()) { |
| 157 | drp->fDifference.fStatus = DiffResource::kUnspecified_Status; |
| 158 | drp->fWhite.fStatus = DiffResource::kUnspecified_Status; |
| 159 | |
| 160 | } else { |
| 161 | drp->fDifference.fFilename = filename_to_diff_filename(filename); |
| 162 | drp->fDifference.fFullPath = outputDir; |
| 163 | drp->fDifference.fFullPath.append(drp->fDifference.fFilename); |
| 164 | drp->fDifference.fStatus = DiffResource::kSpecified_Status; |
| 165 | |
| 166 | drp->fWhite.fFilename = filename_to_white_filename(filename); |
| 167 | drp->fWhite.fFullPath = outputDir; |
| 168 | drp->fWhite.fFullPath.append(drp->fWhite.fFilename); |
| 169 | drp->fWhite.fStatus = DiffResource::kSpecified_Status; |
| 170 | |
| 171 | if (DiffRecord::kDifferentPixels_Result == drp->fResult) { |
| 172 | if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) { |
| 173 | drp->fDifference.fStatus = DiffResource::kExists_Status; |
| 174 | } else { |
| 175 | drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status; |
| 176 | } |
| 177 | if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) { |
| 178 | drp->fWhite.fStatus = DiffResource::kExists_Status; |
| 179 | } else { |
| 180 | drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |