SkPixmapPriv.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2015 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 SkPixmapPriv_DEFINED
  8. #define SkPixmapPriv_DEFINED
  9. #include "include/codec/SkEncodedOrigin.h"
  10. #include "include/core/SkPixmap.h"
  11. #include "src/core/SkAutoPixmapStorage.h"
  12. class SkPixmapPriv {
  13. public:
  14. /**
  15. * Copy the pixels in this pixmap into dst, applying the orientation transformations specified
  16. * by the flags. If the inputs are invalid, this returns false and no copy is made.
  17. */
  18. static bool Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin);
  19. static bool ShouldSwapWidthHeight(SkEncodedOrigin o);
  20. static SkImageInfo SwapWidthHeight(const SkImageInfo& info);
  21. /**
  22. * Decode an image and then copy into dst, applying origin.
  23. *
  24. * @param dst SkPixmap to write the final image, after
  25. * applying the origin.
  26. * @param origin SkEncodedOrigin to apply to the raw pixels.
  27. * @param decode Function for decoding into a pixmap without
  28. * applying the origin.
  29. */
  30. static bool Orient(const SkPixmap& dst, SkEncodedOrigin origin,
  31. std::function<bool(const SkPixmap&)> decode) {
  32. SkAutoPixmapStorage storage;
  33. const SkPixmap* tmp = &dst;
  34. if (origin != kTopLeft_SkEncodedOrigin) {
  35. auto info = dst.info();
  36. if (ShouldSwapWidthHeight(origin)) {
  37. info = SwapWidthHeight(info);
  38. }
  39. if (!storage.tryAlloc(info)) {
  40. return false;
  41. }
  42. tmp = &storage;
  43. }
  44. if (!decode(*tmp)) {
  45. return false;
  46. }
  47. if (tmp != &dst) {
  48. return Orient(dst, *tmp, origin);
  49. }
  50. return true;
  51. }
  52. static void ResetPixmapKeepInfo(SkPixmap* pm, const void* address, size_t rowBytes) {
  53. pm->fRowBytes = rowBytes;
  54. pm->fPixels = address;
  55. }
  56. };
  57. #endif