| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 7 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 8 | #include "gm_expectations.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 11 | #include "SkCommandLineFlags.h" |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
| scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 13 | #include "SkForceLinking.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 14 | #include "SkGraphics.h" |
| 15 | #include "SkImageDecoder.h" |
| 16 | #include "SkImageEncoder.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 17 | #include "SkOSFile.h" |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 18 | #include "SkRandom.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 19 | #include "SkStream.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 20 | #include "SkTArray.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 21 | #include "SkTemplates.h" |
| 22 | |
| scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 23 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 24 | |
| scroggo@google.com | 6d99de1 | 2013-08-06 18:56:53 +0000 | [diff] [blame] | 25 | DEFINE_string(config, "None", "Preferred config to decode into. [None|8888|565|A8]"); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 26 | DEFINE_string(createExpectationsPath, "", "Path to write JSON expectations."); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 27 | DEFINE_string(mismatchPath, "", "Folder to write mismatched images to."); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 28 | DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required."); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 29 | DEFINE_string(readExpectationsPath, "", "Path to read JSON expectations from."); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 30 | DEFINE_bool(reencode, true, "Reencode the images to test encoding."); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 31 | DEFINE_bool(testSubsetDecoding, true, "Test decoding subsets of images."); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 32 | DEFINE_string2(writePath, w, "", "Write rendered images into this directory."); |
| scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 33 | DEFINE_bool(skip, false, "Skip writing zeroes."); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 34 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 35 | struct Format { |
| 36 | SkImageEncoder::Type fType; |
| 37 | SkImageDecoder::Format fFormat; |
| 38 | const char* fSuffix; |
| 39 | }; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 40 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 41 | static const Format gFormats[] = { |
| 42 | { SkImageEncoder::kBMP_Type, SkImageDecoder::kBMP_Format, ".bmp" }, |
| 43 | { SkImageEncoder::kGIF_Type, SkImageDecoder::kGIF_Format, ".gif" }, |
| 44 | { SkImageEncoder::kICO_Type, SkImageDecoder::kICO_Format, ".ico" }, |
| 45 | { SkImageEncoder::kJPEG_Type, SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 46 | { SkImageEncoder::kPNG_Type, SkImageDecoder::kPNG_Format, ".png" }, |
| 47 | { SkImageEncoder::kWBMP_Type, SkImageDecoder::kWBMP_Format, ".wbmp" }, |
| 48 | { SkImageEncoder::kWEBP_Type, SkImageDecoder::kWEBP_Format, ".webp" } |
| 49 | }; |
| 50 | |
| 51 | static SkImageEncoder::Type format_to_type(SkImageDecoder::Format format) { |
| 52 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 53 | if (gFormats[i].fFormat == format) { |
| 54 | return gFormats[i].fType; |
| 55 | } |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 56 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 57 | return SkImageEncoder::kUnknown_Type; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 60 | static const char* suffix_for_type(SkImageEncoder::Type type) { |
| 61 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 62 | if (gFormats[i].fType == type) { |
| 63 | return gFormats[i].fSuffix; |
| 64 | } |
| 65 | } |
| 66 | return ""; |
| 67 | } |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 68 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 69 | static SkImageDecoder::Format guess_format_from_suffix(const char suffix[]) { |
| 70 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 71 | if (strcmp(suffix, gFormats[i].fSuffix) == 0) { |
| 72 | return gFormats[i].fFormat; |
| 73 | } |
| 74 | } |
| 75 | return SkImageDecoder::kUnknown_Format; |
| 76 | } |
| 77 | |
| 78 | static void make_outname(SkString* dst, const char outDir[], const char src[], |
| 79 | const char suffix[]) { |
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 80 | SkString basename = SkOSPath::SkBasename(src); |
| 81 | dst->set(SkOSPath::SkPathJoin(outDir, basename.c_str())); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 82 | if (!dst->endsWith(suffix)) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 83 | const char* cstyleDst = dst->c_str(); |
| 84 | const char* dot = strrchr(cstyleDst, '.'); |
| 85 | if (dot != NULL) { |
| 86 | int32_t index = SkToS32(dot - cstyleDst); |
| 87 | dst->remove(index, dst->size() - index); |
| 88 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 89 | dst->append(suffix); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 93 | // Store the names of the filenames to report later which ones failed, succeeded, and were |
| 94 | // invalid. |
| 95 | static SkTArray<SkString, false> gInvalidStreams; |
| 96 | static SkTArray<SkString, false> gMissingCodecs; |
| 97 | static SkTArray<SkString, false> gDecodeFailures; |
| 98 | static SkTArray<SkString, false> gEncodeFailures; |
| 99 | static SkTArray<SkString, false> gSuccessfulDecodes; |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 100 | static SkTArray<SkString, false> gSuccessfulSubsetDecodes; |
| 101 | static SkTArray<SkString, false> gFailedSubsetDecodes; |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 102 | // Files/subsets that do not have expectations. Not reported as a failure of the test so |
| 103 | // the bots will not turn red with each new image test. |
| 104 | static SkTArray<SkString, false> gMissingExpectations; |
| 105 | static SkTArray<SkString, false> gMissingSubsetExpectations; |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 106 | |
| scroggo@google.com | 6d99de1 | 2013-08-06 18:56:53 +0000 | [diff] [blame] | 107 | static SkBitmap::Config gPrefConfig(SkBitmap::kNo_Config); |
| 108 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 109 | // Expections read from a file specified by readExpectationsPath. The expectations must have been |
| 110 | // previously written using createExpectationsPath. |
| 111 | SkAutoTUnref<skiagm::JsonExpectationsSource> gJsonExpectations; |
| 112 | |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 113 | static bool write_bitmap(const char outName[], const SkBitmap& bm) { |
| 114 | return SkImageEncoder::EncodeFile(outName, bm, SkImageEncoder::kPNG_Type, 100); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Return a random SkIRect inside the range specified. |
| 119 | * @param rand Random number generator. |
| 120 | * @param maxX Exclusive maximum x-coordinate. SkIRect's fLeft and fRight will be |
| 121 | * in the range [0, maxX) |
| 122 | * @param maxY Exclusive maximum y-coordinate. SkIRect's fTop and fBottom will be |
| 123 | * in the range [0, maxY) |
| 124 | * @return SkIRect Non-empty, non-degenerate rectangle. |
| 125 | */ |
| 126 | static SkIRect generate_random_rect(SkRandom* rand, int32_t maxX, int32_t maxY) { |
| 127 | SkASSERT(maxX > 1 && maxY > 1); |
| 128 | int32_t left = rand->nextULessThan(maxX); |
| 129 | int32_t right = rand->nextULessThan(maxX); |
| 130 | int32_t top = rand->nextULessThan(maxY); |
| 131 | int32_t bottom = rand->nextULessThan(maxY); |
| 132 | SkIRect rect = SkIRect::MakeLTRB(left, top, right, bottom); |
| 133 | rect.sort(); |
| 134 | // Make sure rect is not empty. |
| 135 | if (rect.fLeft == rect.fRight) { |
| 136 | if (rect.fLeft > 0) { |
| 137 | rect.fLeft--; |
| 138 | } else { |
| 139 | rect.fRight++; |
| 140 | // This branch is only taken if 0 == rect.fRight, and |
| 141 | // maxX must be at least 2, so it must still be in |
| 142 | // range. |
| 143 | SkASSERT(rect.fRight < maxX); |
| 144 | } |
| 145 | } |
| 146 | if (rect.fTop == rect.fBottom) { |
| 147 | if (rect.fTop > 0) { |
| 148 | rect.fTop--; |
| 149 | } else { |
| 150 | rect.fBottom++; |
| 151 | // Again, this must be in range. |
| 152 | SkASSERT(rect.fBottom < maxY); |
| 153 | } |
| 154 | } |
| 155 | return rect; |
| 156 | } |
| 157 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 158 | // Stored expectations to be written to a file if createExpectationsPath is specified. |
| 159 | static Json::Value gExpectationsToWrite; |
| 160 | |
| 161 | /** |
| epoger@google.com | d4993ff | 2013-05-24 14:33:28 +0000 | [diff] [blame] | 162 | * If expectations are to be recorded, record the bitmap expectations into global |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 163 | * expectations array. |
| 164 | */ |
| 165 | static void write_expectations(const SkBitmap& bitmap, const char* filename) { |
| 166 | if (!FLAGS_createExpectationsPath.isEmpty()) { |
| 167 | // Creates an Expectations object, and add it to the list to write. |
| 168 | skiagm::Expectations expectation(bitmap); |
| 169 | Json::Value value = expectation.asJsonValue(); |
| 170 | gExpectationsToWrite[filename] = value; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| scroggo@google.com | bc91e8b | 2013-06-27 20:21:01 +0000 | [diff] [blame] | 175 | * Return true if this filename is a known failure, and therefore a failure |
| 176 | * to decode should be ignored. |
| 177 | */ |
| 178 | static bool expect_to_fail(const char* filename) { |
| 179 | if (NULL == gJsonExpectations.get()) { |
| 180 | return false; |
| 181 | } |
| 182 | skiagm::Expectations jsExpectations = gJsonExpectations->get(filename); |
| 183 | return jsExpectations.ignoreFailure(); |
| 184 | } |
| 185 | |
| 186 | /** |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 187 | * Compare against an expectation for this filename, if there is one. |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 188 | * @param digest GmResultDigest, computed from the decoded bitmap, to compare to the |
| 189 | * expectation. |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 190 | * @param filename String used to find the expected value. |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 191 | * @param failureArray Array to add a failure message to on failure. |
| 192 | * @param missingArray Array to add missing expectation to on failure. |
| scroggo@google.com | 6ca30ca | 2013-05-14 17:30:17 +0000 | [diff] [blame] | 193 | * @return bool True in any of these cases: |
| 194 | * - the bitmap matches the expectation. |
| scroggo@google.com | 6ca30ca | 2013-05-14 17:30:17 +0000 | [diff] [blame] | 195 | * False in any of these cases: |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 196 | * - there is no expectations file. |
| scroggo@google.com | 6ca30ca | 2013-05-14 17:30:17 +0000 | [diff] [blame] | 197 | * - there is an expectations file, but no expectation for this bitmap. |
| 198 | * - there is an expectation for this bitmap, but it did not match. |
| 199 | * - expectation could not be computed from the bitmap. |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 200 | */ |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 201 | static bool compare_to_expectations_if_necessary(const skiagm::GmResultDigest& digest, |
| 202 | const char* filename, |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 203 | SkTArray<SkString, false>* failureArray, |
| 204 | SkTArray<SkString, false>* missingArray) { |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 205 | if (!digest.isValid()) { |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 206 | if (failureArray != NULL) { |
| 207 | failureArray->push_back().printf("decoded %s, but could not create a GmResultDigest.", |
| 208 | filename); |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 213 | if (NULL == gJsonExpectations.get()) { |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 214 | return false; |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | skiagm::Expectations jsExpectation = gJsonExpectations->get(filename); |
| 218 | if (jsExpectation.empty()) { |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 219 | if (missingArray != NULL) { |
| 220 | missingArray->push_back().printf("decoded %s, but could not find expectation.", |
| scroggo@google.com | 6ca30ca | 2013-05-14 17:30:17 +0000 | [diff] [blame] | 221 | filename); |
| 222 | } |
| 223 | return false; |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 226 | if (jsExpectation.match(digest)) { |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
| 230 | if (failureArray != NULL) { |
| 231 | failureArray->push_back().printf("decoded %s, but the result does not match " |
| 232 | "expectations.", |
| 233 | filename); |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 238 | /** |
| 239 | * Helper function to write a bitmap subset to a file. Only called if subsets were created |
| 240 | * and a writePath was provided. Creates a subdirectory called 'subsets' and writes a PNG to |
| 241 | * that directory. Also creates a subdirectory called 'extracted' and writes a bitmap created |
| 242 | * using extractSubset to a PNG in that directory. Both files will represent the same |
| 243 | * subrectangle and have the same name for comparison. |
| 244 | * @param writePath Parent directory to hold the folders for the PNG files to write. Must |
| 245 | * not be NULL. |
| 246 | * @param filename Basename of the original file. Used to name the new files. Must not be |
| 247 | * NULL. |
| 248 | * @param subsetDim String representing the dimensions of the subset. Used to name the new |
| 249 | * files. Must not be NULL. |
| 250 | * @param bitmapFromDecodeSubset Pointer to SkBitmap created by SkImageDecoder::DecodeSubset, |
| 251 | * using rect as the area to decode. |
| 252 | * @param rect Rectangle of the area decoded into bitmapFromDecodeSubset. Used to call |
| 253 | * extractSubset on originalBitmap to create a bitmap with the same dimensions/pixels as |
| 254 | * bitmapFromDecodeSubset (assuming decodeSubset worked properly). |
| 255 | * @param originalBitmap SkBitmap decoded from the same stream as bitmapFromDecodeSubset, |
| 256 | * using SkImageDecoder::decode to get the entire image. Used to create a PNG file for |
| 257 | * comparison to the PNG created by bitmapFromDecodeSubset. |
| 258 | * @return bool Whether the function succeeded at drawing the decoded subset and the extracted |
| 259 | * subset to files. |
| 260 | */ |
| 261 | static bool write_subset(const char* writePath, const char* filename, const char* subsetDim, |
| 262 | SkBitmap* bitmapFromDecodeSubset, SkIRect rect, |
| 263 | const SkBitmap& originalBitmap) { |
| 264 | // All parameters must be valid. |
| 265 | SkASSERT(writePath != NULL); |
| 266 | SkASSERT(filename != NULL); |
| 267 | SkASSERT(subsetDim != NULL); |
| 268 | SkASSERT(bitmapFromDecodeSubset != NULL); |
| 269 | |
| 270 | // Create a subdirectory to hold the results of decodeSubset. |
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 271 | SkString dir = SkOSPath::SkPathJoin(writePath, "subsets"); |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 272 | if (!sk_mkdir(dir.c_str())) { |
| 273 | gFailedSubsetDecodes.push_back().printf("Successfully decoded %s from %s, but failed to " |
| 274 | "create a directory to write to.", subsetDim, |
| 275 | filename); |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | // Write the subset to a file whose name includes the dimensions. |
| 280 | SkString suffix = SkStringPrintf("_%s.png", subsetDim); |
| 281 | SkString outPath; |
| 282 | make_outname(&outPath, dir.c_str(), filename, suffix.c_str()); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 283 | SkAssertResult(write_bitmap(outPath.c_str(), *bitmapFromDecodeSubset)); |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 284 | gSuccessfulSubsetDecodes.push_back().printf("\twrote %s", outPath.c_str()); |
| 285 | |
| 286 | // Also use extractSubset from the original for visual comparison. |
| 287 | // Write the result to a file in a separate subdirectory. |
| 288 | SkBitmap extractedSubset; |
| 289 | if (!originalBitmap.extractSubset(&extractedSubset, rect)) { |
| 290 | gFailedSubsetDecodes.push_back().printf("Successfully decoded %s from %s, but failed to " |
| 291 | "extract a similar subset for comparison.", |
| 292 | subsetDim, filename); |
| 293 | return false; |
| 294 | } |
| 295 | |
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 296 | SkString dirExtracted = SkOSPath::SkPathJoin(writePath, "extracted"); |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 297 | if (!sk_mkdir(dirExtracted.c_str())) { |
| 298 | gFailedSubsetDecodes.push_back().printf("Successfully decoded %s from %s, but failed to " |
| 299 | "create a directory for extractSubset comparison.", |
| 300 | subsetDim, filename); |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | make_outname(&outPath, dirExtracted.c_str(), filename, suffix.c_str()); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 305 | SkAssertResult(write_bitmap(outPath.c_str(), extractedSubset)); |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 306 | return true; |
| 307 | } |
| 308 | |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 309 | // FIXME: This test could be run on windows/mac once we remove their dependence on |
| 310 | // getLength. See https://code.google.com/p/skia/issues/detail?id=1570 |
| 311 | #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) |
| 312 | |
| 313 | /** |
| 314 | * Dummy class for testing to ensure that a stream without a length decodes the same |
| 315 | * as a stream with a length. |
| 316 | */ |
| 317 | class FILEStreamWithoutLength : public SkFILEStream { |
| 318 | public: |
| 319 | FILEStreamWithoutLength(const char path[]) |
| 320 | : INHERITED(path) {} |
| 321 | |
| 322 | virtual bool hasLength() const SK_OVERRIDE { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | private: |
| 327 | typedef SkFILEStream INHERITED; |
| 328 | }; |
| 329 | |
| 330 | /** |
| 331 | * Test that decoding a stream which reports to not have a length still results in the |
| 332 | * same image as if it did report to have a length. Assumes that codec was used to |
| 333 | * successfully decode the file using SkFILEStream. |
| 334 | * @param srcPath The path to the file, for recreating the length-less stream. |
| 335 | * @param codec The SkImageDecoder originally used to decode srcPath, which will be used |
| 336 | * again to decode the length-less stream. |
| 337 | * @param digest GmResultDigest computed from decoding the stream the first time. |
| 338 | * Decoding the length-less stream is expected to result in a matching digest. |
| 339 | */ |
| 340 | static void test_stream_without_length(const char srcPath[], SkImageDecoder* codec, |
| 341 | const skiagm::GmResultDigest& digest) { |
| 342 | if (!digest.isValid()) { |
| 343 | // An error was already reported. |
| 344 | return; |
| 345 | } |
| 346 | SkASSERT(srcPath); |
| 347 | SkASSERT(codec); |
| 348 | FILEStreamWithoutLength stream(srcPath); |
| 349 | // This will only be called after a successful decode. Creating a stream from the same |
| 350 | // path should never fail. |
| 351 | SkASSERT(stream.isValid()); |
| 352 | SkBitmap bm; |
| 353 | if (!codec->decode(&stream, &bm, gPrefConfig, SkImageDecoder::kDecodePixels_Mode)) { |
| 354 | gDecodeFailures.push_back().appendf("Without using getLength, %s failed to decode\n", |
| 355 | srcPath); |
| 356 | return; |
| 357 | } |
| 358 | skiagm::GmResultDigest lengthLessDigest(bm); |
| 359 | if (!lengthLessDigest.isValid()) { |
| 360 | gDecodeFailures.push_back().appendf("Without using getLength, %s failed to build " |
| 361 | "a digest\n", srcPath); |
| 362 | return; |
| 363 | } |
| 364 | if (!lengthLessDigest.equals(digest)) { |
| 365 | gDecodeFailures.push_back().appendf("Without using getLength, %s did not match digest " |
| 366 | "that uses getLength\n", srcPath); |
| 367 | } |
| 368 | } |
| 369 | #endif // defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) |
| 370 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 371 | static void decodeFileAndWrite(const char srcPath[], const SkString* writePath) { |
| 372 | SkBitmap bitmap; |
| 373 | SkFILEStream stream(srcPath); |
| 374 | if (!stream.isValid()) { |
| 375 | gInvalidStreams.push_back().set(srcPath); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 380 | if (NULL == codec) { |
| 381 | gMissingCodecs.push_back().set(srcPath); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | SkAutoTDelete<SkImageDecoder> ad(codec); |
| 386 | |
| scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 387 | codec->setSkipWritingZeroes(FLAGS_skip); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 388 | stream.rewind(); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 389 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 390 | // Create a string representing just the filename itself, for use in json expectations. |
| scroggo@google.com | ccd7afb | 2013-05-28 16:45:07 +0000 | [diff] [blame] | 391 | SkString basename = SkOSPath::SkBasename(srcPath); |
| 392 | const char* filename = basename.c_str(); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 393 | |
| scroggo@google.com | 6d99de1 | 2013-08-06 18:56:53 +0000 | [diff] [blame] | 394 | if (!codec->decode(&stream, &bitmap, gPrefConfig, |
| scroggo@google.com | bc91e8b | 2013-06-27 20:21:01 +0000 | [diff] [blame] | 395 | SkImageDecoder::kDecodePixels_Mode)) { |
| 396 | if (expect_to_fail(filename)) { |
| 397 | gSuccessfulDecodes.push_back().appendf( |
| 398 | "failed to decode %s, which is a known failure.", srcPath); |
| 399 | } else { |
| 400 | gDecodeFailures.push_back().set(srcPath); |
| 401 | } |
| 402 | return; |
| 403 | } |
| 404 | |
| scroggo@google.com | 6f67b3b | 2013-07-18 20:08:26 +0000 | [diff] [blame] | 405 | // Test decoding just the bounds. The bounds should always match. |
| 406 | { |
| 407 | stream.rewind(); |
| 408 | SkBitmap dim; |
| 409 | if (!codec->decode(&stream, &dim, SkImageDecoder::kDecodeBounds_Mode)) { |
| 410 | SkString failure = SkStringPrintf("failed to decode bounds for %s", srcPath); |
| 411 | gDecodeFailures.push_back() = failure; |
| 412 | } else { |
| 413 | // Now check that the bounds match: |
| 414 | if (dim.width() != bitmap.width() || dim.height() != bitmap.height()) { |
| 415 | SkString failure = SkStringPrintf("bounds do not match for %s", srcPath); |
| 416 | gDecodeFailures.push_back() = failure; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 421 | skiagm::GmResultDigest digest(bitmap); |
| 422 | if (compare_to_expectations_if_necessary(digest, filename, |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 423 | &gDecodeFailures, |
| 424 | &gMissingExpectations)) { |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 425 | gSuccessfulDecodes.push_back().printf("%s [%d %d]", srcPath, bitmap.width(), |
| 426 | bitmap.height()); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 427 | } else if (!FLAGS_mismatchPath.isEmpty()) { |
| 428 | SkString outPath; |
| 429 | make_outname(&outPath, FLAGS_mismatchPath[0], srcPath, ".png"); |
| 430 | if (write_bitmap(outPath.c_str(), bitmap)) { |
| 431 | gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str()); |
| 432 | } else { |
| 433 | gEncodeFailures.push_back().set(outPath); |
| 434 | } |
| 435 | } |
| 436 | |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 437 | // FIXME: This test could be run on windows/mac once we remove their dependence on |
| 438 | // getLength. See https://code.google.com/p/skia/issues/detail?id=1570 |
| 439 | #if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX) |
| 440 | test_stream_without_length(srcPath, codec, digest); |
| 441 | #endif |
| 442 | |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 443 | if (writePath != NULL) { |
| 444 | SkString outPath; |
| 445 | make_outname(&outPath, writePath->c_str(), srcPath, ".png"); |
| 446 | if (write_bitmap(outPath.c_str(), bitmap)) { |
| 447 | gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str()); |
| 448 | } else { |
| 449 | gEncodeFailures.push_back().set(outPath); |
| 450 | } |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | write_expectations(bitmap, filename); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 454 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 455 | if (FLAGS_testSubsetDecoding) { |
| scroggo@google.com | 0018f75 | 2013-05-03 20:39:22 +0000 | [diff] [blame] | 456 | SkDEBUGCODE(bool couldRewind =) stream.rewind(); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 457 | SkASSERT(couldRewind); |
| 458 | int width, height; |
| 459 | // Build the tile index for decoding subsets. If the image is 1x1, skip subset |
| 460 | // decoding since there are no smaller subsets. |
| 461 | if (codec->buildTileIndex(&stream, &width, &height) && width > 1 && height > 1) { |
| 462 | SkASSERT(bitmap.width() == width && bitmap.height() == height); |
| 463 | // Call decodeSubset multiple times: |
| 464 | SkRandom rand(0); |
| 465 | for (int i = 0; i < 5; i++) { |
| 466 | SkBitmap bitmapFromDecodeSubset; |
| 467 | // FIXME: Come up with a more representative set of rectangles. |
| 468 | SkIRect rect = generate_random_rect(&rand, width, height); |
| 469 | SkString subsetDim = SkStringPrintf("[%d,%d,%d,%d]", rect.fLeft, rect.fTop, |
| 470 | rect.fRight, rect.fBottom); |
| scroggo@google.com | 6d99de1 | 2013-08-06 18:56:53 +0000 | [diff] [blame] | 471 | if (codec->decodeSubset(&bitmapFromDecodeSubset, rect, gPrefConfig)) { |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 472 | SkString subsetName = SkStringPrintf("%s_%s", filename, subsetDim.c_str()); |
| scroggo@google.com | cd7a73c | 2013-08-28 18:33:31 +0000 | [diff] [blame] | 473 | skiagm::GmResultDigest subsetDigest(bitmapFromDecodeSubset); |
| 474 | if (compare_to_expectations_if_necessary(subsetDigest, |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 475 | subsetName.c_str(), |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 476 | &gFailedSubsetDecodes, |
| 477 | &gMissingSubsetExpectations)) { |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 478 | gSuccessfulSubsetDecodes.push_back().printf("Decoded subset %s from %s", |
| 479 | subsetDim.c_str(), srcPath); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 480 | } else if (!FLAGS_mismatchPath.isEmpty()) { |
| 481 | write_subset(FLAGS_mismatchPath[0], filename, subsetDim.c_str(), |
| 482 | &bitmapFromDecodeSubset, rect, bitmap); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | write_expectations(bitmapFromDecodeSubset, subsetName.c_str()); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 486 | if (writePath != NULL) { |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 487 | write_subset(writePath->c_str(), filename, subsetDim.c_str(), |
| 488 | &bitmapFromDecodeSubset, rect, bitmap); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 489 | } |
| 490 | } else { |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 491 | gFailedSubsetDecodes.push_back().printf("Failed to decode region %s from %s", |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 492 | subsetDim.c_str(), srcPath); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| scroggo@google.com | 9da0e1d | 2013-05-15 18:14:36 +0000 | [diff] [blame] | 497 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 498 | if (FLAGS_reencode) { |
| 499 | // Encode to the format the file was originally in, or PNG if the encoder for the same |
| 500 | // format is unavailable. |
| 501 | SkImageDecoder::Format format = codec->getFormat(); |
| 502 | if (SkImageDecoder::kUnknown_Format == format) { |
| 503 | if (stream.rewind()) { |
| 504 | format = SkImageDecoder::GetStreamFormat(&stream); |
| 505 | } |
| 506 | if (SkImageDecoder::kUnknown_Format == format) { |
| 507 | const char* dot = strrchr(srcPath, '.'); |
| 508 | if (NULL != dot) { |
| 509 | format = guess_format_from_suffix(dot); |
| 510 | } |
| 511 | if (SkImageDecoder::kUnknown_Format == format) { |
| 512 | SkDebugf("Could not determine type for '%s'\n", srcPath); |
| 513 | format = SkImageDecoder::kPNG_Format; |
| 514 | } |
| 515 | |
| 516 | } |
| 517 | } else { |
| 518 | SkASSERT(!stream.rewind() || SkImageDecoder::GetStreamFormat(&stream) == format); |
| 519 | } |
| 520 | SkImageEncoder::Type type = format_to_type(format); |
| 521 | // format should never be kUnknown_Format, so type should never be kUnknown_Type. |
| 522 | SkASSERT(type != SkImageEncoder::kUnknown_Type); |
| 523 | |
| 524 | SkImageEncoder* encoder = SkImageEncoder::Create(type); |
| 525 | if (NULL == encoder) { |
| 526 | type = SkImageEncoder::kPNG_Type; |
| 527 | encoder = SkImageEncoder::Create(type); |
| 528 | SkASSERT(encoder); |
| 529 | } |
| 530 | SkAutoTDelete<SkImageEncoder> ade(encoder); |
| 531 | // Encode to a stream. |
| 532 | SkDynamicMemoryWStream wStream; |
| 533 | if (!encoder->encodeStream(&wStream, bitmap, 100)) { |
| 534 | gEncodeFailures.push_back().printf("Failed to reencode %s to type '%s'", srcPath, |
| 535 | suffix_for_type(type)); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | SkAutoTUnref<SkData> data(wStream.copyToData()); |
| 540 | if (writePath != NULL && type != SkImageEncoder::kPNG_Type) { |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 541 | // Write the encoded data to a file. Do not write to PNG, which was already written. |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 542 | SkString outPath; |
| 543 | make_outname(&outPath, writePath->c_str(), srcPath, suffix_for_type(type)); |
| 544 | SkFILEWStream file(outPath.c_str()); |
| 545 | if(file.write(data->data(), data->size())) { |
| 546 | gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str()); |
| 547 | } else { |
| 548 | gEncodeFailures.push_back().printf("Failed to write %s", outPath.c_str()); |
| 549 | } |
| 550 | } |
| 551 | // Ensure that the reencoded data can still be decoded. |
| 552 | SkMemoryStream memStream(data); |
| 553 | SkBitmap redecodedBitmap; |
| 554 | SkImageDecoder::Format formatOnSecondDecode; |
| scroggo@google.com | 6d99de1 | 2013-08-06 18:56:53 +0000 | [diff] [blame] | 555 | if (SkImageDecoder::DecodeStream(&memStream, &redecodedBitmap, gPrefConfig, |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 556 | SkImageDecoder::kDecodePixels_Mode, |
| 557 | &formatOnSecondDecode)) { |
| 558 | SkASSERT(format_to_type(formatOnSecondDecode) == type); |
| 559 | } else { |
| 560 | gDecodeFailures.push_back().printf("Failed to redecode %s after reencoding to '%s'", |
| 561 | srcPath, suffix_for_type(type)); |
| 562 | } |
| 563 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /////////////////////////////////////////////////////////////////////////////// |
| 567 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 568 | // If strings is not empty, print title, followed by each string on its own line starting |
| 569 | // with a tab. |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 570 | // @return bool True if strings had at least one entry. |
| 571 | static bool print_strings(const char* title, const SkTArray<SkString, false>& strings) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 572 | if (strings.count() > 0) { |
| 573 | SkDebugf("%s:\n", title); |
| 574 | for (int i = 0; i < strings.count(); i++) { |
| 575 | SkDebugf("\t%s\n", strings[i].c_str()); |
| 576 | } |
| 577 | SkDebugf("\n"); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 578 | return true; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 579 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 580 | return false; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 583 | /** |
| 584 | * If directory is non null and does not end with a path separator, append one. |
| 585 | * @param directory SkString representing the path to a directory. If the last character is not a |
| 586 | * path separator (specific to the current OS), append one. |
| 587 | */ |
| 588 | static void append_path_separator_if_necessary(SkString* directory) { |
| 589 | if (directory != NULL && directory->c_str()[directory->size() - 1] != SkPATH_SEPARATOR) { |
| 590 | directory->appendf("%c", SkPATH_SEPARATOR); |
| 591 | } |
| 592 | } |
| 593 | |
| scroggo@google.com | 925cdca | 2013-06-28 20:04:42 +0000 | [diff] [blame] | 594 | /** |
| 595 | * Return true if the filename represents an image. |
| 596 | */ |
| 597 | static bool is_image_file(const char* filename) { |
| 598 | const char* gImageExtensions[] = { |
| 599 | ".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".bmp", ".BMP", |
| 600 | ".webp", ".WEBP", ".ico", ".ICO", ".wbmp", ".WBMP", ".gif", ".GIF" |
| 601 | }; |
| 602 | for (size_t i = 0; i < SK_ARRAY_COUNT(gImageExtensions); ++i) { |
| 603 | if (SkStrEndsWith(filename, gImageExtensions[i])) { |
| 604 | return true; |
| 605 | } |
| 606 | } |
| 607 | return false; |
| 608 | } |
| 609 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 610 | int tool_main(int argc, char** argv); |
| 611 | int tool_main(int argc, char** argv) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 612 | SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); |
| 613 | SkCommandLineFlags::Parse(argc, argv); |
| 614 | |
| 615 | if (FLAGS_readPath.count() < 1) { |
| 616 | SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 621 | SkAutoGraphics ag; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 622 | |
| scroggo@google.com | 3832da1 | 2013-06-19 19:12:53 +0000 | [diff] [blame] | 623 | if (!FLAGS_readExpectationsPath.isEmpty() && sk_exists(FLAGS_readExpectationsPath[0])) { |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 624 | gJsonExpectations.reset(SkNEW_ARGS(skiagm::JsonExpectationsSource, |
| 625 | (FLAGS_readExpectationsPath[0]))); |
| 626 | } |
| 627 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 628 | SkString outDir; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 629 | SkString* outDirPtr; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 630 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 631 | if (FLAGS_writePath.count() == 1) { |
| 632 | outDir.set(FLAGS_writePath[0]); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 633 | append_path_separator_if_necessary(&outDir); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 634 | outDirPtr = &outDir; |
| 635 | } else { |
| 636 | outDirPtr = NULL; |
| 637 | } |
| 638 | |
| scroggo@google.com | 6d99de1 | 2013-08-06 18:56:53 +0000 | [diff] [blame] | 639 | if (FLAGS_config.count() == 1) { |
| 640 | // Only consider the first config specified on the command line. |
| 641 | const char* config = FLAGS_config[0]; |
| 642 | if (0 == strcmp(config, "8888")) { |
| 643 | gPrefConfig = SkBitmap::kARGB_8888_Config; |
| 644 | } else if (0 == strcmp(config, "565")) { |
| 645 | gPrefConfig = SkBitmap::kRGB_565_Config; |
| 646 | } else if (0 == strcmp(config, "A8")) { |
| 647 | gPrefConfig = SkBitmap::kA8_Config; |
| 648 | } else if (0 != strcmp(config, "None")) { |
| 649 | SkDebugf("Invalid preferred config\n"); |
| 650 | return -1; |
| 651 | } |
| 652 | } |
| 653 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 654 | for (int i = 0; i < FLAGS_readPath.count(); i++) { |
| scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 655 | const char* readPath = FLAGS_readPath[i]; |
| 656 | if (strlen(readPath) < 1) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 657 | break; |
| 658 | } |
| scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 659 | if (sk_isdir(readPath)) { |
| 660 | const char* dir = readPath; |
| 661 | SkOSFile::Iter iter(dir); |
| 662 | SkString filename; |
| 663 | while (iter.next(&filename)) { |
| scroggo@google.com | 925cdca | 2013-06-28 20:04:42 +0000 | [diff] [blame] | 664 | if (!is_image_file(filename.c_str())) { |
| 665 | continue; |
| 666 | } |
| scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 667 | SkString fullname = SkOSPath::SkPathJoin(dir, filename.c_str()); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 668 | decodeFileAndWrite(fullname.c_str(), outDirPtr); |
| scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 669 | } |
| scroggo@google.com | 925cdca | 2013-06-28 20:04:42 +0000 | [diff] [blame] | 670 | } else if (sk_exists(readPath) && is_image_file(readPath)) { |
| scroggo@google.com | 2b9424b | 2013-06-21 19:12:47 +0000 | [diff] [blame] | 671 | decodeFileAndWrite(readPath, outDirPtr); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 675 | if (!FLAGS_createExpectationsPath.isEmpty()) { |
| 676 | // Use an empty value for everything besides expectations, since the reader only cares |
| 677 | // about the expectations. |
| 678 | Json::Value nullValue; |
| 679 | Json::Value root = skiagm::CreateJsonTree(gExpectationsToWrite, nullValue, nullValue, |
| 680 | nullValue, nullValue); |
| 681 | std::string jsonStdString = root.toStyledString(); |
| scroggo@google.com | cf5eb6a | 2013-06-07 12:43:15 +0000 | [diff] [blame] | 682 | SkFILEWStream stream(FLAGS_createExpectationsPath[0]); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 683 | stream.write(jsonStdString.c_str(), jsonStdString.length()); |
| 684 | } |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 685 | // Add some space, since codecs may print warnings without newline. |
| 686 | SkDebugf("\n\n"); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 687 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 688 | bool failed = print_strings("Invalid files", gInvalidStreams); |
| 689 | failed |= print_strings("Missing codec", gMissingCodecs); |
| 690 | failed |= print_strings("Failed to decode", gDecodeFailures); |
| 691 | failed |= print_strings("Failed to encode", gEncodeFailures); |
| 692 | print_strings("Decoded", gSuccessfulDecodes); |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 693 | print_strings("Missing expectations", gMissingExpectations); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 694 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 695 | if (FLAGS_testSubsetDecoding) { |
| 696 | failed |= print_strings("Failed subset decodes", gFailedSubsetDecodes); |
| 697 | print_strings("Decoded subsets", gSuccessfulSubsetDecodes); |
| scroggo@google.com | e339eb0 | 2013-08-06 18:51:30 +0000 | [diff] [blame] | 698 | print_strings("Missing subset expectations", gMissingSubsetExpectations); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 701 | return failed ? -1 : 0; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 704 | #if !defined SK_BUILD_FOR_IOS |
| 705 | int main(int argc, char * const argv[]) { |
| 706 | return tool_main(argc, (char**) argv); |
| 707 | } |
| 708 | #endif |