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 "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkForceLinking.h" | 10 #include "SkForceLinking.h" |
11 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
12 #include "SkOSFile.h" | 12 #include "SkOSFile.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
14 #include "SkString.h" | 14 #include "SkString.h" |
15 #include "Test.h" | 15 #include "Test.h" |
16 | 16 |
17 __SK_FORCE_IMAGE_DECODER_LINKING; | 17 __SK_FORCE_IMAGE_DECODER_LINKING; |
18 | 18 |
19 /** | 19 /** |
20 * Interprets c as an unpremultiplied color, and returns the | 20 * Interprets c as an unpremultiplied color, and returns the |
21 * premultiplied equivalent. | 21 * premultiplied equivalent. |
22 */ | 22 */ |
23 SkPMColor SkPreMultiplyUnPMColor(SkPMColor c) { | 23 static SkPMColor premultiply_unpmcolor(SkPMColor c) { |
24 U8CPU a = SkGetPackedA32(c); | 24 U8CPU a = SkGetPackedA32(c); |
25 U8CPU r = SkGetPackedR32(c); | 25 U8CPU r = SkGetPackedR32(c); |
26 U8CPU g = SkGetPackedG32(c); | 26 U8CPU g = SkGetPackedG32(c); |
27 U8CPU b = SkGetPackedB32(c); | 27 U8CPU b = SkGetPackedB32(c); |
28 return SkPreMultiplyARGB(a, r, g, b); | 28 return SkPreMultiplyARGB(a, r, g, b); |
29 } | 29 } |
30 | 30 |
31 /** | 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 /** |
32 * Test decoding an image in premultiplied mode and unpremultiplied mode and co
mpare | 57 * Test decoding an image in premultiplied mode and unpremultiplied mode and co
mpare |
33 * them. | 58 * them. |
34 */ | 59 */ |
35 static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filen
ame) { | 60 static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filen
ame) { |
36 // Decode a resource: | 61 // Decode a resource: |
37 SkBitmap bm8888; | 62 SkBitmap bm8888; |
38 SkBitmap bm8888Unpremul; | 63 SkBitmap bm8888Unpremul; |
39 | 64 |
40 SkFILEStream stream(filename.c_str()); | 65 SkFILEStream stream(filename.c_str()); |
41 | 66 |
42 // JPEG is always opaque, so requesting unpremultiplied does not change anyt
hing. | 67 SkImageDecoder::Format format = SkImageDecoder::GetStreamFormat(&stream); |
43 if (SkImageDecoder::GetStreamFormat(&stream) == SkImageDecoder::kJPEG_Format
) { | 68 if (skip_image_format(format)) { |
44 return; | 69 return; |
45 } | 70 } |
46 | 71 |
47 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); | 72 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream)); |
48 if (NULL == decoder.get()) { | 73 if (NULL == decoder.get()) { |
49 SkDebugf("couldn't decode %s\n", filename.c_str()); | 74 SkDebugf("couldn't decode %s\n", filename.c_str()); |
50 return; | 75 return; |
51 } | 76 } |
52 | 77 |
53 bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config
, | 78 bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config
, |
(...skipping 28 matching lines...) Expand all Loading... |
82 return; | 107 return; |
83 } | 108 } |
84 | 109 |
85 // Now compare the two bitmaps. | 110 // Now compare the two bitmaps. |
86 for (int i = 0; i < bm8888.width(); ++i) { | 111 for (int i = 0; i < bm8888.width(); ++i) { |
87 for (int j = 0; j < bm8888.height(); ++j) { | 112 for (int j = 0; j < bm8888.height(); ++j) { |
88 // "c0" is the color of the premultiplied bitmap at (i, j). | 113 // "c0" is the color of the premultiplied bitmap at (i, j). |
89 const SkPMColor c0 = *bm8888.getAddr32(i, j); | 114 const SkPMColor c0 = *bm8888.getAddr32(i, j); |
90 // "c1" is the result of premultiplying the color of the unpremultip
lied | 115 // "c1" is the result of premultiplying the color of the unpremultip
lied |
91 // bitmap at (i, j). | 116 // bitmap at (i, j). |
92 const SkPMColor c1 = SkPreMultiplyUnPMColor(*bm8888Unpremul.getAddr3
2(i, j)); | 117 const SkPMColor c1 = premultiply_unpmcolor(*bm8888Unpremul.getAddr32
(i, j)); |
93 // Compute the difference for each component. | 118 // Compute the difference for each component. |
94 int da = SkAbs32(SkGetPackedA32(c0) - SkGetPackedA32(c1)); | 119 int da = SkAbs32(SkGetPackedA32(c0) - SkGetPackedA32(c1)); |
95 int dr = SkAbs32(SkGetPackedR32(c0) - SkGetPackedR32(c1)); | 120 int dr = SkAbs32(SkGetPackedR32(c0) - SkGetPackedR32(c1)); |
96 int dg = SkAbs32(SkGetPackedG32(c0) - SkGetPackedG32(c1)); | 121 int dg = SkAbs32(SkGetPackedG32(c0) - SkGetPackedG32(c1)); |
97 int db = SkAbs32(SkGetPackedB32(c0) - SkGetPackedB32(c1)); | 122 int db = SkAbs32(SkGetPackedB32(c0) - SkGetPackedB32(c1)); |
98 | 123 |
99 // Alpha component must be exactly the same. | 124 // Alpha component must be exactly the same. |
100 REPORTER_ASSERT(reporter, 0 == da); | 125 REPORTER_ASSERT(reporter, 0 == da); |
101 // Other components may differ if rounding is done differently, | 126 // Other components may differ if rounding is done differently, |
102 // but currently that is not the case. If an image fails here | 127 // but currently that is not the case. If an image fails here |
(...skipping 20 matching lines...) Expand all Loading... |
123 compare_unpremul(reporter, filename); | 148 compare_unpremul(reporter, filename); |
124 } while (iter.next(&basename)); | 149 } while (iter.next(&basename)); |
125 } else { | 150 } else { |
126 SkDebugf("Failed to find any files :(\n"); | 151 SkDebugf("Failed to find any files :(\n"); |
127 } | 152 } |
128 } | 153 } |
129 | 154 |
130 #include "TestClassDef.h" | 155 #include "TestClassDef.h" |
131 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, | 156 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, |
132 test_imageDecodingTests) | 157 test_imageDecodingTests) |
OLD | NEW |