SkImageEncoder.h 2.2 KB

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