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 | 14 |
15 /** | 15 /** |
16 * Write an integer value into a bytebuffer in little-endian order. | 16 * Write an integer value to a stream in little-endian order. |
17 */ | 17 */ |
18 static void write_int_to_buffer(int val, char* buf) { | 18 static void write_int_to_buffer(uint32_t val, SkWStream* out) { |
19 val = SkEndian_SwapLE32(val); | 19 val = SkEndian_SwapLE32(val); |
20 for (int byte=0; byte<4; byte++) { | 20 for (size_t byte = 0; byte < 4; ++byte) { |
21 *buf++ = (char)(val & 0xff); | 21 out->write8((uint8_t)(val & 0xff)); |
22 val = val >> 8; | 22 val = val >> 8; |
23 } | 23 } |
24 } | 24 } |
25 | 25 |
26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( | 26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, |
27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDi
gest *result) { | 27 SkHashDigest *result) { |
28 size_t pixelBufferSize = transformer.bytesNeededTotal(); | 28 size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4; |
29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns | 29 size_t totalBufferSize = pixelBufferSize + 2 * sizeof(uint32_t); |
30 | 30 |
31 SkAutoMalloc bufferManager(totalBufferSize); | 31 SkAutoMalloc bufferManager(totalBufferSize); |
32 char *bufferStart = static_cast<char *>(bufferManager.get()); | 32 char *bufferStart = static_cast<char *>(bufferManager.get()); |
33 char *bufPtr = bufferStart; | 33 SkMemoryWStream out(bufferStart, totalBufferSize); |
| 34 |
34 // start with the x/y dimensions | 35 // start with the x/y dimensions |
35 write_int_to_buffer(bitmap.width(), bufPtr); | 36 write_int_to_buffer(SkToU32(bitmap.width()), &out); |
36 bufPtr += 4; | 37 write_int_to_buffer(SkToU32(bitmap.height()), &out); |
37 write_int_to_buffer(bitmap.height(), bufPtr); | |
38 bufPtr += 4; | |
39 | 38 |
40 // add all the pixel data | 39 // add all the pixel data |
41 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { | 40 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); |
| 41 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { |
42 return false; | 42 return false; |
43 } | 43 } |
| 44 |
44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); | 45 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
45 return true; | 46 return true; |
46 } | 47 } |
47 | 48 |
48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { | 49 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { |
49 const SkBitmapTransformer::PixelFormat kPixelFormat = | 50 if (ComputeDigestInternal(bitmap, result)) { |
50 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | 51 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 } | 52 } |
58 | 53 |
59 // Hmm, that didn't work. Maybe if we create a new | 54 // Hmm, that didn't work. Maybe if we create a new |
60 // kARGB_8888_Config version of the bitmap it will work better? | 55 // kARGB_8888_Config version of the bitmap it will work better? |
61 SkBitmap copyBitmap; | 56 SkBitmap copyBitmap; |
62 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); | 57 if (!bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config)) { |
63 const SkBitmapTransformer copyTransformer = | |
64 SkBitmapTransformer(copyBitmap, kPixelFormat); | |
65 if (copyTransformer.isValid(true)) { | |
66 return ComputeDigestInternal(copyBitmap, copyTransformer, result); | |
67 } else { | |
68 return false; | 58 return false; |
69 } | 59 } |
| 60 return ComputeDigestInternal(copyBitmap, result); |
70 } | 61 } |
OLD | NEW |