summaryrefslogtreecommitdiffstats
path: root/framesequence/jni/FrameSequence_webp.h
blob: a29574c8a2d65e1b1fda7db0de3d6b5de770c9d9 (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
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef RASTERMILL_FRAMESQUENCE_WEBP_H
#define RASTERMILL_FRAMESQUENCE_WEBP_H

#include "config.h"
#include "webp/decode.h"
#include "webp/demux.h"

#include "Stream.h"
#include "Color.h"
#include "FrameSequence.h"

// Parser for a possibly-animated WebP bitstream.
class FrameSequence_webp : public FrameSequence {
public:
    FrameSequence_webp(Stream* stream);
    virtual ~FrameSequence_webp();

    virtual int getWidth() const {
        if (!mDemux) {
            return 0;
        }
        return WebPDemuxGetI(mDemux, WEBP_FF_CANVAS_WIDTH);
    }

    virtual int getHeight() const {
        if (!mDemux) {
            return 0;
        }
        return WebPDemuxGetI(mDemux, WEBP_FF_CANVAS_HEIGHT);
    }

    virtual bool isOpaque() const {
        return !(mFormatFlags & ALPHA_FLAG);
    }

    virtual int getFrameCount() const {
        if (!mDemux) {
            return 0;
        }
        return WebPDemuxGetI(mDemux, WEBP_FF_FRAME_COUNT);
    }

    virtual int getDefaultLoopCount() const {
        return mLoopCount;
    }

    virtual jobject getRawByteBuffer() const {
        return mRawByteBuffer;
    }

    virtual FrameSequenceState* createState() const;

    WebPDemuxer* getDemuxer() const { return mDemux; }

    bool isKeyFrame(size_t frameNr) const { return mIsKeyFrame[frameNr]; }

private:
    void constructDependencyChain();

    WebPData mData;
    WebPDemuxer* mDemux;
    int mLoopCount;
    uint32_t mFormatFlags;
    // mIsKeyFrame[i] is true if ith canvas can be constructed without decoding any prior frames.
    bool* mIsKeyFrame;
    jobject mRawByteBuffer;
};

// Produces frames of a possibly-animated WebP file for display.
class FrameSequenceState_webp : public FrameSequenceState {
public:
    FrameSequenceState_webp(const FrameSequence_webp& frameSequence);
    virtual ~FrameSequenceState_webp();

    // Returns frame's delay time in milliseconds.
    virtual long drawFrame(int frameNr,
            Color8888* outputPtr, int outputPixelStride, int previousFrameNr);

private:
    void initializeFrame(const WebPIterator& currIter, Color8888* currBuffer, int currStride,
            const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride);
    bool decodeFrame(const WebPIterator& iter, Color8888* currBuffer, int currStride,
            const WebPIterator& prevIter, const Color8888* prevBuffer, int prevStride);

    const FrameSequence_webp& mFrameSequence;
    WebPDecoderConfig mDecoderConfig;
    Color8888* mPreservedBuffer;
};

#endif //RASTERMILL_FRAMESQUENCE_WEBP_H