OLD | NEW |
---|---|
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" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
12 #include "SkData.h" | 12 #include "SkData.h" |
13 #include "SkDecodingImageGenerator.h" | |
13 #include "SkForceLinking.h" | 14 #include "SkForceLinking.h" |
14 #include "SkGradientShader.h" | 15 #include "SkGradientShader.h" |
15 #include "SkImageDecoder.h" | 16 #include "SkImageDecoder.h" |
16 #include "SkImageEncoder.h" | 17 #include "SkImageEncoder.h" |
17 #include "SkOSFile.h" | 18 #include "SkOSFile.h" |
18 #include "SkPoint.h" | 19 #include "SkPoint.h" |
19 #include "SkShader.h" | 20 #include "SkShader.h" |
20 #include "SkStream.h" | 21 #include "SkStream.h" |
21 #include "SkString.h" | 22 #include "SkString.h" |
22 #include "Test.h" | 23 #include "Test.h" |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 test_unpremul(reporter); | 233 test_unpremul(reporter); |
233 #ifdef SK_DEBUG | 234 #ifdef SK_DEBUG |
234 test_stream_life(); | 235 test_stream_life(); |
235 test_row_proc_choice(); | 236 test_row_proc_choice(); |
236 #endif | 237 #endif |
237 } | 238 } |
238 | 239 |
239 #include "TestClassDef.h" | 240 #include "TestClassDef.h" |
240 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, | 241 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, |
241 test_imageDecodingTests) | 242 test_imageDecodingTests) |
243 | |
244 //////////////////////////////////////////////////////////////////////////////// | |
245 | |
246 static inline bool checkrounding(int value, int dividend, int divisor) { | |
247 // returns true if (dividend/divisor) rounds up OR down to value | |
248 return (((divisor * value) > (dividend - divisor)) | |
249 && ((divisor * value) < (dividend + divisor))); | |
250 } | |
251 /** | |
252 * SkDecodingImageGenerator has an Options struct which lets the | |
253 * client of the generator set sample size, dithering, prefer quality | |
254 * over speed, and bitmap config. This test loops through a list of | |
255 * sets of options and tries them on a set of 5 small encoded images. | |
256 */ | |
257 DEF_TEST(ImageDecoderOptions, reporter) { | |
258 // expected output for 8x8 bitmap | |
259 const int expectedWidth = 8; | |
260 const int expectedHeight = 8; | |
261 const SkColor expectedPixels[] = { | |
262 0xffbba570, 0xff395f5d, 0xffe25c39, 0xff197666, | |
263 0xff3cba27, 0xffdefcb0, 0xffc13874, 0xfffa0093, | |
264 0xffbda60e, 0xffc01db6, 0xff2bd688, 0xff9362d4, | |
265 0xffc641b2, 0xffa5cede, 0xff606eba, 0xff8f4bf3, | |
266 0xff3bf742, 0xff8f02a8, 0xff5509df, 0xffc7027e, | |
267 0xff24aa8a, 0xff886c96, 0xff625481, 0xff403689, | |
268 0xffc52152, 0xff78ccd6, 0xffdcb4ab, 0xff09d27d, | |
269 0xffca00f3, 0xff605d47, 0xff446fb2, 0xff576e46, | |
270 0xff273df9, 0xffb41a83, 0xfff812c3, 0xffccab67, | |
271 0xff034218, 0xff7db9a7, 0xff821048, 0xfffe4ab4, | |
272 0xff6fac98, 0xff941d27, 0xff5fe411, 0xfffbb283, | |
273 0xffd86e99, 0xff169162, 0xff71128c, 0xff39cab4, | |
274 0xffa7fe63, 0xff4c956b, 0xffbc22e0, 0xffb272e4, | |
275 0xff129f4a, 0xffe34513, 0xff3d3742, 0xffbd190a, | |
276 0xffb07222, 0xff2e23f8, 0xfff089d9, 0xffb35738, | |
277 0xffa86022, 0xff3340fe, 0xff95fe71, 0xff6a71df | |
278 }; | |
279 SK_COMPILE_ASSERT((expectedWidth * expectedHeight) | |
280 == SK_ARRAY_COUNT(expectedPixels), array_size_mismatch); | |
281 const char* files[] = { | |
282 "randPixels.bmp", | |
283 "randPixels.jpg", | |
284 "randPixels.png", | |
285 "randPixels.webp", | |
286 "randPixels.gif" | |
287 }; | |
288 | |
289 SkString resourcePath = skiatest::Test::GetResourcePath(); | |
290 SkString directory = SkOSPath::SkPathJoin(resourcePath.c_str(), "encoding"); | |
291 if (!sk_exists(directory.c_str())) { | |
292 return; | |
293 } | |
294 | |
295 typedef SkDecodingImageGenerator::Options Options; | |
296 const Options optionList[] = { | |
297 Options(), | |
298 Options(1, true, false, SkBitmap::kNo_Config), | |
299 Options(2, true, false, SkBitmap::kNo_Config), | |
300 Options(3, true, false, SkBitmap::kNo_Config), | |
301 Options(4, true, false, SkBitmap::kNo_Config), | |
302 Options(1, false, false, SkBitmap::kNo_Config), | |
303 Options(2, false, false, SkBitmap::kNo_Config), | |
304 Options(3, false, false, SkBitmap::kNo_Config), | |
305 Options(4, false, false, SkBitmap::kNo_Config), | |
306 Options(1, true, false, SkBitmap::kARGB_8888_Config), | |
307 Options(2, true, false, SkBitmap::kARGB_8888_Config), | |
308 Options(3, true, false, SkBitmap::kARGB_8888_Config), | |
309 Options(4, true, false, SkBitmap::kARGB_8888_Config), | |
310 Options(1, true, false, SkBitmap::kRGB_565_Config), | |
311 Options(2, true, false, SkBitmap::kRGB_565_Config), | |
312 Options(3, true, false, SkBitmap::kRGB_565_Config), | |
313 Options(4, true, false, SkBitmap::kRGB_565_Config), | |
314 Options(1, false, false, SkBitmap::kRGB_565_Config), | |
315 Options(2, false, false, SkBitmap::kRGB_565_Config), | |
316 Options(3, false, false, SkBitmap::kRGB_565_Config), | |
317 Options(4, false, false, SkBitmap::kRGB_565_Config), | |
318 Options(1, true, false, SkBitmap::kA8_Config), | |
319 Options(2, true, false, SkBitmap::kA8_Config), | |
320 Options(3, true, false, SkBitmap::kA8_Config), | |
321 Options(4, true, false, SkBitmap::kA8_Config), | |
322 Options(1, false, false, SkBitmap::kA8_Config), | |
323 Options(2, false, false, SkBitmap::kA8_Config), | |
324 Options(3, false, false, SkBitmap::kA8_Config), | |
325 Options(4, false, false, SkBitmap::kA8_Config), | |
326 Options(1, true, false, SkBitmap::kARGB_4444_Config), | |
327 Options(2, true, false, SkBitmap::kARGB_4444_Config), | |
328 Options(3, true, false, SkBitmap::kARGB_4444_Config), | |
329 Options(4, true, false, SkBitmap::kARGB_4444_Config), | |
330 Options(1, false, false, SkBitmap::kARGB_4444_Config), | |
331 Options(2, false, false, SkBitmap::kARGB_4444_Config), | |
332 Options(3, false, false, SkBitmap::kARGB_4444_Config), | |
333 Options(4, false, false, SkBitmap::kARGB_4444_Config), | |
334 Options(1, true, true, SkBitmap::kNo_Config), | |
335 Options(2, true, true, SkBitmap::kNo_Config), | |
336 Options(3, true, true, SkBitmap::kNo_Config), | |
337 Options(4, true, true, SkBitmap::kNo_Config), | |
338 Options(1, false, true, SkBitmap::kNo_Config), | |
339 Options(2, false, true, SkBitmap::kNo_Config), | |
340 Options(3, false, true, SkBitmap::kNo_Config), | |
341 Options(4, false, true, SkBitmap::kNo_Config), | |
342 Options(1, true, true, SkBitmap::kARGB_8888_Config), | |
343 Options(2, true, true, SkBitmap::kARGB_8888_Config), | |
344 Options(3, true, true, SkBitmap::kARGB_8888_Config), | |
345 Options(4, true, true, SkBitmap::kARGB_8888_Config), | |
346 Options(1, true, true, SkBitmap::kRGB_565_Config), | |
347 Options(2, true, true, SkBitmap::kRGB_565_Config), | |
348 Options(3, true, true, SkBitmap::kRGB_565_Config), | |
349 Options(4, true, true, SkBitmap::kRGB_565_Config), | |
350 Options(1, false, true, SkBitmap::kRGB_565_Config), | |
351 Options(2, false, true, SkBitmap::kRGB_565_Config), | |
352 Options(3, false, true, SkBitmap::kRGB_565_Config), | |
353 Options(4, false, true, SkBitmap::kRGB_565_Config), | |
354 Options(1, true, true, SkBitmap::kA8_Config), | |
355 Options(2, true, true, SkBitmap::kA8_Config), | |
356 Options(3, true, true, SkBitmap::kA8_Config), | |
357 Options(4, true, true, SkBitmap::kA8_Config), | |
358 Options(1, false, true, SkBitmap::kA8_Config), | |
359 Options(2, false, true, SkBitmap::kA8_Config), | |
360 Options(3, false, true, SkBitmap::kA8_Config), | |
361 Options(4, false, true, SkBitmap::kA8_Config), | |
362 Options(1, true, true, SkBitmap::kARGB_4444_Config), | |
363 Options(2, true, true, SkBitmap::kARGB_4444_Config), | |
364 Options(3, true, true, SkBitmap::kARGB_4444_Config), | |
365 Options(4, true, true, SkBitmap::kARGB_4444_Config), | |
366 Options(1, false, true, SkBitmap::kARGB_4444_Config), | |
367 Options(2, false, true, SkBitmap::kARGB_4444_Config), | |
368 Options(3, false, true, SkBitmap::kARGB_4444_Config), | |
369 Options(4, false, true, SkBitmap::kARGB_4444_Config), | |
370 }; | |
371 | |
372 for (size_t fidx = 0; fidx < SK_ARRAY_COUNT(files); ++fidx) { | |
373 SkString path = SkOSPath::SkPathJoin(directory.c_str(), files[fidx]); | |
374 if (!sk_exists(path.c_str())) { | |
375 continue; | |
376 } | |
377 | |
378 #ifdef TEST_SKDATA_DECODING | |
379 SkAutoDataUnref encoded(SkData::NewFromFileName(path.c_str())); | |
scroggo
2013/12/12 22:33:46
Why only test one or the other?
hal.canary
2013/12/13 15:48:48
I didn't want another for loop.
| |
380 #else | |
381 // Seems wise to test this code path more. | |
382 SkAutoTUnref<SkStreamRewindable> encoded( | |
383 SkStream::NewFromFile(path.c_str())); | |
384 #endif // TEST_SKDATA_DECODING | |
385 REPORTER_ASSERT(reporter, encoded.get() != NULL); | |
386 if (NULL == encoded.get()) { | |
387 continue; | |
388 } | |
389 | |
390 for (int oidx = -1; oidx < (int)SK_ARRAY_COUNT(optionList); ++oidx) { | |
391 // Test with NULL option pointer as well. | |
392 const Options* opts = (oidx == -1) ? NULL : &(optionList[oidx]); | |
393 | |
394 SkBitmap bm; | |
395 | |
396 #ifdef TEST_SKDATA_DECODING | |
397 SkData* input = encoded.get(); | |
398 #else | |
399 SkStreamRewindable* input = encoded->duplicate(); | |
400 REPORTER_ASSERT(reporter, input != NULL); | |
401 if (NULL == input) { | |
402 return; | |
403 } | |
404 #endif // TEST_SKDATA_DECODING | |
405 | |
406 bool success | |
407 = SkDecodingImageGenerator::Install(input, &bm, opts, NULL); | |
408 REPORTER_ASSERT(reporter, success | |
409 || (SkBitmap::kARGB_4444_Config | |
410 == opts->fRequestedConfig)); | |
411 if (!success) { | |
412 continue; | |
413 } | |
414 int sample = (opts != NULL) ? opts->fSampleSize : 1; | |
415 REPORTER_ASSERT(reporter, | |
416 checkrounding(bm.height(), expectedHeight, sample)); | |
417 REPORTER_ASSERT(reporter, | |
418 checkrounding(bm.width(), expectedWidth, sample)); | |
419 | |
420 SkAutoLockPixels alp(bm); | |
421 REPORTER_ASSERT(reporter, bm.getPixels() != NULL); | |
422 | |
423 REPORTER_ASSERT(reporter, (opts == NULL) | |
424 || (SkBitmap::kNo_Config == opts->fRequestedConfig) | |
425 || (bm.config() == opts->fRequestedConfig)); | |
426 | |
427 // Condition under which we should check the decoding results: | |
428 if ((SkBitmap::kARGB_8888_Config == bm.config()) | |
429 && (NULL != bm.getPixels()) | |
430 && (!path.endsWith(".jpg")) // lossy | |
431 && (!path.endsWith(".webp")) // decoder error | |
432 && (sample == 1)) { // scaled | |
433 bool pixelError = false; | |
434 const SkColor* correctPixels = expectedPixels; | |
435 SkASSERT(bm.height() == expectedHeight); | |
436 SkASSERT(bm.width() == expectedWidth); | |
437 for (int y = 0; y < bm.height(); ++y) { | |
438 for (int x = 0; x < bm.width(); ++x) { | |
439 pixelError |= (*correctPixels != bm.getColor(x, y)); | |
440 ++correctPixels; | |
441 } | |
442 } | |
443 REPORTER_ASSERT(reporter, !pixelError); | |
444 } | |
445 } | |
446 } | |
447 } | |
OLD | NEW |