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(); |
| 66 | if (!codec->decode(&stream, &resource.fBitmap, SkBitmap::kARGB_8888_Config, mode)) { |
| 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; |
| 88 | bitmap.copyTo(©, SkBitmap::kARGB_8888_Config); |
| 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; |
| 105 | int oldSubstringLen = strlen(oldSubstring); |
| 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(); |
| 118 | int dotOffset = strrchr(cstring, '.') - cstring; |
| 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 { |
| 147 | drp->fDifference.fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 148 | drp->fDifference.fBitmap.allocPixels(); |
| 149 | |
| 150 | drp->fWhite.fBitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 151 | drp->fWhite.fBitmap.allocPixels(); |
| 152 | |
| 153 | SkASSERT(DiffRecord::kUnknown_Result == drp->fResult); |
| 154 | compute_diff(drp, dmp, colorThreshold); |
| 155 | SkASSERT(DiffRecord::kUnknown_Result != drp->fResult); |
| 156 | } |
| 157 | |
| 158 | if (outputDir.isEmpty()) { |
| 159 | drp->fDifference.fStatus = DiffResource::kUnspecified_Status; |
| 160 | drp->fWhite.fStatus = DiffResource::kUnspecified_Status; |
| 161 | |
| 162 | } else { |
| 163 | drp->fDifference.fFilename = filename_to_diff_filename(filename); |
| 164 | drp->fDifference.fFullPath = outputDir; |
| 165 | drp->fDifference.fFullPath.append(drp->fDifference.fFilename); |
| 166 | drp->fDifference.fStatus = DiffResource::kSpecified_Status; |
| 167 | |
| 168 | drp->fWhite.fFilename = filename_to_white_filename(filename); |
| 169 | drp->fWhite.fFullPath = outputDir; |
| 170 | drp->fWhite.fFullPath.append(drp->fWhite.fFilename); |
| 171 | drp->fWhite.fStatus = DiffResource::kSpecified_Status; |
| 172 | |
| 173 | if (DiffRecord::kDifferentPixels_Result == drp->fResult) { |
| 174 | if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) { |
| 175 | drp->fDifference.fStatus = DiffResource::kExists_Status; |
| 176 | } else { |
| 177 | drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status; |
| 178 | } |
| 179 | if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) { |
| 180 | drp->fWhite.fStatus = DiffResource::kExists_Status; |
| 181 | } else { |
| 182 | drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |