root/trunk/BAL/ImageDecoder/WebCore/ICO/WK/BCICOImageDecoderWK.h

Revision 1380, 5.6 kB (checked in by gpenin, 7 months ago)

merge with webkit revision 54942

Line 
1 /*
2  * Copyright (c) 2008, 2009, Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef ICOImageDecoder_h
32 #define ICOImageDecoder_h
33
34 #include "BMPImageReader.h"
35
36 namespace WebCore {
37
38     class PNGImageDecoder;
39
40     // This class decodes the ICO and CUR image formats.
41     class ICOImageDecoder : public ImageDecoder {
42     public:
43         ICOImageDecoder();
44         virtual ~ICOImageDecoder();
45
46         // ImageDecoder
47         virtual String filenameExtension() const { return "ico"; }
48         virtual void setData(SharedBuffer*, bool allDataReceived);
49         virtual bool isSizeAvailable();
50         virtual IntSize size() const;
51         virtual IntSize frameSizeAtIndex(size_t) const;
52         virtual bool setSize(unsigned width, unsigned height);
53         virtual size_t frameCount();
54         virtual RGBA32Buffer* frameBufferAtIndex(size_t);
55
56     private:
57         enum ImageType {
58             Unknown,
59             BMP,
60             PNG,
61         };
62
63         struct IconDirectoryEntry {
64             IntSize m_size;
65             uint16_t m_bitCount;
66             uint32_t m_imageOffset;
67         };
68
69         // Returns true if |a| is a preferable icon entry to |b|.
70         // Larger sizes, or greater bitdepths at the same size, are preferable.
71         static bool compareEntries(const IconDirectoryEntry& a, const IconDirectoryEntry& b);
72
73         inline uint16_t readUint16(int offset) const
74         {
75             return BMPImageReader::readUint16(m_data.get(), m_decodedOffset + offset);
76         }
77
78         inline uint32_t readUint32(int offset) const
79         {
80             return BMPImageReader::readUint32(m_data.get(), m_decodedOffset + offset);
81         }
82
83         // If the desired PNGImageDecoder exists, gives it the appropriate data.
84         void setDataForPNGDecoderAtIndex(size_t);
85
86         // Decodes the entry at |index|.  If |onlySize| is true, stops decoding
87         // after calculating the image size.  If decoding fails but there is no
88         // more data coming, sets the "decode failure" flag.
89         //
90         // NOTE: If the desired entry is a PNG, this doesn't actually trigger
91         // decoding, it merely ensures the decoder is created and ready to
92         // decode.  The caller will then call a function on the PNGImageDecoder
93         // that actually triggers decoding.
94         void decode(size_t index, bool onlySize);
95
96         // Decodes the directory and directory entries at the beginning of the
97         // data, and initializes members.  Returns true if all decoding
98         // succeeded.  Once this returns true, all entries' sizes are known.
99         bool decodeDirectory();
100
101         // Decodes the specified entry.
102         bool decodeAtIndex(size_t);
103
104         // Processes the ICONDIR at the beginning of the data.  Returns true if
105         // the directory could be decoded.
106         bool processDirectory();
107
108         // Processes the ICONDIRENTRY records after the directory.  Keeps the
109         // "best" entry as the one we'll decode.  Returns true if the entries
110         // could be decoded.
111         bool processDirectoryEntries();
112
113         // Reads and returns a directory entry from the current offset into
114         // |data|.
115         IconDirectoryEntry readDirectoryEntry();
116
117         // Determines whether the desired entry is a BMP or PNG.  Returns true
118         // if the type could be determined.
119         ImageType imageTypeAtIndex(size_t);
120
121         // An index into |m_data| representing how much we've already decoded.
122         // Note that this only tracks data _this_ class decodes; once the
123         // BMPImageReader takes over this will not be updated further.
124         size_t m_decodedOffset;
125
126         // The headers for the ICO.
127         typedef Vector<IconDirectoryEntry> IconDirectoryEntries;
128         IconDirectoryEntries m_dirEntries;
129
130         // The image decoders for the various frames.
131         typedef Vector<OwnPtr<BMPImageReader> > BMPReaders;
132         BMPReaders m_bmpReaders;
133         typedef Vector<OwnPtr<PNGImageDecoder> > PNGDecoders;
134         PNGDecoders m_pngDecoders;
135
136         // Valid only while a BMPImageReader is decoding, this holds the size
137         // for the particular entry being decoded.
138         IntSize m_frameSize;
139     };
140
141 } // namespace WebCore
142
143 #endif
144
Note: See TracBrowser for help on using the browser.