OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef ImageEncoder_h |
| 6 #define ImageEncoder_h |
| 7 |
| 8 #include "platform/PlatformExport.h" |
| 9 #include "platform/wtf/Vector.h" |
| 10 #include "third_party/skia/include/core/SkStream.h" |
| 11 #include "third_party/skia/include/encode/SkJpegEncoder.h" |
| 12 #include "third_party/skia/include/encode/SkPngEncoder.h" |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 class VectorWStream : public SkWStream { |
| 17 public: |
| 18 VectorWStream(Vector<unsigned char>* dst) : dst_(dst) { |
| 19 DCHECK(dst_); |
| 20 DCHECK_EQ(0UL, dst->size()); |
| 21 } |
| 22 |
| 23 bool write(const void* buffer, size_t size) override { |
| 24 dst_->Append((const unsigned char*)buffer, size); |
| 25 return true; |
| 26 } |
| 27 |
| 28 size_t bytesWritten() const override { return dst_->size(); } |
| 29 |
| 30 private: |
| 31 // Does not have ownership. |
| 32 Vector<unsigned char>* dst_; |
| 33 }; |
| 34 |
| 35 class PLATFORM_EXPORT ImageEncoder { |
| 36 public: |
| 37 static bool Encode(Vector<unsigned char>* dst, |
| 38 const SkPixmap& src, |
| 39 const SkJpegEncoder::Options&); |
| 40 |
| 41 static bool Encode(Vector<unsigned char>* dst, |
| 42 const SkPixmap& src, |
| 43 const SkPngEncoder::Options&); |
| 44 |
| 45 static std::unique_ptr<ImageEncoder> Create(Vector<unsigned char>* dst, |
| 46 const SkPixmap& src, |
| 47 const SkJpegEncoder::Options&); |
| 48 |
| 49 static std::unique_ptr<ImageEncoder> Create(Vector<unsigned char>* dst, |
| 50 const SkPixmap& src, |
| 51 const SkPngEncoder::Options&); |
| 52 |
| 53 bool encodeRows(int numRows) { return encoder_->encodeRows(numRows); } |
| 54 |
| 55 /** |
| 56 * If quality is in [0, 1], this will simply convert to a [0, 100] |
| 57 * integer scale (which is what is used by libjpeg-turbo). |
| 58 * |
| 59 * Otherwise, this will return the default value. |
| 60 */ |
| 61 static int ComputeJpegQuality(double quality); |
| 62 |
| 63 private: |
| 64 ImageEncoder(Vector<unsigned char>* dst) : dst_(dst) {} |
| 65 |
| 66 VectorWStream dst_; |
| 67 std::unique_ptr<SkEncoder> encoder_; |
| 68 }; |
| 69 }; |
| 70 |
| 71 #endif |
OLD | NEW |