SkImageEncoder_CG.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2008 The Android Open Source Project
  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. #include "src/images/SkImageEncoderPriv.h"
  8. #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
  9. #include "include/core/SkBitmap.h"
  10. #include "include/core/SkData.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/core/SkUnPreMultiply.h"
  13. #include "include/private/SkColorData.h"
  14. #include "include/private/SkTemplates.h"
  15. #include "include/utils/mac/SkCGUtils.h"
  16. #include "src/core/SkStreamPriv.h"
  17. #include "src/utils/mac/SkUniqueCFRef.h"
  18. #ifdef SK_BUILD_FOR_MAC
  19. #include <ApplicationServices/ApplicationServices.h>
  20. #endif
  21. #ifdef SK_BUILD_FOR_IOS
  22. #include <CoreGraphics/CoreGraphics.h>
  23. #include <ImageIO/ImageIO.h>
  24. #include <MobileCoreServices/MobileCoreServices.h>
  25. #endif
  26. static size_t consumer_put(void* info, const void* buffer, size_t count) {
  27. SkWStream* stream = reinterpret_cast<SkWStream*>(info);
  28. return stream->write(buffer, count) ? count : 0;
  29. }
  30. static void consumer_release(void* info) {
  31. // we do nothing, since by design we don't "own" the stream (i.e. info)
  32. }
  33. static SkUniqueCFRef<CGDataConsumerRef> SkStreamToCGDataConsumer(SkWStream* stream) {
  34. CGDataConsumerCallbacks procs;
  35. procs.putBytes = consumer_put;
  36. procs.releaseConsumer = consumer_release;
  37. // we don't own/reference the stream, so it our consumer must not live
  38. // longer that our caller's ownership of the stream
  39. return SkUniqueCFRef<CGDataConsumerRef>(CGDataConsumerCreate(stream, &procs));
  40. }
  41. static SkUniqueCFRef<CGImageDestinationRef> SkStreamToImageDestination(SkWStream* stream,
  42. CFStringRef type) {
  43. SkUniqueCFRef<CGDataConsumerRef> consumer = SkStreamToCGDataConsumer(stream);
  44. if (nullptr == consumer) {
  45. return nullptr;
  46. }
  47. return SkUniqueCFRef<CGImageDestinationRef>(
  48. CGImageDestinationCreateWithDataConsumer(consumer.get(), type, 1, nullptr));
  49. }
  50. /* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes
  51. to our SkWStream. Since we don't reference/own the SkWStream, our consumer
  52. must only live for the duration of the onEncode() method.
  53. */
  54. bool SkEncodeImageWithCG(SkWStream* stream, const SkPixmap& pixmap, SkEncodedImageFormat format) {
  55. SkBitmap bm;
  56. if (!bm.installPixels(pixmap)) {
  57. return false;
  58. }
  59. bm.setImmutable();
  60. CFStringRef type;
  61. switch (format) {
  62. case SkEncodedImageFormat::kICO:
  63. type = kUTTypeICO;
  64. break;
  65. case SkEncodedImageFormat::kBMP:
  66. type = kUTTypeBMP;
  67. break;
  68. case SkEncodedImageFormat::kGIF:
  69. type = kUTTypeGIF;
  70. break;
  71. case SkEncodedImageFormat::kJPEG:
  72. type = kUTTypeJPEG;
  73. break;
  74. case SkEncodedImageFormat::kPNG:
  75. // PNG encoding an ARGB_4444 bitmap gives the following errors in GM:
  76. // <Error>: CGImageDestinationAddImage image could not be converted to destination
  77. // format.
  78. // <Error>: CGImageDestinationFinalize image destination does not have enough images
  79. // So instead we copy to 8888.
  80. if (bm.colorType() == kARGB_4444_SkColorType) {
  81. SkBitmap bitmapN32;
  82. bitmapN32.allocPixels(bm.info().makeColorType(kN32_SkColorType));
  83. bm.readPixels(bitmapN32.info(), bitmapN32.getPixels(), bitmapN32.rowBytes(), 0, 0);
  84. bm.swap(bitmapN32);
  85. }
  86. type = kUTTypePNG;
  87. break;
  88. default:
  89. return false;
  90. }
  91. SkUniqueCFRef<CGImageDestinationRef> dst = SkStreamToImageDestination(stream, type);
  92. if (nullptr == dst) {
  93. return false;
  94. }
  95. SkUniqueCFRef<CGImageRef> image(SkCreateCGImageRef(bm));
  96. if (nullptr == image) {
  97. return false;
  98. }
  99. CGImageDestinationAddImage(dst.get(), image.get(), nullptr);
  100. return CGImageDestinationFinalize(dst.get());
  101. }
  102. #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)