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 */
7#ifndef SkScalingCodec_DEFINED
8#define SkScalingCodec_DEFINED
9
10#include "include/codec/SkCodec.h"
11
12// Helper class for an SkCodec that supports arbitrary downscaling.
13class SkScalingCodec : public SkCodec {
14protected:
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.
23 dim.fWidth = std::max(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
24 dim.fHeight = std::max(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
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
35private:
36 typedef SkCodec INHERITED;
37};
38
39#endif // SkScalingCodec_DEFINED
40