| 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 | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 10 | #include "SkBitmapHasher.h" |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 12 | #include "SkCommandLineFlags.h" |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 13 | #include "SkData.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 | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 23 | DEFINE_string(createExpectationsPath, "", "Path to write JSON expectations."); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 24 | 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] | 25 | DEFINE_string(readExpectationsPath, "", "Path to read JSON expectations from."); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 26 | DEFINE_string2(writePath, w, "", "Write rendered images into this directory."); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 27 | DEFINE_bool(reencode, true, "Reencode the images to test encoding."); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 28 | DEFINE_bool(testSubsetDecoding, true, "Test decoding subsets of images."); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 29 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 30 | struct Format { |
| 31 | SkImageEncoder::Type fType; |
| 32 | SkImageDecoder::Format fFormat; |
| 33 | const char* fSuffix; |
| 34 | }; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 35 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 36 | static const Format gFormats[] = { |
| 37 | { SkImageEncoder::kBMP_Type, SkImageDecoder::kBMP_Format, ".bmp" }, |
| 38 | { SkImageEncoder::kGIF_Type, SkImageDecoder::kGIF_Format, ".gif" }, |
| 39 | { SkImageEncoder::kICO_Type, SkImageDecoder::kICO_Format, ".ico" }, |
| 40 | { SkImageEncoder::kJPEG_Type, SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 41 | { SkImageEncoder::kPNG_Type, SkImageDecoder::kPNG_Format, ".png" }, |
| 42 | { SkImageEncoder::kWBMP_Type, SkImageDecoder::kWBMP_Format, ".wbmp" }, |
| 43 | { SkImageEncoder::kWEBP_Type, SkImageDecoder::kWEBP_Format, ".webp" } |
| 44 | }; |
| 45 | |
| 46 | static SkImageEncoder::Type format_to_type(SkImageDecoder::Format format) { |
| 47 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 48 | if (gFormats[i].fFormat == format) { |
| 49 | return gFormats[i].fType; |
| 50 | } |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 51 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 52 | return SkImageEncoder::kUnknown_Type; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 55 | static const char* suffix_for_type(SkImageEncoder::Type type) { |
| 56 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 57 | if (gFormats[i].fType == type) { |
| 58 | return gFormats[i].fSuffix; |
| 59 | } |
| 60 | } |
| 61 | return ""; |
| 62 | } |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 63 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 64 | static SkImageDecoder::Format guess_format_from_suffix(const char suffix[]) { |
| 65 | for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { |
| 66 | if (strcmp(suffix, gFormats[i].fSuffix) == 0) { |
| 67 | return gFormats[i].fFormat; |
| 68 | } |
| 69 | } |
| 70 | return SkImageDecoder::kUnknown_Format; |
| 71 | } |
| 72 | |
| 73 | static void make_outname(SkString* dst, const char outDir[], const char src[], |
| 74 | const char suffix[]) { |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 75 | dst->set(outDir); |
| 76 | const char* start = strrchr(src, '/'); |
| 77 | if (start) { |
| 78 | start += 1; // skip the actual last '/' |
| 79 | } else { |
| 80 | start = src; |
| 81 | } |
| 82 | dst->append(start); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 83 | if (!dst->endsWith(suffix)) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 84 | const char* cstyleDst = dst->c_str(); |
| 85 | const char* dot = strrchr(cstyleDst, '.'); |
| 86 | if (dot != NULL) { |
| 87 | int32_t index = SkToS32(dot - cstyleDst); |
| 88 | dst->remove(index, dst->size() - index); |
| 89 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 90 | dst->append(suffix); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 94 | // Store the names of the filenames to report later which ones failed, succeeded, and were |
| 95 | // invalid. |
| 96 | static SkTArray<SkString, false> gInvalidStreams; |
| 97 | static SkTArray<SkString, false> gMissingCodecs; |
| 98 | static SkTArray<SkString, false> gDecodeFailures; |
| 99 | static SkTArray<SkString, false> gEncodeFailures; |
| 100 | static SkTArray<SkString, false> gSuccessfulDecodes; |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 101 | static SkTArray<SkString, false> gSuccessfulSubsetDecodes; |
| 102 | static SkTArray<SkString, false> gFailedSubsetDecodes; |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 103 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 104 | // Expections read from a file specified by readExpectationsPath. The expectations must have been |
| 105 | // previously written using createExpectationsPath. |
| 106 | SkAutoTUnref<skiagm::JsonExpectationsSource> gJsonExpectations; |
| 107 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 108 | static bool write_bitmap(const char outName[], SkBitmap* bm) { |
| 109 | SkBitmap bitmap8888; |
| 110 | if (SkBitmap::kARGB_8888_Config != bm->config()) { |
| 111 | if (!bm->copyTo(&bitmap8888, SkBitmap::kARGB_8888_Config)) { |
| 112 | return false; |
| 113 | } |
| 114 | bm = &bitmap8888; |
| 115 | } |
| 116 | // FIXME: This forces all pixels to be opaque, like the many implementations |
| 117 | // of force_all_opaque. These should be unified if they cannot be eliminated. |
| 118 | SkAutoLockPixels lock(*bm); |
| 119 | for (int y = 0; y < bm->height(); y++) { |
| 120 | for (int x = 0; x < bm->width(); x++) { |
| 121 | *bm->getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 122 | } |
| 123 | } |
| 124 | return SkImageEncoder::EncodeFile(outName, *bm, SkImageEncoder::kPNG_Type, 100); |
| 125 | } |
| 126 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Return a random SkIRect inside the range specified. |
| 129 | * @param rand Random number generator. |
| 130 | * @param maxX Exclusive maximum x-coordinate. SkIRect's fLeft and fRight will be |
| 131 | * in the range [0, maxX) |
| 132 | * @param maxY Exclusive maximum y-coordinate. SkIRect's fTop and fBottom will be |
| 133 | * in the range [0, maxY) |
| 134 | * @return SkIRect Non-empty, non-degenerate rectangle. |
| 135 | */ |
| 136 | static SkIRect generate_random_rect(SkRandom* rand, int32_t maxX, int32_t maxY) { |
| 137 | SkASSERT(maxX > 1 && maxY > 1); |
| 138 | int32_t left = rand->nextULessThan(maxX); |
| 139 | int32_t right = rand->nextULessThan(maxX); |
| 140 | int32_t top = rand->nextULessThan(maxY); |
| 141 | int32_t bottom = rand->nextULessThan(maxY); |
| 142 | SkIRect rect = SkIRect::MakeLTRB(left, top, right, bottom); |
| 143 | rect.sort(); |
| 144 | // Make sure rect is not empty. |
| 145 | if (rect.fLeft == rect.fRight) { |
| 146 | if (rect.fLeft > 0) { |
| 147 | rect.fLeft--; |
| 148 | } else { |
| 149 | rect.fRight++; |
| 150 | // This branch is only taken if 0 == rect.fRight, and |
| 151 | // maxX must be at least 2, so it must still be in |
| 152 | // range. |
| 153 | SkASSERT(rect.fRight < maxX); |
| 154 | } |
| 155 | } |
| 156 | if (rect.fTop == rect.fBottom) { |
| 157 | if (rect.fTop > 0) { |
| 158 | rect.fTop--; |
| 159 | } else { |
| 160 | rect.fBottom++; |
| 161 | // Again, this must be in range. |
| 162 | SkASSERT(rect.fBottom < maxY); |
| 163 | } |
| 164 | } |
| 165 | return rect; |
| 166 | } |
| 167 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 168 | // Stored expectations to be written to a file if createExpectationsPath is specified. |
| 169 | static Json::Value gExpectationsToWrite; |
| 170 | |
| 171 | /** |
| 172 | * If expectations are to be recorded, record the expected checksum of bitmap into global |
| 173 | * expectations array. |
| 174 | */ |
| 175 | static void write_expectations(const SkBitmap& bitmap, const char* filename) { |
| 176 | if (!FLAGS_createExpectationsPath.isEmpty()) { |
| 177 | // Creates an Expectations object, and add it to the list to write. |
| 178 | skiagm::Expectations expectation(bitmap); |
| 179 | Json::Value value = expectation.asJsonValue(); |
| 180 | gExpectationsToWrite[filename] = value; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Return the name of the file, ignoring the directory structure. |
| 186 | * Does not create a new string. |
| 187 | * @param fullPath Full path to the file. |
| 188 | * @return string The basename of the file - anything beyond the final slash, or the full name |
| 189 | * if there is no slash. |
| 190 | * TODO: Might this be useful as a utility function in SkOSFile? Would it be more appropriate to |
| 191 | * create a new string? |
| 192 | */ |
| 193 | static const char* SkBasename(const char* fullPath) { |
| 194 | const char* filename = strrchr(fullPath, SkPATH_SEPARATOR); |
| 195 | if (NULL == filename || ++filename == '\0') { |
| 196 | filename = fullPath; |
| 197 | } |
| 198 | return filename; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Compare against an expectation for this filename, if there is one. |
| 203 | * @param bitmap SkBitmap to compare to the expected value. |
| 204 | * @param filename String used to find the expected value. |
| scroggo@google.com | 6ca30ca | 2013-05-14 17:30:17 +0000 | [diff] [blame^] | 205 | * @return bool True in any of these cases: |
| 206 | * - the bitmap matches the expectation. |
| 207 | * - there is no expectations file. |
| 208 | * False in any of these cases: |
| 209 | * - there is an expectations file, but no expectation for this bitmap. |
| 210 | * - there is an expectation for this bitmap, but it did not match. |
| 211 | * - expectation could not be computed from the bitmap. |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 212 | */ |
| 213 | static bool compare_to_expectations_if_necessary(const SkBitmap& bitmap, const char* filename, |
| 214 | SkTArray<SkString, false>* failureArray) { |
| 215 | if (NULL == gJsonExpectations.get()) { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | skiagm::Expectations jsExpectation = gJsonExpectations->get(filename); |
| 220 | if (jsExpectation.empty()) { |
| scroggo@google.com | 6ca30ca | 2013-05-14 17:30:17 +0000 | [diff] [blame^] | 221 | if (failureArray != NULL) { |
| 222 | failureArray->push_back().printf("decoded %s, but could not find expectation.", |
| 223 | filename); |
| 224 | } |
| 225 | return false; |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | SkHashDigest checksum; |
| 229 | if (!SkBitmapHasher::ComputeDigest(bitmap, &checksum)) { |
| 230 | if (failureArray != NULL) { |
| 231 | failureArray->push_back().printf("decoded %s, but could not create a checksum.", |
| 232 | filename); |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | if (jsExpectation.match(checksum)) { |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | if (failureArray != NULL) { |
| 242 | failureArray->push_back().printf("decoded %s, but the result does not match " |
| 243 | "expectations.", |
| 244 | filename); |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 249 | static void decodeFileAndWrite(const char srcPath[], const SkString* writePath) { |
| 250 | SkBitmap bitmap; |
| 251 | SkFILEStream stream(srcPath); |
| 252 | if (!stream.isValid()) { |
| 253 | gInvalidStreams.push_back().set(srcPath); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 258 | if (NULL == codec) { |
| 259 | gMissingCodecs.push_back().set(srcPath); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | SkAutoTDelete<SkImageDecoder> ad(codec); |
| 264 | |
| 265 | stream.rewind(); |
| 266 | if (!codec->decode(&stream, &bitmap, SkBitmap::kARGB_8888_Config, |
| 267 | SkImageDecoder::kDecodePixels_Mode)) { |
| 268 | gDecodeFailures.push_back().set(srcPath); |
| 269 | return; |
| 270 | } |
| 271 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 272 | // Create a string representing just the filename itself, for use in json expectations. |
| 273 | const char* filename = SkBasename(srcPath); |
| 274 | |
| 275 | if (compare_to_expectations_if_necessary(bitmap, filename, &gDecodeFailures)) { |
| 276 | gSuccessfulDecodes.push_back().printf("%s [%d %d]", srcPath, bitmap.width(), |
| 277 | bitmap.height()); |
| 278 | } |
| 279 | |
| 280 | write_expectations(bitmap, filename); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 281 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 282 | if (FLAGS_testSubsetDecoding) { |
| scroggo@google.com | 0018f75 | 2013-05-03 20:39:22 +0000 | [diff] [blame] | 283 | SkDEBUGCODE(bool couldRewind =) stream.rewind(); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 284 | SkASSERT(couldRewind); |
| 285 | int width, height; |
| 286 | // Build the tile index for decoding subsets. If the image is 1x1, skip subset |
| 287 | // decoding since there are no smaller subsets. |
| 288 | if (codec->buildTileIndex(&stream, &width, &height) && width > 1 && height > 1) { |
| 289 | SkASSERT(bitmap.width() == width && bitmap.height() == height); |
| 290 | // Call decodeSubset multiple times: |
| 291 | SkRandom rand(0); |
| 292 | for (int i = 0; i < 5; i++) { |
| 293 | SkBitmap bitmapFromDecodeSubset; |
| 294 | // FIXME: Come up with a more representative set of rectangles. |
| 295 | SkIRect rect = generate_random_rect(&rand, width, height); |
| 296 | SkString subsetDim = SkStringPrintf("[%d,%d,%d,%d]", rect.fLeft, rect.fTop, |
| 297 | rect.fRight, rect.fBottom); |
| 298 | if (codec->decodeSubset(&bitmapFromDecodeSubset, rect, SkBitmap::kNo_Config)) { |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 299 | SkString subsetName = SkStringPrintf("%s_%s", filename, subsetDim.c_str()); |
| 300 | if (compare_to_expectations_if_necessary(bitmapFromDecodeSubset, |
| 301 | subsetName.c_str(), |
| 302 | &gFailedSubsetDecodes)) { |
| 303 | gSuccessfulSubsetDecodes.push_back().printf("Decoded subset %s from %s", |
| 304 | subsetDim.c_str(), srcPath); |
| 305 | } |
| 306 | |
| 307 | write_expectations(bitmapFromDecodeSubset, subsetName.c_str()); |
| 308 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 309 | if (writePath != NULL) { |
| 310 | // Write the region to a file whose name includes the dimensions. |
| 311 | SkString suffix = SkStringPrintf("_%s.png", subsetDim.c_str()); |
| 312 | SkString outPath; |
| 313 | make_outname(&outPath, writePath->c_str(), srcPath, suffix.c_str()); |
| scroggo@google.com | 8b5ff5c | 2013-05-03 20:21:17 +0000 | [diff] [blame] | 314 | SkDEBUGCODE(bool success =) |
| 315 | write_bitmap(outPath.c_str(), &bitmapFromDecodeSubset); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 316 | SkASSERT(success); |
| 317 | gSuccessfulSubsetDecodes.push_back().printf("\twrote %s", outPath.c_str()); |
| 318 | // Also use extractSubset from the original for visual comparison. |
| 319 | SkBitmap extractedSubset; |
| 320 | if (bitmap.extractSubset(&extractedSubset, rect)) { |
| 321 | suffix.printf("_%s_extracted.png", subsetDim.c_str()); |
| 322 | make_outname(&outPath, writePath->c_str(), srcPath, suffix.c_str()); |
| scroggo@google.com | 0018f75 | 2013-05-03 20:39:22 +0000 | [diff] [blame] | 323 | SkDEBUGCODE(success =) write_bitmap(outPath.c_str(), &extractedSubset); |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 324 | SkASSERT(success); |
| 325 | } |
| 326 | } |
| 327 | } else { |
| 328 | gFailedSubsetDecodes.push_back().printf("Failed to decode region %s from %s\n", |
| 329 | subsetDim.c_str(), srcPath); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 334 | if (FLAGS_reencode) { |
| 335 | // Encode to the format the file was originally in, or PNG if the encoder for the same |
| 336 | // format is unavailable. |
| 337 | SkImageDecoder::Format format = codec->getFormat(); |
| 338 | if (SkImageDecoder::kUnknown_Format == format) { |
| 339 | if (stream.rewind()) { |
| 340 | format = SkImageDecoder::GetStreamFormat(&stream); |
| 341 | } |
| 342 | if (SkImageDecoder::kUnknown_Format == format) { |
| 343 | const char* dot = strrchr(srcPath, '.'); |
| 344 | if (NULL != dot) { |
| 345 | format = guess_format_from_suffix(dot); |
| 346 | } |
| 347 | if (SkImageDecoder::kUnknown_Format == format) { |
| 348 | SkDebugf("Could not determine type for '%s'\n", srcPath); |
| 349 | format = SkImageDecoder::kPNG_Format; |
| 350 | } |
| 351 | |
| 352 | } |
| 353 | } else { |
| 354 | SkASSERT(!stream.rewind() || SkImageDecoder::GetStreamFormat(&stream) == format); |
| 355 | } |
| 356 | SkImageEncoder::Type type = format_to_type(format); |
| 357 | // format should never be kUnknown_Format, so type should never be kUnknown_Type. |
| 358 | SkASSERT(type != SkImageEncoder::kUnknown_Type); |
| 359 | |
| 360 | SkImageEncoder* encoder = SkImageEncoder::Create(type); |
| 361 | if (NULL == encoder) { |
| 362 | type = SkImageEncoder::kPNG_Type; |
| 363 | encoder = SkImageEncoder::Create(type); |
| 364 | SkASSERT(encoder); |
| 365 | } |
| 366 | SkAutoTDelete<SkImageEncoder> ade(encoder); |
| 367 | // Encode to a stream. |
| 368 | SkDynamicMemoryWStream wStream; |
| 369 | if (!encoder->encodeStream(&wStream, bitmap, 100)) { |
| 370 | gEncodeFailures.push_back().printf("Failed to reencode %s to type '%s'", srcPath, |
| 371 | suffix_for_type(type)); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | SkAutoTUnref<SkData> data(wStream.copyToData()); |
| 376 | if (writePath != NULL && type != SkImageEncoder::kPNG_Type) { |
| 377 | // Write the encoded data to a file. Do not write to PNG, which will be written later, |
| 378 | // regardless of the input format. |
| 379 | SkString outPath; |
| 380 | make_outname(&outPath, writePath->c_str(), srcPath, suffix_for_type(type)); |
| 381 | SkFILEWStream file(outPath.c_str()); |
| 382 | if(file.write(data->data(), data->size())) { |
| 383 | gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str()); |
| 384 | } else { |
| 385 | gEncodeFailures.push_back().printf("Failed to write %s", outPath.c_str()); |
| 386 | } |
| 387 | } |
| 388 | // Ensure that the reencoded data can still be decoded. |
| 389 | SkMemoryStream memStream(data); |
| 390 | SkBitmap redecodedBitmap; |
| 391 | SkImageDecoder::Format formatOnSecondDecode; |
| 392 | if (SkImageDecoder::DecodeStream(&memStream, &redecodedBitmap, SkBitmap::kNo_Config, |
| 393 | SkImageDecoder::kDecodePixels_Mode, |
| 394 | &formatOnSecondDecode)) { |
| 395 | SkASSERT(format_to_type(formatOnSecondDecode) == type); |
| 396 | } else { |
| 397 | gDecodeFailures.push_back().printf("Failed to redecode %s after reencoding to '%s'", |
| 398 | srcPath, suffix_for_type(type)); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (writePath != NULL) { |
| 403 | SkString outPath; |
| 404 | make_outname(&outPath, writePath->c_str(), srcPath, ".png"); |
| 405 | if (write_bitmap(outPath.c_str(), &bitmap)) { |
| 406 | gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str()); |
| 407 | } else { |
| 408 | gEncodeFailures.push_back().set(outPath); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /////////////////////////////////////////////////////////////////////////////// |
| 414 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 415 | // If strings is not empty, print title, followed by each string on its own line starting |
| 416 | // with a tab. |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 417 | // @return bool True if strings had at least one entry. |
| 418 | 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] | 419 | if (strings.count() > 0) { |
| 420 | SkDebugf("%s:\n", title); |
| 421 | for (int i = 0; i < strings.count(); i++) { |
| 422 | SkDebugf("\t%s\n", strings[i].c_str()); |
| 423 | } |
| 424 | SkDebugf("\n"); |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 425 | return true; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 426 | } |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 427 | return false; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 430 | /** |
| 431 | * If directory is non null and does not end with a path separator, append one. |
| 432 | * @param directory SkString representing the path to a directory. If the last character is not a |
| 433 | * path separator (specific to the current OS), append one. |
| 434 | */ |
| 435 | static void append_path_separator_if_necessary(SkString* directory) { |
| 436 | if (directory != NULL && directory->c_str()[directory->size() - 1] != SkPATH_SEPARATOR) { |
| 437 | directory->appendf("%c", SkPATH_SEPARATOR); |
| 438 | } |
| 439 | } |
| 440 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 441 | int tool_main(int argc, char** argv); |
| 442 | int tool_main(int argc, char** argv) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 443 | SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); |
| 444 | SkCommandLineFlags::Parse(argc, argv); |
| 445 | |
| 446 | if (FLAGS_readPath.count() < 1) { |
| 447 | SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 452 | SkAutoGraphics ag; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 453 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 454 | if (!FLAGS_readExpectationsPath.isEmpty()) { |
| 455 | gJsonExpectations.reset(SkNEW_ARGS(skiagm::JsonExpectationsSource, |
| 456 | (FLAGS_readExpectationsPath[0]))); |
| 457 | } |
| 458 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 459 | SkString outDir; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 460 | SkString* outDirPtr; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 461 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 462 | if (FLAGS_writePath.count() == 1) { |
| 463 | outDir.set(FLAGS_writePath[0]); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 464 | append_path_separator_if_necessary(&outDir); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 465 | outDirPtr = &outDir; |
| 466 | } else { |
| 467 | outDirPtr = NULL; |
| 468 | } |
| 469 | |
| 470 | for (int i = 0; i < FLAGS_readPath.count(); i++) { |
| 471 | if (strlen(FLAGS_readPath[i]) < 1) { |
| 472 | break; |
| 473 | } |
| 474 | SkOSFile::Iter iter(FLAGS_readPath[i]); |
| 475 | SkString filename; |
| 476 | if (iter.next(&filename)) { |
| 477 | SkString directory(FLAGS_readPath[i]); |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 478 | append_path_separator_if_necessary(&directory); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 479 | do { |
| 480 | SkString fullname(directory); |
| 481 | fullname.append(filename); |
| 482 | decodeFileAndWrite(fullname.c_str(), outDirPtr); |
| 483 | } while (iter.next(&filename)); |
| 484 | } else { |
| 485 | decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| scroggo@google.com | 6843bdb | 2013-05-08 19:14:23 +0000 | [diff] [blame] | 489 | if (!FLAGS_createExpectationsPath.isEmpty()) { |
| 490 | // Use an empty value for everything besides expectations, since the reader only cares |
| 491 | // about the expectations. |
| 492 | Json::Value nullValue; |
| 493 | Json::Value root = skiagm::CreateJsonTree(gExpectationsToWrite, nullValue, nullValue, |
| 494 | nullValue, nullValue); |
| 495 | std::string jsonStdString = root.toStyledString(); |
| 496 | SkString path = SkStringPrintf("%s%cresults.json", FLAGS_createExpectationsPath[0], |
| 497 | SkPATH_SEPARATOR); |
| 498 | SkFILEWStream stream(path.c_str()); |
| 499 | stream.write(jsonStdString.c_str(), jsonStdString.length()); |
| 500 | } |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 501 | // Add some space, since codecs may print warnings without newline. |
| 502 | SkDebugf("\n\n"); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 503 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 504 | bool failed = print_strings("Invalid files", gInvalidStreams); |
| 505 | failed |= print_strings("Missing codec", gMissingCodecs); |
| 506 | failed |= print_strings("Failed to decode", gDecodeFailures); |
| 507 | failed |= print_strings("Failed to encode", gEncodeFailures); |
| 508 | print_strings("Decoded", gSuccessfulDecodes); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 509 | |
| scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 510 | if (FLAGS_testSubsetDecoding) { |
| 511 | failed |= print_strings("Failed subset decodes", gFailedSubsetDecodes); |
| 512 | print_strings("Decoded subsets", gSuccessfulSubsetDecodes); |
| 513 | } |
| 514 | |
| scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 515 | return failed ? -1 : 0; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 518 | #if !defined SK_BUILD_FOR_IOS |
| 519 | int main(int argc, char * const argv[]) { |
| 520 | return tool_main(argc, (char**) argv); |
| 521 | } |
| 522 | #endif |