1 /*
2 * Copyright 2009 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
8 #include "SkImageEncoder.h"
9 #include "SkBitmap.h"
10 #include "SkPixelSerializer.h"
11 #include "SkPixmap.h"
12 #include "SkStream.h"
13 #include "SkTemplates.h"
14
~SkImageEncoder()15 SkImageEncoder::~SkImageEncoder() {}
16
encodeStream(SkWStream * stream,const SkBitmap & bm,int quality)17 bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm,
18 int quality) {
19 quality = SkMin32(100, SkMax32(0, quality));
20 return this->onEncode(stream, bm, quality);
21 }
22
encodeFile(const char file[],const SkBitmap & bm,int quality)23 bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm,
24 int quality) {
25 quality = SkMin32(100, SkMax32(0, quality));
26 SkFILEWStream stream(file);
27 return this->onEncode(&stream, bm, quality);
28 }
29
encodeData(const SkBitmap & bm,int quality)30 SkData* SkImageEncoder::encodeData(const SkBitmap& bm, int quality) {
31 SkDynamicMemoryWStream stream;
32 quality = SkMin32(100, SkMax32(0, quality));
33 if (this->onEncode(&stream, bm, quality)) {
34 return stream.copyToData();
35 }
36 return nullptr;
37 }
38
EncodeFile(const char file[],const SkBitmap & bm,Type t,int quality)39 bool SkImageEncoder::EncodeFile(const char file[], const SkBitmap& bm, Type t,
40 int quality) {
41 SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
42 return enc.get() && enc.get()->encodeFile(file, bm, quality);
43 }
44
EncodeStream(SkWStream * stream,const SkBitmap & bm,Type t,int quality)45 bool SkImageEncoder::EncodeStream(SkWStream* stream, const SkBitmap& bm, Type t,
46 int quality) {
47 SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
48 return enc.get() && enc.get()->encodeStream(stream, bm, quality);
49 }
50
EncodeData(const SkBitmap & bm,Type t,int quality)51 SkData* SkImageEncoder::EncodeData(const SkBitmap& bm, Type t, int quality) {
52 SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t));
53 return enc.get() ? enc.get()->encodeData(bm, quality) : nullptr;
54 }
55
EncodeData(const SkImageInfo & info,const void * pixels,size_t rowBytes,Type t,int quality)56 SkData* SkImageEncoder::EncodeData(const SkImageInfo& info, const void* pixels, size_t rowBytes,
57 Type t, int quality) {
58 SkBitmap bm;
59 if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) {
60 return nullptr;
61 }
62 bm.setImmutable();
63 return SkImageEncoder::EncodeData(bm, t, quality);
64 }
65
EncodeData(const SkPixmap & pixmap,Type t,int quality)66 SkData* SkImageEncoder::EncodeData(const SkPixmap& pixmap,
67 Type t, int quality) {
68 SkBitmap bm;
69 if (!bm.installPixels(pixmap)) {
70 return nullptr;
71 }
72 bm.setImmutable();
73 return SkImageEncoder::EncodeData(bm, t, quality);
74 }
75
76 namespace {
77 class ImageEncoderPixelSerializer final : public SkPixelSerializer {
78 protected:
onUseEncodedData(const void *,size_t)79 bool onUseEncodedData(const void*, size_t) override { return true; }
onEncode(const SkPixmap & pmap)80 SkData* onEncode(const SkPixmap& pmap) override {
81 return SkImageEncoder::EncodeData(pmap, SkImageEncoder::kPNG_Type, 100);
82 }
83 };
84 } // namespace
85
CreatePixelSerializer()86 SkPixelSerializer* SkImageEncoder::CreatePixelSerializer() {
87 return new ImageEncoderPixelSerializer;
88 }
89