SkImageEncoder.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. */
  7. #ifndef SkImageEncoder_DEFINED
  8. #define SkImageEncoder_DEFINED
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkData.h"
  11. #include "include/core/SkEncodedImageFormat.h"
  12. #include "include/core/SkStream.h"
  13. /**
  14. * Encode SkPixmap in the given binary image format.
  15. *
  16. * @param dst results are written to this stream.
  17. * @param src source pixels.
  18. * @param format image format, not all formats are supported.
  19. * @param quality range from 0-100, this is supported by jpeg and webp.
  20. * higher values correspond to improved visual quality, but less compression.
  21. *
  22. * @return false iff input is bad or format is unsupported.
  23. *
  24. * Will always return false if Skia is compiled without image
  25. * encoders.
  26. *
  27. * For SkEncodedImageFormat::kWEBP, if quality is 100, it will use lossless compression. Otherwise
  28. * it will use lossy.
  29. *
  30. * For examples of encoding an image to a file or to a block of memory,
  31. * see tools/ToolUtils.h.
  32. */
  33. SK_API bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
  34. SkEncodedImageFormat format, int quality);
  35. /**
  36. * The following helper function wraps SkEncodeImage().
  37. */
  38. inline bool SkEncodeImage(SkWStream* dst, const SkBitmap& src, SkEncodedImageFormat f, int q) {
  39. SkPixmap pixmap;
  40. return src.peekPixels(&pixmap) && SkEncodeImage(dst, pixmap, f, q);
  41. }
  42. /**
  43. * Encode SkPixmap in the given binary image format.
  44. *
  45. * @param src source pixels.
  46. * @param format image format, not all formats are supported.
  47. * @param quality range from 0-100, this is supported by jpeg and webp.
  48. * higher values correspond to improved visual quality, but less compression.
  49. *
  50. * @return encoded data or nullptr if input is bad or format is unsupported.
  51. *
  52. * Will always return nullptr if Skia is compiled without image
  53. * encoders.
  54. *
  55. * For SkEncodedImageFormat::kWEBP, if quality is 100, it will use lossless compression. Otherwise
  56. * it will use lossy.
  57. */
  58. SK_API sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality);
  59. /**
  60. * Helper that extracts the pixmap from the bitmap, and then calls SkEncodePixmap()
  61. */
  62. SK_API sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality);
  63. #endif // SkImageEncoder_DEFINED