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 #include "SkMD5.h" |
14 | 15 |
15 /** | 16 /** |
16 * Write an integer value into a bytebuffer in little-endian order. | 17 * Write an int32_t value into a bytebuffer in little-endian order. |
17 */ | 18 */ |
18 static void write_int_to_buffer(int val, char* buf) { | 19 static void write_int32_to_buffer(int32_t val, char* buf) { |
19 val = SkEndian_SwapLE32(val); | |
20 for (int byte=0; byte<4; byte++) { | 20 for (int byte=0; byte<4; byte++) { |
21 *buf++ = (char)(val & 0xff); | 21 *buf++ = (char)(val & 0xff); |
22 val = val >> 8; | 22 val = val >> 8; |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
| 26 #ifdef BITMAP_HASH_TYPE_SkHashDigest |
26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( | 27 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( |
27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDi
gest *result) { | 28 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, BITMAP_H
ASH_TYPE *result) { |
| 29 SkMD5 hasher; |
| 30 |
| 31 // Start with x/y dimensions. |
| 32 uint8_t dimensions[8]; |
| 33 char *bufPtr = reinterpret_cast<char *>(dimensions); |
| 34 write_int32_to_buffer(bitmap.width(), bufPtr); |
| 35 write_int32_to_buffer(bitmap.height(), bufPtr + 4); |
| 36 hasher.update(dimensions, sizeof(dimensions)); |
| 37 |
| 38 // Add all the pixel data, one transformed row at a time. |
| 39 size_t rowBufferSize = transformer.bytesNeededPerRow(); |
| 40 SkAutoMalloc bufferManager(rowBufferSize); |
| 41 char *buffer = static_cast<char *>(bufferManager.get()); |
| 42 for (int row = 0; row < transformer.numRows(); row++) { |
| 43 if (!transformer.copyRowToPixelBuffer(row, buffer, rowBufferSize)) { |
| 44 return false; |
| 45 } |
| 46 hasher.update(reinterpret_cast<const uint8_t*>(buffer), rowBufferSize); |
| 47 } |
| 48 |
| 49 SkMD5::Digest digest; |
| 50 hasher.finish(digest); |
| 51 // EPOGER: instead of creating the extra copy on the stack and running copyF
rom, use malloc and then just make the SkHashDigest hold a reference to it? |
| 52 result->copyFrom(&digest, sizeof(digest)); |
| 53 return true; |
| 54 } |
| 55 #else |
| 56 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( |
| 57 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, BITMAP_H
ASH_TYPE *result) { |
28 size_t pixelBufferSize = transformer.bytesNeededTotal(); | 58 size_t pixelBufferSize = transformer.bytesNeededTotal(); |
29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns | 59 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns |
30 | 60 |
31 SkAutoMalloc bufferManager(totalBufferSize); | 61 SkAutoMalloc bufferManager(totalBufferSize); |
32 char *bufferStart = static_cast<char *>(bufferManager.get()); | 62 char *bufferStart = static_cast<char *>(bufferManager.get()); |
33 char *bufPtr = bufferStart; | 63 char *bufPtr = bufferStart; |
34 // start with the x/y dimensions | 64 // start with the x/y dimensions |
35 write_int_to_buffer(bitmap.width(), bufPtr); | 65 write_int32_to_buffer(bitmap.width(), bufPtr); |
36 bufPtr += 4; | 66 bufPtr += 4; |
37 write_int_to_buffer(bitmap.height(), bufPtr); | 67 write_int32_to_buffer(bitmap.height(), bufPtr); |
38 bufPtr += 4; | 68 bufPtr += 4; |
39 | 69 |
40 // add all the pixel data | 70 // add all the pixel data |
41 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { | 71 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { |
42 return false; | 72 return false; |
43 } | 73 } |
44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); | 74 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
45 return true; | 75 return true; |
46 } | 76 } |
| 77 #endif |
47 | 78 |
48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { | 79 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, BITMAP_HAS
H_TYPE *result) { |
49 const SkBitmapTransformer::PixelFormat kPixelFormat = | 80 const SkBitmapTransformer::PixelFormat kPixelFormat = |
50 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | 81 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; |
51 | 82 |
52 // First, try to transform the existing bitmap. | 83 // First, try to transform the existing bitmap. |
53 const SkBitmapTransformer transformer = | 84 const SkBitmapTransformer transformer = |
54 SkBitmapTransformer(bitmap, kPixelFormat); | 85 SkBitmapTransformer(bitmap, kPixelFormat); |
55 if (transformer.isValid(false)) { | 86 if (transformer.isValid(false)) { |
56 return ComputeDigestInternal(bitmap, transformer, result); | 87 return ComputeDigestInternal(bitmap, transformer, result); |
57 } | 88 } |
58 | 89 |
59 // Hmm, that didn't work. Maybe if we create a new | 90 // Hmm, that didn't work. Maybe if we create a new |
60 // kARGB_8888_Config version of the bitmap it will work better? | 91 // kARGB_8888_Config version of the bitmap it will work better? |
61 SkBitmap copyBitmap; | 92 SkBitmap copyBitmap; |
62 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); | 93 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); |
63 const SkBitmapTransformer copyTransformer = | 94 const SkBitmapTransformer copyTransformer = |
64 SkBitmapTransformer(copyBitmap, kPixelFormat); | 95 SkBitmapTransformer(copyBitmap, kPixelFormat); |
65 if (copyTransformer.isValid(true)) { | 96 if (copyTransformer.isValid(true)) { |
66 return ComputeDigestInternal(copyBitmap, copyTransformer, result); | 97 return ComputeDigestInternal(copyBitmap, copyTransformer, result); |
67 } else { | 98 } else { |
68 return false; | 99 return false; |
69 } | 100 } |
70 } | 101 } |
OLD | NEW |