blob: 6df3ab9f27a7d8923e2122a2b25d1d1d1b6909da [file] [log] [blame]
epoger@google.com31114c62012-12-12 17:22:23 +00001
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.com908f5832013-04-12 02:23:55 +000010#include "SkBitmapHasher.h"
epoger@google.com31114c62012-12-12 17:22:23 +000011#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 */
18static 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.com908f5832013-04-12 02:23:55 +000026/*static*/ bool SkBitmapHasher::ComputeDigestInternal(
27 const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDigest *result) {
scroggo@google.com807da9d2013-03-07 18:14:11 +000028 size_t pixelBufferSize = transformer.bytesNeededTotal();
29 size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensions
epoger@google.com31114c62012-12-12 17:22:23 +000030
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.com908f5832013-04-12 02:23:55 +000042 return false;
epoger@google.com31114c62012-12-12 17:22:23 +000043 }
epoger@google.com908f5832013-04-12 02:23:55 +000044 *result = SkCityHash::Compute64(bufferStart, totalBufferSize);
45 return true;
epoger@google.com31114c62012-12-12 17:22:23 +000046}
47
epoger@google.com908f5832013-04-12 02:23:55 +000048/*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDigest *result) {
epoger@google.com31114c62012-12-12 17:22:23 +000049 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.com908f5832013-04-12 02:23:55 +000056 return ComputeDigestInternal(bitmap, transformer, result);
epoger@google.com31114c62012-12-12 17:22:23 +000057 }
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(&copyBitmap, SkBitmap::kARGB_8888_Config);
63 const SkBitmapTransformer copyTransformer =
64 SkBitmapTransformer(copyBitmap, kPixelFormat);
65 if (copyTransformer.isValid(true)) {
epoger@google.com908f5832013-04-12 02:23:55 +000066 return ComputeDigestInternal(copyBitmap, copyTransformer, result);
epoger@google.com31114c62012-12-12 17:22:23 +000067 } else {
epoger@google.com908f5832013-04-12 02:23:55 +000068 return false;
epoger@google.com31114c62012-12-12 17:22:23 +000069 }
70}