| 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 BMP image format. |
| 38 | class BMPImageDecoder final : public ScalableImageDecoder { |
| 39 | public: |
| 40 | static Ref<ScalableImageDecoder> create(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption) |
| 41 | { |
| 42 | return adoptRef(*new BMPImageDecoder(alphaOption, gammaAndColorProfileOption)); |
| 43 | } |
| 44 | |
| 45 | // ScalableImageDecoder |
| 46 | String filenameExtension() const final { return "bmp"_s ; } |
| 47 | void setData(SharedBuffer&, bool allDataReceived) final; |
| 48 | ScalableImageDecoderFrame* frameBufferAtIndex(size_t index) final; |
| 49 | // CAUTION: setFailed() deletes |m_reader|. Be careful to avoid |
| 50 | // accessing deleted memory, especially when calling this from inside |
| 51 | // BMPImageReader! |
| 52 | bool setFailed() final; |
| 53 | |
| 54 | private: |
| 55 | BMPImageDecoder(AlphaOption, GammaAndColorProfileOption); |
| 56 | void tryDecodeSize(bool allDataReceived) final { decode(true, allDataReceived); } |
| 57 | |
| 58 | inline uint32_t readUint32(int offset) const |
| 59 | { |
| 60 | return BMPImageReader::readUint32(m_data.get(), m_decodedOffset + offset); |
| 61 | } |
| 62 | |
| 63 | // Decodes the image. If |onlySize| is true, stops decoding after |
| 64 | // calculating the image size. If decoding fails but there is no more |
| 65 | // data coming, sets the "decode failure" flag. |
| 66 | void decode(bool onlySize, bool allDataReceived); |
| 67 | |
| 68 | // Decodes the image. If |onlySize| is true, stops decoding after |
| 69 | // calculating the image size. Returns whether decoding succeeded. |
| 70 | bool decodeHelper(bool onlySize); |
| 71 | |
| 72 | // Processes the file header at the beginning of the data. Sets |
| 73 | // |*imgDataOffset| based on the header contents. Returns true if the |
| 74 | // file header could be decoded. |
| 75 | bool (size_t* imgDataOffset); |
| 76 | |
| 77 | // An index into |m_data| representing how much we've already decoded. |
| 78 | // Note that this only tracks data _this_ class decodes; once the |
| 79 | // BMPImageReader takes over this will not be updated further. |
| 80 | size_t m_decodedOffset; |
| 81 | |
| 82 | // The reader used to do most of the BMP decoding. |
| 83 | std::unique_ptr<BMPImageReader> m_reader; |
| 84 | }; |
| 85 | |
| 86 | } // namespace WebCore |
| 87 | |