2
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
26
#ifndef IMAGE_DECODER_H_
27
#define IMAGE_DECODER_H_
31
#include "ImageSource.h"
32
#include <wtf/Vector.h>
36
typedef Vector<unsigned> RGBA32Array;
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.
43
enum FrameStatus { FrameEmpty, FramePartial, FrameComplete };
45
RGBA32Buffer() : m_height(0), m_status(FrameEmpty), m_duration(0),
46
m_includeInNextFrame(false), m_hasAlpha(false)
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; }
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; }
64
static void setRGBA(unsigned& pos, unsigned r, unsigned g, unsigned b, unsigned a)
66
// We store this data pre-multiplied.
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);
76
pos = (a << 24 | r << 16 | g << 8 | b);
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.
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.
98
ImageDecoder() :m_sizeAvailable(false), m_failed(false) {}
99
virtual ~ImageDecoder() {}
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; }
104
// Whether or not the size information has been decoded yet.
105
virtual bool isSizeAvailable() const = 0;
107
// Requests the size.
108
virtual IntSize size() const { return m_size; }
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; }
115
// The number of repetitions to perform for an animation loop.
116
virtual int repetitionCount() const { return cAnimationNone; }
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
121
virtual RGBA32Buffer* frameBufferAtIndex(size_t index) = 0;
123
// Whether or not the underlying image format even supports alpha transparency.
124
virtual bool supportsAlpha() const { return true; }
126
bool failed() const { return m_failed; }
127
void setFailed() { m_failed = true; }
130
Vector<char> m_data; // The encoded data.
131
Vector<RGBA32Buffer> m_frameBufferCache;
132
bool m_sizeAvailable;
133
mutable bool m_failed;