SkScalingCodec.h 1.3 KB

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