OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkBitmapHasher.h" | 10 #include "SkBitmapHasher.h" |
11 #include "SkBitmapTransformer.h" | 11 #include "SkBitmapTransformer.h" |
12 #include "SkCityHash.h" | 12 #include "SkCityHash.h" |
13 #include "SkEndian.h" | 13 #include "SkEndian.h" |
14 | 14 |
15 /** | 15 /** |
16 * Write an integer value into a bytebuffer in little-endian order. | 16 * Write an int32_t value into a bytebuffer in little-endian order. |
17 */ | 17 */ |
18 static void write_int_to_buffer(int val, char* buf) { | 18 static void write_int32_to_buffer(int32_t val, char* buf) { |
19 val = SkEndian_SwapLE32(val); | |
20 for (int byte=0; byte<4; byte++) { | 19 for (int byte=0; byte<4; byte++) { |
21 *buf++ = (char)(val & 0xff); | 20 *buf++ = (char)(val & 0xff); |
22 val = val >> 8; | 21 val = val >> 8; |
23 } | 22 } |
24 } | 23 } |
25 | 24 |
| 25 #ifdef BITMAP_HASH_TYPE_SkHashDigest |
| 26 /** |
| 27 * Write an int64_t value into a bytebuffer in little-endian order. |
| 28 */ |
| 29 static void write_int64_to_buffer(int64_t val, char* buf) { |
| 30 for (int byte=0; byte<8; byte++) { |
| 31 *buf++ = (char)(val & 0xff); |
| 32 val = val >> 8; |
| 33 } |
| 34 } |
| 35 #endif |
| 36 |
26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( | 37 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( |
27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDi
gest *result) { | 38 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, BITMAP_H
ASH_TYPE *result) { |
28 size_t pixelBufferSize = transformer.bytesNeededTotal(); | 39 size_t pixelBufferSize = transformer.bytesNeededTotal(); |
29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns | 40 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns |
30 | 41 |
31 SkAutoMalloc bufferManager(totalBufferSize); | 42 SkAutoMalloc bufferManager(totalBufferSize); |
32 char *bufferStart = static_cast<char *>(bufferManager.get()); | 43 char *bufferStart = static_cast<char *>(bufferManager.get()); |
33 char *bufPtr = bufferStart; | 44 char *bufPtr = bufferStart; |
34 // start with the x/y dimensions | 45 // start with the x/y dimensions |
35 write_int_to_buffer(bitmap.width(), bufPtr); | 46 write_int32_to_buffer(bitmap.width(), bufPtr); |
36 bufPtr += 4; | 47 bufPtr += 4; |
37 write_int_to_buffer(bitmap.height(), bufPtr); | 48 write_int32_to_buffer(bitmap.height(), bufPtr); |
38 bufPtr += 4; | 49 bufPtr += 4; |
39 | 50 |
40 // add all the pixel data | 51 // add all the pixel data |
41 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { | 52 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { |
42 return false; | 53 return false; |
43 } | 54 } |
| 55 #ifdef BITMAP_HASH_TYPE_SkHashDigest |
| 56 uint64_t cityhash = SkCityHash::Compute64(bufferStart, totalBufferSize); |
| 57 char tempResult[8]; |
| 58 write_int64_to_buffer(cityhash, tempResult); |
| 59 result->copyFrom(tempResult, 8); |
| 60 #else |
44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); | 61 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
| 62 #endif |
45 return true; | 63 return true; |
46 } | 64 } |
47 | 65 |
48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { | 66 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, BITMAP_HAS
H_TYPE *result) { |
49 const SkBitmapTransformer::PixelFormat kPixelFormat = | 67 const SkBitmapTransformer::PixelFormat kPixelFormat = |
50 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | 68 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; |
51 | 69 |
52 // First, try to transform the existing bitmap. | 70 // First, try to transform the existing bitmap. |
53 const SkBitmapTransformer transformer = | 71 const SkBitmapTransformer transformer = |
54 SkBitmapTransformer(bitmap, kPixelFormat); | 72 SkBitmapTransformer(bitmap, kPixelFormat); |
55 if (transformer.isValid(false)) { | 73 if (transformer.isValid(false)) { |
56 return ComputeDigestInternal(bitmap, transformer, result); | 74 return ComputeDigestInternal(bitmap, transformer, result); |
57 } | 75 } |
58 | 76 |
59 // Hmm, that didn't work. Maybe if we create a new | 77 // Hmm, that didn't work. Maybe if we create a new |
60 // kARGB_8888_Config version of the bitmap it will work better? | 78 // kARGB_8888_Config version of the bitmap it will work better? |
61 SkBitmap copyBitmap; | 79 SkBitmap copyBitmap; |
62 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); | 80 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); |
63 const SkBitmapTransformer copyTransformer = | 81 const SkBitmapTransformer copyTransformer = |
64 SkBitmapTransformer(copyBitmap, kPixelFormat); | 82 SkBitmapTransformer(copyBitmap, kPixelFormat); |
65 if (copyTransformer.isValid(true)) { | 83 if (copyTransformer.isValid(true)) { |
66 return ComputeDigestInternal(copyBitmap, copyTransformer, result); | 84 return ComputeDigestInternal(copyBitmap, copyTransformer, result); |
67 } else { | 85 } else { |
68 return false; | 86 return false; |
69 } | 87 } |
70 } | 88 } |
OLD | NEW |