| 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 #include "platform/image-decoders/bmp/BMPImageDecoder.h" | |
| 32 | |
| 33 #include "platform/image-decoders/FastSharedBufferReader.h" | |
| 34 #include "platform/wtf/PtrUtil.h" | |
| 35 | |
| 36 namespace blink { | |
| 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 kSizeOfFileHeader = 14; | |
| 42 | |
| 43 BMPImageDecoder::BMPImageDecoder(AlphaOption alpha_option, | |
| 44 const ColorBehavior& color_behavior, | |
| 45 size_t max_decoded_bytes) | |
| 46 : ImageDecoder(alpha_option, color_behavior, max_decoded_bytes), | |
| 47 decoded_offset_(0) {} | |
| 48 | |
| 49 void BMPImageDecoder::OnSetData(SegmentReader* data) { | |
| 50 if (reader_) | |
| 51 reader_->SetData(data); | |
| 52 } | |
| 53 | |
| 54 bool BMPImageDecoder::SetFailed() { | |
| 55 reader_.reset(); | |
| 56 return ImageDecoder::SetFailed(); | |
| 57 } | |
| 58 | |
| 59 void BMPImageDecoder::Decode(bool only_size) { | |
| 60 if (Failed()) | |
| 61 return; | |
| 62 | |
| 63 // If we couldn't decode the image but we've received all the data, decoding | |
| 64 // has failed. | |
| 65 if (!DecodeHelper(only_size) && IsAllDataReceived()) | |
| 66 SetFailed(); | |
| 67 // If we're done decoding the image, we don't need the BMPImageReader | |
| 68 // anymore. (If we failed, |reader_| has already been cleared.) | |
| 69 else if (!frame_buffer_cache_.IsEmpty() && | |
| 70 (frame_buffer_cache_.front().GetStatus() == | |
| 71 ImageFrame::kFrameComplete)) | |
| 72 reader_.reset(); | |
| 73 } | |
| 74 | |
| 75 bool BMPImageDecoder::DecodeHelper(bool only_size) { | |
| 76 size_t img_data_offset = 0; | |
| 77 if ((decoded_offset_ < kSizeOfFileHeader) && | |
| 78 !ProcessFileHeader(img_data_offset)) | |
| 79 return false; | |
| 80 | |
| 81 if (!reader_) { | |
| 82 reader_ = WTF::WrapUnique( | |
| 83 new BMPImageReader(this, decoded_offset_, img_data_offset, false)); | |
| 84 reader_->SetData(data_.Get()); | |
| 85 } | |
| 86 | |
| 87 if (!frame_buffer_cache_.IsEmpty()) | |
| 88 reader_->SetBuffer(&frame_buffer_cache_.front()); | |
| 89 | |
| 90 return reader_->DecodeBMP(only_size); | |
| 91 } | |
| 92 | |
| 93 bool BMPImageDecoder::ProcessFileHeader(size_t& img_data_offset) { | |
| 94 // Read file header. | |
| 95 DCHECK(!decoded_offset_); | |
| 96 if (data_->size() < kSizeOfFileHeader) | |
| 97 return false; | |
| 98 | |
| 99 char buffer[kSizeOfFileHeader]; | |
| 100 FastSharedBufferReader fast_reader(data_); | |
| 101 const char* file_header = | |
| 102 fast_reader.GetConsecutiveData(0, kSizeOfFileHeader, buffer); | |
| 103 const uint16_t file_type = | |
| 104 (file_header[0] << 8) | static_cast<uint8_t>(file_header[1]); | |
| 105 img_data_offset = BMPImageReader::ReadUint32(&file_header[10]); | |
| 106 decoded_offset_ = kSizeOfFileHeader; | |
| 107 | |
| 108 // See if this is a bitmap filetype we understand. | |
| 109 enum { | |
| 110 BMAP = 0x424D, // "BM" | |
| 111 // The following additional OS/2 2.x header values (see | |
| 112 // http://www.fileformat.info/format/os2bmp/egff.htm ) aren't widely | |
| 113 // decoded, and are unlikely to be in much use. | |
| 114 /* | |
| 115 ICON = 0x4943, // "IC" | |
| 116 POINTER = 0x5054, // "PT" | |
| 117 COLORICON = 0x4349, // "CI" | |
| 118 COLORPOINTER = 0x4350, // "CP" | |
| 119 BITMAPARRAY = 0x4241, // "BA" | |
| 120 */ | |
| 121 }; | |
| 122 return (file_type == BMAP) || SetFailed(); | |
| 123 } | |
| 124 | |
| 125 } // namespace blink | |
| OLD | NEW |