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