OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2009 The Android Open Source Project | 2 * Copyright 2009 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImageEncoder.h" | 8 #include "SkImageEncoder.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkPixelSerializer.h" |
| 11 #include "SkPixmap.h" |
10 #include "SkStream.h" | 12 #include "SkStream.h" |
11 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
12 | 14 |
13 SkImageEncoder::~SkImageEncoder() {} | 15 SkImageEncoder::~SkImageEncoder() {} |
14 | 16 |
15 bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm, | 17 bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm, |
16 int quality) { | 18 int quality) { |
17 quality = SkMin32(100, SkMax32(0, quality)); | 19 quality = SkMin32(100, SkMax32(0, quality)); |
18 return this->onEncode(stream, bm, quality); | 20 return this->onEncode(stream, bm, quality); |
19 } | 21 } |
(...skipping 30 matching lines...) Expand all Loading... |
50 SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t)); | 52 SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t)); |
51 return enc.get() ? enc.get()->encodeData(bm, quality) : nullptr; | 53 return enc.get() ? enc.get()->encodeData(bm, quality) : nullptr; |
52 } | 54 } |
53 | 55 |
54 SkData* SkImageEncoder::EncodeData(const SkImageInfo& info, const void* pixels,
size_t rowBytes, | 56 SkData* SkImageEncoder::EncodeData(const SkImageInfo& info, const void* pixels,
size_t rowBytes, |
55 Type t, int quality) { | 57 Type t, int quality) { |
56 SkBitmap bm; | 58 SkBitmap bm; |
57 if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) { | 59 if (!bm.installPixels(info, const_cast<void*>(pixels), rowBytes)) { |
58 return nullptr; | 60 return nullptr; |
59 } | 61 } |
60 SkAutoTDelete<SkImageEncoder> enc(SkImageEncoder::Create(t)); | 62 bm.setImmutable(); |
61 return enc.get() ? enc.get()->encodeData(bm, quality) : nullptr; | 63 return SkImageEncoder::EncodeData(bm, t, quality); |
62 } | 64 } |
| 65 |
| 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: |
| 79 bool onUseEncodedData(const void*, size_t) override { return true; } |
| 80 SkData* onEncode(const SkPixmap& pmap) override { |
| 81 return SkImageEncoder::EncodeData(pmap, SkImageEncoder::kPNG_Type, 100); |
| 82 } |
| 83 }; |
| 84 } // namespace |
| 85 |
| 86 SkPixelSerializer* SkImageEncoder::CreatePixelSerializer() { |
| 87 return new ImageEncoderPixelSerializer; |
| 88 } |
OLD | NEW |