| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * 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 |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapHasher.h" | 9 #include "SkBitmapHasher.h" |
| 10 #include "SkEndian.h" | 10 #include "SkEndian.h" |
| 11 #include "SkImageEncoder.h" | 11 #include "SkImageEncoder_argb.h" |
| 12 | |
| 13 #include "SkMD5.h" | 12 #include "SkMD5.h" |
| 14 | 13 |
| 15 /** | 14 /** |
| 16 * Write an int32 value to a stream in little-endian order. | 15 * Write an int32 value to a stream in little-endian order. |
| 17 */ | 16 */ |
| 18 static void write_int32_to_buffer(uint32_t val, SkWStream* out) { | 17 static void write_int32_to_buffer(uint32_t val, SkWStream* out) { |
| 19 val = SkEndian_SwapLE32(val); | 18 val = SkEndian_SwapLE32(val); |
| 20 for (size_t byte = 0; byte < 4; ++byte) { | 19 for (size_t byte = 0; byte < 4; ++byte) { |
| 21 out->write8((uint8_t)(val & 0xff)); | 20 out->write8((uint8_t)(val & 0xff)); |
| 22 val = val >> 8; | 21 val = val >> 8; |
| 23 } | 22 } |
| 24 } | 23 } |
| 25 | 24 |
| 26 /** | 25 /** |
| 27 * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64. | 26 * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64. |
| 28 */ | 27 */ |
| 29 static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) { | 28 static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) { |
| 30 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); | 29 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray))); |
| 31 } | 30 } |
| 32 | 31 |
| 33 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, ui
nt64_t *result) { | 32 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, ui
nt64_t *result) { |
| 34 SkMD5 out; | 33 SkMD5 out; |
| 35 | 34 |
| 36 // start with the x/y dimensions | 35 // start with the x/y dimensions |
| 37 write_int32_to_buffer(SkToU32(bitmap.width()), &out); | 36 write_int32_to_buffer(SkToU32(bitmap.width()), &out); |
| 38 write_int32_to_buffer(SkToU32(bitmap.height()), &out); | 37 write_int32_to_buffer(SkToU32(bitmap.height()), &out); |
| 39 | 38 |
| 40 // add all the pixel data | 39 // add all the pixel data |
| 41 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); | 40 SkAutoTDelete<SkImageEncoder> enc(SkNEW(SkARGBImageEncoder)); |
| 42 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { | 41 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { |
| 43 return false; | 42 return false; |
| 44 } | 43 } |
| 45 | 44 |
| 46 SkMD5::Digest digest; | 45 SkMD5::Digest digest; |
| 47 out.finish(digest); | 46 out.finish(digest); |
| 48 *result = first_8_bytes_as_uint64(digest.data); | 47 *result = first_8_bytes_as_uint64(digest.data); |
| 49 return true; | 48 return true; |
| 50 } | 49 } |
| 51 | 50 |
| 52 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, uint64_t *
result) { | 51 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, uint64_t *
result) { |
| 53 if (ComputeDigestInternal(bitmap, result)) { | 52 if (ComputeDigestInternal(bitmap, result)) { |
| 54 return true; | 53 return true; |
| 55 } | 54 } |
| 56 | 55 |
| 57 // Hmm, that didn't work. Maybe if we create a new | 56 // Hmm, that didn't work. Maybe if we create a new |
| 58 // version of the bitmap it will work better? | 57 // version of the bitmap it will work better? |
| 59 SkBitmap copyBitmap; | 58 SkBitmap copyBitmap; |
| 60 if (!bitmap.copyTo(©Bitmap, kN32_SkColorType)) { | 59 if (!bitmap.copyTo(©Bitmap, kN32_SkColorType)) { |
| 61 return false; | 60 return false; |
| 62 } | 61 } |
| 63 return ComputeDigestInternal(copyBitmap, result); | 62 return ComputeDigestInternal(copyBitmap, result); |
| 64 } | 63 } |
| OLD | NEW |