1/*
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 SkSampler_DEFINED
8#define SkSampler_DEFINED
9
10#include "SkCodec.h"
11#include "SkCodecPriv.h"
12#include "SkTypes.h"
13
14class SkSampler : public SkNoncopyable {
15public:
16    /**
17     *  Update the sampler to sample every sampleX'th pixel. Returns the
18     *  width after sampling.
19     */
20    int setSampleX(int sampleX) {
21        return this->onSetSampleX(sampleX);
22    }
23
24    /**
25     *  Update the sampler to sample every sampleY'th row.
26     */
27    void setSampleY(int sampleY) {
28        fSampleY = sampleY;
29    }
30
31    /**
32     *  Retrieve the value set for sampleY.
33     */
34    int sampleY() const {
35        return fSampleY;
36    }
37
38    /**
39     *  Based on fSampleY, return whether this row belongs in the output.
40     *
41     *  @param row Row of the image, starting with the first row in the subset.
42     */
43    bool rowNeeded(int row) const {
44        return (row - get_start_coord(fSampleY)) % fSampleY == 0;
45    }
46
47    /**
48     * Fill the remainder of the destination with a single color
49     *
50     * @param info
51     * Contains the color type of the rows to fill.
52     * Contains the width of the destination rows to fill
53     * Contains the number of rows that we need to fill.
54     *
55     * @param dst
56     * The destination row to fill from.
57     *
58     * @param rowBytes
59     * Stride in bytes of the destination.
60     *
61     * @param colorOrIndex
62     * If colorType is kF16, colorOrIndex is treated as a 64-bit color.
63     * If colorType is kN32, colorOrIndex is treated as a 32-bit color.
64     * If colorType is k565, colorOrIndex is treated as a 16-bit color.
65     * If colorType is kGray, colorOrIndex is treated as an 8-bit color.
66     * If colorType is kIndex, colorOrIndex is treated as an 8-bit index.
67     * Other SkColorTypes are not supported.
68     *
69     * @param zeroInit
70     * Indicates whether memory is already zero initialized.
71     *
72     */
73    static void Fill(const SkImageInfo& info, void* dst, size_t rowBytes,
74            uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit);
75
76    /**
77     * Allow subclasses to implement unique versions of fill().
78     */
79    virtual void fill(const SkImageInfo& info, void* dst, size_t rowBytes,
80            uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit) {}
81
82    SkSampler()
83        : fSampleY(1)
84    {}
85
86    virtual ~SkSampler() {}
87private:
88    int fSampleY;
89
90    virtual int onSetSampleX(int) = 0;
91};
92
93#endif // SkSampler_DEFINED
94