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