/*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, uint64_t *result) { SkMD5 out; // start with the x/y dimensions write_int32_to_buffer(SkToU32(bitmap.width()), &out); write_int32_to_buffer(SkToU32(bitmap.height()), &out); // add all the pixel data SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { return false; } SkMD5::Digest digest; out.finish(digest); *result = first_8_bytes_as_uint64(digest.data); return true; }
DEF_TEST(ARGBImageEncoder, reporter) { // Bytes we expect to get: const int kWidth = 3; const int kHeight = 5; const unsigned char comparisonBuffer[] = { // kHeight rows, each with kWidth pixels, premultiplied ARGB for each pixel 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, 0xff,0xff,0x00,0x00, // red 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, 0xff,0x00,0xff,0x00, // green 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, 0xff,0x00,0x00,0xff, // blue }; SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); for (size_t ctIndex = 0; ctIndex < SK_ARRAY_COUNT(gColorTypes); ++ctIndex) { // A bitmap that should generate the above bytes: SkBitmap bitmap; { bitmap.allocPixels(SkImageInfo::Make(kWidth, kHeight, gColorTypes[ctIndex], kOpaque_SkAlphaType)); bitmap.eraseColor(SK_ColorBLUE); // Change rows [0,1] from blue to [red,green]. SkCanvas canvas(bitmap); SkPaint paint; paint.setColor(SK_ColorRED); canvas.drawIRect(SkIRect::MakeLTRB(0, 0, kWidth, 1), paint); paint.setColor(SK_ColorGREEN); canvas.drawIRect(SkIRect::MakeLTRB(0, 1, kWidth, 2), paint); } // Transform the bitmap. int bufferSize = bitmap.width() * bitmap.height() * 4; SkAutoMalloc pixelBufferManager(bufferSize); char *pixelBuffer = static_cast<char *>(pixelBufferManager.get()); SkMemoryWStream out(pixelBuffer, bufferSize); REPORTER_ASSERT(reporter, enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)); // Confirm we got the expected results. REPORTER_ASSERT(reporter, bufferSize == sizeof(comparisonBuffer)); REPORTER_ASSERT(reporter, memcmp(pixelBuffer, comparisonBuffer, bufferSize) == 0); } }
/*static*/ bool SkBitmapHasher::ComputeDigestInternal(const SkBitmap& bitmap, SkHashDigest *result) { size_t pixelBufferSize = bitmap.width() * bitmap.height() * 4; size_t totalBufferSize = pixelBufferSize + 2 * sizeof(uint32_t); SkAutoMalloc bufferManager(totalBufferSize); char *bufferStart = static_cast<char *>(bufferManager.get()); SkMemoryWStream out(bufferStart, totalBufferSize); // start with the x/y dimensions write_int_to_buffer(SkToU32(bitmap.width()), &out); write_int_to_buffer(SkToU32(bitmap.height()), &out); // add all the pixel data SkAutoTDelete<SkImageEncoder> enc(CreateARGBImageEncoder()); if (!enc->encodeStream(&out, bitmap, SkImageEncoder::kDefaultQuality)) { return false; } *result = SkCityHash::Compute64(bufferStart, totalBufferSize); return true; }