scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkBitmap.h" |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkColor.h" |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 13 | #include "SkForceLinking.h" |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 15 | #include "SkImageDecoder.h" |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 16 | #include "SkImageEncoder.h" |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 17 | #include "SkOSFile.h" |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 18 | #include "SkPoint.h" |
| 19 | #include "SkShader.h" |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 20 | #include "SkStream.h" |
| 21 | #include "SkString.h" |
| 22 | #include "Test.h" |
| 23 | |
| 24 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 25 | |
| 26 | /** |
| 27 | * Interprets c as an unpremultiplied color, and returns the |
| 28 | * premultiplied equivalent. |
| 29 | */ |
| 30 | static SkPMColor premultiply_unpmcolor(SkPMColor c) { |
| 31 | U8CPU a = SkGetPackedA32(c); |
| 32 | U8CPU r = SkGetPackedR32(c); |
| 33 | U8CPU g = SkGetPackedG32(c); |
| 34 | U8CPU b = SkGetPackedB32(c); |
| 35 | return SkPreMultiplyARGB(a, r, g, b); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Return true if this stream format should be skipped, due |
| 40 | * to do being an opaque format or not a valid format. |
| 41 | */ |
| 42 | static bool skip_image_format(SkImageDecoder::Format format) { |
| 43 | switch (format) { |
| 44 | case SkImageDecoder::kPNG_Format: |
| 45 | case SkImageDecoder::kWEBP_Format: |
| 46 | return false; |
| 47 | // Skip unknown since it will not be decoded anyway. |
| 48 | case SkImageDecoder::kUnknown_Format: |
| 49 | // Technically ICO and BMP supports alpha channels, but our image |
| 50 | // decoders do not, so skip them as well. |
| 51 | case SkImageDecoder::kICO_Format: |
| 52 | case SkImageDecoder::kBMP_Format: |
| 53 | // The rest of these are opaque. |
| 54 | case SkImageDecoder::kWBMP_Format: |
| 55 | case SkImageDecoder::kGIF_Format: |
| 56 | case SkImageDecoder::kJPEG_Format: |
| 57 | return true; |
| 58 | } |
| 59 | SkASSERT(false); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Test decoding an image in premultiplied mode and unpremultiplied mode and compare |
| 65 | * them. |
| 66 | */ |
| 67 | static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filename) { |
| 68 | // Decode a resource: |
| 69 | SkBitmap bm8888; |
| 70 | SkBitmap bm8888Unpremul; |
| 71 | |
| 72 | SkFILEStream stream(filename.c_str()); |
| 73 | |
| 74 | SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&stream); |
| 75 | if (skip_image_format(format)) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 80 | if (NULL == decoder.get()) { |
| 81 | SkDebugf("couldn't decode %s\n", filename.c_str()); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config, |
| 86 | SkImageDecoder::kDecodePixels_Mode); |
| 87 | if (!success) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | success = stream.rewind(); |
| 92 | REPORTER_ASSERT(reporter, success); |
| 93 | if (!success) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | decoder->setRequireUnpremultipliedColors(true); |
| 98 | success = decoder->decode(&stream, &bm8888Unpremul, SkBitmap::kARGB_8888_Config, |
| 99 | SkImageDecoder::kDecodePixels_Mode); |
| 100 | if (!success) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | bool dimensionsMatch = bm8888.width() == bm8888Unpremul.width() |
| 105 | && bm8888.height() == bm8888Unpremul.height(); |
| 106 | REPORTER_ASSERT(reporter, dimensionsMatch); |
| 107 | if (!dimensionsMatch) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | // Only do the comparison if the two bitmaps are both 8888. |
| 112 | if (bm8888.config() != SkBitmap::kARGB_8888_Config |
| 113 | || bm8888Unpremul.config() != SkBitmap::kARGB_8888_Config) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // Now compare the two bitmaps. |
| 118 | for (int i = 0; i < bm8888.width(); ++i) { |
| 119 | for (int j = 0; j < bm8888.height(); ++j) { |
| 120 | // "c0" is the color of the premultiplied bitmap at (i, j). |
| 121 | const SkPMColor c0 = *bm8888.getAddr32(i, j); |
| 122 | // "c1" is the result of premultiplying the color of the unpremultiplied |
| 123 | // bitmap at (i, j). |
| 124 | const SkPMColor c1 = premultiply_unpmcolor(*bm8888Unpremul.getAddr32(i, j)); |
| 125 | // Compute the difference for each component. |
| 126 | int da = SkAbs32(SkGetPackedA32(c0) - SkGetPackedA32(c1)); |
| 127 | int dr = SkAbs32(SkGetPackedR32(c0) - SkGetPackedR32(c1)); |
| 128 | int dg = SkAbs32(SkGetPackedG32(c0) - SkGetPackedG32(c1)); |
| 129 | int db = SkAbs32(SkGetPackedB32(c0) - SkGetPackedB32(c1)); |
| 130 | |
| 131 | // Alpha component must be exactly the same. |
| 132 | REPORTER_ASSERT(reporter, 0 == da); |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 133 | |
| 134 | // Color components may not match exactly due to rounding error. |
| 135 | REPORTER_ASSERT(reporter, dr <= 1); |
| 136 | REPORTER_ASSERT(reporter, dg <= 1); |
| 137 | REPORTER_ASSERT(reporter, db <= 1); |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 142 | // Create a fake ImageDecoder to test setPrefConfigTable for |
| 143 | // backwards compatibility. |
| 144 | class PrefConfigTestingImageDecoder : public SkImageDecoder { |
| 145 | public: |
| 146 | void testPrefConfigTable(skiatest::Reporter* reporter) { |
| 147 | // Arbitrary list of Configs. The important thing about |
| 148 | // the list is that each one is different, so we can test |
| 149 | // to make sure the correct config is chosen. |
| 150 | const SkBitmap::Config configs[] = { |
commit-bot@chromium.org | 11f392e | 2013-11-26 12:51:57 +0000 | [diff] [blame] | 151 | SkBitmap::kA8_Config, |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 152 | SkBitmap::kA8_Config, |
| 153 | SkBitmap::kIndex8_Config, |
| 154 | SkBitmap::kRGB_565_Config, |
| 155 | SkBitmap::kARGB_4444_Config, |
| 156 | SkBitmap::kARGB_8888_Config, |
| 157 | }; |
| 158 | this->setPrefConfigTable(configs); |
| 159 | REPORTER_ASSERT(reporter, configs[0] == this->getPrefConfig(kIndex_SrcDepth, false)); |
| 160 | REPORTER_ASSERT(reporter, configs[1] == this->getPrefConfig(kIndex_SrcDepth, true)); |
| 161 | REPORTER_ASSERT(reporter, configs[4] == this->getPrefConfig(k32Bit_SrcDepth, false)); |
| 162 | REPORTER_ASSERT(reporter, configs[5] == this->getPrefConfig(k32Bit_SrcDepth, true)); |
| 163 | } |
| 164 | |
| 165 | protected: |
| 166 | virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) SK_OVERRIDE { |
| 167 | return false; |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | static void test_pref_config_table(skiatest::Reporter* reporter) { |
| 172 | PrefConfigTestingImageDecoder decoder; |
| 173 | decoder.testPrefConfigTable(reporter); |
| 174 | } |
| 175 | |
| 176 | static void test_unpremul(skiatest::Reporter* reporter) { |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 177 | // This test cannot run if there is no resource path. |
| 178 | SkString resourcePath = skiatest::Test::GetResourcePath(); |
| 179 | if (resourcePath.isEmpty()) { |
bungeman@google.com | 3c996f8 | 2013-10-24 21:39:35 +0000 | [diff] [blame] | 180 | SkDebugf("Could not run unpremul test because resourcePath not specified."); |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | SkOSFile::Iter iter(resourcePath.c_str()); |
| 184 | SkString basename; |
| 185 | if (iter.next(&basename)) { |
| 186 | do { |
| 187 | SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basename.c_str()); |
| 188 | //SkDebugf("about to decode \"%s\"\n", filename.c_str()); |
| 189 | compare_unpremul(reporter, filename); |
| 190 | } while (iter.next(&basename)); |
| 191 | } else { |
| 192 | SkDebugf("Failed to find any files :(\n"); |
| 193 | } |
| 194 | } |
| 195 | |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 196 | #ifdef SK_DEBUG |
| 197 | // Create a stream containing a bitmap encoded to Type type. |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 198 | static SkMemoryStream* create_image_stream(SkImageEncoder::Type type) { |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 199 | SkBitmap bm; |
| 200 | const int size = 50; |
| 201 | bm.setConfig(SkBitmap::kARGB_8888_Config, size, size); |
| 202 | bm.allocPixels(); |
| 203 | SkCanvas canvas(bm); |
| 204 | SkPoint points[2] = { |
| 205 | { SkIntToScalar(0), SkIntToScalar(0) }, |
| 206 | { SkIntToScalar(size), SkIntToScalar(size) } |
| 207 | }; |
| 208 | SkColor colors[2] = { SK_ColorWHITE, SK_ColorBLUE }; |
| 209 | SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL, |
| 210 | SK_ARRAY_COUNT(colors), |
| 211 | SkShader::kClamp_TileMode); |
| 212 | SkPaint paint; |
| 213 | paint.setShader(shader)->unref(); |
| 214 | canvas.drawPaint(paint); |
| 215 | // Now encode it to a stream. |
| 216 | SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(bm, type, 100)); |
| 217 | if (NULL == data.get()) { |
| 218 | return NULL; |
| 219 | } |
| 220 | return SkNEW_ARGS(SkMemoryStream, (data.get())); |
| 221 | } |
| 222 | |
| 223 | // For every format that supports tile based decoding, ensure that |
| 224 | // calling decodeSubset will not fail if the caller has unreffed the |
| 225 | // stream provided in buildTileIndex. |
| 226 | // Only runs in debug mode since we are testing for a crash. |
| 227 | static void test_stream_life() { |
| 228 | const SkImageEncoder::Type gTypes[] = { |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 229 | #ifdef SK_BUILD_FOR_ANDROID |
scroggo@google.com | d79277f | 2013-08-07 19:53:53 +0000 | [diff] [blame] | 230 | SkImageEncoder::kJPEG_Type, |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 231 | SkImageEncoder::kPNG_Type, |
| 232 | #endif |
| 233 | SkImageEncoder::kWEBP_Type, |
| 234 | }; |
| 235 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTypes); ++i) { |
scroggo@google.com | 9dfac23 | 2013-09-16 17:36:39 +0000 | [diff] [blame] | 236 | //SkDebugf("encoding to %i\n", i); |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 237 | SkAutoTUnref<SkMemoryStream> stream(create_image_stream(gTypes[i])); |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 238 | if (NULL == stream.get()) { |
| 239 | SkDebugf("no stream\n"); |
| 240 | continue; |
| 241 | } |
| 242 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
| 243 | if (NULL == decoder.get()) { |
| 244 | SkDebugf("no decoder\n"); |
| 245 | continue; |
| 246 | } |
| 247 | int width, height; |
| 248 | if (!decoder->buildTileIndex(stream.get(), &width, &height)) { |
| 249 | SkDebugf("could not build a tile index\n"); |
| 250 | continue; |
| 251 | } |
| 252 | // Now unref the stream to make sure it survives |
| 253 | stream.reset(NULL); |
| 254 | SkBitmap bm; |
| 255 | decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height), |
| 256 | SkBitmap::kARGB_8888_Config); |
| 257 | } |
| 258 | } |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 259 | |
| 260 | // Test inside SkScaledBitmapSampler.cpp |
| 261 | extern void test_row_proc_choice(); |
| 262 | |
| 263 | #endif // SK_DEBUG |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 264 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 265 | static void test_imageDecodingTests(skiatest::Reporter* reporter) { |
| 266 | test_unpremul(reporter); |
| 267 | test_pref_config_table(reporter); |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 268 | #ifdef SK_DEBUG |
| 269 | test_stream_life(); |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 270 | test_row_proc_choice(); |
scroggo@google.com | 826d63a | 2013-07-18 20:06:28 +0000 | [diff] [blame] | 271 | #endif |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 272 | } |
| 273 | |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 274 | #include "TestClassDef.h" |
| 275 | DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, |
| 276 | test_imageDecodingTests) |