1
/* Copyright 2008, The Android Open Source Project
3
** Licensed under the Apache License, Version 2.0 (the "License");
4
** you may not use this file except in compliance with the License.
5
** You may obtain a copy of the License at
7
** http://www.apache.org/licenses/LICENSE-2.0
9
** Unless required by applicable law or agreed to in writing, software
10
** distributed under the License is distributed on an "AS IS" BASIS,
11
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
** See the License for the specific language governing permissions and
13
** limitations under the License.
16
#include <Carbon/Carbon.h>
17
#include "SkImageDecoder.h"
18
#include "SkImageEncoder.h"
21
#include "SkTemplates.h"
23
static void malloc_release_proc(void* info, const void* data, size_t size) {
27
static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) {
28
// TODO: use callbacks, so we don't have to load all the data into RAM
29
size_t len = stream->getLength();
30
void* data = sk_malloc_throw(len);
31
stream->read(data, len);
33
return CGDataProviderCreateWithData(data, data, len, malloc_release_proc);
36
static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) {
37
CGDataProviderRef data = SkStreamToDataProvider(stream);
38
CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0);
39
CGDataProviderRelease(data);
43
class SkImageDecoder_CG : public SkImageDecoder {
45
virtual bool onDecode(SkStream* stream, SkBitmap* bm,
46
SkBitmap::Config pref, Mode);
49
#define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast)
51
bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm,
52
SkBitmap::Config pref, Mode mode) {
53
CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream);
55
if (NULL == imageSrc) {
58
SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc);
60
CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL);
64
SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image);
66
const int width = CGImageGetWidth(image);
67
const int height = CGImageGetHeight(image);
68
bm->setConfig(SkBitmap::kARGB_8888_Config, width, height);
69
if (SkImageDecoder::kDecodeBounds_Mode == mode) {
73
if (!this->allocPixelRef(bm, NULL)) {
80
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
81
CGContextRef cg = CGBitmapContextCreate(bm->getPixels(), width, height,
82
8, bm->rowBytes(), cs, BITMAP_INFO);
83
CGContextDrawImage(cg, CGRectMake(0, 0, width, height), image);
85
CGColorSpaceRelease(cs);
91
///////////////////////////////////////////////////////////////////////////////
93
SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) {
94
return SkNEW(SkImageDecoder_CG);
97
/////////////////////////////////////////////////////////////////////////
99
SkMovie* SkMovie::DecodeStream(SkStream* stream) {
103
/////////////////////////////////////////////////////////////////////////
105
static size_t consumer_put(void* info, const void* buffer, size_t count) {
106
SkWStream* stream = reinterpret_cast<SkWStream*>(info);
107
return stream->write(buffer, count) ? count : 0;
110
static void consumer_release(void* info) {
111
// we do nothing, since by design we don't "own" the stream (i.e. info)
114
static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) {
115
CGDataConsumerCallbacks procs;
116
procs.putBytes = consumer_put;
117
procs.releaseConsumer = consumer_release;
118
// we don't own/reference the stream, so it our consumer must not live
119
// longer that our caller's ownership of the stream
120
return CGDataConsumerCreate(stream, &procs);
123
static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream,
125
CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream);
126
if (NULL == consumer) {
129
SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer);
131
return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL);
134
class SkImageEncoder_CG : public SkImageEncoder {
136
SkImageEncoder_CG(Type t) : fType(t) {}
139
virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality);
145
extern CGImageRef SkCreateCGImageRef(const SkBitmap&);
147
/* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes
148
to our SkWStream. Since we don't reference/own the SkWStream, our consumer
149
must only live for the duration of the onEncode() method.
151
bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm,
165
CGImageDestinationRef dst = SkStreamToImageDestination(stream, type);
169
SkAutoTCallVProc<const void, CFRelease> ardst(dst);
171
CGImageRef image = SkCreateCGImageRef(bm);
175
SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image);
177
CGImageDestinationAddImage(dst, image, NULL);
178
CGImageDestinationFinalize(dst);
182
SkImageEncoder* SkImageEncoder::Create(Type t) {
190
return SkNEW_ARGS(SkImageEncoder_CG, (t));