SkCGUtils.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 SkCGUtils_DEFINED
  8. #define SkCGUtils_DEFINED
  9. #include "SkSize.h"
  10. #include "SkImageInfo.h"
  11. #include "SkImage.h"
  12. #include "SkPixmap.h"
  13. #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
  14. #ifdef SK_BUILD_FOR_MAC
  15. #include <ApplicationServices/ApplicationServices.h>
  16. #endif
  17. #ifdef SK_BUILD_FOR_IOS
  18. #include <CoreGraphics/CoreGraphics.h>
  19. #endif
  20. class SkBitmap;
  21. class SkData;
  22. class SkPixmap;
  23. class SkStreamRewindable;
  24. SK_API CGContextRef SkCreateCGContext(const SkPixmap&);
  25. /**
  26. * Given a CGImage, allocate an SkBitmap and copy the image's pixels into it. If scaleToFit is not
  27. * null, use it to determine the size of the bitmap, and scale the image to fill the bitmap.
  28. * Otherwise use the image's width/height.
  29. *
  30. * On failure, return false, and leave bitmap unchanged.
  31. */
  32. SK_API bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef src);
  33. SK_API sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef);
  34. /**
  35. * Copy the pixels from src into the memory specified by info/rowBytes/dstPixels. On failure,
  36. * return false (e.g. ImageInfo incompatible with src).
  37. */
  38. SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* dstPixels,
  39. CGImageRef src);
  40. static inline bool SkCopyPixelsFromCGImage(const SkPixmap& dst, CGImageRef src) {
  41. return SkCopyPixelsFromCGImage(dst.info(), dst.rowBytes(), dst.writable_addr(), src);
  42. }
  43. /**
  44. * Create an imageref from the specified bitmap using the specified colorspace.
  45. * If space is NULL, then CGColorSpaceCreateDeviceRGB() is used.
  46. */
  47. SK_API CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
  48. CGColorSpaceRef space);
  49. /**
  50. * Create an imageref from the specified bitmap using the colorspace returned
  51. * by CGColorSpaceCreateDeviceRGB()
  52. */
  53. static inline CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
  54. return SkCreateCGImageRefWithColorspace(bm, NULL);
  55. }
  56. /**
  57. * Draw the bitmap into the specified CG context. The bitmap will be converted
  58. * to a CGImage using the generic RGB colorspace. (x,y) specifies the position
  59. * of the top-left corner of the bitmap. The bitmap is converted using the
  60. * colorspace returned by CGColorSpaceCreateDeviceRGB()
  61. */
  62. void SkCGDrawBitmap(CGContextRef, const SkBitmap&, float x, float y);
  63. /**
  64. * Return a provider that wraps the specified stream.
  65. * When the provider is finally deleted, it will delete the stream.
  66. */
  67. CGDataProviderRef SkCreateDataProviderFromStream(std::unique_ptr<SkStreamRewindable>);
  68. CGDataProviderRef SkCreateDataProviderFromData(sk_sp<SkData>);
  69. #endif // defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
  70. #endif // SkCGUtils_DEFINED