get_images_from_skps.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2016 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. #include "include/codec/SkCodec.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkColorSpace.h"
  10. #include "include/core/SkData.h"
  11. #include "include/core/SkPicture.h"
  12. #include "include/core/SkSerialProcs.h"
  13. #include "include/core/SkStream.h"
  14. #include "include/private/SkTHash.h"
  15. #include "src/core/SkMD5.h"
  16. #include "src/core/SkOSFile.h"
  17. #include "src/utils/SkJSONWriter.h"
  18. #include "src/utils/SkOSPath.h"
  19. #include "tools/flags/CommandLineFlags.h"
  20. #include <iostream>
  21. #include <map>
  22. static DEFINE_string2(skps, s, "skps", "A path to a directory of skps or a single skp.");
  23. static DEFINE_string2(out, o, "img-out", "A path to an output directory.");
  24. static DEFINE_bool(testDecode, false,
  25. "Indicates if we want to test that the images decode successfully.");
  26. static DEFINE_bool(writeImages, true,
  27. "Indicates if we want to write out supported/decoded images.");
  28. static DEFINE_bool(writeFailedImages, false,
  29. "Indicates if we want to write out unsupported/failed to decode images.");
  30. static DEFINE_string2(failuresJsonPath, j, "",
  31. "Dump SKP and count of unknown images to the specified JSON file. Will not be "
  32. "written anywhere if empty.");
  33. static int gKnown;
  34. static const char* gOutputDir;
  35. static std::map<std::string, unsigned int> gSkpToUnknownCount = {};
  36. static std::map<std::string, unsigned int> gSkpToUnsupportedCount;
  37. static SkTHashSet<SkMD5::Digest> gSeen;
  38. struct Sniffer {
  39. std::string skpName;
  40. Sniffer(std::string name) {
  41. skpName = name;
  42. }
  43. void sniff(const void* ptr, size_t len) {
  44. SkMD5 md5;
  45. md5.write(ptr, len);
  46. SkMD5::Digest digest = md5.finish();
  47. if (gSeen.contains(digest)) {
  48. return;
  49. }
  50. gSeen.add(digest);
  51. sk_sp<SkData> data(SkData::MakeWithoutCopy(ptr, len));
  52. std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(data);
  53. if (!codec) {
  54. // FIXME: This code is currently unreachable because we create an empty generator when
  55. // we fail to create a codec.
  56. SkDebugf("Codec could not be created for %s\n", skpName.c_str());
  57. gSkpToUnknownCount[skpName]++;
  58. return;
  59. }
  60. SkString ext;
  61. switch (codec->getEncodedFormat()) {
  62. case SkEncodedImageFormat::kBMP: ext = "bmp"; break;
  63. case SkEncodedImageFormat::kGIF: ext = "gif"; break;
  64. case SkEncodedImageFormat::kICO: ext = "ico"; break;
  65. case SkEncodedImageFormat::kJPEG: ext = "jpg"; break;
  66. case SkEncodedImageFormat::kPNG: ext = "png"; break;
  67. case SkEncodedImageFormat::kDNG: ext = "dng"; break;
  68. case SkEncodedImageFormat::kWBMP: ext = "wbmp"; break;
  69. case SkEncodedImageFormat::kWEBP: ext = "webp"; break;
  70. default:
  71. // This should be unreachable because we cannot create a codec if we do not know
  72. // the image type.
  73. SkASSERT(false);
  74. }
  75. auto writeImage = [&] (const char* name, int num) {
  76. SkString path;
  77. path.appendf("%s/%s%d.%s", gOutputDir, name, num, ext.c_str());
  78. SkFILEWStream file(path.c_str());
  79. file.write(ptr, len);
  80. SkDebugf("%s\n", path.c_str());
  81. };
  82. if (FLAGS_testDecode) {
  83. SkBitmap bitmap;
  84. SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
  85. bitmap.allocPixels(info);
  86. const SkCodec::Result result = codec->getPixels(
  87. info, bitmap.getPixels(), bitmap.rowBytes());
  88. switch (result) {
  89. case SkCodec::kSuccess:
  90. case SkCodec::kIncompleteInput:
  91. case SkCodec::kErrorInInput:
  92. break;
  93. default:
  94. SkDebugf("Decoding failed for %s\n", skpName.c_str());
  95. if (FLAGS_writeFailedImages) {
  96. writeImage("unknown", gSkpToUnknownCount[skpName]);
  97. }
  98. gSkpToUnknownCount[skpName]++;
  99. return;
  100. }
  101. }
  102. if (FLAGS_writeImages) {
  103. writeImage("", gKnown);
  104. }
  105. gKnown++;
  106. }
  107. };
  108. static bool get_images_from_file(const SkString& file) {
  109. Sniffer sniff(file.c_str());
  110. auto stream = SkStream::MakeFromFile(file.c_str());
  111. SkDeserialProcs procs;
  112. procs.fImageProc = [](const void* data, size_t size, void* ctx) -> sk_sp<SkImage> {
  113. ((Sniffer*)ctx)->sniff(data, size);
  114. return nullptr;
  115. };
  116. procs.fImageCtx = &sniff;
  117. return SkPicture::MakeFromStream(stream.get(), &procs) != nullptr;
  118. }
  119. int main(int argc, char** argv) {
  120. CommandLineFlags::SetUsage(
  121. "Usage: get_images_from_skps -s <dir of skps> -o <dir for output images> --testDecode "
  122. "-j <output JSON path> --writeImages, --writeFailedImages\n");
  123. CommandLineFlags::Parse(argc, argv);
  124. const char* inputs = FLAGS_skps[0];
  125. gOutputDir = FLAGS_out[0];
  126. if (!sk_isdir(gOutputDir)) {
  127. CommandLineFlags::PrintUsage();
  128. return 1;
  129. }
  130. if (sk_isdir(inputs)) {
  131. SkOSFile::Iter iter(inputs, "skp");
  132. for (SkString file; iter.next(&file); ) {
  133. if (!get_images_from_file(SkOSPath::Join(inputs, file.c_str()))) {
  134. return 2;
  135. }
  136. }
  137. } else {
  138. if (!get_images_from_file(SkString(inputs))) {
  139. return 2;
  140. }
  141. }
  142. /**
  143. JSON results are written out in the following format:
  144. {
  145. "failures": {
  146. "skp1": 12,
  147. "skp4": 2,
  148. ...
  149. },
  150. "unsupported": {
  151. "skp9": 13,
  152. "skp17": 3,
  153. ...
  154. }
  155. "totalFailures": 32,
  156. "totalUnsupported": 9,
  157. "totalSuccesses": 21,
  158. }
  159. */
  160. unsigned int totalFailures = 0,
  161. totalUnsupported = 0;
  162. SkDynamicMemoryWStream memStream;
  163. SkJSONWriter writer(&memStream, SkJSONWriter::Mode::kPretty);
  164. writer.beginObject();
  165. {
  166. writer.beginObject("failures");
  167. {
  168. for(const auto& failure : gSkpToUnknownCount) {
  169. SkDebugf("%s %d\n", failure.first.c_str(), failure.second);
  170. totalFailures += failure.second;
  171. writer.appendU32(failure.first.c_str(), failure.second);
  172. }
  173. }
  174. writer.endObject();
  175. writer.appendU32("totalFailures", totalFailures);
  176. #ifdef SK_DEBUG
  177. writer.beginObject("unsupported");
  178. {
  179. for (const auto& unsupported : gSkpToUnsupportedCount) {
  180. SkDebugf("%s %d\n", unsupported.first.c_str(), unsupported.second);
  181. totalUnsupported += unsupported.second;
  182. writer.appendHexU32(unsupported.first.c_str(), unsupported.second);
  183. }
  184. }
  185. writer.endObject();
  186. writer.appendU32("totalUnsupported", totalUnsupported);
  187. #endif
  188. writer.appendS32("totalSuccesses", gKnown);
  189. SkDebugf("%d known, %d failures, %d unsupported\n",
  190. gKnown, totalFailures, totalUnsupported);
  191. }
  192. writer.endObject();
  193. writer.flush();
  194. if (totalFailures > 0 || totalUnsupported > 0) {
  195. if (!FLAGS_failuresJsonPath.isEmpty()) {
  196. SkDebugf("Writing failures to %s\n", FLAGS_failuresJsonPath[0]);
  197. SkFILEWStream stream(FLAGS_failuresJsonPath[0]);
  198. auto jsonStream = memStream.detachAsStream();
  199. stream.writeStream(jsonStream.get(), jsonStream->getLength());
  200. }
  201. }
  202. return 0;
  203. }