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 SkMaskSwizzler_DEFINED
8#define SkMaskSwizzler_DEFINED
9
10#include "SkMasks.h"
11#include "SkSampler.h"
12#include "SkSwizzler.h"
13#include "SkTypes.h"
14
15/*
16 *
17 * Used to swizzle images whose pixel components are extracted by bit masks
18 * Currently only used by bmp
19 *
20 */
21class SkMaskSwizzler : public SkSampler {
22public:
23
24    /*
25     * Create a new swizzler
26     * @param masks Unowned pointer to helper class
27     */
28    static SkMaskSwizzler* CreateMaskSwizzler(const SkImageInfo& dstInfo,
29                                              const SkImageInfo& srcInfo,
30                                              SkMasks* masks,
31                                              uint32_t bitsPerPixel,
32                                              const SkCodec::Options& options);
33
34    /*
35     * Swizzle a row
36     */
37    void swizzle(void* dst, const uint8_t* SK_RESTRICT src);
38
39    /**
40     * Implement fill using a custom width.
41     */
42    void fill(const SkImageInfo& info, void* dst, size_t rowBytes, uint64_t colorOrIndex,
43            SkCodec::ZeroInitialized zeroInit) override {
44        const SkImageInfo fillInfo = info.makeWH(fDstWidth, info.height());
45        SkSampler::Fill(fillInfo, dst, rowBytes, colorOrIndex, zeroInit);
46    }
47
48    /**
49     *  Returns the byte offset at which we write to destination memory, taking
50     *  scaling, subsetting, and partial frames into account.
51     *  A similar function exists on SkSwizzler.
52     */
53    int swizzleWidth() const { return fDstWidth; }
54
55private:
56
57    /*
58     * Row procedure used for swizzle
59     */
60    typedef void (*RowProc)(void* dstRow, const uint8_t* srcRow, int width,
61            SkMasks* masks, uint32_t startX, uint32_t sampleX);
62
63    SkMaskSwizzler(SkMasks* masks, RowProc proc, int subsetWidth, int srcOffset);
64
65    int onSetSampleX(int) override;
66
67    SkMasks*        fMasks;           // unowned
68    const RowProc   fRowProc;
69
70    // FIXME: Can this class share more with SkSwizzler? These variables are all the same.
71    const int       fSubsetWidth;     // Width of the subset of source before any sampling.
72    int             fDstWidth;        // Width of dst, which may differ with sampling.
73    int             fSampleX;
74    int             fSrcOffset;
75    int             fX0;
76};
77
78#endif
79