Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Hal Canary | 02eefbe | 2019-06-26 13:54:14 -0400 | [diff] [blame] | 7 | #ifndef SkScalingCodec_DEFINED |
| 8 | #define SkScalingCodec_DEFINED |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/codec/SkCodec.h" |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 11 | |
| 12 | // Helper class for an SkCodec that supports arbitrary downscaling. |
| 13 | class SkScalingCodec : public SkCodec { |
| 14 | protected: |
| 15 | SkScalingCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream, |
| 16 | SkEncodedOrigin origin = kTopLeft_SkEncodedOrigin) |
| 17 | : INHERITED(std::move(info), srcFormat, std::move(stream), origin) {} |
| 18 | |
| 19 | SkISize onGetScaledDimensions(float desiredScale) const override { |
| 20 | SkISize dim = this->dimensions(); |
| 21 | // SkCodec treats zero dimensional images as errors, so the minimum size |
| 22 | // that we will recommend is 1x1. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 23 | dim.fWidth = std::max(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); |
| 24 | dim.fHeight = std::max(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 25 | return dim; |
| 26 | } |
| 27 | |
| 28 | bool onDimensionsSupported(const SkISize& requested) override { |
| 29 | SkISize dim = this->dimensions(); |
| 30 | int w = requested.width(); |
| 31 | int h = requested.height(); |
| 32 | return 1 <= w && w <= dim.width() && 1 <= h && h <= dim.height(); |
| 33 | } |
| 34 | |
| 35 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 36 | using INHERITED = SkCodec; |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
Hal Canary | 02eefbe | 2019-06-26 13:54:14 -0400 | [diff] [blame] | 39 | #endif // SkScalingCodec_DEFINED |