michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkBitmap.h" michael@0: #include "SkBitmapHasher.h" michael@0: #include "SkEndian.h" michael@0: #include "SkImageEncoder.h" michael@0: michael@0: #include "SkMD5.h" michael@0: michael@0: /** michael@0: * Write an int32 value to a stream in little-endian order. michael@0: */ michael@0: static void write_int32_to_buffer(uint32_t val, SkWStream* out) { michael@0: val = SkEndian_SwapLE32(val); michael@0: for (size_t byte = 0; byte < 4; ++byte) { michael@0: out->write8((uint8_t)(val & 0xff)); michael@0: val = val >> 8; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64. michael@0: */ michael@0: static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) { michael@0: return SkEndian_SwapLE64(*(reinterpret_cast(bytearray))); michael@0: } michael@0: michael@0: /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, uint64_t *result) { michael@0: SkMD5 out; michael@0: michael@0: // start with the x/y dimensions michael@0: write_int32_to_buffer(SkToU32(bitmap.width()), &out); michael@0: write_int32_to_buffer(SkToU32(bitmap.height()), &out); michael@0: michael@0: // add all the pixel data michael@0: SkAutoTDelete enc(CreateARGBImageEncoder()); michael@0: if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { michael@0: return false; michael@0: } michael@0: michael@0: SkMD5::Digest digest; michael@0: out.finish(digest); michael@0: *result = first_8_bytes_as_uint64(digest.data); michael@0: return true; michael@0: } michael@0: michael@0: /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, uint64_t *result) { michael@0: if (ComputeDigestInternal(bitmap, result)) { michael@0: return true; michael@0: } michael@0: michael@0: // Hmm, that didn't work. Maybe if we create a new michael@0: // kARGB_8888_Config version of the bitmap it will work better? michael@0: SkBitmap copyBitmap; michael@0: if (!bitmap.copyTo(©Bitmap, kPMColor_SkColorType)) { michael@0: return false; michael@0: } michael@0: return ComputeDigestInternal(copyBitmap, result); michael@0: }