| 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 | #pragma once |
| 32 | |
| 33 | #include "BMPImageReader.h" |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | // This class decodes the ICO and CUR image formats. |
| 38 | class ICOImageDecoder final : public ScalableImageDecoder { |
| 39 | public: |
| 40 | static Ref<ScalableImageDecoder> create(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption) |
| 41 | { |
| 42 | return adoptRef(*new ICOImageDecoder(alphaOption, gammaAndColorProfileOption)); |
| 43 | } |
| 44 | |
| 45 | virtual ~ICOImageDecoder(); |
| 46 | |
| 47 | // ScalableImageDecoder |
| 48 | String filenameExtension() const final { return "ico"_s ; } |
| 49 | void setData(SharedBuffer&, bool allDataReceived) final; |
| 50 | IntSize size() const final; |
| 51 | IntSize frameSizeAtIndex(size_t, SubsamplingLevel) const final; |
| 52 | bool setSize(const IntSize&) final; |
| 53 | size_t frameCount() const final; |
| 54 | ScalableImageDecoderFrame* frameBufferAtIndex(size_t) final; |
| 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 | bool setFailed() final; |
| 59 | Optional<IntPoint> hotSpot() const final; |
| 60 | |
| 61 | private: |
| 62 | enum ImageType { |
| 63 | Unknown, |
| 64 | BMP, |
| 65 | PNG, |
| 66 | }; |
| 67 | |
| 68 | enum FileType { |
| 69 | ICON = 1, |
| 70 | CURSOR = 2, |
| 71 | }; |
| 72 | |
| 73 | struct IconDirectoryEntry { |
| 74 | IntSize m_size; |
| 75 | uint16_t m_bitCount; |
| 76 | IntPoint m_hotSpot; |
| 77 | uint32_t m_imageOffset; |
| 78 | }; |
| 79 | |
| 80 | ICOImageDecoder(AlphaOption, GammaAndColorProfileOption); |
| 81 | void tryDecodeSize(bool allDataReceived) final { decode(0, true, allDataReceived); } |
| 82 | |
| 83 | // Returns true if |a| is a preferable icon entry to |b|. |
| 84 | // Larger sizes, or greater bitdepths at the same size, are preferable. |
| 85 | static bool compareEntries(const IconDirectoryEntry& a, const IconDirectoryEntry& b); |
| 86 | |
| 87 | inline uint16_t readUint16(int offset) const |
| 88 | { |
| 89 | return BMPImageReader::readUint16(m_data.get(), m_decodedOffset + offset); |
| 90 | } |
| 91 | |
| 92 | inline uint32_t readUint32(int offset) const |
| 93 | { |
| 94 | return BMPImageReader::readUint32(m_data.get(), m_decodedOffset + offset); |
| 95 | } |
| 96 | |
| 97 | // If the desired PNGImageDecoder exists, gives it the appropriate data. |
| 98 | void setDataForPNGDecoderAtIndex(size_t); |
| 99 | |
| 100 | // Decodes the entry at |index|. If |onlySize| is true, stops decoding |
| 101 | // after calculating the image size. If decoding fails but there is no |
| 102 | // more data coming, sets the "decode failure" flag. |
| 103 | void decode(size_t index, bool onlySize, bool allDataReceived); |
| 104 | |
| 105 | // Decodes the directory and directory entries at the beginning of the |
| 106 | // data, and initializes members. Returns true if all decoding |
| 107 | // succeeded. Once this returns true, all entries' sizes are known. |
| 108 | bool decodeDirectory(); |
| 109 | |
| 110 | // Decodes the specified entry. |
| 111 | bool decodeAtIndex(size_t); |
| 112 | |
| 113 | // Processes the ICONDIR at the beginning of the data. Returns true if |
| 114 | // the directory could be decoded. |
| 115 | bool processDirectory(); |
| 116 | |
| 117 | // Processes the ICONDIRENTRY records after the directory. Keeps the |
| 118 | // "best" entry as the one we'll decode. Returns true if the entries |
| 119 | // could be decoded. |
| 120 | bool processDirectoryEntries(); |
| 121 | |
| 122 | // Returns the hot-spot for |index|, returns WTF::nullopt if there is none. |
| 123 | Optional<IntPoint> hotSpotAtIndex(size_t) const; |
| 124 | |
| 125 | // Reads and returns a directory entry from the current offset into |
| 126 | // |data|. |
| 127 | IconDirectoryEntry readDirectoryEntry(); |
| 128 | |
| 129 | // Determines whether the desired entry is a BMP or PNG. Returns true |
| 130 | // if the type could be determined. |
| 131 | ImageType imageTypeAtIndex(size_t); |
| 132 | |
| 133 | // An index into |m_data| representing how much we've already decoded. |
| 134 | // Note that this only tracks data _this_ class decodes; once the |
| 135 | // BMPImageReader takes over this will not be updated further. |
| 136 | size_t m_decodedOffset; |
| 137 | |
| 138 | // Which type of file (ICO/CUR) this is. |
| 139 | FileType m_fileType; |
| 140 | |
| 141 | // The headers for the ICO. |
| 142 | typedef Vector<IconDirectoryEntry> IconDirectoryEntries; |
| 143 | IconDirectoryEntries m_dirEntries; |
| 144 | |
| 145 | // The image decoders for the various frames. |
| 146 | typedef Vector<std::unique_ptr<BMPImageReader>> BMPReaders; |
| 147 | BMPReaders m_bmpReaders; |
| 148 | typedef Vector<RefPtr<ScalableImageDecoder>> PNGDecoders; |
| 149 | PNGDecoders m_pngDecoders; |
| 150 | |
| 151 | // Valid only while a BMPImageReader is decoding, this holds the size |
| 152 | // for the particular entry being decoded. |
| 153 | IntSize m_frameSize; |
| 154 | }; |
| 155 | |
| 156 | } // namespace WebCore |
| 157 | |