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 into a bytebuffer in little-endian order. |
epoger
2013/04/19 21:13:02
"into a bytebuffer" -> "to a stream"
bungeman-skia
2013/04/22 18:31:14
Done.
| |
17 */ | 17 */ |
18 static void write_int_to_buffer(int val, char* buf) { | 18 static void write_int_to_buffer(int 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 < sizeof(int); ++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(int); |
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 SkWStream* out = new SkMemoryWStream(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(bitmap.width(), out); |
36 bufPtr += 4; | 37 write_int_to_buffer(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()); |
epoger
2013/04/19 21:13:02
Is there a significant performance penalty for cre
| |
41 if (!enc->encodeStream(out, bitmap, 100)) { | |
epoger
2013/04/19 21:13:02
Where does the magic "100" come from? Can we refe
bungeman-skia
2013/04/22 18:31:14
Ah, you need a good editor which will show documen
| |
42 return false; | 42 return false; |
43 } | 43 } |
44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); | 44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
45 return true; | 45 return true; |
46 } | 46 } |
47 | 47 |
48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige st *result) { | 48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige st *result) { |
49 const SkBitmapTransformer::PixelFormat kPixelFormat = | 49 if (ComputeDigestInternal(bitmap, result)) { |
50 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | 50 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 } | 51 } |
58 | 52 |
59 // Hmm, that didn't work. Maybe if we create a new | 53 // Hmm, that didn't work. Maybe if we create a new |
epoger
2013/04/19 21:13:02
I don't know why we would encounter this anymore,
bungeman-skia
2013/04/22 18:31:14
Well, this doesn't support A8 or A1, since I didn'
| |
60 // kARGB_8888_Config version of the bitmap it will work better? | 54 // kARGB_8888_Config version of the bitmap it will work better? |
61 SkBitmap copyBitmap; | 55 SkBitmap copyBitmap; |
62 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); | 56 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); |
63 const SkBitmapTransformer copyTransformer = | 57 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 } | 58 } |
OLD | NEW |