1a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)/*
2a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
3a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
4a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * modification, are permitted provided that the following conditions are
6a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * met:
7a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
8a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
11a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
12a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * in the documentation and/or other materials provided with the
13a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * distribution.
14a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
15a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
16a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * this software without specific prior written permission.
17a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
18a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) */
30a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
31a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#ifndef ICOImageDecoder_h
32a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#define ICOImageDecoder_h
33a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
34a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/image-decoders/bmp/BMPImageReader.h"
35a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
36c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
37a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
38a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)class PNGImageDecoder;
39a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
40a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// This class decodes the ICO and CUR image formats.
41a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)class PLATFORM_EXPORT ICOImageDecoder : public ImageDecoder {
42a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)public:
43a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ICOImageDecoder(ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption, size_t maxDecodedBytes);
44a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    virtual ~ICOImageDecoder();
45a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
46a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // ImageDecoder
4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual String filenameExtension() const OVERRIDE { return "ico"; }
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void setData(SharedBuffer*, bool allDataReceived) OVERRIDE;
4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool isSizeAvailable() OVERRIDE;
5009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual IntSize size() const OVERRIDE;
5109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual IntSize frameSizeAtIndex(size_t) const OVERRIDE;
5209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool setSize(unsigned width, unsigned height) OVERRIDE;
5309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual size_t frameCount() OVERRIDE;
5409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual ImageFrame* frameBufferAtIndex(size_t) OVERRIDE;
55a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // CAUTION: setFailed() deletes all readers and decoders.  Be careful to
56a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // avoid accessing deleted memory, especially when calling this from
57a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // inside BMPImageReader!
5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool setFailed() OVERRIDE;
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool hotSpot(IntPoint&) const OVERRIDE;
60a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
61a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)private:
62a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    enum ImageType {
63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        Unknown,
64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        BMP,
65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        PNG,
66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    };
67a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
68a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    enum FileType {
69a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        ICON = 1,
70a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        CURSOR = 2,
71a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    };
72a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
73a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    struct IconDirectoryEntry {
74a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        IntSize m_size;
75a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        uint16_t m_bitCount;
76a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        IntPoint m_hotSpot;
77a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        uint32_t m_imageOffset;
78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    };
79a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
80a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Returns true if |a| is a preferable icon entry to |b|.
81a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Larger sizes, or greater bitdepths at the same size, are preferable.
82a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    static bool compareEntries(const IconDirectoryEntry& a, const IconDirectoryEntry& b);
83a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
84a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    inline uint16_t readUint16(int offset) const
85a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    {
86a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        return BMPImageReader::readUint16(m_data.get(), m_decodedOffset + offset);
87a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
88a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
89a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    inline uint32_t readUint32(int offset) const
90a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    {
91a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        return BMPImageReader::readUint32(m_data.get(), m_decodedOffset + offset);
92a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
93a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
94a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // If the desired PNGImageDecoder exists, gives it the appropriate data.
95a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    void setDataForPNGDecoderAtIndex(size_t);
96a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
97a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Decodes the entry at |index|.  If |onlySize| is true, stops decoding
98a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // after calculating the image size.  If decoding fails but there is no
99a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // more data coming, sets the "decode failure" flag.
100a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    void decode(size_t index, bool onlySize);
101a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
102a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Decodes the directory and directory entries at the beginning of the
103a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // data, and initializes members.  Returns true if all decoding
104a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // succeeded.  Once this returns true, all entries' sizes are known.
105a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    bool decodeDirectory();
106a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
107a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Decodes the specified entry.
108a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    bool decodeAtIndex(size_t);
109a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
110a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Processes the ICONDIR at the beginning of the data.  Returns true if
111a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // the directory could be decoded.
112a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    bool processDirectory();
113a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
114a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Processes the ICONDIRENTRY records after the directory.  Keeps the
115a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // "best" entry as the one we'll decode.  Returns true if the entries
116a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // could be decoded.
117a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    bool processDirectoryEntries();
118a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
119a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Stores the hot-spot for |index| in |hotSpot| and returns true,
120a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // or returns false if there is none.
121a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    bool hotSpotAtIndex(size_t index, IntPoint& hotSpot) const;
122a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
123a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Reads and returns a directory entry from the current offset into
124a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // |data|.
125a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    IconDirectoryEntry readDirectoryEntry();
126a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
127a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Determines whether the desired entry is a BMP or PNG.  Returns true
128a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // if the type could be determined.
129a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ImageType imageTypeAtIndex(size_t);
130a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
131a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // An index into |m_data| representing how much we've already decoded.
132a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Note that this only tracks data _this_ class decodes; once the
133a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // BMPImageReader takes over this will not be updated further.
134a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    size_t m_decodedOffset;
135a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
136a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Which type of file (ICO/CUR) this is.
137a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    FileType m_fileType;
138a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
139a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // The headers for the ICO.
140a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    typedef Vector<IconDirectoryEntry> IconDirectoryEntries;
141a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    IconDirectoryEntries m_dirEntries;
142a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
143a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // The image decoders for the various frames.
144a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    typedef Vector<OwnPtr<BMPImageReader> > BMPReaders;
145a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    BMPReaders m_bmpReaders;
146a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    typedef Vector<OwnPtr<PNGImageDecoder> > PNGDecoders;
147a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    PNGDecoders m_pngDecoders;
148a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
149a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Valid only while a BMPImageReader is decoding, this holds the size
150a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // for the particular entry being decoded.
151a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    IntSize m_frameSize;
152a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)};
153a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
154c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
155a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
156a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#endif
157