| 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 "SkBitmapChecksummer.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 | 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. |
| 17 */ | 17 */ |
| 18 static void write_int_to_buffer(int val, char* buf) { | 18 static void write_int_to_buffer(int val, char* buf) { |
| 19 val = SkEndian_SwapLE32(val); | 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 /*static*/ uint64_t SkBitmapChecksummer::Compute64Internal( | 26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal( |
| 27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer) { | 27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDi
gest *result) { |
| 28 size_t pixelBufferSize = transformer.bytesNeededTotal(); | 28 size_t pixelBufferSize = transformer.bytesNeededTotal(); |
| 29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns | 29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensio
ns |
| 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 char *bufPtr = bufferStart; |
| 34 // start with the x/y dimensions | 34 // start with the x/y dimensions |
| 35 write_int_to_buffer(bitmap.width(), bufPtr); | 35 write_int_to_buffer(bitmap.width(), bufPtr); |
| 36 bufPtr += 4; | 36 bufPtr += 4; |
| 37 write_int_to_buffer(bitmap.height(), bufPtr); | 37 write_int_to_buffer(bitmap.height(), bufPtr); |
| 38 bufPtr += 4; | 38 bufPtr += 4; |
| 39 | 39 |
| 40 // add all the pixel data | 40 // add all the pixel data |
| 41 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { | 41 if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { |
| 42 return 0; | 42 return false; |
| 43 } | 43 } |
| 44 return SkCityHash::Compute64(bufferStart, totalBufferSize); | 44 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
| 45 return true; |
| 45 } | 46 } |
| 46 | 47 |
| 47 /*static*/ uint64_t SkBitmapChecksummer::Compute64(const SkBitmap& bitmap) { | 48 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige
st *result) { |
| 48 const SkBitmapTransformer::PixelFormat kPixelFormat = | 49 const SkBitmapTransformer::PixelFormat kPixelFormat = |
| 49 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; | 50 SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; |
| 50 | 51 |
| 51 // First, try to transform the existing bitmap. | 52 // First, try to transform the existing bitmap. |
| 52 const SkBitmapTransformer transformer = | 53 const SkBitmapTransformer transformer = |
| 53 SkBitmapTransformer(bitmap, kPixelFormat); | 54 SkBitmapTransformer(bitmap, kPixelFormat); |
| 54 if (transformer.isValid(false)) { | 55 if (transformer.isValid(false)) { |
| 55 return Compute64Internal(bitmap, transformer); | 56 return ComputeDigestInternal(bitmap, transformer, result); |
| 56 } | 57 } |
| 57 | 58 |
| 58 // Hmm, that didn't work. Maybe if we create a new | 59 // Hmm, that didn't work. Maybe if we create a new |
| 59 // kARGB_8888_Config version of the bitmap it will work better? | 60 // kARGB_8888_Config version of the bitmap it will work better? |
| 60 SkBitmap copyBitmap; | 61 SkBitmap copyBitmap; |
| 61 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); | 62 bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); |
| 62 const SkBitmapTransformer copyTransformer = | 63 const SkBitmapTransformer copyTransformer = |
| 63 SkBitmapTransformer(copyBitmap, kPixelFormat); | 64 SkBitmapTransformer(copyBitmap, kPixelFormat); |
| 64 if (copyTransformer.isValid(true)) { | 65 if (copyTransformer.isValid(true)) { |
| 65 return Compute64Internal(copyBitmap, copyTransformer); | 66 return ComputeDigestInternal(copyBitmap, copyTransformer, result); |
| 66 } else { | 67 } else { |
| 67 return 0; | 68 return false; |
| 68 } | 69 } |
| 69 } | 70 } |
| OLD | NEW |