epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SkBitmap.h" |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 10 | #include "SkBitmapHasher.h" |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 11 | #include "SkBitmapTransformer.h" |
| 12 | #include "SkCityHash.h" |
| 13 | #include "SkEndian.h" |
| 14 | |
| 15 | /** |
| 16 | * Write an integer value into a bytebuffer in little-endian order. |
| 17 | */ |
| 18 | static void write_int_to_buffer(int val, char* buf) { |
| 19 | val = SkEndian_SwapLE32(val); |
| 20 | for (int byte=0; byte<4; byte++) { |
| 21 | *buf++ = (char)(val & 0xff); |
| 22 | val = val >> 8; |
| 23 | } |
| 24 | } |
| 25 | |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 26 | /*static*/ bool SkBitmapHasher::ComputeDigestInternal( |
| 27 | const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDigest *result) { |
scroggo@google.com | 807da9d | 2013-03-07 18:14:11 +0000 | [diff] [blame] | 28 | size_t pixelBufferSize = transformer.bytesNeededTotal(); |
| 29 | size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensions |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 30 | |
| 31 | SkAutoMalloc bufferManager(totalBufferSize); |
| 32 | char *bufferStart = static_cast<char *>(bufferManager.get()); |
| 33 | char *bufPtr = bufferStart; |
| 34 | // start with the x/y dimensions |
| 35 | write_int_to_buffer(bitmap.width(), bufPtr); |
| 36 | bufPtr += 4; |
| 37 | write_int_to_buffer(bitmap.height(), bufPtr); |
| 38 | bufPtr += 4; |
| 39 | |
| 40 | // add all the pixel data |
| 41 | if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) { |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 42 | return false; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 43 | } |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 44 | *result = SkCityHash::Compute64(bufferStart, totalBufferSize); |
| 45 | return true; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 46 | } |
| 47 | |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 48 | /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDigest *result) { |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 49 | const SkBitmapTransformer::PixelFormat kPixelFormat = |
| 50 | SkBitmapTransformer::kARGB_8888_Premul_PixelFormat; |
| 51 | |
| 52 | // First, try to transform the existing bitmap. |
| 53 | const SkBitmapTransformer transformer = |
| 54 | SkBitmapTransformer(bitmap, kPixelFormat); |
| 55 | if (transformer.isValid(false)) { |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 56 | return ComputeDigestInternal(bitmap, transformer, result); |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Hmm, that didn't work. Maybe if we create a new |
| 60 | // kARGB_8888_Config version of the bitmap it will work better? |
| 61 | SkBitmap copyBitmap; |
| 62 | bitmap.copyTo(©Bitmap, SkBitmap::kARGB_8888_Config); |
| 63 | const SkBitmapTransformer copyTransformer = |
| 64 | SkBitmapTransformer(copyBitmap, kPixelFormat); |
| 65 | if (copyTransformer.isValid(true)) { |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 66 | return ComputeDigestInternal(copyBitmap, copyTransformer, result); |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 67 | } else { |
epoger@google.com | 908f583 | 2013-04-12 02:23:55 +0000 | [diff] [blame] | 68 | return false; |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 69 | } |
| 70 | } |