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

Unified Diff: tests/ImageDecodingTest.cpp

Issue 93703004: Change SkDecodingImageGenerator API (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years 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 side-by-side diff with in-line comments
Download patch
Index: tests/ImageDecodingTest.cpp
diff --git a/tests/ImageDecodingTest.cpp b/tests/ImageDecodingTest.cpp
index be8626f0f6beb9dcc52b6065dfb028c58788e1a2..9976a6dbeaf39c6cbd411146c84c80b3c01fb154 100644
--- a/tests/ImageDecodingTest.cpp
+++ b/tests/ImageDecodingTest.cpp
@@ -10,6 +10,7 @@
#include "SkColor.h"
#include "SkColorPriv.h"
#include "SkData.h"
+#include "SkDecodingImageGenerator.h"
#include "SkForceLinking.h"
#include "SkGradientShader.h"
#include "SkImageDecoder.h"
@@ -239,3 +240,208 @@ static void test_imageDecodingTests(skiatest::Reporter* reporter) {
#include "TestClassDef.h"
DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass,
test_imageDecodingTests)
+
+////////////////////////////////////////////////////////////////////////////////
+
+static inline bool checkrounding(int value, int dividend, int divisor) {
+ // returns true if (dividend/divisor) rounds up OR down to value
+ return (((divisor * value) > (dividend - divisor))
+ && ((divisor * value) < (dividend + divisor)));
+}
+/**
+ * SkDecodingImageGenerator has an Options struct which lets the
+ * client of the generator set sample size, dithering, prefer quality
+ * over speed, and bitmap config. This test loops through a list of
+ * sets of options and tries them on a set of 5 small encoded images.
+ */
+DEF_TEST(ImageDecoderOptions, reporter) {
+ // expected output for 8x8 bitmap
+ const int expectedWidth = 8;
+ const int expectedHeight = 8;
+ const SkColor expectedPixels[] = {
+ 0xffbba570, 0xff395f5d, 0xffe25c39, 0xff197666,
+ 0xff3cba27, 0xffdefcb0, 0xffc13874, 0xfffa0093,
+ 0xffbda60e, 0xffc01db6, 0xff2bd688, 0xff9362d4,
+ 0xffc641b2, 0xffa5cede, 0xff606eba, 0xff8f4bf3,
+ 0xff3bf742, 0xff8f02a8, 0xff5509df, 0xffc7027e,
+ 0xff24aa8a, 0xff886c96, 0xff625481, 0xff403689,
+ 0xffc52152, 0xff78ccd6, 0xffdcb4ab, 0xff09d27d,
+ 0xffca00f3, 0xff605d47, 0xff446fb2, 0xff576e46,
+ 0xff273df9, 0xffb41a83, 0xfff812c3, 0xffccab67,
+ 0xff034218, 0xff7db9a7, 0xff821048, 0xfffe4ab4,
+ 0xff6fac98, 0xff941d27, 0xff5fe411, 0xfffbb283,
+ 0xffd86e99, 0xff169162, 0xff71128c, 0xff39cab4,
+ 0xffa7fe63, 0xff4c956b, 0xffbc22e0, 0xffb272e4,
+ 0xff129f4a, 0xffe34513, 0xff3d3742, 0xffbd190a,
+ 0xffb07222, 0xff2e23f8, 0xfff089d9, 0xffb35738,
+ 0xffa86022, 0xff3340fe, 0xff95fe71, 0xff6a71df
+ };
+ SK_COMPILE_ASSERT((expectedWidth * expectedHeight)
+ == SK_ARRAY_COUNT(expectedPixels), array_size_mismatch);
+ const char* files[] = {
+ "randPixels.bmp",
+ "randPixels.jpg",
+ "randPixels.png",
+ "randPixels.webp",
+ "randPixels.gif"
+ };
+
+ SkString resourcePath = skiatest::Test::GetResourcePath();
+ SkString directory = SkOSPath::SkPathJoin(resourcePath.c_str(), "encoding");
+ if (!sk_exists(directory.c_str())) {
+ return;
+ }
+
+ typedef SkDecodingImageGenerator::Options Options;
+ const Options optionList[] = {
+ Options(),
+ Options(1, true, false, SkBitmap::kNo_Config),
+ Options(2, true, false, SkBitmap::kNo_Config),
+ Options(3, true, false, SkBitmap::kNo_Config),
+ Options(4, true, false, SkBitmap::kNo_Config),
+ Options(1, false, false, SkBitmap::kNo_Config),
+ Options(2, false, false, SkBitmap::kNo_Config),
+ Options(3, false, false, SkBitmap::kNo_Config),
+ Options(4, false, false, SkBitmap::kNo_Config),
+ Options(1, true, false, SkBitmap::kARGB_8888_Config),
+ Options(2, true, false, SkBitmap::kARGB_8888_Config),
+ Options(3, true, false, SkBitmap::kARGB_8888_Config),
+ Options(4, true, false, SkBitmap::kARGB_8888_Config),
+ Options(1, true, false, SkBitmap::kRGB_565_Config),
+ Options(2, true, false, SkBitmap::kRGB_565_Config),
+ Options(3, true, false, SkBitmap::kRGB_565_Config),
+ Options(4, true, false, SkBitmap::kRGB_565_Config),
+ Options(1, false, false, SkBitmap::kRGB_565_Config),
+ Options(2, false, false, SkBitmap::kRGB_565_Config),
+ Options(3, false, false, SkBitmap::kRGB_565_Config),
+ Options(4, false, false, SkBitmap::kRGB_565_Config),
+ Options(1, true, false, SkBitmap::kA8_Config),
+ Options(2, true, false, SkBitmap::kA8_Config),
+ Options(3, true, false, SkBitmap::kA8_Config),
+ Options(4, true, false, SkBitmap::kA8_Config),
+ Options(1, false, false, SkBitmap::kA8_Config),
+ Options(2, false, false, SkBitmap::kA8_Config),
+ Options(3, false, false, SkBitmap::kA8_Config),
+ Options(4, false, false, SkBitmap::kA8_Config),
+ Options(1, true, false, SkBitmap::kARGB_4444_Config),
+ Options(2, true, false, SkBitmap::kARGB_4444_Config),
+ Options(3, true, false, SkBitmap::kARGB_4444_Config),
+ Options(4, true, false, SkBitmap::kARGB_4444_Config),
+ Options(1, false, false, SkBitmap::kARGB_4444_Config),
+ Options(2, false, false, SkBitmap::kARGB_4444_Config),
+ Options(3, false, false, SkBitmap::kARGB_4444_Config),
+ Options(4, false, false, SkBitmap::kARGB_4444_Config),
+ Options(1, true, true, SkBitmap::kNo_Config),
+ Options(2, true, true, SkBitmap::kNo_Config),
+ Options(3, true, true, SkBitmap::kNo_Config),
+ Options(4, true, true, SkBitmap::kNo_Config),
+ Options(1, false, true, SkBitmap::kNo_Config),
+ Options(2, false, true, SkBitmap::kNo_Config),
+ Options(3, false, true, SkBitmap::kNo_Config),
+ Options(4, false, true, SkBitmap::kNo_Config),
+ Options(1, true, true, SkBitmap::kARGB_8888_Config),
+ Options(2, true, true, SkBitmap::kARGB_8888_Config),
+ Options(3, true, true, SkBitmap::kARGB_8888_Config),
+ Options(4, true, true, SkBitmap::kARGB_8888_Config),
+ Options(1, true, true, SkBitmap::kRGB_565_Config),
+ Options(2, true, true, SkBitmap::kRGB_565_Config),
+ Options(3, true, true, SkBitmap::kRGB_565_Config),
+ Options(4, true, true, SkBitmap::kRGB_565_Config),
+ Options(1, false, true, SkBitmap::kRGB_565_Config),
+ Options(2, false, true, SkBitmap::kRGB_565_Config),
+ Options(3, false, true, SkBitmap::kRGB_565_Config),
+ Options(4, false, true, SkBitmap::kRGB_565_Config),
+ Options(1, true, true, SkBitmap::kA8_Config),
+ Options(2, true, true, SkBitmap::kA8_Config),
+ Options(3, true, true, SkBitmap::kA8_Config),
+ Options(4, true, true, SkBitmap::kA8_Config),
+ Options(1, false, true, SkBitmap::kA8_Config),
+ Options(2, false, true, SkBitmap::kA8_Config),
+ Options(3, false, true, SkBitmap::kA8_Config),
+ Options(4, false, true, SkBitmap::kA8_Config),
+ Options(1, true, true, SkBitmap::kARGB_4444_Config),
+ Options(2, true, true, SkBitmap::kARGB_4444_Config),
+ Options(3, true, true, SkBitmap::kARGB_4444_Config),
+ Options(4, true, true, SkBitmap::kARGB_4444_Config),
+ Options(1, false, true, SkBitmap::kARGB_4444_Config),
+ Options(2, false, true, SkBitmap::kARGB_4444_Config),
+ Options(3, false, true, SkBitmap::kARGB_4444_Config),
+ Options(4, false, true, SkBitmap::kARGB_4444_Config),
+ };
+
+ for (size_t fidx = 0; fidx < SK_ARRAY_COUNT(files); ++fidx) {
+ SkString path = SkOSPath::SkPathJoin(directory.c_str(), files[fidx]);
+ if (!sk_exists(path.c_str())) {
+ continue;
+ }
+
+ #ifdef TEST_SKDATA_DECODING
+ 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.
+ #else
+ // Seems wise to test this code path more.
+ SkAutoTUnref<SkStreamRewindable> encoded(
+ SkStream::NewFromFile(path.c_str()));
+ #endif // TEST_SKDATA_DECODING
+ REPORTER_ASSERT(reporter, encoded.get() != NULL);
+ if (NULL == encoded.get()) {
+ continue;
+ }
+
+ for (int oidx = -1; oidx < (int)SK_ARRAY_COUNT(optionList); ++oidx) {
+ // Test with NULL option pointer as well.
+ const Options* opts = (oidx == -1) ? NULL : &(optionList[oidx]);
+
+ SkBitmap bm;
+
+ #ifdef TEST_SKDATA_DECODING
+ SkData* input = encoded.get();
+ #else
+ SkStreamRewindable* input = encoded->duplicate();
+ REPORTER_ASSERT(reporter, input != NULL);
+ if (NULL == input) {
+ return;
+ }
+ #endif // TEST_SKDATA_DECODING
+
+ bool success
+ = SkDecodingImageGenerator::Install(input, &bm, opts, NULL);
+ REPORTER_ASSERT(reporter, success
+ || (SkBitmap::kARGB_4444_Config
+ == opts->fRequestedConfig));
+ if (!success) {
+ continue;
+ }
+ int sample = (opts != NULL) ? opts->fSampleSize : 1;
+ REPORTER_ASSERT(reporter,
+ checkrounding(bm.height(), expectedHeight, sample));
+ REPORTER_ASSERT(reporter,
+ checkrounding(bm.width(), expectedWidth, sample));
+
+ SkAutoLockPixels alp(bm);
+ REPORTER_ASSERT(reporter, bm.getPixels() != NULL);
+
+ REPORTER_ASSERT(reporter, (opts == NULL)
+ || (SkBitmap::kNo_Config == opts->fRequestedConfig)
+ || (bm.config() == opts->fRequestedConfig));
+
+ // Condition under which we should check the decoding results:
+ if ((SkBitmap::kARGB_8888_Config == bm.config())
+ && (NULL != bm.getPixels())
+ && (!path.endsWith(".jpg")) // lossy
+ && (!path.endsWith(".webp")) // decoder error
+ && (sample == 1)) { // scaled
+ bool pixelError = false;
+ const SkColor* correctPixels = expectedPixels;
+ SkASSERT(bm.height() == expectedHeight);
+ SkASSERT(bm.width() == expectedWidth);
+ for (int y = 0; y < bm.height(); ++y) {
+ for (int x = 0; x < bm.width(); ++x) {
+ pixelError |= (*correctPixels != bm.getColor(x, y));
+ ++correctPixels;
+ }
+ }
+ REPORTER_ASSERT(reporter, !pixelError);
+ }
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698