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

Side by Side Diff: tests/ImageDecodingTest.cpp

Issue 16410009: Add an option to create unpremultiplied bitmaps. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix for andriod only code. Created 7 years, 6 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ports/SkImageDecoder_WIC.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 co mpare
58 * them.
59 */
60 static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filen ame) {
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_Con fig,
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 unpremultip lied
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);
126 // Other components may differ if rounding is done differently,
127 // but currently that is not the case. If an image fails here
128 // in the future, we can change these to account for differences.
129 REPORTER_ASSERT(reporter, 0 == dr);
130 REPORTER_ASSERT(reporter, 0 == dg);
131 REPORTER_ASSERT(reporter, 0 == db);
132 }
133 }
134 }
135
136 static void test_imageDecodingTests(skiatest::Reporter* reporter) {
137 // This test cannot run if there is no resource path.
138 SkString resourcePath = skiatest::Test::GetResourcePath();
139 if (resourcePath.isEmpty()) {
140 return;
141 }
142 SkOSFile::Iter iter(resourcePath.c_str());
143 SkString basename;
144 if (iter.next(&basename)) {
145 do {
146 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str());
147 //SkDebugf("about to decode \"%s\"\n", filename.c_str());
148 compare_unpremul(reporter, filename);
149 } while (iter.next(&basename));
150 } else {
151 SkDebugf("Failed to find any files :(\n");
152 }
153 }
154
155 #include "TestClassDef.h"
156 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass,
157 test_imageDecodingTests)
OLDNEW
« no previous file with comments | « src/ports/SkImageDecoder_WIC.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698