OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/image-encoders/ImageEncoder.h" | 5 #include "platform/image-encoders/ImageEncoder.h" |
6 | 6 |
7 namespace blink { | 7 namespace blink { |
8 | 8 |
9 bool ImageEncoder::Encode(Vector<unsigned char>* dst, | 9 bool ImageEncoder::Encode(Vector<unsigned char>* dst, |
10 const SkPixmap& src, | 10 const SkPixmap& src, |
11 const SkJpegEncoder::Options& options) { | 11 const SkJpegEncoder::Options& options) { |
12 VectorWStream dst_stream(dst); | 12 VectorWStream dst_stream(dst); |
13 return SkJpegEncoder::Encode(&dst_stream, src, options); | 13 return SkJpegEncoder::Encode(&dst_stream, src, options); |
14 } | 14 } |
15 | 15 |
16 bool ImageEncoder::Encode(Vector<unsigned char>* dst, | 16 bool ImageEncoder::Encode(Vector<unsigned char>* dst, |
17 const SkPixmap& src, | 17 const SkPixmap& src, |
18 const SkPngEncoder::Options& options) { | 18 const SkPngEncoder::Options& options) { |
19 VectorWStream dst_stream(dst); | 19 VectorWStream dst_stream(dst); |
20 return SkPngEncoder::Encode(&dst_stream, src, options); | 20 return SkPngEncoder::Encode(&dst_stream, src, options); |
21 } | 21 } |
22 | 22 |
| 23 bool ImageEncoder::Encode(Vector<unsigned char>* dst, |
| 24 const SkPixmap& src, |
| 25 const SkWebpEncoder::Options& options) { |
| 26 VectorWStream dst_stream(dst); |
| 27 return SkWebpEncoder::Encode(&dst_stream, src, options); |
| 28 } |
| 29 |
23 std::unique_ptr<ImageEncoder> ImageEncoder::Create( | 30 std::unique_ptr<ImageEncoder> ImageEncoder::Create( |
24 Vector<unsigned char>* dst, | 31 Vector<unsigned char>* dst, |
25 const SkPixmap& src, | 32 const SkPixmap& src, |
26 const SkJpegEncoder::Options& options) { | 33 const SkJpegEncoder::Options& options) { |
27 std::unique_ptr<ImageEncoder> image_encoder(new ImageEncoder(dst)); | 34 std::unique_ptr<ImageEncoder> image_encoder(new ImageEncoder(dst)); |
28 image_encoder->encoder_ = | 35 image_encoder->encoder_ = |
29 SkJpegEncoder::Make(&image_encoder->dst_, src, options); | 36 SkJpegEncoder::Make(&image_encoder->dst_, src, options); |
30 if (!image_encoder->encoder_) { | 37 if (!image_encoder->encoder_) { |
31 return nullptr; | 38 return nullptr; |
32 } | 39 } |
(...skipping 10 matching lines...) Expand all Loading... |
43 SkPngEncoder::Make(&image_encoder->dst_, src, options); | 50 SkPngEncoder::Make(&image_encoder->dst_, src, options); |
44 if (!image_encoder->encoder_) { | 51 if (!image_encoder->encoder_) { |
45 return nullptr; | 52 return nullptr; |
46 } | 53 } |
47 | 54 |
48 return image_encoder; | 55 return image_encoder; |
49 } | 56 } |
50 | 57 |
51 int ImageEncoder::ComputeJpegQuality(double quality) { | 58 int ImageEncoder::ComputeJpegQuality(double quality) { |
52 int compression_quality = 92; // Default value | 59 int compression_quality = 92; // Default value |
53 if (quality >= 0.0 && quality <= 1.0) | 60 if (0.0f <= quality && quality <= 1.0) |
54 compression_quality = static_cast<int>(quality * 100 + 0.5); | 61 compression_quality = static_cast<int>(quality * 100 + 0.5); |
55 return compression_quality; | 62 return compression_quality; |
56 } | 63 } |
| 64 |
| 65 SkWebpEncoder::Options ImageEncoder::ComputeWebpOptions( |
| 66 double quality, |
| 67 SkTransferFunctionBehavior unpremulBehavior) { |
| 68 SkWebpEncoder::Options options; |
| 69 options.fUnpremulBehavior = unpremulBehavior; |
| 70 |
| 71 if (quality == 1.0) { |
| 72 // Choose a lossless encode. When performing a lossless encode, higher |
| 73 // quality corresponds to slower encoding and smaller output size. |
| 74 options.fCompression = SkWebpEncoder::Compression::kLossless; |
| 75 options.fQuality = 75.0f; |
| 76 } else { |
| 77 options.fQuality = 80.0f; // Default value |
| 78 if (0.0f <= quality && quality <= 1.0) |
| 79 options.fQuality = quality * 100.0f; |
| 80 } |
| 81 |
| 82 return options; |
| 83 } |
57 }; | 84 }; |
OLD | NEW |