| 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 |
#include "include/codec/SkEncodedOrigin.h" |
| 12 |
#include "include/core/SkScalar.h" |
| 13 |
#include "include/core/SkSize.h" |
| 14 |
#include "include/core/SkStream.h" |
| 15 |
#include "include/private/SkEncodedInfo.h" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <memory> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
// Helper class for an SkCodec that supports arbitrary downscaling. |
| 22 |
class SkScalingCodec : public SkCodec { |
| 23 |
protected: |
| 24 |
SkScalingCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream, |
| 25 |
SkEncodedOrigin origin = kTopLeft_SkEncodedOrigin) |
| 26 |
: SkCodec(std::move(info), srcFormat, std::move(stream), origin) {} |
| 27 |
|
| 28 |
SkISize onGetScaledDimensions(float desiredScale) const override { |
| 29 |
SkISize dim = this->dimensions(); |
| 30 |
// SkCodec treats zero dimensional images as errors, so the minimum size |
| 31 |
// that we will recommend is 1x1. |
| 32 |
dim.fWidth = std::max(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); |
| 33 |
dim.fHeight = std::max(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); |
| 34 |
return dim; |
| 35 |
} |
| 36 |
|
| 37 |
bool onDimensionsSupported(const SkISize& requested) override { |
| 38 |
SkISize dim = this->dimensions(); |
| 39 |
int w = requested.width(); |
| 40 |
int h = requested.height(); |
| 41 |
return 1 <= w && w <= dim.width() && 1 <= h && h <= dim.height(); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
#endif // SkScalingCodec_DEFINED |