2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapHasher.h"
11 #include "SkImageEncoder.h"
16 * Write an int32 value to a stream in little-endian order.
18 static void write_int32_to_buffer(uint32_t val, SkWStream* out) {
19 val = SkEndian_SwapLE32(val);
20 for (size_t byte = 0; byte < 4; ++byte) {
21 out->write8((uint8_t)(val & 0xff));
27 * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64.
29 static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) {
30 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray)));
33 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, uint64_t *result) {
36 // start with the x/y dimensions
37 write_int32_to_buffer(SkToU32(bitmap.width()), &out);
38 write_int32_to_buffer(SkToU32(bitmap.height()), &out);
40 // add all the pixel data
41 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
42 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) {
48 *result = first_8_bytes_as_uint64(digest.data);
52 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, uint64_t *result) {
53 if (ComputeDigestInternal(bitmap, result)) {
57 // Hmm, that didn't work. Maybe if we create a new
58 // kARGB_8888_Config version of the bitmap it will work better?
60 if (!bitmap.copyTo(©Bitmap, kN32_SkColorType)) {
63 return ComputeDigestInternal(copyBitmap, result);