blob: 40d963dad890507f59e7d607701196125a37331f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
Hal Canary1fcc4042016-11-30 17:07:59 -05008#include "SkImageEncoderPriv.h"
9
mtklein1ee76512015-11-02 10:20:27 -080010#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
11
Ben Wagner7b562292018-09-19 22:31:07 -040012#include "mac/SkUniqueCFRef.h"
msarettd442d322016-03-25 11:08:14 -070013#include "SkBitmap.h"
scroggo@google.comdaaea2d2013-06-14 20:39:48 +000014#include "SkCGUtils.h"
Cary Clarka4083c92017-09-15 11:59:23 -040015#include "SkColorData.h"
scroggoa9132752016-01-19 07:53:39 -080016#include "SkData.h"
reed@android.com0767e472008-12-23 16:06:51 +000017#include "SkStream.h"
halcanary67ec1f82014-06-27 11:36:20 -070018#include "SkStreamPriv.h"
reed@android.com0767e472008-12-23 16:06:51 +000019#include "SkTemplates.h"
scroggo@google.comdaaea2d2013-06-14 20:39:48 +000020#include "SkUnPreMultiply.h"
reed@android.com0767e472008-12-23 16:06:51 +000021
yangsu@google.com900d8772011-06-24 18:56:00 +000022#ifdef SK_BUILD_FOR_MAC
23#include <ApplicationServices/ApplicationServices.h>
24#endif
25
26#ifdef SK_BUILD_FOR_IOS
27#include <CoreGraphics/CoreGraphics.h>
caryclark@google.com35f5ac92012-09-18 15:41:18 +000028#include <ImageIO/ImageIO.h>
caryclark@google.com594dd3c2012-09-24 19:33:57 +000029#include <MobileCoreServices/MobileCoreServices.h>
yangsu@google.com900d8772011-06-24 18:56:00 +000030#endif
yangsu@google.comc134f392011-06-23 22:27:30 +000031
reed@android.com0ae6b242008-12-23 16:49:54 +000032static 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;
35}
36
37static void consumer_release(void* info) {
38 // we do nothing, since by design we don't "own" the stream (i.e. info)
39}
40
Ben Wagner7b562292018-09-19 22:31:07 -040041static SkUniqueCFRef<CGDataConsumerRef> SkStreamToCGDataConsumer(SkWStream* stream) {
reed@android.com0ae6b242008-12-23 16:49:54 +000042 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
Ben Wagner7b562292018-09-19 22:31:07 -040047 return SkUniqueCFRef<CGDataConsumerRef>(CGDataConsumerCreate(stream, &procs));
reed@android.com0ae6b242008-12-23 16:49:54 +000048}
49
Ben Wagner7b562292018-09-19 22:31:07 -040050static SkUniqueCFRef<CGImageDestinationRef> SkStreamToImageDestination(SkWStream* stream,
51 CFStringRef type) {
52 SkUniqueCFRef<CGDataConsumerRef> consumer = SkStreamToCGDataConsumer(stream);
halcanary96fcdcc2015-08-27 07:41:13 -070053 if (nullptr == consumer) {
54 return nullptr;
reed@android.com0ae6b242008-12-23 16:49:54 +000055 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000056
Ben Wagner7b562292018-09-19 22:31:07 -040057 return SkUniqueCFRef<CGImageDestinationRef>(
58 CGImageDestinationCreateWithDataConsumer(consumer.get(), type, 1, nullptr));
reed@android.com0ae6b242008-12-23 16:49:54 +000059}
60
reed@android.com0ae6b242008-12-23 16:49:54 +000061/* 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.
64 */
Hal Canary1fcc4042016-11-30 17:07:59 -050065bool SkEncodeImageWithCG(SkWStream* stream, const SkPixmap& pixmap, SkEncodedImageFormat format) {
66 SkBitmap bm;
67 if (!bm.installPixels(pixmap)) {
68 return false;
69 }
70 bm.setImmutable();
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000071
reed@android.com0ae6b242008-12-23 16:49:54 +000072 CFStringRef type;
Hal Canary1fcc4042016-11-30 17:07:59 -050073 switch (format) {
Hal Canarydb683012016-11-23 08:55:18 -070074 case SkEncodedImageFormat::kICO:
scroggo@google.com4c6adf92013-04-17 21:07:55 +000075 type = kUTTypeICO;
76 break;
Hal Canarydb683012016-11-23 08:55:18 -070077 case SkEncodedImageFormat::kBMP:
scroggo@google.com4c6adf92013-04-17 21:07:55 +000078 type = kUTTypeBMP;
79 break;
Hal Canarydb683012016-11-23 08:55:18 -070080 case SkEncodedImageFormat::kGIF:
scroggo@google.com4c6adf92013-04-17 21:07:55 +000081 type = kUTTypeGIF;
82 break;
Hal Canarydb683012016-11-23 08:55:18 -070083 case SkEncodedImageFormat::kJPEG:
reed@android.com0ae6b242008-12-23 16:49:54 +000084 type = kUTTypeJPEG;
85 break;
Hal Canarydb683012016-11-23 08:55:18 -070086 case SkEncodedImageFormat::kPNG:
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000087 // PNG encoding an ARGB_4444 bitmap gives the following errors in GM:
88 // <Error>: CGImageDestinationAddImage image could not be converted to destination
89 // format.
90 // <Error>: CGImageDestinationFinalize image destination does not have enough images
91 // So instead we copy to 8888.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000092 if (bm.colorType() == kARGB_4444_SkColorType) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -040093 SkBitmap bitmapN32;
94 bitmapN32.allocPixels(bm.info().makeColorType(kN32_SkColorType));
95 bm.readPixels(bitmapN32.info(), bitmapN32.getPixels(), bitmapN32.rowBytes(), 0, 0);
96 bm.swap(bitmapN32);
scroggo@google.com5a7c6be2012-10-04 21:46:08 +000097 }
reed@android.com0ae6b242008-12-23 16:49:54 +000098 type = kUTTypePNG;
99 break;
100 default:
101 return false;
102 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
Ben Wagner7b562292018-09-19 22:31:07 -0400104 SkUniqueCFRef<CGImageDestinationRef> dst = SkStreamToImageDestination(stream, type);
halcanary96fcdcc2015-08-27 07:41:13 -0700105 if (nullptr == dst) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000106 return false;
107 }
reed@android.com0ae6b242008-12-23 16:49:54 +0000108
Ben Wagner7b562292018-09-19 22:31:07 -0400109 SkUniqueCFRef<CGImageRef> image(SkCreateCGImageRef(bm));
halcanary96fcdcc2015-08-27 07:41:13 -0700110 if (nullptr == image) {
reed@android.com0ae6b242008-12-23 16:49:54 +0000111 return false;
112 }
epoger@google.com0928c4a2012-01-31 15:14:08 +0000113
Ben Wagner7b562292018-09-19 22:31:07 -0400114 CGImageDestinationAddImage(dst.get(), image.get(), nullptr);
115 return CGImageDestinationFinalize(dst.get());
reed@android.com0ae6b242008-12-23 16:49:54 +0000116}
117
mtklein1ee76512015-11-02 10:20:27 -0800118#endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)