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 BMPImageReader_h
32#define BMPImageReader_h
33
34#include <stdint.h>
35#include "ImageDecoder.h"
36
37namespace WebCore {
38
39    // This class decodes a BMP image.  It is used in the BMP and ICO decoders,
40    // which wrap it in the appropriate code to read file headers, etc.
41    class BMPImageReader {
42    public:
43        // Read a value from |data[offset]|, converting from little to native
44        // endianness.
45        static inline uint16_t readUint16(SharedBuffer* data, int offset)
46        {
47            uint16_t result;
48            memcpy(&result, &data->data()[offset], 2);
49        #if CPU(BIG_ENDIAN)
50            result = ((result & 0xff) << 8) | ((result & 0xff00) >> 8);
51        #endif
52            return result;
53        }
54
55        static inline uint32_t readUint32(SharedBuffer* data, int offset)
56        {
57            uint32_t result;
58            memcpy(&result, &data->data()[offset], 4);
59        #if CPU(BIG_ENDIAN)
60            result = ((result & 0xff) << 24) | ((result & 0xff00) << 8) | ((result & 0xff0000) >> 8) | ((result & 0xff000000) >> 24);
61        #endif
62            return result;
63        }
64
65        // |parent| is the decoder that owns us.
66        // |startOffset| points to the start of the BMP within the file.
67        // |buffer| points at an empty ImageFrame that we'll initialize and
68        // fill with decoded data.
69        BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t imgDataOffset, bool usesAndMask);
70
71        void setBuffer(ImageFrame* buffer) { m_buffer = buffer; }
72        void setData(SharedBuffer* data) { m_data = data; }
73
74        // Does the actual decoding.  If |onlySize| is true, decoding only
75        // progresses as far as necessary to get the image size.  Returns
76        // whether decoding succeeded.
77        bool decodeBMP(bool onlySize);
78
79    private:
80        // The various BMP compression types.  We don't currently decode all
81        // these.
82        enum CompressionType {
83            // Universal types
84            RGB = 0,
85            RLE8 = 1,
86            RLE4 = 2,
87            // Windows V3+ only
88            BITFIELDS = 3,
89            JPEG = 4,
90            PNG = 5,
91            // OS/2 2.x-only
92            HUFFMAN1D,  // Stored in file as 3
93            RLE24,      // Stored in file as 4
94        };
95        enum AndMaskState {
96            None,
97            NotYetDecoded,
98            Decoding,
99        };
100        enum ProcessingResult {
101            Success,
102            Failure,
103            InsufficientData,
104        };
105
106        // These are based on the Windows BITMAPINFOHEADER and RGBTRIPLE
107        // structs, but with unnecessary entries removed.
108        struct BitmapInfoHeader {
109            uint32_t biSize;
110            int32_t biWidth;
111            int32_t biHeight;
112            uint16_t biBitCount;
113            CompressionType biCompression;
114            uint32_t biClrUsed;
115        };
116        struct RGBTriple {
117            uint8_t rgbBlue;
118            uint8_t rgbGreen;
119            uint8_t rgbRed;
120        };
121
122        inline uint16_t readUint16(int offset) const
123        {
124            return readUint16(m_data.get(), m_decodedOffset + offset);
125        }
126
127        inline uint32_t readUint32(int offset) const
128        {
129            return readUint32(m_data.get(), m_decodedOffset + offset);
130        }
131
132        // Determines the size of the BMP info header.  Returns true if the size
133        // is valid.
134        bool readInfoHeaderSize();
135
136        // Processes the BMP info header.  Returns true if the info header could
137        // be decoded.
138        bool processInfoHeader();
139
140        // Helper function for processInfoHeader() which does the actual reading
141        // of header values from the byte stream.  Returns false on error.
142        bool readInfoHeader();
143
144        // Returns true if this is a Windows V4+ BMP.
145        inline bool isWindowsV4Plus() const
146        {
147            // Windows V4 info header is 108 bytes.  V5 is 124 bytes.
148            return (m_infoHeader.biSize == 108) || (m_infoHeader.biSize == 124);
149        }
150
151        // Returns false if consistency errors are found in the info header.
152        bool isInfoHeaderValid() const;
153
154        // For BI_BITFIELDS images, initializes the m_bitMasks[] and
155        // m_bitOffsets[] arrays.  processInfoHeader() will initialize these for
156        // other compression types where needed.
157        bool processBitmasks();
158
159        // For paletted images, allocates and initializes the m_colorTable[]
160        // array.
161        bool processColorTable();
162
163        // Processes an RLE-encoded image.  Returns true if the entire image was
164        // decoded.
165        bool processRLEData();
166
167        // Processes a set of non-RLE-compressed pixels.  Two cases:
168        //   * inRLE = true: the data is inside an RLE-encoded bitmap.  Tries to
169        //     process |numPixels| pixels on the current row.
170        //   * inRLE = false: the data is inside a non-RLE-encoded bitmap.
171        //     |numPixels| is ignored.  Expects |m_coord| to point at the
172        //     beginning of the next row to be decoded.  Tries to process as
173        //     many complete rows as possible.  Returns InsufficientData if
174        //     there wasn't enough data to decode the whole image.
175        //
176        // This function returns a ProcessingResult instead of a bool so that it
177        // can avoid calling m_parent->setFailed(), which could lead to memory
178        // corruption since that will delete |this| but some callers still want
179        // to access member variables after this returns.
180        ProcessingResult processNonRLEData(bool inRLE, int numPixels);
181
182        // Returns true if the current y-coordinate plus |numRows| would be past
183        // the end of the image.  Here "plus" means "toward the end of the
184        // image", so downwards for m_isTopDown images and upwards otherwise.
185        inline bool pastEndOfImage(int numRows)
186        {
187            return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height()) : ((m_coord.y() - numRows) < 0);
188        }
189
190        // Returns the pixel data for the current X coordinate in a uint32_t.
191        // Assumes m_decodedOffset has been set to the beginning of the current
192        // row.
193        // NOTE: Only as many bytes of the return value as are needed to hold
194        // the pixel data will actually be set.
195        inline uint32_t readCurrentPixel(int bytesPerPixel) const
196        {
197            const int offset = m_coord.x() * bytesPerPixel;
198            switch (bytesPerPixel) {
199            case 2:
200                return readUint16(offset);
201
202            case 3: {
203                // It doesn't matter that we never set the most significant byte
204                // of the return value here in little-endian mode, the caller
205                // won't read it.
206                uint32_t pixel;
207                memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3);
208        #if CPU(BIG_ENDIAN)
209                pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixel & 0xff000000) >> 24);
210        #endif
211                return pixel;
212            }
213
214            case 4:
215                return readUint32(offset);
216
217            default:
218                ASSERT_NOT_REACHED();
219                return 0;
220            }
221        }
222
223        // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A)
224        // in the given pixel data.
225        inline unsigned getComponent(uint32_t pixel, int component) const
226        {
227            return ((pixel & m_bitMasks[component]) >> m_bitShiftsRight[component]) << m_bitShiftsLeft[component];
228        }
229
230        inline unsigned getAlpha(uint32_t pixel) const
231        {
232            // For images without alpha, return alpha of 0xff.
233            return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff;
234        }
235
236        // Sets the current pixel to the color given by |colorIndex|.  This also
237        // increments the relevant local variables to move the current pixel
238        // right by one.
239        inline void setI(size_t colorIndex)
240        {
241            setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGreen, m_colorTable[colorIndex].rgbBlue, 0xff);
242        }
243
244        // Like setI(), but with the individual component values specified.
245        inline void setRGBA(unsigned red,
246                            unsigned green,
247                            unsigned blue,
248                            unsigned alpha)
249        {
250            m_buffer->setRGBA(m_coord.x(), m_coord.y(), red, green, blue, alpha);
251            m_coord.move(1, 0);
252        }
253
254        // Fills pixels from the current X-coordinate up to, but not including,
255        // |endCoord| with the color given by the individual components.  This
256        // also increments the relevant local variables to move the current
257        // pixel right to |endCoord|.
258        inline void fillRGBA(int endCoord,
259                             unsigned red,
260                             unsigned green,
261                             unsigned blue,
262                             unsigned alpha)
263        {
264            while (m_coord.x() < endCoord)
265                setRGBA(red, green, blue, alpha);
266        }
267
268        // Resets the relevant local variables to start drawing at the left edge
269        // of the "next" row, where "next" is above or below the current row
270        // depending on the value of |m_isTopDown|.
271        void moveBufferToNextRow();
272
273        // The decoder that owns us.
274        ImageDecoder* m_parent;
275
276        // The destination for the pixel data.
277        ImageFrame* m_buffer;
278
279        // The file to decode.
280        RefPtr<SharedBuffer> m_data;
281
282        // An index into |m_data| representing how much we've already decoded.
283        size_t m_decodedOffset;
284
285        // The file offset at which the BMP info header starts.
286        size_t m_headerOffset;
287
288        // The file offset at which the actual image bits start.  When decoding
289        // ICO files, this is set to 0, since it's not stored anywhere in a
290        // header; the reader functions expect the image data to start
291        // immediately after the header and (if necessary) color table.
292        size_t m_imgDataOffset;
293
294        // The BMP info header.
295        BitmapInfoHeader m_infoHeader;
296
297        // True if this is an OS/2 1.x (aka Windows 2.x) BMP.  The struct
298        // layouts for this type of BMP are slightly different from the later,
299        // more common formats.
300        bool m_isOS21x;
301
302        // True if this is an OS/2 2.x BMP.  The meanings of compression types 3
303        // and 4 for this type of BMP differ from Windows V3+ BMPs.
304        //
305        // This will be falsely negative in some cases, but only ones where the
306        // way we misinterpret the data is irrelevant.
307        bool m_isOS22x;
308
309        // True if the BMP is not vertically flipped, that is, the first line of
310        // raster data in the file is the top line of the image.
311        bool m_isTopDown;
312
313        // These flags get set to false as we finish each processing stage.
314        bool m_needToProcessBitmasks;
315        bool m_needToProcessColorTable;
316
317        // Masks/offsets for the color values for non-palette formats.  These
318        // are bitwise, with array entries 0, 1, 2, 3 corresponding to R, G, B,
319        // A.
320        //
321        // The right/left shift values are meant to be applied after the masks.
322        // We need to right shift to compensate for the bitfields' offsets into
323        // the 32 bits of pixel data, and left shift to scale the color values
324        // up for fields with less than 8 bits of precision.  Sadly, we can't
325        // just combine these into one shift value because the net shift amount
326        // could go either direction.  (If only "<< -x" were equivalent to
327        // ">> x"...)
328        uint32_t m_bitMasks[4];
329        int m_bitShiftsRight[4];
330        int m_bitShiftsLeft[4];
331
332        // The color palette, for paletted formats.
333        size_t m_tableSizeInBytes;
334        Vector<RGBTriple> m_colorTable;
335
336        // The coordinate to which we've decoded the image.
337        IntPoint m_coord;
338
339        // Variables that track whether we've seen pixels with alpha values != 0
340        // and == 0, respectively.  See comments in processNonRLEData() on how
341        // these are used.
342        bool m_seenNonZeroAlphaPixel;
343        bool m_seenZeroAlphaPixel;
344
345        // ICOs store a 1bpp "mask" immediately after the main bitmap image data
346        // (and, confusingly, add its height to the biHeight value in the info
347        // header, thus doubling it).  This variable tracks whether we have such
348        // a mask and if we've started decoding it yet.
349        AndMaskState m_andMaskState;
350    };
351
352} // namespace WebCore
353
354#endif
355