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" |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkForceLinking.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkOSFile.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkString.h" |
| 15 | #include "Test.h" |
| 16 | |
| 17 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 18 | |
| 19 | /** |
| 20 | * Interprets c as an unpremultiplied color, and returns the |
| 21 | * premultiplied equivalent. |
| 22 | */ |
| 23 | static SkPMColor premultiply_unpmcolor(SkPMColor c) { |
| 24 | U8CPU a = SkGetPackedA32(c); |
| 25 | U8CPU r = SkGetPackedR32(c); |
| 26 | U8CPU g = SkGetPackedG32(c); |
| 27 | U8CPU b = SkGetPackedB32(c); |
| 28 | return SkPreMultiplyARGB(a, r, g, b); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Return true if this stream format should be skipped, due |
| 33 | * to do being an opaque format or not a valid format. |
| 34 | */ |
| 35 | static bool skip_image_format(SkImageDecoder::Format format) { |
| 36 | switch (format) { |
| 37 | case SkImageDecoder::kPNG_Format: |
| 38 | case SkImageDecoder::kWEBP_Format: |
| 39 | return false; |
| 40 | // Skip unknown since it will not be decoded anyway. |
| 41 | case SkImageDecoder::kUnknown_Format: |
| 42 | // Technically ICO and BMP supports alpha channels, but our image |
| 43 | // decoders do not, so skip them as well. |
| 44 | case SkImageDecoder::kICO_Format: |
| 45 | case SkImageDecoder::kBMP_Format: |
| 46 | // The rest of these are opaque. |
| 47 | case SkImageDecoder::kWBMP_Format: |
| 48 | case SkImageDecoder::kGIF_Format: |
| 49 | case SkImageDecoder::kJPEG_Format: |
| 50 | return true; |
| 51 | } |
| 52 | SkASSERT(false); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Test decoding an image in premultiplied mode and unpremultiplied mode and compare |
| 58 | * them. |
| 59 | */ |
| 60 | static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filename) { |
| 61 | // Decode a resource: |
| 62 | SkBitmap bm8888; |
| 63 | SkBitmap bm8888Unpremul; |
| 64 | |
| 65 | SkFILEStream stream(filename.c_str()); |
| 66 | |
| 67 | SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&stream); |
| 68 | if (skip_image_format(format)) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
| 73 | if (NULL == decoder.get()) { |
| 74 | SkDebugf("couldn't decode %s\n", filename.c_str()); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config, |
| 79 | SkImageDecoder::kDecodePixels_Mode); |
| 80 | if (!success) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | success = stream.rewind(); |
| 85 | REPORTER_ASSERT(reporter, success); |
| 86 | if (!success) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | decoder->setRequireUnpremultipliedColors(true); |
| 91 | success = decoder->decode(&stream, &bm8888Unpremul, SkBitmap::kARGB_8888_Config, |
| 92 | SkImageDecoder::kDecodePixels_Mode); |
| 93 | if (!success) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | bool dimensionsMatch = bm8888.width() == bm8888Unpremul.width() |
| 98 | && bm8888.height() == bm8888Unpremul.height(); |
| 99 | REPORTER_ASSERT(reporter, dimensionsMatch); |
| 100 | if (!dimensionsMatch) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Only do the comparison if the two bitmaps are both 8888. |
| 105 | if (bm8888.config() != SkBitmap::kARGB_8888_Config |
| 106 | || bm8888Unpremul.config() != SkBitmap::kARGB_8888_Config) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Now compare the two bitmaps. |
| 111 | for (int i = 0; i < bm8888.width(); ++i) { |
| 112 | for (int j = 0; j < bm8888.height(); ++j) { |
| 113 | // "c0" is the color of the premultiplied bitmap at (i, j). |
| 114 | const SkPMColor c0 = *bm8888.getAddr32(i, j); |
| 115 | // "c1" is the result of premultiplying the color of the unpremultiplied |
| 116 | // bitmap at (i, j). |
| 117 | const SkPMColor c1 = premultiply_unpmcolor(*bm8888Unpremul.getAddr32(i, j)); |
| 118 | // Compute the difference for each component. |
| 119 | int da = SkAbs32(SkGetPackedA32(c0) - SkGetPackedA32(c1)); |
| 120 | int dr = SkAbs32(SkGetPackedR32(c0) - SkGetPackedR32(c1)); |
| 121 | int dg = SkAbs32(SkGetPackedG32(c0) - SkGetPackedG32(c1)); |
| 122 | int db = SkAbs32(SkGetPackedB32(c0) - SkGetPackedB32(c1)); |
| 123 | |
| 124 | // Alpha component must be exactly the same. |
| 125 | REPORTER_ASSERT(reporter, 0 == da); |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 126 | |
| 127 | // Color components may not match exactly due to rounding error. |
| 128 | REPORTER_ASSERT(reporter, dr <= 1); |
| 129 | REPORTER_ASSERT(reporter, dg <= 1); |
| 130 | REPORTER_ASSERT(reporter, db <= 1); |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void test_imageDecodingTests(skiatest::Reporter* reporter) { |
| 136 | // This test cannot run if there is no resource path. |
| 137 | SkString resourcePath = skiatest::Test::GetResourcePath(); |
| 138 | if (resourcePath.isEmpty()) { |
| 139 | return; |
| 140 | } |
| 141 | SkOSFile::Iter iter(resourcePath.c_str()); |
| 142 | SkString basename; |
| 143 | if (iter.next(&basename)) { |
| 144 | do { |
| 145 | SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basename.c_str()); |
| 146 | //SkDebugf("about to decode \"%s\"\n", filename.c_str()); |
| 147 | compare_unpremul(reporter, filename); |
| 148 | } while (iter.next(&basename)); |
| 149 | } else { |
| 150 | SkDebugf("Failed to find any files :(\n"); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #include "TestClassDef.h" |
| 155 | DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, |
| 156 | test_imageDecodingTests) |