2 * Copyright 2008 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/images/SkImageEncoderPriv.h"
10 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
12 #include "include/core/SkBitmap.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkStream.h"
15 #include "include/core/SkUnPreMultiply.h"
16 #include "include/private/SkColorData.h"
17 #include "include/private/SkTemplates.h"
18 #include "include/utils/mac/SkCGUtils.h"
19 #include "src/core/SkStreamPriv.h"
20 #include "src/utils/mac/SkUniqueCFRef.h"
22 #ifdef SK_BUILD_FOR_MAC
23 #include <ApplicationServices/ApplicationServices.h>
26 #ifdef SK_BUILD_FOR_IOS
27 #include <CoreGraphics/CoreGraphics.h>
28 #include <ImageIO/ImageIO.h>
29 #include <MobileCoreServices/MobileCoreServices.h>
32 static size_t consumer_put(void* info, const void* buffer, size_t count) {
33 SkWStream* stream = reinterpret_cast<SkWStream*>(info);
34 return stream->write(buffer, count) ? count : 0;
37 static void consumer_release(void* info) {
38 // we do nothing, since by design we don't "own" the stream (i.e. info)
41 static SkUniqueCFRef<CGDataConsumerRef> SkStreamToCGDataConsumer(SkWStream* stream) {
42 CGDataConsumerCallbacks procs;
43 procs.putBytes = consumer_put;
44 procs.releaseConsumer = consumer_release;
45 // we don't own/reference the stream, so it our consumer must not live
46 // longer that our caller's ownership of the stream
47 return SkUniqueCFRef<CGDataConsumerRef>(CGDataConsumerCreate(stream, &procs));
50 static SkUniqueCFRef<CGImageDestinationRef> SkStreamToImageDestination(SkWStream* stream,
52 SkUniqueCFRef<CGDataConsumerRef> consumer = SkStreamToCGDataConsumer(stream);
53 if (nullptr == consumer) {
57 return SkUniqueCFRef<CGImageDestinationRef>(
58 CGImageDestinationCreateWithDataConsumer(consumer.get(), type, 1, nullptr));
61 /* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes
62 to our SkWStream. Since we don't reference/own the SkWStream, our consumer
63 must only live for the duration of the onEncode() method.
65 bool SkEncodeImageWithCG(SkWStream* stream, const SkPixmap& pixmap, SkEncodedImageFormat format) {
67 if (!bm.installPixels(pixmap)) {
74 case SkEncodedImageFormat::kICO:
77 case SkEncodedImageFormat::kBMP:
80 case SkEncodedImageFormat::kGIF:
83 case SkEncodedImageFormat::kJPEG:
86 case SkEncodedImageFormat::kPNG:
87 // PNG encoding an ARGB_4444 bitmap gives the following errors in GM:
88 // <Error>: CGImageDestinationAddImage image could not be converted to destination
90 // <Error>: CGImageDestinationFinalize image destination does not have enough images
91 // So instead we copy to 8888.
92 if (bm.colorType() == kARGB_4444_SkColorType) {
94 bitmapN32.allocPixels(bm.info().makeColorType(kN32_SkColorType));
95 bm.readPixels(bitmapN32.info(), bitmapN32.getPixels(), bitmapN32.rowBytes(), 0, 0);
104 SkUniqueCFRef<CGImageDestinationRef> dst = SkStreamToImageDestination(stream, type);
105 if (nullptr == dst) {
109 SkUniqueCFRef<CGImageRef> image(SkCreateCGImageRef(bm));
110 if (nullptr == image) {
114 CGImageDestinationAddImage(dst.get(), image.get(), nullptr);
115 return CGImageDestinationFinalize(dst.get());
118 #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)