2 * Copyright (c) 2008, 2009, Google 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 are
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
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.
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.
31 #ifndef ICOImageDecoder_h
32 #define ICOImageDecoder_h
34 #include "BMPImageReader.h"
38 class PNGImageDecoder;
40 // This class decodes the ICO and CUR image formats.
41 class ICOImageDecoder : public ImageDecoder {
43 ICOImageDecoder(ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption);
44 virtual ~ICOImageDecoder();
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 ImageFrame* frameBufferAtIndex(size_t);
55 // CAUTION: setFailed() deletes all readers and decoders. Be careful to
56 // avoid accessing deleted memory, especially when calling this from
57 // inside BMPImageReader!
58 virtual bool setFailed();
67 struct IconDirectoryEntry {
70 uint32_t m_imageOffset;
73 // Returns true if |a| is a preferable icon entry to |b|.
74 // Larger sizes, or greater bitdepths at the same size, are preferable.
75 static bool compareEntries(const IconDirectoryEntry& a, const IconDirectoryEntry& b);
77 inline uint16_t readUint16(int offset) const
79 return BMPImageReader::readUint16(m_data.get(), m_decodedOffset + offset);
82 inline uint32_t readUint32(int offset) const
84 return BMPImageReader::readUint32(m_data.get(), m_decodedOffset + offset);
87 // If the desired PNGImageDecoder exists, gives it the appropriate data.
88 void setDataForPNGDecoderAtIndex(size_t);
90 // Decodes the entry at |index|. If |onlySize| is true, stops decoding
91 // after calculating the image size. If decoding fails but there is no
92 // more data coming, sets the "decode failure" flag.
93 void decode(size_t index, bool onlySize);
95 // Decodes the directory and directory entries at the beginning of the
96 // data, and initializes members. Returns true if all decoding
97 // succeeded. Once this returns true, all entries' sizes are known.
98 bool decodeDirectory();
100 // Decodes the specified entry.
101 bool decodeAtIndex(size_t);
103 // Processes the ICONDIR at the beginning of the data. Returns true if
104 // the directory could be decoded.
105 bool processDirectory();
107 // Processes the ICONDIRENTRY records after the directory. Keeps the
108 // "best" entry as the one we'll decode. Returns true if the entries
110 bool processDirectoryEntries();
112 // Reads and returns a directory entry from the current offset into
114 IconDirectoryEntry readDirectoryEntry();
116 // Determines whether the desired entry is a BMP or PNG. Returns true
117 // if the type could be determined.
118 ImageType imageTypeAtIndex(size_t);
120 // An index into |m_data| representing how much we've already decoded.
121 // Note that this only tracks data _this_ class decodes; once the
122 // BMPImageReader takes over this will not be updated further.
123 size_t m_decodedOffset;
125 // The headers for the ICO.
126 typedef Vector<IconDirectoryEntry> IconDirectoryEntries;
127 IconDirectoryEntries m_dirEntries;
129 // The image decoders for the various frames.
130 typedef Vector<OwnPtr<BMPImageReader> > BMPReaders;
131 BMPReaders m_bmpReaders;
132 typedef Vector<OwnPtr<PNGImageDecoder> > PNGDecoders;
133 PNGDecoders m_pngDecoders;
135 // Valid only while a BMPImageReader is decoding, this holds the size
136 // for the particular entry being decoded.
140 } // namespace WebCore