aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar Matt Sarett <msarett@google.com>2017-05-09 14:02:10 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-05-09 18:37:10 +0000
commitfe3190846c9af2cf19a76d7ab9799d7fa69c2369 (patch)
treecab2adff1d287769e501f25f726585134a5228c2 /tests
parent443531126ac7d281c49fca48674ec7e14436c2b6 (diff)
Add downsampling option to SkJpegEncoder
Bug: 713862 Change-Id: Ibad6ecf836ccfd355499e1bace7bcd4ba772a97a Reviewed-on: https://skia-review.googlesource.com/15891 Commit-Queue: Matt Sarett <msarett@google.com> Reviewed-by: Leon Scroggins <scroggo@google.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/EncodeTest.cpp69
1 files changed, 65 insertions, 4 deletions
diff --git a/tests/EncodeTest.cpp b/tests/EncodeTest.cpp
index 45a0ff83aa..7046e671c9 100644
--- a/tests/EncodeTest.cpp
+++ b/tests/EncodeTest.cpp
@@ -86,7 +86,27 @@ DEF_TEST(Encode, r) {
test_encode(r, SkEncodedImageFormat::kPNG);
}
-static inline bool equals(const SkBitmap& a, const SkBitmap& b) {
+static inline bool almost_equals(SkPMColor a, SkPMColor b, int tolerance) {
+ if (SkTAbs((int)SkGetPackedR32(a) - (int)SkGetPackedR32(b)) > tolerance) {
+ return false;
+ }
+
+ if (SkTAbs((int)SkGetPackedG32(a) - (int)SkGetPackedG32(b)) > tolerance) {
+ return false;
+ }
+
+ if (SkTAbs((int)SkGetPackedB32(a) - (int)SkGetPackedB32(b)) > tolerance) {
+ return false;
+ }
+
+ if (SkTAbs((int)SkGetPackedA32(a) - (int)SkGetPackedA32(b)) > tolerance) {
+ return false;
+ }
+
+ return true;
+}
+
+static inline bool almost_equals(const SkBitmap& a, const SkBitmap& b, int tolerance) {
if (a.info() != b.info()) {
return false;
}
@@ -94,7 +114,7 @@ static inline bool equals(const SkBitmap& a, const SkBitmap& b) {
SkASSERT(kN32_SkColorType == a.colorType());
for (int y = 0; y < a.height(); y++) {
for (int x = 0; x < a.width(); x++) {
- if (*a.getAddr32(x, y) != *b.getAddr32(x, y)) {
+ if (!almost_equals(*a.getAddr32(x, y), *b.getAddr32(x, y), tolerance)) {
return false;
}
}
@@ -103,6 +123,47 @@ static inline bool equals(const SkBitmap& a, const SkBitmap& b) {
return true;
}
+DEF_TEST(Encode_JpegDownsample, r) {
+ SkBitmap bitmap;
+ bool success = GetResourceAsBitmap("mandrill_128.png", &bitmap);
+ if (!success) {
+ return;
+ }
+
+ SkPixmap src;
+ success = bitmap.peekPixels(&src);
+ REPORTER_ASSERT(r, success);
+ if (!success) {
+ return;
+ }
+
+ SkDynamicMemoryWStream dst0, dst1, dst2;
+ SkJpegEncoder::Options options;
+ success = SkJpegEncoder::Encode(&dst0, src, options);
+ REPORTER_ASSERT(r, success);
+
+ options.fDownsample = SkJpegEncoder::Downsample::k422;
+ success = SkJpegEncoder::Encode(&dst1, src, options);
+ REPORTER_ASSERT(r, success);
+
+ options.fDownsample = SkJpegEncoder::Downsample::k444;
+ success = SkJpegEncoder::Encode(&dst2, src, options);
+ REPORTER_ASSERT(r, success);
+
+ sk_sp<SkData> data0 = dst0.detachAsData();
+ sk_sp<SkData> data1 = dst1.detachAsData();
+ sk_sp<SkData> data2 = dst2.detachAsData();
+ REPORTER_ASSERT(r, data0->size() < data1->size());
+ REPORTER_ASSERT(r, data1->size() < data2->size());
+
+ SkBitmap bm0, bm1, bm2;
+ SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0, SkImage::kRO_LegacyBitmapMode);
+ SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1, SkImage::kRO_LegacyBitmapMode);
+ SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2, SkImage::kRO_LegacyBitmapMode);
+ REPORTER_ASSERT(r, almost_equals(bm0, bm1, 60));
+ REPORTER_ASSERT(r, almost_equals(bm1, bm2, 60));
+}
+
DEF_TEST(Encode_PngOptions, r) {
SkBitmap bitmap;
bool success = GetResourceAsBitmap("mandrill_128.png", &bitmap);
@@ -140,6 +201,6 @@ DEF_TEST(Encode_PngOptions, r) {
SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0, SkImage::kRO_LegacyBitmapMode);
SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1, SkImage::kRO_LegacyBitmapMode);
SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2, SkImage::kRO_LegacyBitmapMode);
- REPORTER_ASSERT(r, equals(bm0, bm1));
- REPORTER_ASSERT(r, equals(bm0, bm2));
+ REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0));
+ REPORTER_ASSERT(r, almost_equals(bm0, bm2, 0));
}