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

Unified Diff: src/utils/SkBitmapHasher.cpp

Issue 14267031: ARGB image encoder for checksums. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Fix memory leak, clean up includes. Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: src/utils/SkBitmapHasher.cpp
===================================================================
--- src/utils/SkBitmapHasher.cpp (revision 8777)
+++ src/utils/SkBitmapHasher.cpp (working copy)
@@ -1,4 +1,3 @@
-
/*
* Copyright 2012 Google Inc.
*
@@ -8,37 +7,38 @@
#include "SkBitmap.h"
#include "SkBitmapHasher.h"
-#include "SkBitmapTransformer.h"
#include "SkCityHash.h"
#include "SkEndian.h"
+#include "SkImageEncoder.h"
+#include "SkStream.h"
/**
* Write an integer value into a bytebuffer in little-endian order.
epoger 2013/04/19 21:13:02 "into a bytebuffer" -> "to a stream"
bungeman-skia 2013/04/22 18:31:14 Done.
*/
-static void write_int_to_buffer(int val, char* buf) {
+static void write_int_to_buffer(int val, SkWStream* out) {
val = SkEndian_SwapLE32(val);
- for (int byte=0; byte<4; byte++) {
- *buf++ = (char)(val & 0xff);
+ for (size_t byte = 0; byte < sizeof(int); ++byte) {
+ out->write8((uint8_t)(val & 0xff));
val = val >> 8;
}
}
-/*static*/ bool SkBitmapHasher::ComputeDigestInternal(
- const SkBitmap& bitmap, const SkBitmapTransformer& transformer, SkHashDigest *result) {
- size_t pixelBufferSize = transformer.bytesNeededTotal();
- size_t totalBufferSize = pixelBufferSize + 8; // leave room for x/y dimensions
+/*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap,
+ SkHashDigest *result) {
+ size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4;
+ size_t totalBufferSize = pixelBufferSize + 2 * sizeof(int);
SkAutoMalloc bufferManager(totalBufferSize);
char *bufferStart = static_cast<char *>(bufferManager.get());
- char *bufPtr = bufferStart;
+ SkWStream* out = new SkMemoryWStream(bufferStart, totalBufferSize);
+
// start with the x/y dimensions
- write_int_to_buffer(bitmap.width(), bufPtr);
- bufPtr += 4;
- write_int_to_buffer(bitmap.height(), bufPtr);
- bufPtr += 4;
+ write_int_to_buffer(bitmap.width(), out);
+ write_int_to_buffer(bitmap.height(), out);
// add all the pixel data
- if (!transformer.copyBitmapToPixelBuffer(bufPtr, pixelBufferSize)) {
+ SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder());
epoger 2013/04/19 21:13:02 Is there a significant performance penalty for cre
+ if (!enc->encodeStream(out, bitmap, 100)) {
epoger 2013/04/19 21:13:02 Where does the magic "100" come from? Can we refe
bungeman-skia 2013/04/22 18:31:14 Ah, you need a good editor which will show documen
return false;
}
*result = SkCityHash::Compute64(bufferStart, totalBufferSize);
@@ -46,25 +46,13 @@
}
/*static*/ bool SkBitmapHasher::ComputeDigest(const SkBitmap& bitmap, SkHashDigest *result) {
- const SkBitmapTransformer::PixelFormat kPixelFormat =
- SkBitmapTransformer::kARGB_8888_Premul_PixelFormat;
-
- // First, try to transform the existing bitmap.
- const SkBitmapTransformer transformer =
- SkBitmapTransformer(bitmap, kPixelFormat);
- if (transformer.isValid(false)) {
- return ComputeDigestInternal(bitmap, transformer, result);
+ if (ComputeDigestInternal(bitmap, result)) {
+ return true;
}
// Hmm, that didn't work. Maybe if we create a new
epoger 2013/04/19 21:13:02 I don't know why we would encounter this anymore,
bungeman-skia 2013/04/22 18:31:14 Well, this doesn't support A8 or A1, since I didn'
// kARGB_8888_Config version of the bitmap it will work better?
SkBitmap copyBitmap;
bitmap.copyTo(&copyBitmap, SkBitmap::kARGB_8888_Config);
- const SkBitmapTransformer copyTransformer =
- SkBitmapTransformer(copyBitmap, kPixelFormat);
- if (copyTransformer.isValid(true)) {
- return ComputeDigestInternal(copyBitmap, copyTransformer, result);
- } else {
- return false;
- }
+ return ComputeDigestInternal(copyBitmap, result);
}

Powered by Google App Engine
This is Rietveld 408576698