blob: a6d2337bc63563d5bce8d3328376787e4ff69db4 [file] [log] [blame]
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -04001/*
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 Canary02eefbe2019-06-26 13:54:14 -04007#ifndef SkScalingCodec_DEFINED
8#define SkScalingCodec_DEFINED
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/codec/SkCodec.h"
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040011
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.
Brian Osman788b9162020-02-07 10:36:46 -050023 dim.fWidth = std::max(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
24 dim.fHeight = std::max(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040025 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:
John Stiles7571f9e2020-09-02 22:42:33 -040036 using INHERITED = SkCodec;
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040037};
38
Hal Canary02eefbe2019-06-26 13:54:14 -040039#endif // SkScalingCodec_DEFINED