~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/platform/image-decoders/ImageDecoder.h

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
18
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
19
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
20
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
21
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
23
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
24
 */
 
25
 
 
26
#ifndef IMAGE_DECODER_H_
 
27
#define IMAGE_DECODER_H_
 
28
 
 
29
#include "config.h"
 
30
#include "IntRect.h"
 
31
#include "ImageSource.h"
 
32
#include <wtf/Vector.h>
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
typedef Vector<unsigned> RGBA32Array;
 
37
 
 
38
// The RGBA32Buffer object represents the decoded image data in RGBA32 format.  This buffer is what all
 
39
// decoders write a single frame into.  Frames are then instantiated for drawing by being handed this buffer.
 
40
class RGBA32Buffer
 
41
{
 
42
public:
 
43
    enum FrameStatus { FrameEmpty, FramePartial, FrameComplete };
 
44
 
 
45
    RGBA32Buffer() : m_height(0), m_status(FrameEmpty), m_duration(0),
 
46
                     m_includeInNextFrame(false), m_hasAlpha(false)
 
47
    {} 
 
48
 
 
49
    RGBA32Array& bytes() { return m_bytes; }
 
50
    const IntRect& rect() const { return m_rect; }
 
51
    unsigned height() { return m_height; }
 
52
    FrameStatus status() const { return m_status; }
 
53
    unsigned duration() const { return m_duration; }
 
54
    bool includeInNextFrame() const { return m_includeInNextFrame; }
 
55
    bool hasAlpha() const { return m_hasAlpha; }
 
56
 
 
57
    void setRect(const IntRect& r) { m_rect = r; }
 
58
    void ensureHeight(unsigned rowIndex) { if (rowIndex > m_height) m_height = rowIndex; }
 
59
    void setStatus(FrameStatus s) { m_status = s; }
 
60
    void setDuration(unsigned duration) { m_duration = duration; }
 
61
    void setIncludeInNextFrame(bool n) { m_includeInNextFrame = n; }
 
62
    void setHasAlpha(bool alpha) { m_hasAlpha = alpha; }
 
63
 
 
64
    static void setRGBA(unsigned& pos, unsigned r, unsigned g, unsigned b, unsigned a)
 
65
    {
 
66
        // We store this data pre-multiplied.
 
67
        if (a == 0)
 
68
            pos = 0;
 
69
        else {
 
70
            if (a < 255) {
 
71
                float alphaPercent = a / 255.0f;
 
72
                r = static_cast<unsigned>(r * alphaPercent);
 
73
                g = static_cast<unsigned>(g * alphaPercent);
 
74
                b = static_cast<unsigned>(b * alphaPercent);
 
75
            }
 
76
            pos = (a << 24 | r << 16 | g << 8 | b);
 
77
        }
 
78
    }
 
79
 
 
80
private:
 
81
    RGBA32Array m_bytes;
 
82
    IntRect m_rect;    // The rect of the original specified frame within the overall buffer.
 
83
                       // This will always just be the entire buffer except for GIF frames
 
84
                       // whose original rect was smaller than the overall image size.
 
85
    unsigned m_height; // The height (the number of rows we've fully decoded).
 
86
    FrameStatus m_status; // Whether or not this frame is completely finished decoding.
 
87
    unsigned m_duration; // The animation delay.
 
88
    bool m_includeInNextFrame; // Whether or not the next buffer should be initially populated with our data.
 
89
    bool m_hasAlpha; // Whether or not any of the pixels in the buffer have transparency.
 
90
};
 
91
 
 
92
// The ImageDecoder class represents a base class for specific image format decoders
 
93
// (e.g., GIF, JPG, PNG, ICO) to derive from.  All decoders decode into RGBA32 format
 
94
// and the base class manages the RGBA32 frame cache.
 
95
class ImageDecoder
 
96
{
 
97
public:
 
98
    ImageDecoder() :m_sizeAvailable(false), m_failed(false) {}
 
99
    virtual ~ImageDecoder() {}
 
100
 
 
101
    // All specific decoder plugins must do something with the data they are given.
 
102
    virtual void setData(const Vector<char>& data, bool allDataReceived) { m_data = data; }
 
103
 
 
104
    // Whether or not the size information has been decoded yet.
 
105
    virtual bool isSizeAvailable() const = 0;
 
106
 
 
107
    // Requests the size.
 
108
    virtual IntSize size() const { return m_size; }
 
109
 
 
110
    // The total number of frames for the image.  Classes that support multiple frames
 
111
    // will scan the image data for the answer if they need to (without necessarily
 
112
    // decoding all of the individual frames).
 
113
    virtual int frameCount() { return 1; }
 
114
 
 
115
    // The number of repetitions to perform for an animation loop.
 
116
    virtual int repetitionCount() const { return cAnimationNone; }
 
117
 
 
118
    // Called to obtain the RGBA32Buffer full of decoded data for rendering.  The
 
119
    // decoder plugin will decode as much of the frame as it can before handing
 
120
    // back the buffer.
 
121
    virtual RGBA32Buffer* frameBufferAtIndex(size_t index) = 0;
 
122
 
 
123
    // Whether or not the underlying image format even supports alpha transparency.
 
124
    virtual bool supportsAlpha() const { return true; }
 
125
 
 
126
    bool failed() const { return m_failed; }
 
127
    void setFailed() { m_failed = true; }
 
128
 
 
129
protected:
 
130
    Vector<char> m_data; // The encoded data.
 
131
    Vector<RGBA32Buffer> m_frameBufferCache;
 
132
    bool m_sizeAvailable;
 
133
    mutable bool m_failed;
 
134
    IntSize m_size;
 
135
};
 
136
 
 
137
}
 
138
 
 
139
#endif