epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +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 | |
| 8 | #include "SkBitmap.h" |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 9 | #include "SkBitmapHasher.h" |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 10 | #include "SkEndian.h" |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 11 | #include "SkImageEncoder.h" |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 12 | |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 13 | #include "SkMD5.h" |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 16 | * Write an int32 value to a stream in little-endian order. |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 17 | */ |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 18 | static void write_int32_to_buffer(uint32_t val, SkWStream* out) { |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 19 | val = SkEndian_SwapLE32(val); |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 20 | for (size_t byte = 0; byte < 4; ++byte) { |
| 21 | out->write8((uint8_t)(val & 0xff)); |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 22 | val = val >> 8; |
| 23 | } |
| 24 | } |
| 25 | |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64. |
| 28 | */ |
| 29 | static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) { |
| 30 | return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); |
| 31 | } |
| 32 | |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 33 | /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, uint64_t *result) { |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 34 | SkMD5 out; |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 35 | |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 36 | // start with the x/y dimensions |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 37 | write_int32_to_buffer(SkToU32(bitmap.width()), &out); |
| 38 | write_int32_to_buffer(SkToU32(bitmap.height()), &out); |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 39 | |
| 40 | // add all the pixel data |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 41 | SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
| 42 | if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 43 | return false; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 44 | } |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 45 | |
epoger@google.com | b4ca46d | 2013-05-03 17:35:39 +0000 | [diff] [blame] | 46 | SkMD5::Digest digest; |
| 47 | out.finish(digest); |
| 48 | *result = first_8_bytes_as_uint64(digest.data); |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 49 | return true; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 52 | /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, uint64_t *result) { |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 53 | if (ComputeDigestInternal(bitmap, result)) { |
| 54 | return true; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Hmm, that didn't work. Maybe if we create a new |
| 58 | // kARGB_8888_Config version of the bitmap it will work better? |
| 59 | SkBitmap copyBitmap; |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 60 | if (!bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config)) { |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 61 | return false; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 62 | } |
bungeman@google.com | 8c6a4f2 | 2013-04-23 18:06:23 +0000 | [diff] [blame] | 63 | return ComputeDigestInternal(copyBitmap, result); |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 64 | } |