Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "Resources.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | #include "SkBitmap.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 12 | #include "SkEncodedImageFormat.h" |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 13 | #include "SkImage.h" |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 14 | #include "SkJpegEncoder.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 15 | #include "SkPngEncoder.h" |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 18 | static bool encode(SkEncodedImageFormat format, SkWStream* dst, const SkPixmap& src) { |
| 19 | switch (format) { |
| 20 | case SkEncodedImageFormat::kJPEG: |
| 21 | return SkJpegEncoder::Encode(dst, src, SkJpegEncoder::Options()); |
| 22 | case SkEncodedImageFormat::kPNG: |
| 23 | return SkPngEncoder::Encode(dst, src, SkPngEncoder::Options()); |
| 24 | default: |
Matt Sarett | 41a8ec0 | 2017-05-05 15:07:41 -0400 | [diff] [blame] | 25 | return false; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | static std::unique_ptr<SkEncoder> make(SkEncodedImageFormat format, SkWStream* dst, |
| 30 | const SkPixmap& src) { |
| 31 | switch (format) { |
| 32 | case SkEncodedImageFormat::kJPEG: |
| 33 | return SkJpegEncoder::Make(dst, src, SkJpegEncoder::Options()); |
| 34 | case SkEncodedImageFormat::kPNG: |
| 35 | return SkPngEncoder::Make(dst, src, SkPngEncoder::Options()); |
| 36 | default: |
| 37 | return nullptr; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | static void test_encode(skiatest::Reporter* r, SkEncodedImageFormat format) { |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 42 | SkBitmap bitmap; |
| 43 | bool success = GetResourceAsBitmap("mandrill_128.png", &bitmap); |
| 44 | if (!success) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | SkPixmap src; |
| 49 | success = bitmap.peekPixels(&src); |
| 50 | REPORTER_ASSERT(r, success); |
| 51 | if (!success) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | SkDynamicMemoryWStream dst0, dst1, dst2, dst3; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 56 | success = encode(format, &dst0, src); |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 57 | REPORTER_ASSERT(r, success); |
| 58 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 59 | auto encoder1 = make(format, &dst1, src); |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 60 | for (int i = 0; i < src.height(); i++) { |
| 61 | success = encoder1->encodeRows(1); |
| 62 | REPORTER_ASSERT(r, success); |
| 63 | } |
| 64 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 65 | auto encoder2 = make(format, &dst2, src); |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 66 | for (int i = 0; i < src.height(); i+=3) { |
| 67 | success = encoder2->encodeRows(3); |
| 68 | REPORTER_ASSERT(r, success); |
| 69 | } |
| 70 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 71 | auto encoder3 = make(format, &dst3, src); |
Matt Sarett | 26b44df | 2017-05-02 16:04:56 -0400 | [diff] [blame] | 72 | success = encoder3->encodeRows(200); |
| 73 | REPORTER_ASSERT(r, success); |
| 74 | |
| 75 | sk_sp<SkData> data0 = dst0.detachAsData(); |
| 76 | sk_sp<SkData> data1 = dst1.detachAsData(); |
| 77 | sk_sp<SkData> data2 = dst2.detachAsData(); |
| 78 | sk_sp<SkData> data3 = dst3.detachAsData(); |
| 79 | REPORTER_ASSERT(r, data0->equals(data1.get())); |
| 80 | REPORTER_ASSERT(r, data0->equals(data2.get())); |
| 81 | REPORTER_ASSERT(r, data0->equals(data3.get())); |
| 82 | } |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 83 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 84 | DEF_TEST(Encode, r) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 85 | test_encode(r, SkEncodedImageFormat::kJPEG); |
| 86 | test_encode(r, SkEncodedImageFormat::kPNG); |
| 87 | } |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 88 | |
Matt Sarett | fe31908 | 2017-05-09 14:02:10 -0400 | [diff] [blame] | 89 | static inline bool almost_equals(SkPMColor a, SkPMColor b, int tolerance) { |
| 90 | if (SkTAbs((int)SkGetPackedR32(a) - (int)SkGetPackedR32(b)) > tolerance) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | if (SkTAbs((int)SkGetPackedG32(a) - (int)SkGetPackedG32(b)) > tolerance) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | if (SkTAbs((int)SkGetPackedB32(a) - (int)SkGetPackedB32(b)) > tolerance) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | if (SkTAbs((int)SkGetPackedA32(a) - (int)SkGetPackedA32(b)) > tolerance) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | static inline bool almost_equals(const SkBitmap& a, const SkBitmap& b, int tolerance) { |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 110 | if (a.info() != b.info()) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | SkASSERT(kN32_SkColorType == a.colorType()); |
| 115 | for (int y = 0; y < a.height(); y++) { |
| 116 | for (int x = 0; x < a.width(); x++) { |
Matt Sarett | fe31908 | 2017-05-09 14:02:10 -0400 | [diff] [blame] | 117 | if (!almost_equals(*a.getAddr32(x, y), *b.getAddr32(x, y), tolerance)) { |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
Matt Sarett | fe31908 | 2017-05-09 14:02:10 -0400 | [diff] [blame] | 126 | DEF_TEST(Encode_JpegDownsample, r) { |
| 127 | SkBitmap bitmap; |
| 128 | bool success = GetResourceAsBitmap("mandrill_128.png", &bitmap); |
| 129 | if (!success) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | SkPixmap src; |
| 134 | success = bitmap.peekPixels(&src); |
| 135 | REPORTER_ASSERT(r, success); |
| 136 | if (!success) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | SkDynamicMemoryWStream dst0, dst1, dst2; |
| 141 | SkJpegEncoder::Options options; |
| 142 | success = SkJpegEncoder::Encode(&dst0, src, options); |
| 143 | REPORTER_ASSERT(r, success); |
| 144 | |
| 145 | options.fDownsample = SkJpegEncoder::Downsample::k422; |
| 146 | success = SkJpegEncoder::Encode(&dst1, src, options); |
| 147 | REPORTER_ASSERT(r, success); |
| 148 | |
| 149 | options.fDownsample = SkJpegEncoder::Downsample::k444; |
| 150 | success = SkJpegEncoder::Encode(&dst2, src, options); |
| 151 | REPORTER_ASSERT(r, success); |
| 152 | |
| 153 | sk_sp<SkData> data0 = dst0.detachAsData(); |
| 154 | sk_sp<SkData> data1 = dst1.detachAsData(); |
| 155 | sk_sp<SkData> data2 = dst2.detachAsData(); |
| 156 | REPORTER_ASSERT(r, data0->size() < data1->size()); |
| 157 | REPORTER_ASSERT(r, data1->size() < data2->size()); |
| 158 | |
| 159 | SkBitmap bm0, bm1, bm2; |
| 160 | SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0, SkImage::kRO_LegacyBitmapMode); |
| 161 | SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1, SkImage::kRO_LegacyBitmapMode); |
| 162 | SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2, SkImage::kRO_LegacyBitmapMode); |
| 163 | REPORTER_ASSERT(r, almost_equals(bm0, bm1, 60)); |
| 164 | REPORTER_ASSERT(r, almost_equals(bm1, bm2, 60)); |
| 165 | } |
| 166 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 167 | DEF_TEST(Encode_PngOptions, r) { |
| 168 | SkBitmap bitmap; |
| 169 | bool success = GetResourceAsBitmap("mandrill_128.png", &bitmap); |
| 170 | if (!success) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | SkPixmap src; |
| 175 | success = bitmap.peekPixels(&src); |
| 176 | REPORTER_ASSERT(r, success); |
| 177 | if (!success) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | SkDynamicMemoryWStream dst0, dst1, dst2; |
| 182 | SkPngEncoder::Options options; |
| 183 | success = SkPngEncoder::Encode(&dst0, src, options); |
| 184 | REPORTER_ASSERT(r, success); |
| 185 | |
| 186 | options.fFilterFlags = SkPngEncoder::FilterFlag::kUp; |
| 187 | success = SkPngEncoder::Encode(&dst1, src, options); |
| 188 | REPORTER_ASSERT(r, success); |
| 189 | |
| 190 | options.fZLibLevel = 3; |
| 191 | success = SkPngEncoder::Encode(&dst2, src, options); |
| 192 | REPORTER_ASSERT(r, success); |
| 193 | |
| 194 | sk_sp<SkData> data0 = dst0.detachAsData(); |
| 195 | sk_sp<SkData> data1 = dst1.detachAsData(); |
| 196 | sk_sp<SkData> data2 = dst2.detachAsData(); |
| 197 | REPORTER_ASSERT(r, data0->size() < data1->size()); |
| 198 | REPORTER_ASSERT(r, data1->size() < data2->size()); |
| 199 | |
| 200 | SkBitmap bm0, bm1, bm2; |
| 201 | SkImage::MakeFromEncoded(data0)->asLegacyBitmap(&bm0, SkImage::kRO_LegacyBitmapMode); |
| 202 | SkImage::MakeFromEncoded(data1)->asLegacyBitmap(&bm1, SkImage::kRO_LegacyBitmapMode); |
| 203 | SkImage::MakeFromEncoded(data2)->asLegacyBitmap(&bm2, SkImage::kRO_LegacyBitmapMode); |
Matt Sarett | fe31908 | 2017-05-09 14:02:10 -0400 | [diff] [blame] | 204 | REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0)); |
| 205 | REPORTER_ASSERT(r, almost_equals(bm0, bm2, 0)); |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 206 | } |