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" |
11 #include "SkImageEncoder.h" | 11 #include "SkImageEncoder.h" |
12 | 12 |
13 #ifdef BITMAPHASHER_USES_TRUNCATED_MD5 | |
14 #include "SkMD5.h" | 13 #include "SkMD5.h" |
15 #else | |
16 #include "SkCityHash.h" | |
17 #include "SkStream.h" | |
18 #endif | |
19 | 14 |
20 /** | 15 /** |
21 * Write an int32 value to a stream in little-endian order. | 16 * Write an int32 value to a stream in little-endian order. |
22 */ | 17 */ |
23 static void write_int32_to_buffer(uint32_t val, SkWStream* out) { | 18 static void write_int32_to_buffer(uint32_t val, SkWStream* out) { |
24 val = SkEndian_SwapLE32(val); | 19 val = SkEndian_SwapLE32(val); |
25 for (size_t byte = 0; byte < 4; ++byte) { | 20 for (size_t byte = 0; byte < 4; ++byte) { |
26 out->write8((uint8_t)(val & 0xff)); | 21 out->write8((uint8_t)(val & 0xff)); |
27 val = val >> 8; | 22 val = val >> 8; |
28 } | 23 } |
29 } | 24 } |
30 | 25 |
31 /** | 26 /** |
32 * 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. |
33 */ | 28 */ |
34 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) { |
35 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); | 30 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); |
36 } | 31 } |
37 | 32 |
38 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, | 33 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, |
39 SkHashDigest *result) { | 34 SkHashDigest *result) { |
40 #ifdef BITMAPHASHER_USES_TRUNCATED_MD5 | |
41 SkMD5 out; | 35 SkMD5 out; |
42 #else | |
43 size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4; | |
44 size_t totalBufferSize = pixelBufferSize + 2 * sizeof(uint32_t); | |
45 | |
46 SkAutoMalloc bufferManager(totalBufferSize); | |
47 char *bufferStart = static_cast<char *>(bufferManager.get()); | |
48 SkMemoryWStream out(bufferStart, totalBufferSize); | |
49 #endif | |
50 | 36 |
51 // start with the x/y dimensions | 37 // start with the x/y dimensions |
52 write_int32_to_buffer(SkToU32(bitmap.width()), &out); | 38 write_int32_to_buffer(SkToU32(bitmap.width()), &out); |
53 write_int32_to_buffer(SkToU32(bitmap.height()), &out); | 39 write_int32_to_buffer(SkToU32(bitmap.height()), &out); |
54 | 40 |
55 // add all the pixel data | 41 // add all the pixel data |
56 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); | 42 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
57 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { | 43 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { |
58 return false; | 44 return false; |
59 } | 45 } |
60 | 46 |
61 #ifdef BITMAPHASHER_USES_TRUNCATED_MD5 | |
62 SkMD5::Digest digest; | 47 SkMD5::Digest digest; |
63 out.finish(digest); | 48 out.finish(digest); |
64 *result = first_8_bytes_as_uint64(digest.data); | 49 *result = first_8_bytes_as_uint64(digest.data); |
65 #else | |
66 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); | |
67 #endif | |
68 return true; | 50 return true; |
69 } | 51 } |
70 | 52 |
71 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { | 53 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { |
72 if (ComputeDigestInternal(bitmap, result)) { | 54 if (ComputeDigestInternal(bitmap, result)) { |
73 return true; | 55 return true; |
74 } | 56 } |
75 | 57 |
76 // Hmm, that didn't work. Maybe if we create a new | 58 // Hmm, that didn't work. Maybe if we create a new |
77 // kARGB_8888_Config version of the bitmap it will work better? | 59 // kARGB_8888_Config version of the bitmap it will work better? |
78 SkBitmap copyBitmap; | 60 SkBitmap copyBitmap; |
79 if (!bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config)) { | 61 if (!bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config)) { |
80 return false; | 62 return false; |
81 } | 63 } |
82 return ComputeDigestInternal(copyBitmap, result); | 64 return ComputeDigestInternal(copyBitmap, result); |
83 } | 65 } |
OLD | NEW |