blob: 2cf3e527a58c12d3cb8468d78c0c4894ae6d99f1 [file] [log] [blame]
scroggo@google.com2bbc2c92013-06-14 15:33:20 +00001/*
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.com826d63a2013-07-18 20:06:28 +00009#include "SkCanvas.h"
10#include "SkColor.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000011#include "SkColorPriv.h"
scroggo@google.com826d63a2013-07-18 20:06:28 +000012#include "SkData.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000013#include "SkForceLinking.h"
scroggo@google.com826d63a2013-07-18 20:06:28 +000014#include "SkGradientShader.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000015#include "SkImageDecoder.h"
scroggo@google.com826d63a2013-07-18 20:06:28 +000016#include "SkImageEncoder.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000017#include "SkOSFile.h"
scroggo@google.com826d63a2013-07-18 20:06:28 +000018#include "SkPoint.h"
19#include "SkShader.h"
scroggo@google.com2bbc2c92013-06-14 15:33:20 +000020#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 */
30static 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 */
42static 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 */
67static 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.comdaaea2d2013-06-14 20:39:48 +0000133
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.com2bbc2c92013-06-14 15:33:20 +0000138 }
139 }
140}
141
scroggo@google.comf698c822013-07-18 19:34:49 +0000142// Create a fake ImageDecoder to test setPrefConfigTable for
143// backwards compatibility.
144class PrefConfigTestingImageDecoder : public SkImageDecoder {
145public:
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[] = {
151 SkBitmap::kA1_Config,
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
165protected:
166 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) SK_OVERRIDE {
167 return false;
168 }
169};
170
171static void test_pref_config_table(skiatest::Reporter* reporter) {
172 PrefConfigTestingImageDecoder decoder;
173 decoder.testPrefConfigTable(reporter);
174}
175
176static void test_unpremul(skiatest::Reporter* reporter) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000177 // This test cannot run if there is no resource path.
178 SkString resourcePath = skiatest::Test::GetResourcePath();
179 if (resourcePath.isEmpty()) {
180 return;
181 }
182 SkOSFile::Iter iter(resourcePath.c_str());
183 SkString basename;
184 if (iter.next(&basename)) {
185 do {
186 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basename.c_str());
187 //SkDebugf("about to decode \"%s\"\n", filename.c_str());
188 compare_unpremul(reporter, filename);
189 } while (iter.next(&basename));
190 } else {
191 SkDebugf("Failed to find any files :(\n");
192 }
193}
194
scroggo@google.com826d63a2013-07-18 20:06:28 +0000195#ifdef SK_DEBUG
196// Create a stream containing a bitmap encoded to Type type.
197static SkStream* create_image_stream(SkImageEncoder::Type type) {
198 SkBitmap bm;
199 const int size = 50;
200 bm.setConfig(SkBitmap::kARGB_8888_Config, size, size);
201 bm.allocPixels();
202 SkCanvas canvas(bm);
203 SkPoint points[2] = {
204 { SkIntToScalar(0), SkIntToScalar(0) },
205 { SkIntToScalar(size), SkIntToScalar(size) }
206 };
207 SkColor colors[2] = { SK_ColorWHITE, SK_ColorBLUE };
208 SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL,
209 SK_ARRAY_COUNT(colors),
210 SkShader::kClamp_TileMode);
211 SkPaint paint;
212 paint.setShader(shader)->unref();
213 canvas.drawPaint(paint);
214 // Now encode it to a stream.
215 SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(bm, type, 100));
216 if (NULL == data.get()) {
217 return NULL;
218 }
219 return SkNEW_ARGS(SkMemoryStream, (data.get()));
220}
221
222// For every format that supports tile based decoding, ensure that
223// calling decodeSubset will not fail if the caller has unreffed the
224// stream provided in buildTileIndex.
225// Only runs in debug mode since we are testing for a crash.
226static void test_stream_life() {
227 const SkImageEncoder::Type gTypes[] = {
228#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
229 SkImageEncoder::kJPEG_Type,
230#endif
231#ifdef SK_BUILD_FOR_ANDROID
232 SkImageEncoder::kPNG_Type,
233#endif
234 SkImageEncoder::kWEBP_Type,
235 };
236 for (size_t i = 0; i < SK_ARRAY_COUNT(gTypes); ++i) {
237 SkDebugf("encoding to %i\n", i);
238 SkAutoTUnref<SkStream> stream(create_image_stream(gTypes[i]));
239 if (NULL == stream.get()) {
240 SkDebugf("no stream\n");
241 continue;
242 }
243 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream));
244 if (NULL == decoder.get()) {
245 SkDebugf("no decoder\n");
246 continue;
247 }
248 int width, height;
249 if (!decoder->buildTileIndex(stream.get(), &width, &height)) {
250 SkDebugf("could not build a tile index\n");
251 continue;
252 }
253 // Now unref the stream to make sure it survives
254 stream.reset(NULL);
255 SkBitmap bm;
256 decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height),
257 SkBitmap::kARGB_8888_Config);
258 }
259}
260#endif
261
scroggo@google.comf698c822013-07-18 19:34:49 +0000262static void test_imageDecodingTests(skiatest::Reporter* reporter) {
263 test_unpremul(reporter);
264 test_pref_config_table(reporter);
scroggo@google.com826d63a2013-07-18 20:06:28 +0000265#ifdef SK_DEBUG
266 test_stream_life();
267#endif
scroggo@google.comf698c822013-07-18 19:34:49 +0000268}
269
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000270#include "TestClassDef.h"
271DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass,
272 test_imageDecodingTests)