blob: 7046e671c9ffa24312dd4e26b66fb8282b70f064 [file] [log] [blame]
Matt Sarett26b44df2017-05-02 16:04:56 -04001/*
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 Sarettc367d032017-05-05 11:13:26 -040012#include "SkEncodedImageFormat.h"
Matt Sarettbe4c9b02017-05-08 12:11:44 -040013#include "SkImage.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040014#include "SkJpegEncoder.h"
Matt Sarettc367d032017-05-05 11:13:26 -040015#include "SkPngEncoder.h"
Matt Sarett26b44df2017-05-02 16:04:56 -040016#include "SkStream.h"
17
Matt Sarettc367d032017-05-05 11:13:26 -040018static 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 Sarett41a8ec02017-05-05 15:07:41 -040025 return false;
Matt Sarettc367d032017-05-05 11:13:26 -040026 }
27}
28
29static 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
41static void test_encode(skiatest::Reporter* r, SkEncodedImageFormat format) {
Matt Sarett26b44df2017-05-02 16:04:56 -040042 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 Sarettc367d032017-05-05 11:13:26 -040056 success = encode(format, &dst0, src);
Matt Sarett26b44df2017-05-02 16:04:56 -040057 REPORTER_ASSERT(r, success);
58
Matt Sarettc367d032017-05-05 11:13:26 -040059 auto encoder1 = make(format, &dst1, src);
Matt Sarett26b44df2017-05-02 16:04:56 -040060 for (int i = 0; i < src.height(); i++) {
61 success = encoder1->encodeRows(1);
62 REPORTER_ASSERT(r, success);
63 }
64
Matt Sarettc367d032017-05-05 11:13:26 -040065 auto encoder2 = make(format, &dst2, src);
Matt Sarett26b44df2017-05-02 16:04:56 -040066 for (int i = 0; i < src.height(); i+=3) {
67 success = encoder2->encodeRows(3);
68 REPORTER_ASSERT(r, success);
69 }
70
Matt Sarettc367d032017-05-05 11:13:26 -040071 auto encoder3 = make(format, &dst3, src);
Matt Sarett26b44df2017-05-02 16:04:56 -040072 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 Sarettc367d032017-05-05 11:13:26 -040083
Matt Sarettbe4c9b02017-05-08 12:11:44 -040084DEF_TEST(Encode, r) {
Matt Sarettc367d032017-05-05 11:13:26 -040085 test_encode(r, SkEncodedImageFormat::kJPEG);
86 test_encode(r, SkEncodedImageFormat::kPNG);
87}
Matt Sarettbe4c9b02017-05-08 12:11:44 -040088
Matt Sarettfe319082017-05-09 14:02:10 -040089static 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
109static inline bool almost_equals(const SkBitmap& a, const SkBitmap& b, int tolerance) {
Matt Sarettbe4c9b02017-05-08 12:11:44 -0400110 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 Sarettfe319082017-05-09 14:02:10 -0400117 if (!almost_equals(*a.getAddr32(x, y), *b.getAddr32(x, y), tolerance)) {
Matt Sarettbe4c9b02017-05-08 12:11:44 -0400118 return false;
119 }
120 }
121 }
122
123 return true;
124}
125
Matt Sarettfe319082017-05-09 14:02:10 -0400126DEF_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 Sarettbe4c9b02017-05-08 12:11:44 -0400167DEF_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 Sarettfe319082017-05-09 14:02:10 -0400204 REPORTER_ASSERT(r, almost_equals(bm0, bm1, 0));
205 REPORTER_ASSERT(r, almost_equals(bm0, bm2, 0));
Matt Sarettbe4c9b02017-05-08 12:11:44 -0400206}