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