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

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: 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
« include/core/SkColor.h ('K') | « 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 /**
21 * Test decoding an image in premultiplied mode and unpremultiplied mode and co mpare
22 * them.
23 */
24 static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filen ame) {
25 // Decode a resource:
26 SkBitmap bm8888;
27 SkBitmap bm8888Unpremul;
28
29 SkFILEStream stream(filename.c_str());
30
31 // JPEG is always opaque, so requesting unpremultiplied does not change anyt hing.
32 if (SkImageDecoder::GetStreamFormat(&stream) == SkImageDecoder::kJPEG_Format ) {
33 return;
34 }
35
36 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
37 if (NULL == decoder.get()) {
38 SkDebugf("couldn't decode %s\n", filename.c_str());
39 return;
40 }
41
42 bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config ,
43 SkImageDecoder::kDecodePixels_Mode);
44 if (!success) {
45 return;
46 }
47
48 REPORTER_ASSERT(reporter, bm8888.premultiplied());
49
50 success = stream.rewind();
51 REPORTER_ASSERT(reporter, success);
52 if (!success) {
53 return;
54 }
55
56 decoder->setRequestUnpremultipliedColors(true);
57 success = decoder->decode(&stream, &bm8888Unpremul, SkBitmap::kARGB_8888_Con fig,
58 SkImageDecoder::kDecodePixels_Mode);
59 REPORTER_ASSERT(reporter, success);
60
61 bool dimensionsMatch = bm8888.width() == bm8888Unpremul.width()
62 && bm8888.height() == bm8888Unpremul.height();
63 REPORTER_ASSERT(reporter, dimensionsMatch);
64 if (!dimensionsMatch) {
65 return;
66 }
67
68 // Only do the comparison if the two bitmaps are both 8888
69 // and only the first is premultiplied.
70 if (bm8888.config() != SkBitmap::kARGB_8888_Config
71 || bm8888Unpremul.config() != SkBitmap::kARGB_8888_Config
72 || bm8888Unpremul.premultiplied()) {
73 return;
74 }
75
76 // Now compare the two bitmaps.
77 for (int i = 0; i < bm8888.width(); ++i) {
78 for (int j = 0; j < bm8888.height(); ++j) {
79 // "c0" is the color of the premultiplied bitmap at (i, j).
80 const SkPMColor c0 = *bm8888.getAddr32(i, j);
81 // "c1" is the result of premultiplying the color of the unpremultip lied
82 // bitmap at (i, j).
83 const SkPMColor c1 = SkPreMultiplyUnPMColor(*bm8888Unpremul.getAddr3 2(i, j));
84 // Compute the difference for each component.
85 int da = SkAbs32(SkGetPackedA32(c0) - SkGetPackedA32(c1));
86 int dr = SkAbs32(SkGetPackedR32(c0) - SkGetPackedR32(c1));
87 int dg = SkAbs32(SkGetPackedG32(c0) - SkGetPackedG32(c1));
88 int db = SkAbs32(SkGetPackedB32(c0) - SkGetPackedB32(c1));
89
90 // Alpha component must be exactly the same.
91 REPORTER_ASSERT(reporter, 0 == da);
92 // Other components may differ if rounding is done differently,
93 // but currently that is not the case. If an image fails here
94 // in the future, we can change these to account for differences.
95 REPORTER_ASSERT(reporter, 0 == dr);
96 REPORTER_ASSERT(reporter, 0 == dg);
97 REPORTER_ASSERT(reporter, 0 == db);
98 }
99 }
100 }
101
102 static void test_imageDecodingTests(skiatest::Reporter* reporter) {
103 // This test cannot run if there is no resource path.
104 SkString resourcePath = skiatest::Test::GetResourcePath();
105 if (resourcePath.isEmpty()) {
106 return;
107 }
108 SkOSFile::Iter iter(resourcePath.c_str());
109 SkString basename;
110 if (iter.next(&basename)) {
111 do {
112 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str());
113 //SkDebugf("about to decode \"%s\"\n", filename.c_str());
114 compare_unpremul(reporter, filename);
115 } while (iter.next(&basename));
116 } else {
117 SkDebugf("Failed to find any files :(\n");
118 }
119 }
120
121 #include "TestClassDef.h"
122 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass,
123 test_imageDecodingTests)
OLDNEW
« include/core/SkColor.h ('K') | « src/ports/SkImageDecoder_WIC.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698