blob: 1bcdf085b213f7eaab1cb9d9eca51a491138c58f [file] [log] [blame]
emmaleerd518ea72015-08-13 13:07:03 -07001/*
2 * Copyright 2015 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 SkScaledCodec_DEFINED
8#define SkScaledCodec_DEFINED
9
10#include "SkCodec.h"
11#include "SkScanlineDecoder.h"
12
13class SkScanlineDecoder;
14class SkStream;
15
16/**
17 * This class implements scaling, by sampling scanlines in the y direction.
18 * x-wise sampling is implemented in the swizzler, when getScanlines() is called.
19 */
20class SkScaledCodec : public SkCodec {
21public:
22 static SkCodec* NewFromStream(SkStream*);
23 static SkCodec* NewFromData(SkData*);
24
25 virtual ~SkScaledCodec();
26
27 /**
28 * returns whether a destination's dimensions are supported for down sampling
29 */
30 static bool DimensionsSupportedForSampling(const SkImageInfo& srcInfo,
31 const SkImageInfo& dstInfo) {
32 // heights must be equal as no native y sampling is supported
33 if (dstInfo.height() != srcInfo.height()) {
34 return false;
35 }
36 // only support down sampling, dstWidth cannot be larger that srcWidth
37 if(dstInfo.width() > srcInfo.width()) {
38 return false;
39 }
40 return true;
41 }
42
43 static void ComputeSampleSize(const SkImageInfo& dstInfo, const SkImageInfo& srcInfo,
44 int* sampleSizeX, int* sampleSizeY);
45
46protected:
47 /**
48 * Recommend a set of destination dimensions given a requested scale
49 */
50 SkISize onGetScaledDimensions(float desiredScale) const override;
51
52 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, SkPMColor*, int*)
53 override;
54 SkEncodedFormat onGetEncodedFormat() const override {
55 return fScanlineDecoder->getEncodedFormat();
56 }
57
58 bool onReallyHasAlpha() const override {
59 return fScanlineDecoder->reallyHasAlpha();
60 }
61
62private:
63
64 SkAutoTDelete<SkScanlineDecoder> fScanlineDecoder;
65
66 explicit SkScaledCodec(SkScanlineDecoder*);
67
68 typedef SkCodec INHERITED;
69};
70#endif // SkScaledCodec_DEFINED