OLD | NEW |
1 #include "DMWriteTask.h" | 1 #include "DMWriteTask.h" |
2 | 2 |
3 #include "DMUtil.h" | 3 #include "DMUtil.h" |
4 #include "SkCommandLineFlags.h" | 4 #include "SkCommandLineFlags.h" |
5 #include "SkImageDecoder.h" | 5 #include "SkImageDecoder.h" |
6 #include "SkImageEncoder.h" | 6 #include "SkImageEncoder.h" |
7 #include "SkString.h" | 7 #include "SkString.h" |
| 8 #include "SkUnPreMultiply.h" |
8 | 9 |
9 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); | 10 DEFINE_string2(writePath, w, "", "If set, write GMs here as .pngs."); |
10 | 11 |
11 namespace DM { | 12 namespace DM { |
12 | 13 |
13 // Splits off the last N suffixes of name (splitting on _) and appends them to o
ut. | 14 // Splits off the last N suffixes of name (splitting on _) and appends them to o
ut. |
14 // Returns the total number of characters consumed. | 15 // Returns the total number of characters consumed. |
15 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { | 16 static int split_suffixes(int N, const char* name, SkTArray<SkString>* out) { |
16 SkTArray<SkString> split; | 17 SkTArray<SkString> split; |
17 SkStrSplit(name, "_", &split); | 18 SkStrSplit(name, "_", &split); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // Remove the suffix and tack on a .png. | 83 // Remove the suffix and tack on a .png. |
83 filename.remove(filename.size() - suffixLength, suffixLength); | 84 filename.remove(filename.size() - suffixLength, suffixLength); |
84 filename.append(".png"); | 85 filename.append(".png"); |
85 | 86 |
86 //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str()); | 87 //SkDebugf("dir %s, filename %s\n", dir.c_str(), filename.c_str()); |
87 | 88 |
88 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str()); | 89 return SkOSPath::SkPathJoin(dir.c_str(), filename.c_str()); |
89 } | 90 } |
90 | 91 |
91 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { | 92 bool WriteTask::Expectations::check(const Task& task, SkBitmap bitmap) const { |
| 93 // PNG is stored unpremultiplied, and going from premul to unpremul to premu
l is lossy. To |
| 94 // skirt this problem, we decode the PNG into an unpremul bitmap, convert ou
r bitmap to unpremul |
| 95 // if needed, and compare those. Each image goes once from premul to unprem
ul, never back. |
92 const SkString path = path_to_expected_image(fRoot, task); | 96 const SkString path = path_to_expected_image(fRoot, task); |
93 | 97 |
94 SkBitmap expected; | 98 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(path.c_str()))
; |
95 if (SkImageDecoder::DecodeFile(path.c_str(), &expected)) { | 99 if (NULL == stream.get()) { |
96 if (expected.config() != bitmap.config()) { | 100 SkDebugf("Could not read %s.\n", path.c_str()); |
97 SkBitmap converted; | 101 return false; |
98 SkAssertResult(expected.copyTo(&converted, bitmap.config())); | |
99 expected.swap(converted); | |
100 } | |
101 SkASSERT(expected.config() == bitmap.config()); | |
102 return BitmapsEqual(expected, bitmap); | |
103 } | 102 } |
104 | 103 |
105 // Couldn't read the file, etc. | 104 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
106 SkDebugf("Problem decoding %s to SkBitmap.\n", path.c_str()); | 105 if (NULL == decoder.get()) { |
107 return false; | 106 SkDebugf("Could not find a decoder for %s.\n", path.c_str()); |
| 107 return false; |
| 108 } |
| 109 |
| 110 SkImageInfo info; |
| 111 SkAssertResult(bitmap.asImageInfo(&info)); |
| 112 |
| 113 SkBitmap expected; |
| 114 expected.setConfig(info); |
| 115 expected.allocPixels(); |
| 116 |
| 117 // expected will be unpremultiplied. |
| 118 decoder->setRequireUnpremultipliedColors(true); |
| 119 if (!decoder->decode(stream, &expected, SkImageDecoder::kDecodePixels_Mode))
{ |
| 120 SkDebugf("Could not decode %s.\n", path.c_str()); |
| 121 return false; |
| 122 } |
| 123 |
| 124 // We always seem to decode to 8888. This puts 565 back in 565. |
| 125 if (expected.config() != bitmap.config()) { |
| 126 SkBitmap converted; |
| 127 SkAssertResult(expected.copyTo(&converted, bitmap.config())); |
| 128 expected.swap(converted); |
| 129 } |
| 130 SkASSERT(expected.config() == bitmap.config()); |
| 131 |
| 132 // Manually unpremultiply 8888 bitmaps to match expected. |
| 133 // Their pixels are shared, concurrently even, so we must copy them. |
| 134 if (info.fColorType == kPMColor_SkColorType) { |
| 135 SkBitmap unpremul; |
| 136 unpremul.setConfig(info); |
| 137 unpremul.allocPixels(); |
| 138 |
| 139 SkAutoLockPixels lockSrc(bitmap), lockDst(unpremul); |
| 140 const SkPMColor* src = (SkPMColor*)bitmap.getPixels(); |
| 141 SkColor* dst = (SkColor*)unpremul.getPixels(); |
| 142 |
| 143 for (size_t i = 0; i < bitmap.getSize()/4; i++) { |
| 144 dst[i] = SkUnPreMultiply::PMColorToColor(src[i]); |
| 145 } |
| 146 bitmap.swap(unpremul); |
| 147 } |
| 148 |
| 149 return BitmapsEqual(expected, bitmap); |
108 } | 150 } |
109 | 151 |
110 } // namespace DM | 152 } // namespace DM |
OLD | NEW |