aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/codec/SkScanlineDecoder.h
blob: 79469739817ba8abd47239173c213ab284fbfa23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkScanlineDecoder_DEFINED
#define SkScanlineDecoder_DEFINED

#include "../private/SkTemplates.h"
#include "SkCodec.h"
#include "SkImageInfo.h"
#include "SkTypes.h"

class SkScanlineDecoder : public SkNoncopyable {
public:
    /**
     *  If this stream represents an encoded image that we know how to decode
     *  in scanlines, return an SkScanlineDecoder that can decode it. Otherwise
     *  return NULL.
     *
     *  start() must be called in order to decode any scanlines.
     *
     *  If NULL is returned, the stream is deleted immediately. Otherwise, the
     *  SkScanlineDecoder takes ownership of it, and will delete it when done
     *  with it.
     */
    static SkScanlineDecoder* NewFromStream(SkStream*);

    /**
     *  Similar to NewFromStream, but reads from an SkData.
     *
     *  Will take a ref if it returns a scanline decoder, else will not affect
     *  the data.
     */
    static SkScanlineDecoder* NewFromData(SkData*);

    /**
     *  Clean up after reading/skipping scanlines.
     *
     *  It is possible that not all scanlines will have been read/skipped.  In
     *  fact, in the case of subset decodes, it is likely that there will be
     *  scanlines at the bottom of the image that have been ignored.
     */
    virtual ~SkScanlineDecoder() {}

    /**
     *  Return a size that approximately supports the desired scale factor.
     *  The codec may not be able to scale efficiently to the exact scale
     *  factor requested, so return a size that approximates that scale.
     *  The returned value is the codec's suggestion for the closest valid
     *  scale that it can natively support
     *  FIXME: share this with SkCodec
     */
    SkISize getScaledDimensions(float desiredScale) {
        return this->onGetScaledDimensions(desiredScale);
    }

    /**
     *  Returns the default info, corresponding to the encoded data.
     */
    const SkImageInfo& getInfo() { return fSrcInfo; }

    /**
     *  Initialize on the first scanline, with the specified options.
     *
     *  This must be called in order to call getScanlnies or skipScanlines. If
     *  it has been called before, this will require rewinding the stream.
     *
     *  @param dstInfo Info of the destination. If the dimensions do not match
     *      those of getInfo, this implies a scale.
     *  @param options Contains decoding options, including if memory is zero
     *      initialized.
     *  @param ctable A pointer to a color table.  When dstInfo.colorType() is
     *      kIndex8, this should be non-NULL and have enough storage for 256
     *      colors.  The color table will be populated after decoding the palette.
     *  @param ctableCount A pointer to the size of the color table.  When
     *      dstInfo.colorType() is kIndex8, this should be non-NULL.  It will
     *      be modified to the true size of the color table (<= 256) after
     *      decoding the palette.
     *  @return Enum representing success or reason for failure.
     */
    SkCodec::Result start(const SkImageInfo& dstInfo, const SkCodec::Options* options,
                          SkPMColor ctable[], int* ctableCount);

    /**
     *  Simplified version of start() that asserts that info is NOT
     *  kIndex8_SkColorType and uses the default Options.
     */
    SkCodec::Result start(const SkImageInfo& dstInfo);

    /**
     *  Write the next countLines scanlines into dst.
     *
     *  Not valid to call before calling start().
     *
     *  @param dst Must be non-null, and large enough to hold countLines
     *      scanlines of size rowBytes.
     *  @param countLines Number of lines to write.
     *  @param rowBytes Number of bytes per row. Must be large enough to hold
     *      a scanline based on the SkImageInfo used to create this object.
     */
    SkCodec::Result getScanlines(void* dst, int countLines, size_t rowBytes) {
        SkASSERT(!fDstInfo.isEmpty());
        if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines <= 0
                || fCurrScanline + countLines > fDstInfo.height()) {
            return SkCodec::kInvalidParameters;
        }
        const SkCodec::Result result = this->onGetScanlines(dst, countLines, rowBytes);
        fCurrScanline += countLines;
        return result;
    }

    /**
     *  Skip count scanlines.
     *
     *  Not valid to call before calling start().
     *
     *  The default version just calls onGetScanlines and discards the dst.
     *  NOTE: If skipped lines are the only lines with alpha, this default
     *  will make reallyHasAlpha return true, when it could have returned
     *  false.
     */
    SkCodec::Result skipScanlines(int countLines) {
        SkASSERT(!fDstInfo.isEmpty());
        if (fCurrScanline + countLines > fDstInfo.height()) {
            // Arguably, we could just skip the scanlines which are remaining,
            // and return kSuccess. We choose to return invalid so the client
            // can catch their bug.
            return SkCodec::kInvalidParameters;
        }
        const SkCodec::Result result = this->onSkipScanlines(countLines);
        fCurrScanline += countLines;
        return result;
    }

    /**
     *  Some images may initially report that they have alpha due to the format
     *  of the encoded data, but then never use any colors which have alpha
     *  less than 100%. This function can be called *after* decoding to
     *  determine if such an image truly had alpha. Calling it before decoding
     *  is undefined.
     *  FIXME: see skbug.com/3582.
     */
    bool reallyHasAlpha() const {
        return this->onReallyHasAlpha();
    }

    /**
     *  Format of the encoded data.
     */
    SkEncodedFormat getEncodedFormat() const { return this->onGetEncodedFormat(); }

    /**
     * returns true if the image must be scaled, in the y direction, after reading, not during.
     * To scale afterwards, we first decode every line and then sample the lines we want afterwards.
     * An example is interlaced pngs, where calling getScanlines once (regardless of the count
     * used) needs to read the entire image, therefore it is inefficient to call
     * getScanlines more than once. Instead, it should only ever be called with all the
     * rows needed.
     */
    bool requiresPostYSampling() {
        return this->onRequiresPostYSampling();
    }

protected:
    SkScanlineDecoder(const SkImageInfo& srcInfo)
        : fSrcInfo(srcInfo)
        , fDstInfo()
        , fCurrScanline(0) {}

    virtual SkISize onGetScaledDimensions(float /* desiredScale */) {
        // By default, scaling is not supported.
        return this->getInfo().dimensions();
    }

    virtual SkEncodedFormat onGetEncodedFormat() const = 0;

    virtual bool onReallyHasAlpha() const { return false; }

    /**
     * returns true if the image type is hard to sample and must be scaled after reading, not during
     * An example is interlaced pngs, where the entire image must be read for each decode
     */
    virtual bool onRequiresPostYSampling() { return false; }

    const SkImageInfo& dstInfo() const { return fDstInfo; }

private:
    const SkImageInfo   fSrcInfo;
    SkImageInfo         fDstInfo;
    int                 fCurrScanline;

    virtual SkCodec::Result onStart(const SkImageInfo& dstInfo,
                                    const SkCodec::Options& options,
                                    SkPMColor ctable[], int* ctableCount) = 0;

    // Naive default version just calls onGetScanlines on temp memory.
    virtual SkCodec::Result onSkipScanlines(int countLines) {
        SkAutoMalloc storage(fDstInfo.minRowBytes());
        // Note that we pass 0 to rowBytes so we continue to use the same memory.
        // Also note that while getScanlines checks that rowBytes is big enough,
        // onGetScanlines bypasses that check.
        // Calling the virtual method also means we do not double count
        // countLines.
        return this->onGetScanlines(storage.get(), countLines, 0);
    }

    virtual SkCodec::Result onGetScanlines(void* dst, int countLines,
                                                    size_t rowBytes) = 0;

};
#endif // SkScanlineDecoder_DEFINED