OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkBitmapHasher.h" | 9 #include "SkBitmapHasher.h" |
10 #include "SkEndian.h" | 10 #include "SkEndian.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 /** | 26 /** |
27 * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64. | 27 * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64. |
28 */ | 28 */ |
29 static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) { | 29 static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) { |
30 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); | 30 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); |
31 } | 31 } |
32 | 32 |
33 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, | 33 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, ui
nt64_t *result) { |
34 SkHashDigest *result) { | |
35 SkMD5 out; | 34 SkMD5 out; |
36 | 35 |
37 // start with the x/y dimensions | 36 // start with the x/y dimensions |
38 write_int32_to_buffer(SkToU32(bitmap.width()), &out); | 37 write_int32_to_buffer(SkToU32(bitmap.width()), &out); |
39 write_int32_to_buffer(SkToU32(bitmap.height()), &out); | 38 write_int32_to_buffer(SkToU32(bitmap.height()), &out); |
40 | 39 |
41 // add all the pixel data | 40 // add all the pixel data |
42 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); | 41 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
43 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { | 42 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { |
44 return false; | 43 return false; |
45 } | 44 } |
46 | 45 |
47 SkMD5::Digest digest; | 46 SkMD5::Digest digest; |
48 out.finish(digest); | 47 out.finish(digest); |
49 *result = first_8_bytes_as_uint64(digest.data); | 48 *result = first_8_bytes_as_uint64(digest.data); |
50 return true; | 49 return true; |
51 } | 50 } |
52 | 51 |
53 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { | 52 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, uint64_t *
result) { |
54 if (ComputeDigestInternal(bitmap, result)) { | 53 if (ComputeDigestInternal(bitmap, result)) { |
55 return true; | 54 return true; |
56 } | 55 } |
57 | 56 |
58 // Hmm, that didn't work. Maybe if we create a new | 57 // Hmm, that didn't work. Maybe if we create a new |
59 // kARGB_8888_Config version of the bitmap it will work better? | 58 // kARGB_8888_Config version of the bitmap it will work better? |
60 SkBitmap copyBitmap; | 59 SkBitmap copyBitmap; |
61 if (!bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config)) { | 60 if (!bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config)) { |
62 return false; | 61 return false; |
63 } | 62 } |
64 return ComputeDigestInternal(copyBitmap, result); | 63 return ComputeDigestInternal(copyBitmap, result); |
65 } | 64 } |
OLD | NEW |