| OLD | NEW |
| (Empty) |
| 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 "platform/image-decoders/FastSharedBufferReader.h" | |
| 35 #include "platform/image-decoders/bmp/BMPImageReader.h" | |
| 36 #include <memory> | |
| 37 | |
| 38 namespace blink { | |
| 39 | |
| 40 class PNGImageDecoder; | |
| 41 | |
| 42 // This class decodes the ICO and CUR image formats. | |
| 43 class PLATFORM_EXPORT ICOImageDecoder final : public ImageDecoder { | |
| 44 WTF_MAKE_NONCOPYABLE(ICOImageDecoder); | |
| 45 | |
| 46 public: | |
| 47 ICOImageDecoder(AlphaOption, const ColorBehavior&, size_t max_decoded_bytes); | |
| 48 ~ICOImageDecoder() override; | |
| 49 | |
| 50 // ImageDecoder: | |
| 51 String FilenameExtension() const override { return "ico"; } | |
| 52 void OnSetData(SegmentReader*) override; | |
| 53 IntSize Size() const override; | |
| 54 IntSize FrameSizeAtIndex(size_t) const override; | |
| 55 bool SetSize(unsigned width, unsigned height) override; | |
| 56 bool FrameIsCompleteAtIndex(size_t) const override; | |
| 57 // CAUTION: SetFailed() deletes all readers and decoders. Be careful to | |
| 58 // avoid accessing deleted memory, especially when calling this from | |
| 59 // inside BMPImageReader! | |
| 60 bool SetFailed() override; | |
| 61 bool HotSpot(IntPoint&) const override; | |
| 62 | |
| 63 private: | |
| 64 enum ImageType { | |
| 65 kUnknown, | |
| 66 BMP, | |
| 67 PNG, | |
| 68 }; | |
| 69 | |
| 70 enum FileType { | |
| 71 ICON = 1, | |
| 72 CURSOR = 2, | |
| 73 }; | |
| 74 | |
| 75 struct IconDirectoryEntry { | |
| 76 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | |
| 77 IntSize size_; | |
| 78 uint16_t bit_count_; | |
| 79 IntPoint hot_spot_; | |
| 80 uint32_t image_offset_; | |
| 81 uint32_t byte_size_; | |
| 82 }; | |
| 83 | |
| 84 // Returns true if |a| is a preferable icon entry to |b|. | |
| 85 // Larger sizes, or greater bitdepths at the same size, are preferable. | |
| 86 static bool CompareEntries(const IconDirectoryEntry& a, | |
| 87 const IconDirectoryEntry& b); | |
| 88 | |
| 89 // ImageDecoder: | |
| 90 void DecodeSize() override { Decode(0, true); } | |
| 91 size_t DecodeFrameCount() override; | |
| 92 void Decode(size_t index) override { Decode(index, false); } | |
| 93 | |
| 94 // TODO (scroggo): These functions are identical to functions in | |
| 95 // BMPImageReader. Share code? | |
| 96 inline uint8_t ReadUint8(size_t offset) const { | |
| 97 return fast_reader_.GetOneByte(decoded_offset_ + offset); | |
| 98 } | |
| 99 | |
| 100 inline uint16_t ReadUint16(int offset) const { | |
| 101 char buffer[2]; | |
| 102 const char* data = | |
| 103 fast_reader_.GetConsecutiveData(decoded_offset_ + offset, 2, buffer); | |
| 104 return BMPImageReader::ReadUint16(data); | |
| 105 } | |
| 106 | |
| 107 inline uint32_t ReadUint32(int offset) const { | |
| 108 char buffer[4]; | |
| 109 const char* data = | |
| 110 fast_reader_.GetConsecutiveData(decoded_offset_ + offset, 4, buffer); | |
| 111 return BMPImageReader::ReadUint32(data); | |
| 112 } | |
| 113 | |
| 114 // If the desired PNGImageDecoder exists, gives it the appropriate data. | |
| 115 void SetDataForPNGDecoderAtIndex(size_t); | |
| 116 | |
| 117 // Decodes the entry at |index|. If |only_size| is true, stops decoding | |
| 118 // after calculating the image size. If decoding fails but there is no | |
| 119 // more data coming, sets the "decode failure" flag. | |
| 120 void Decode(size_t index, bool only_size); | |
| 121 | |
| 122 // Decodes the directory and directory entries at the beginning of the | |
| 123 // data, and initializes members. Returns true if all decoding | |
| 124 // succeeded. Once this returns true, all entries' sizes are known. | |
| 125 bool DecodeDirectory(); | |
| 126 | |
| 127 // Decodes the specified entry. | |
| 128 bool DecodeAtIndex(size_t); | |
| 129 | |
| 130 // Processes the ICONDIR at the beginning of the data. Returns true if | |
| 131 // the directory could be decoded. | |
| 132 bool ProcessDirectory(); | |
| 133 | |
| 134 // Processes the ICONDIRENTRY records after the directory. Keeps the | |
| 135 // "best" entry as the one we'll decode. Returns true if the entries | |
| 136 // could be decoded. | |
| 137 bool ProcessDirectoryEntries(); | |
| 138 | |
| 139 // Stores the hot-spot for |index| in |hot_spot| and returns true, | |
| 140 // or returns false if there is none. | |
| 141 bool HotSpotAtIndex(size_t index, IntPoint& hot_spot) const; | |
| 142 | |
| 143 // Reads and returns a directory entry from the current offset into | |
| 144 // |data|. | |
| 145 IconDirectoryEntry ReadDirectoryEntry(); | |
| 146 | |
| 147 // Determines whether the desired entry is a BMP or PNG. Returns true | |
| 148 // if the type could be determined. | |
| 149 ImageType ImageTypeAtIndex(size_t); | |
| 150 | |
| 151 FastSharedBufferReader fast_reader_; | |
| 152 | |
| 153 // An index into |data_| representing how much we've already decoded. | |
| 154 // Note that this only tracks data _this_ class decodes; once the | |
| 155 // BMPImageReader takes over this will not be updated further. | |
| 156 size_t decoded_offset_; | |
| 157 | |
| 158 // Which type of file (ICO/CUR) this is. | |
| 159 FileType file_type_; | |
| 160 | |
| 161 // The headers for the ICO. | |
| 162 typedef Vector<IconDirectoryEntry> IconDirectoryEntries; | |
| 163 IconDirectoryEntries dir_entries_; | |
| 164 | |
| 165 // Count of directory entries is parsed from header before initializing | |
| 166 // dir_entries_. dir_entries_ is populated only when full header | |
| 167 // information including directory entries is available. | |
| 168 size_t dir_entries_count_; | |
| 169 | |
| 170 // The image decoders for the various frames. | |
| 171 typedef Vector<std::unique_ptr<BMPImageReader>> BMPReaders; | |
| 172 BMPReaders bmp_readers_; | |
| 173 typedef Vector<std::unique_ptr<PNGImageDecoder>> PNGDecoders; | |
| 174 PNGDecoders png_decoders_; | |
| 175 | |
| 176 // Valid only while a BMPImageReader is decoding, this holds the size | |
| 177 // for the particular entry being decoded. | |
| 178 IntSize frame_size_; | |
| 179 | |
| 180 // Used to pass on to an internally created PNG decoder. | |
| 181 const ColorBehavior color_behavior_; | |
| 182 }; | |
| 183 | |
| 184 } // namespace blink | |
| 185 | |
| 186 #endif | |
| OLD | NEW |