Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: src/utils/SkBitmapHasher.cpp

Issue 14054012: SkBitmapHasher: use 64-bit-truncated MD5 instead of 64-bit CityHash (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: mandate_little_endian Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "SkCityHash.h"
11 #include "SkEndian.h" 10 #include "SkEndian.h"
12 #include "SkImageEncoder.h" 11 #include "SkImageEncoder.h"
12
13 #ifdef BITMAPHASHER_USES_TRUNCATED_MD5
14 #include "SkMD5.h"
15 #else
16 #include "SkCityHash.h"
13 #include "SkStream.h" 17 #include "SkStream.h"
18 #endif
14 19
15 /** 20 /**
16 * Write an integer value to a stream in little-endian order. 21 * Write an int32 value to a stream in little-endian order.
17 */ 22 */
18 static void write_int_to_buffer(uint32_t val, SkWStream* out) { 23 static void write_int32_to_buffer(uint32_t val, SkWStream* out) {
19 val = SkEndian_SwapLE32(val); 24 val = SkEndian_SwapLE32(val);
20 for (size_t byte = 0; byte < 4; ++byte) { 25 for (size_t byte = 0; byte < 4; ++byte) {
21 out->write8((uint8_t)(val & 0xff)); 26 out->write8((uint8_t)(val & 0xff));
22 val = val >> 8; 27 val = val >> 8;
23 } 28 }
24 } 29 }
25 30
31 /**
32 * Return the first 8 bytes of a bytearray, encoded as a little-endian uint64.
33 */
34 static inline uint64_t first_8_bytes_as_uint64(const uint8_t *bytearray) {
35 return SkEndian_SwapLE64(*(reinterpret_cast<const uint64_t *>(bytearray)));
36 }
37
26 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, 38 /*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap,
27 SkHashDigest *result) { 39 SkHashDigest *result) {
40 #ifdef BITMAPHASHER_USES_TRUNCATED_MD5
41 SkMD5 out;
42 #else
28 size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4; 43 size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4;
29 size_t totalBufferSize = pixelBufferSize + 2 * sizeof(uint32_t); 44 size_t totalBufferSize = pixelBufferSize + 2 * sizeof(uint32_t);
30 45
31 SkAutoMalloc bufferManager(totalBufferSize); 46 SkAutoMalloc bufferManager(totalBufferSize);
32 char *bufferStart = static_cast<char *>(bufferManager.get()); 47 char *bufferStart = static_cast<char *>(bufferManager.get());
33 SkMemoryWStream out(bufferStart, totalBufferSize); 48 SkMemoryWStream out(bufferStart, totalBufferSize);
49 #endif
34 50
35 // start with the x/y dimensions 51 // start with the x/y dimensions
36 write_int_to_buffer(SkToU32(bitmap.width()), &out); 52 write_int32_to_buffer(SkToU32(bitmap.width()), &out);
37 write_int_to_buffer(SkToU32(bitmap.height()), &out); 53 write_int32_to_buffer(SkToU32(bitmap.height()), &out);
38 54
39 // add all the pixel data 55 // add all the pixel data
40 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); 56 SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
41 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { 57 if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) {
42 return false; 58 return false;
43 } 59 }
44 60
61 #ifdef BITMAPHASHER_USES_TRUNCATED_MD5
62 SkMD5::Digest digest;
63 out.finish(digest);
64 *result = first_8_bytes_as_uint64(digest.data);
65 #else
45 *result = SkCityHash::Compute64(bufferStart, totalBufferSize); 66 *result = SkCityHash::Compute64(bufferStart, totalBufferSize);
67 #endif
46 return true; 68 return true;
47 } 69 }
48 70
49 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige st *result) { 71 /*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDige st *result) {
50 if (ComputeDigestInternal(bitmap, result)) { 72 if (ComputeDigestInternal(bitmap, result)) {
51 return true; 73 return true;
52 } 74 }
53 75
54 // Hmm, that didn't work. Maybe if we create a new 76 // Hmm, that didn't work. Maybe if we create a new
55 // kARGB_8888_Config version of the bitmap it will work better? 77 // kARGB_8888_Config version of the bitmap it will work better?
56 SkBitmap copyBitmap; 78 SkBitmap copyBitmap;
57 if (!bitmap.copyTo(&copyBitmap, SkBitmap::kARGB_8888_Config)) { 79 if (!bitmap.copyTo(&copyBitmap, SkBitmap::kARGB_8888_Config)) {
58 return false; 80 return false;
59 } 81 }
60 return ComputeDigestInternal(copyBitmap, result); 82 return ComputeDigestInternal(copyBitmap, result);
61 } 83 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698