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

Side by Side Diff: tests/ImageDecodingTest.cpp

Issue 223893002: Properly set alpha type in webp decode. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase. Created 6 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 unified diff | Download patch
« no previous file with comments | « src/images/SkImageDecoder_libwebp.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkColor.h" 10 #include "SkColor.h"
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 do { 156 do {
157 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str()); 157 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str());
158 // SkDebugf("about to decode \"%s\"\n", filename.c_str()); 158 // SkDebugf("about to decode \"%s\"\n", filename.c_str());
159 compare_unpremul(reporter, filename); 159 compare_unpremul(reporter, filename);
160 } while (iter.next(&basename)); 160 } while (iter.next(&basename));
161 } else { 161 } else {
162 SkDebugf("Failed to find any files :(\n"); 162 SkDebugf("Failed to find any files :(\n");
163 } 163 }
164 } 164 }
165 165
166 // Test that the alpha type is what we expect.
167 static void test_alphaType(skiatest::Reporter* reporter, const SkString& filenam e,
168 bool requireUnpremul) {
169 SkBitmap bm;
170 SkFILEStream stream(filename.c_str());
171
172 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
173 if (NULL == decoder.get()) {
174 return;
175 }
176
177 decoder->setRequireUnpremultipliedColors(requireUnpremul);
178
179 // Decode just the bounds. This should always succeed.
180 bool success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config,
181 SkImageDecoder::kDecodeBounds_Mode);
182 REPORTER_ASSERT(reporter, success);
183 if (!success) {
184 return;
185 }
186
187 // Keep track of the alpha type for testing later. If the full decode
188 // succeeds, the alpha type should be the same, unless the full decode
189 // determined that the alpha type should actually be opaque, which may
190 // not be known when only decoding the bounds.
191 const SkAlphaType boundsAlphaType = bm.alphaType();
192
193 // rewind should always succeed on SkFILEStream.
194 success = stream.rewind();
195 REPORTER_ASSERT(reporter, success);
196 if (!success) {
197 return;
198 }
199
200 success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config,
201 SkImageDecoder::kDecodePixels_Mode);
202
203 if (!success) {
204 // When the decoder is set to require unpremul, if it does not support
205 // unpremul it will fail. This is the only reason the decode should
206 // fail (since we know the files we are using to test can be decoded).
207 REPORTER_ASSERT(reporter, requireUnpremul);
208 return;
209 }
210
211 // The bounds decode should return with either the requested
212 // premul/unpremul or opaque, if that value could be determined when only
213 // decoding the bounds.
214 if (requireUnpremul) {
215 REPORTER_ASSERT(reporter, kUnpremul_SkAlphaType == boundsAlphaType
216 || kOpaque_SkAlphaType == boundsAlphaType);
217 } else {
218 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == boundsAlphaType
219 || kOpaque_SkAlphaType == boundsAlphaType);
220 }
221
222 // When decoding the full image, the alpha type should match the one
223 // returned by the bounds decode, unless the full decode determined that
224 // the alpha type is actually opaque.
225 REPORTER_ASSERT(reporter, bm.alphaType() == boundsAlphaType
226 || bm.alphaType() == kOpaque_SkAlphaType);
227 }
228
229 DEF_TEST(ImageDecoding_alphaType, reporter) {
230 SkString resourcePath = skiatest::Test::GetResourcePath();
231 if (resourcePath.isEmpty()) {
232 SkDebugf("Could not run alphaType test because resourcePath not specifie d.");
233 return;
234 }
235
236 SkOSFile::Iter iter(resourcePath.c_str());
237 SkString basename;
238 if (iter.next(&basename)) {
239 do {
240 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str());
241 for (int truth = 0; truth <= 1; ++truth) {
242 test_alphaType(reporter, filename, SkToBool(truth));
243 }
244 } while (iter.next(&basename));
245 } else {
246 SkDebugf("Failed to find any files :(\n");
247 }
248
249 }
250
251 // Using known images, test that decoding into unpremul and premul behave as exp ected.
252 DEF_TEST(ImageDecoding_unpremul, reporter) {
253 SkString resourcePath = skiatest::Test::GetResourcePath();
254 if (resourcePath.isEmpty()) {
255 SkDebugf("Could not run unpremul test because resourcePath not specified .");
256 return;
257 }
258 const char* root = "half-transparent-white-pixel";
259 const char* suffixes[] = { ".png", ".webp" };
260
261 for (size_t i = 0; i < SK_ARRAY_COUNT(suffixes); ++i) {
262 SkString basename = SkStringPrintf("%s%s", root, suffixes[i]);
263 SkString fullName = SkOSPath::SkPathJoin(resourcePath.c_str(), basename. c_str());
264
265 SkBitmap bm;
266 SkFILEStream stream(fullName.c_str());
267
268 if (!stream.isValid()) {
269 SkDebugf("file %s missing from resource directoy %s\n",
270 basename.c_str(), resourcePath.c_str());
271 continue;
272 }
273
274 // This should never fail since we know the images we're decoding.
275 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
276 REPORTER_ASSERT(reporter, NULL != decoder.get());
277 if (NULL == decoder.get()) {
278 continue;
279 }
280
281 // Test unpremultiplied. We know what color this should result in.
282 decoder->setRequireUnpremultipliedColors(true);
283 bool success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config ,
284 SkImageDecoder::kDecodePixels_Mode);
285 REPORTER_ASSERT(reporter, success);
286 if (!success) {
287 continue;
288 }
289
290 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1);
291 {
292 SkAutoLockPixels alp(bm);
293 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7fffffff);
294 }
295
296 success = stream.rewind();
297 REPORTER_ASSERT(reporter, success);
298 if (!success) {
299 continue;
300 }
301
302 // Test premultiplied. Once again, we know which color this should
303 // result in.
304 decoder->setRequireUnpremultipliedColors(false);
305 success = decoder->decode(&stream, &bm, SkBitmap::kARGB_8888_Config,
306 SkImageDecoder::kDecodePixels_Mode);
307 REPORTER_ASSERT(reporter, success);
308 if (!success) {
309 continue;
310 }
311
312 REPORTER_ASSERT(reporter, bm.width() == 1 && bm.height() == 1);
313 {
314 SkAutoLockPixels alp(bm);
315 REPORTER_ASSERT(reporter, bm.getAddr32(0, 0)[0] == 0x7f7f7f7f);
316 }
317 }
318 }
319
166 #ifdef SK_DEBUG 320 #ifdef SK_DEBUG
167 // Create a stream containing a bitmap encoded to Type type. 321 // Create a stream containing a bitmap encoded to Type type.
168 static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) { 322 static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) {
169 SkBitmap bm; 323 SkBitmap bm;
170 const int size = 50; 324 const int size = 50;
171 bm.allocN32Pixels(size, size); 325 bm.allocN32Pixels(size, size);
172 SkCanvas canvas(bm); 326 SkCanvas canvas(bm);
173 SkPoint points[2] = { 327 SkPoint points[2] = {
174 { SkIntToScalar(0), SkIntToScalar(0) }, 328 { SkIntToScalar(0), SkIntToScalar(0) },
175 { SkIntToScalar(size), SkIntToScalar(size) } 329 { SkIntToScalar(size), SkIntToScalar(size) }
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 } 722 }
569 SkDecodingImageGenerator::Options options(scaleList[i], 723 SkDecodingImageGenerator::Options options(scaleList[i],
570 ditherList[j]); 724 ditherList[j]);
571 test_options(reporter, options, encodedStream, encodedData, 725 test_options(reporter, options, encodedStream, encodedData,
572 useDataList[m], path); 726 useDataList[m], path);
573 } 727 }
574 } 728 }
575 } 729 }
576 } 730 }
577 } 731 }
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder_libwebp.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698