OLD | NEW |
---|---|
1 | |
2 /* | 1 /* |
3 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
4 * | 3 * |
5 * 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 |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
10 #include "SkBitmapHasher.h" | 9 #include "SkBitmapHasher.h" |
11 #include "SkBitmapTransformer.h" | |
12 #include "SkCityHash.h" | 10 #include "SkCityHash.h" |
13 #include "SkEndian.h" | 11 #include "SkEndian.h" |
12 #include "SkImageEncoder.h" | |
13 #include "SkStream.h" | |
14 #include "SkMD5.h" | |
14 | 15 |
16 #define SK_DIGEST_CONST 0 | |
17 #define SK_DIGEST_CITYHASH 1 | |
18 #define SK_DIGEST_MD5 2 | |
19 #define SK_DIGEST SK_DIGEST_CITYHASH | |
20 | |
21 #if SK_DIGEST != SK_DIGEST_CONST | |
15 /** | 22 /** |
16 * Write an integer value into a bytebuffer in little-endian order. | 23 * Write an integer value to a stream in little-endian order. |
17 */ | 24 */ |
18 static void write_int_to_buffer(int val, char* buf) { | 25 static void write_int_to_buffer(int val, SkWStream* out) { |
19 val = SkEndian_SwapLE32(val); | 26 val = SkEndian_SwapLE32(val); |
20 for (int byte=0; byte<4; byte++) { | 27 for (size_t byte = 0; byte < sizeof(int); ++byte) { |
21 *buf++ = (char)(val & 0xff); | 28 out->write8((uint8_t)(val & 0xff)); |
22 val = val >> 8; | 29 val = val >> 8; |
23 } | 30 } |
24 } | 31 } |
32 #endif | |
25 | 33 |
26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( | 34 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, |
27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDi gest *result) { | 35 SkHashDigest *result) { |
28 size_t pixelBufferSize = transformer.bytesNeededTotal(); | 36 #if SK_DIGEST == SK_DIGEST_CONST |
29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio ns | 37 *result = 0; |
38 #else | |
39 | |
40 #if SK_DIGEST == SK_DIGEST_CITYHASH | |
41 size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4; | |
42 size_t totalBufferSize = pixelBufferSize + 2 * sizeof(int); | |
30 | 43 |
31 SkAutoMalloc bufferManager(totalBufferSize); | 44 SkAutoMalloc bufferManager(totalBufferSize); |
32 char *bufferStart = static_cast<char *>(bufferManager.get()); | 45 char *bufferStart = static_cast<char *>(bufferManager.get()); |
33 char *bufPtr = bufferStart; | 46 SkMemoryWStream out(bufferStart, totalBufferSize); |
47 #elif SK_DIGEST == SK_DIGEST_MD5 | |
48 SkMD5 out; | |
49 #endif | |
34 // start with the x/y dimensions | 50 // start with the x/y dimensions |
35 write_int_to_buffer(bitmap.width(), bufPtr); | 51 write_int_to_buffer(bitmap.width(), &out); |
36 bufPtr += 4; | 52 write_int_to_buffer(bitmap.height(), &out); |
37 write_int_to_buffer(bitmap.height(), bufPtr); | |
38 bufPtr += 4; | |
39 | 53 |
40 // add all the pixel data | 54 // add all the pixel data |
41 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { | 55 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
56 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { | |
epoger
2013/04/22 19:53:28
So, I guess SkMemoryWStream is going to keep reall
bungeman-skia
2013/04/22 20:08:00
No. We're only using the SkMemoryWStream in the Ci
| |
42 return false; | 57 return false; |
43 } | 58 } |
59 | |
60 #if SK_DIGEST == SK_DIGEST_CITYHASH | |
44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); | 61 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
62 #elif SK_DIGEST == SK_DIGEST_MD5 | |
63 SkMD5::Digest digest; | |
64 out.finish(digest); | |
65 *result = *((SkHashDigest*)&digest); | |
epoger
2013/04/22 19:53:28
Since SkHashDigest is just a typedef for uint64_t,
bungeman-skia
2013/04/22 20:08:00
I was interested in the difference between MD5 and
| |
66 #endif | |
67 #endif | |
45 return true; | 68 return true; |
46 } | 69 } |
47 | 70 |
48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige st *result) { | 71 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige st *result) { |
49 const SkBitmapTransformer::PixelFormat kPixelFormat = | 72 if (ComputeDigestInternal(bitmap, result)) { |
50 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | 73 return true; |
51 | |
52 // First, try to transform the existing bitmap. | |
53 const SkBitmapTransformer transformer = | |
54 SkBitmapTransformer(bitmap, kPixelFormat); | |
55 if (transformer.isValid(false)) { | |
56 return ComputeDigestInternal(bitmap, transformer, result); | |
57 } | 74 } |
58 | 75 |
59 // Hmm, that didn't work. Maybe if we create a new | 76 // Hmm, that didn't work. Maybe if we create a new |
60 // kARGB_8888_Config version of the bitmap it will work better? | 77 // kARGB_8888_Config version of the bitmap it will work better? |
61 SkBitmap copyBitmap; | 78 SkBitmap copyBitmap; |
62 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); | 79 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); |
63 const SkBitmapTransformer copyTransformer = | 80 return ComputeDigestInternal(copyBitmap, result); |
64 SkBitmapTransformer(copyBitmap, kPixelFormat); | |
65 if (copyTransformer.isValid(true)) { | |
66 return ComputeDigestInternal(copyBitmap, copyTransformer, result); | |
67 } else { | |
68 return false; | |
69 } | |
70 } | 81 } |
OLD | NEW |