| 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 | #include "config.h" |
| 32 | #include "BMPImageDecoder.h" |
| 33 | |
| 34 | #include "BMPImageReader.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | // Number of bits in .BMP used to store the file header (doesn't match |
| 39 | // "sizeof(BMPImageDecoder::BitmapFileHeader)" since we omit some fields and |
| 40 | // don't pack). |
| 41 | static const size_t = 14; |
| 42 | |
| 43 | BMPImageDecoder::BMPImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption) |
| 44 | : ScalableImageDecoder(alphaOption, gammaAndColorProfileOption) |
| 45 | , m_decodedOffset(0) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void BMPImageDecoder::setData(SharedBuffer& data, bool allDataReceived) |
| 50 | { |
| 51 | if (failed()) |
| 52 | return; |
| 53 | |
| 54 | ScalableImageDecoder::setData(data, allDataReceived); |
| 55 | if (m_reader) |
| 56 | m_reader->setData(&data); |
| 57 | } |
| 58 | |
| 59 | ScalableImageDecoderFrame* BMPImageDecoder::frameBufferAtIndex(size_t index) |
| 60 | { |
| 61 | if (index) |
| 62 | return 0; |
| 63 | |
| 64 | if (m_frameBufferCache.isEmpty()) |
| 65 | m_frameBufferCache.grow(1); |
| 66 | |
| 67 | auto* buffer = &m_frameBufferCache.first(); |
| 68 | if (!buffer->isComplete()) |
| 69 | decode(false, isAllDataReceived()); |
| 70 | return buffer; |
| 71 | } |
| 72 | |
| 73 | bool BMPImageDecoder::setFailed() |
| 74 | { |
| 75 | m_reader = nullptr; |
| 76 | return ScalableImageDecoder::setFailed(); |
| 77 | } |
| 78 | |
| 79 | void BMPImageDecoder::decode(bool onlySize, bool allDataReceived) |
| 80 | { |
| 81 | if (failed()) |
| 82 | return; |
| 83 | |
| 84 | // If we couldn't decode the image but we've received all the data, decoding |
| 85 | // has failed. |
| 86 | if (!decodeHelper(onlySize) && allDataReceived) |
| 87 | setFailed(); |
| 88 | // If we're done decoding the image, we don't need the BMPImageReader |
| 89 | // anymore. (If we failed, |m_reader| has already been cleared.) |
| 90 | else if (!m_frameBufferCache.isEmpty() && m_frameBufferCache.first().isComplete()) |
| 91 | m_reader = nullptr; |
| 92 | } |
| 93 | |
| 94 | bool BMPImageDecoder::decodeHelper(bool onlySize) |
| 95 | { |
| 96 | size_t imgDataOffset = 0; |
| 97 | if ((m_decodedOffset < sizeOfFileHeader) && !processFileHeader(&imgDataOffset)) |
| 98 | return false; |
| 99 | |
| 100 | if (!m_reader) { |
| 101 | m_reader = std::make_unique<BMPImageReader>(this, m_decodedOffset, imgDataOffset, false); |
| 102 | m_reader->setData(m_data.get()); |
| 103 | } |
| 104 | |
| 105 | if (!m_frameBufferCache.isEmpty()) |
| 106 | m_reader->setBuffer(&m_frameBufferCache.first()); |
| 107 | |
| 108 | return m_reader->decodeBMP(onlySize); |
| 109 | } |
| 110 | |
| 111 | bool BMPImageDecoder::(size_t* imgDataOffset) |
| 112 | { |
| 113 | ASSERT(imgDataOffset); |
| 114 | |
| 115 | // Read file header. |
| 116 | ASSERT(!m_decodedOffset); |
| 117 | if (m_data->size() < sizeOfFileHeader) |
| 118 | return false; |
| 119 | const uint16_t fileType = (m_data->data()[0] << 8) | static_cast<uint8_t>(m_data->data()[1]); |
| 120 | *imgDataOffset = readUint32(10); |
| 121 | m_decodedOffset = sizeOfFileHeader; |
| 122 | |
| 123 | // See if this is a bitmap filetype we understand. |
| 124 | enum { |
| 125 | BMAP = 0x424D, // "BM" |
| 126 | // The following additional OS/2 2.x header values (see |
| 127 | // http://www.fileformat.info/format/os2bmp/egff.htm ) aren't widely |
| 128 | // decoded, and are unlikely to be in much use. |
| 129 | /* |
| 130 | ICON = 0x4943, // "IC" |
| 131 | POINTER = 0x5054, // "PT" |
| 132 | COLORICON = 0x4349, // "CI" |
| 133 | COLORPOINTER = 0x4350, // "CP" |
| 134 | BITMAPARRAY = 0x4241, // "BA" |
| 135 | */ |
| 136 | }; |
| 137 | return (fileType == BMAP) || setFailed(); |
| 138 | } |
| 139 | |
| 140 | } // namespace WebCore |
| 141 | |