SkJpegEncoder.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 SkJpegEncoder_DEFINED
  8. #define SkJpegEncoder_DEFINED
  9. #include "include/encode/SkEncoder.h"
  10. class SkJpegEncoderMgr;
  11. class SkWStream;
  12. class SK_API SkJpegEncoder : public SkEncoder {
  13. public:
  14. enum class AlphaOption {
  15. kIgnore,
  16. kBlendOnBlack,
  17. };
  18. enum class Downsample {
  19. /**
  20. * Reduction by a factor of two in both the horizontal and vertical directions.
  21. */
  22. k420,
  23. /**
  24. * Reduction by a factor of two in the horizontal direction.
  25. */
  26. k422,
  27. /**
  28. * No downsampling.
  29. */
  30. k444,
  31. };
  32. struct Options {
  33. /**
  34. * |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality.
  35. */
  36. int fQuality = 100;
  37. /**
  38. * Choose the downsampling factor for the U and V components. This is only
  39. * meaningful if the |src| is not kGray, since kGray will not be encoded as YUV.
  40. *
  41. * Our default value matches the libjpeg-turbo default.
  42. */
  43. Downsample fDownsample = Downsample::k420;
  44. /**
  45. * Jpegs must be opaque. This instructs the encoder on how to handle input
  46. * images with alpha.
  47. *
  48. * The default is to ignore the alpha channel and treat the image as opaque.
  49. * Another option is to blend the pixels onto a black background before encoding.
  50. * In the second case, the encoder supports linear or legacy blending.
  51. */
  52. AlphaOption fAlphaOption = AlphaOption::kIgnore;
  53. };
  54. /**
  55. * Encode the |src| pixels to the |dst| stream.
  56. * |options| may be used to control the encoding behavior.
  57. *
  58. * Returns true on success. Returns false on an invalid or unsupported |src|.
  59. */
  60. static bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
  61. /**
  62. * Create a jpeg encoder that will encode the |src| pixels to the |dst| stream.
  63. * |options| may be used to control the encoding behavior.
  64. *
  65. * |dst| is unowned but must remain valid for the lifetime of the object.
  66. *
  67. * This returns nullptr on an invalid or unsupported |src|.
  68. */
  69. static std::unique_ptr<SkEncoder> Make(SkWStream* dst, const SkPixmap& src,
  70. const Options& options);
  71. ~SkJpegEncoder() override;
  72. protected:
  73. bool onEncodeRows(int numRows) override;
  74. private:
  75. SkJpegEncoder(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src);
  76. std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
  77. typedef SkEncoder INHERITED;
  78. };
  79. #endif