reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2009 The Android Open Source Project |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 8 | #include "SkImageEncoder.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkStream.h" |
| 11 | #include "SkTemplates.h" |
| 12 | |
| 13 | SkImageEncoder::~SkImageEncoder() {} |
| 14 | |
| 15 | bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm, |
| 16 | int quality) { |
| 17 | quality = SkMin32(100, SkMax32(0, quality)); |
| 18 | return this->onEncode(stream, bm, quality); |
| 19 | } |
| 20 | |
| 21 | bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm, |
| 22 | int quality) { |
| 23 | quality = SkMin32(100, SkMax32(0, quality)); |
| 24 | SkFILEWStream stream(file); |
| 25 | return this->onEncode(&stream, bm, quality); |
| 26 | } |
| 27 | |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 28 | SkData* SkImageEncoder::encodeData(const SkBitmap& bm, int quality) { |
| 29 | SkDynamicMemoryWStream stream; |
| 30 | quality = SkMin32(100, SkMax32(0, quality)); |
| 31 | if (this->onEncode(&stream, bm, quality)) { |
| 32 | return stream.copyToData(); |
| 33 | } |
| 34 | return NULL; |
| 35 | } |
| 36 | |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 37 | bool SkImageEncoder::EncodeFile(const char file[], const SkBitmap& bm, Type t, |
| 38 | int quality) { |
| 39 | SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t)); |
| 40 | return enc.get() && enc.get()->encodeFile(file, bm, quality); |
| 41 | } |
| 42 | |
| 43 | bool SkImageEncoder::EncodeStream(SkWStream* stream, const SkBitmap& bm, Type t, |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 44 | int quality) { |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 45 | SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t)); |
| 46 | return enc.get() && enc.get()->encodeStream(stream, bm, quality); |
| 47 | } |
reed@google.com | 4b0757b | 2013-05-20 16:33:41 +0000 | [diff] [blame] | 48 | |
| 49 | SkData* SkImageEncoder::EncodeData(const SkBitmap& bm, Type t, int quality) { |
| 50 | SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t)); |
| 51 | return enc.get() ? enc.get()->encodeData(bm, quality) : NULL; |
| 52 | } |