SkPngEncoder.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2017 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. #ifndef SkPngEncoder_DEFINED
  8. #define SkPngEncoder_DEFINED
  9. #include "include/core/SkDataTable.h"
  10. #include "include/encode/SkEncoder.h"
  11. class SkPngEncoderMgr;
  12. class SkWStream;
  13. class SK_API SkPngEncoder : public SkEncoder {
  14. public:
  15. enum class FilterFlag : int {
  16. kZero = 0x00,
  17. kNone = 0x08,
  18. kSub = 0x10,
  19. kUp = 0x20,
  20. kAvg = 0x40,
  21. kPaeth = 0x80,
  22. kAll = kNone | kSub | kUp | kAvg | kPaeth,
  23. };
  24. struct Options {
  25. /**
  26. * Selects which filtering strategies to use.
  27. *
  28. * If a single filter is chosen, libpng will use that filter for every row.
  29. *
  30. * If multiple filters are chosen, libpng will use a heuristic to guess which filter
  31. * will encode smallest, then apply that filter. This happens on a per row basis,
  32. * different rows can use different filters.
  33. *
  34. * Using a single filter (or less filters) is typically faster. Trying all of the
  35. * filters may help minimize the output file size.
  36. *
  37. * Our default value matches libpng's default.
  38. */
  39. FilterFlag fFilterFlags = FilterFlag::kAll;
  40. /**
  41. * Must be in [0, 9] where 9 corresponds to maximal compression. This value is passed
  42. * directly to zlib. 0 is a special case to skip zlib entirely, creating dramatically
  43. * larger pngs.
  44. *
  45. * Our default value matches libpng's default.
  46. */
  47. int fZLibLevel = 6;
  48. /**
  49. * Represents comments in the tEXt ancillary chunk of the png.
  50. * The 2i-th entry is the keyword for the i-th comment,
  51. * and the (2i + 1)-th entry is the text for the i-th comment.
  52. */
  53. sk_sp<SkDataTable> fComments;
  54. };
  55. /**
  56. * Encode the |src| pixels to the |dst| stream.
  57. * |options| may be used to control the encoding behavior.
  58. *
  59. * Returns true on success. Returns false on an invalid or unsupported |src|.
  60. */
  61. static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
  62. /**
  63. * Create a png encoder that will encode the |src| pixels to the |dst| stream.
  64. * |options| may be used to control the encoding behavior.
  65. *
  66. * |dst| is unowned but must remain valid for the lifetime of the object.
  67. *
  68. * This returns nullptr on an invalid or unsupported |src|.
  69. */
  70. static std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src,
  71. const Options& options);
  72. ~SkPngEncoder() override;
  73. protected:
  74. bool onEncodeRows(int numRows) override;
  75. SkPngEncoder(std::unique_ptr<SkPngEncoderMgr>, const SkPixmap& src);
  76. std::unique_ptr<SkPngEncoderMgr> fEncoderMgr;
  77. typedef SkEncoder INHERITED;
  78. };
  79. static inline SkPngEncoder::FilterFlag operator|(SkPngEncoder::FilterFlag x,
  80. SkPngEncoder::FilterFlag y) {
  81. return (SkPngEncoder::FilterFlag)((int)x | (int)y);
  82. }
  83. #endif