diff options
Diffstat (limited to 'tools/sk_tool_utils.h')
-rw-r--r-- | tools/sk_tool_utils.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tools/sk_tool_utils.h b/tools/sk_tool_utils.h index c6abbcf7c2..526f0ef505 100644 --- a/tools/sk_tool_utils.h +++ b/tools/sk_tool_utils.h @@ -225,6 +225,31 @@ namespace sk_tool_utils { SkTDArray<TopoTestNode*> fDependencies; }; + template <typename T> + inline bool EncodeImageToFile(const char* path, const T& src, SkEncodedImageFormat f, int q) { + SkFILEWStream file(path); + return file.isValid() && SkEncodeImage(&file, src, f, q); + } + + template <typename T> + inline sk_sp<SkData> EncodeImageToData(const T& src, SkEncodedImageFormat f, int q) { + SkDynamicMemoryWStream buf; + return SkEncodeImage(&buf, src , f, q) ? buf.detachAsData() : nullptr; + } + + /** + * Uses SkEncodeImage to serialize images that are not already + * encoded as SkEncodedImageFormat::kPNG images. + */ + inline sk_sp<SkPixelSerializer> MakePixelSerializer() { + struct EncodeImagePixelSerializer final : SkPixelSerializer { + bool onUseEncodedData(const void*, size_t) override { return true; } + SkData* onEncode(const SkPixmap& pmap) override { + return EncodeImageToData(pmap, SkEncodedImageFormat::kPNG, 100).release(); + } + }; + return sk_make_sp<EncodeImagePixelSerializer>(); + } } // namespace sk_tool_utils #endif // sk_tool_utils_DEFINED |