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_
30 #include "ImageSource.h"
31 #include "SharedBuffer.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 const RGBA32Array& bytes() const { return m_bytes; }
50 RGBA32Array& bytes() { return m_bytes; }
51 const IntRect& rect() const { return m_rect; }
52 unsigned height() const { return m_height; }
53 FrameStatus status() const { return m_status; }
54 unsigned duration() const { return m_duration; }
55 bool includeInNextFrame() const { return m_includeInNextFrame; }
56 bool hasAlpha() const { return m_hasAlpha; }
58 void setRect(const IntRect& r) { m_rect = r; }
59 void ensureHeight(unsigned rowIndex) { if (rowIndex > m_height) m_height = rowIndex; }
60 void setStatus(FrameStatus s) { m_status = s; }
61 void setDuration(unsigned duration) { m_duration = duration; }
62 void setIncludeInNextFrame(bool n) { m_includeInNextFrame = n; }
63 void setHasAlpha(bool alpha) { m_hasAlpha = alpha; }
65 static void setRGBA(unsigned& pos, unsigned r, unsigned g, unsigned b, unsigned a)
67 // We store this data pre-multiplied.
72 float alphaPercent = a / 255.0f;
73 r = static_cast<unsigned>(r * alphaPercent);
74 g = static_cast<unsigned>(g * alphaPercent);
75 b = static_cast<unsigned>(b * alphaPercent);
77 pos = (a << 24 | r << 16 | g << 8 | b);
83 IntRect m_rect; // The rect of the original specified frame within the overall buffer.
84 // This will always just be the entire buffer except for GIF frames
85 // whose original rect was smaller than the overall image size.
86 unsigned m_height; // The height (the number of rows we've fully decoded).
87 FrameStatus m_status; // Whether or not this frame is completely finished decoding.
88 unsigned m_duration; // The animation delay.
89 bool m_includeInNextFrame; // Whether or not the next buffer should be initially populated with our data.
90 bool m_hasAlpha; // Whether or not any of the pixels in the buffer have transparency.
93 // The ImageDecoder class represents a base class for specific image format decoders
94 // (e.g., GIF, JPG, PNG, ICO) to derive from. All decoders decode into RGBA32 format
95 // and the base class manages the RGBA32 frame cache.
99 ImageDecoder() :m_sizeAvailable(false), m_failed(false) {}
100 virtual ~ImageDecoder() {}
102 // All specific decoder plugins must do something with the data they are given.
103 virtual void setData(SharedBuffer* data, bool allDataReceived) { m_data = data; }
105 // Whether or not the size information has been decoded yet.
106 virtual bool isSizeAvailable() const = 0;
108 // Requests the size.
109 virtual IntSize size() const { return m_size; }
111 // The total number of frames for the image. Classes that support multiple frames
112 // will scan the image data for the answer if they need to (without necessarily
113 // decoding all of the individual frames).
114 virtual int frameCount() { return 1; }
116 // The number of repetitions to perform for an animation loop.
117 virtual int repetitionCount() const { return cAnimationNone; }
119 // Called to obtain the RGBA32Buffer full of decoded data for rendering. The
120 // decoder plugin will decode as much of the frame as it can before handing
122 virtual RGBA32Buffer* frameBufferAtIndex(size_t index) = 0;
124 // Whether or not the underlying image format even supports alpha transparency.
125 virtual bool supportsAlpha() const { return true; }
127 bool failed() const { return m_failed; }
128 void setFailed() { m_failed = true; }
131 RefPtr<SharedBuffer> m_data; // The encoded data.
132 Vector<RGBA32Buffer> m_frameBufferCache;
133 bool m_sizeAvailable;
134 mutable bool m_failed;