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

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: Respond to comments 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
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 // Defined in SkColor.cpp
20 extern SkPMColor SkPreMultiplyUnPMColor(SkPMColor);
21
22 /**
23 * Test decoding an image in premultiplied mode and unpremultiplied mode and co mpare
24 * them.
25 */
26 static void compare_unpremul(skiatest::Reporter* reporter, const SkString& filen ame) {
27 // Decode a resource:
28 SkBitmap bm8888;
29 SkBitmap bm8888Unpremul;
30
31 SkFILEStream stream(filename.c_str());
32
33 // JPEG is always opaque, so requesting unpremultiplied does not change anyt hing.
34 if (SkImageDecoder::GetStreamFormat(&stream) == SkImageDecoder::kJPEG_Format ) {
35 return;
36 }
37
38 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
39 if (NULL == decoder.get()) {
40 SkDebugf("couldn't decode %s\n", filename.c_str());
41 return;
42 }
43
44 bool success = decoder->decode(&stream, &bm8888, SkBitmap::kARGB_8888_Config ,
45 SkImageDecoder::kDecodePixels_Mode);
46 if (!success) {
47 return;
48 }
49
50 success = stream.rewind();
51 REPORTER_ASSERT(reporter, success);
52 if (!success) {
53 return;
54 }
55
56 decoder->setRequireUnpremultipliedColors(true);
57 success = decoder->decode(&stream, &bm8888Unpremul, SkBitmap::kARGB_8888_Con fig,
58 SkImageDecoder::kDecodePixels_Mode);
59 if (!success) {
60 return;
61 }
62
63 bool dimensionsMatch = bm8888.width() == bm8888Unpremul.width()
64 && bm8888.height() == bm8888Unpremul.height();
65 REPORTER_ASSERT(reporter, dimensionsMatch);
66 if (!dimensionsMatch) {
67 return;
68 }
69
70 // Only do the comparison if the two bitmaps are both 8888.
71 if (bm8888.config() != SkBitmap::kARGB_8888_Config
72 || bm8888Unpremul.config() != SkBitmap::kARGB_8888_Config) {
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
« src/images/SkImageDecoder_libpng.cpp ('K') | « src/ports/SkImageDecoder_WIC.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698