| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved. | 2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef BMPImageReader_h | 31 #ifndef BMPImageReader_h |
| 32 #define BMPImageReader_h | 32 #define BMPImageReader_h |
| 33 | 33 |
| 34 #include "platform/image-decoders/FastSharedBufferReader.h" | 34 #include <stdint.h> |
| 35 #include "platform/image-decoders/ImageDecoder.h" | 35 #include "platform/image-decoders/ImageDecoder.h" |
| 36 #include "wtf/CPU.h" | 36 #include "wtf/CPU.h" |
| 37 #include <stdint.h> | |
| 38 | 37 |
| 39 namespace blink { | 38 namespace blink { |
| 40 | 39 |
| 41 // This class decodes a BMP image. It is used in the BMP and ICO decoders, | 40 // This class decodes a BMP image. It is used in the BMP and ICO decoders, |
| 42 // which wrap it in the appropriate code to read file headers, etc. | 41 // which wrap it in the appropriate code to read file headers, etc. |
| 43 class PLATFORM_EXPORT BMPImageReader { | 42 class PLATFORM_EXPORT BMPImageReader { |
| 44 WTF_MAKE_FAST_ALLOCATED(BMPImageReader); | 43 WTF_MAKE_FAST_ALLOCATED(BMPImageReader); |
| 45 public: | 44 public: |
| 46 // Read a value from |buffer|, converting to an int assuming little | 45 // Read a value from |data[offset]|, converting from little to native |
| 47 // endianness | 46 // endianness. |
| 48 static inline uint16_t readUint16(const char* buffer) | 47 static inline uint16_t readUint16(SharedBuffer* data, int offset) |
| 49 { | 48 { |
| 50 return *reinterpret_cast<const uint16_t*>(buffer); | 49 uint16_t result; |
| 50 memcpy(&result, &data->data()[offset], 2); |
| 51 #if CPU(BIG_ENDIAN) |
| 52 result = ((result & 0xff) << 8) | ((result & 0xff00) >> 8); |
| 53 #endif |
| 54 return result; |
| 51 } | 55 } |
| 52 | 56 |
| 53 static inline uint32_t readUint32(const char* buffer) | 57 static inline uint32_t readUint32(SharedBuffer* data, int offset) |
| 54 { | 58 { |
| 55 return *reinterpret_cast<const uint32_t*>(buffer); | 59 uint32_t result; |
| 60 memcpy(&result, &data->data()[offset], 4); |
| 61 #if CPU(BIG_ENDIAN) |
| 62 result = ((result & 0xff) << 24) | ((result & 0xff00) << 8) | ((result &
0xff0000) >> 8) | ((result & 0xff000000) >> 24); |
| 63 #endif |
| 64 return result; |
| 56 } | 65 } |
| 57 | 66 |
| 58 // |parent| is the decoder that owns us. | 67 // |parent| is the decoder that owns us. |
| 59 // |startOffset| points to the start of the BMP within the file. | 68 // |startOffset| points to the start of the BMP within the file. |
| 60 // |buffer| points at an empty ImageFrame that we'll initialize and | 69 // |buffer| points at an empty ImageFrame that we'll initialize and |
| 61 // fill with decoded data. | 70 // fill with decoded data. |
| 62 BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t i
mgDataOffset, bool isInICO); | 71 BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t i
mgDataOffset, bool isInICO); |
| 63 | 72 |
| 64 void setBuffer(ImageFrame* buffer) { m_buffer = buffer; } | 73 void setBuffer(ImageFrame* buffer) { m_buffer = buffer; } |
| 65 void setData(SharedBuffer* data) | 74 void setData(SharedBuffer* data) { m_data = data; } |
| 66 { | |
| 67 m_data = data; | |
| 68 m_fastReader.setData(data); | |
| 69 } | |
| 70 | 75 |
| 71 // Does the actual decoding. If |onlySize| is true, decoding only | 76 // Does the actual decoding. If |onlySize| is true, decoding only |
| 72 // progresses as far as necessary to get the image size. Returns | 77 // progresses as far as necessary to get the image size. Returns |
| 73 // whether decoding succeeded. | 78 // whether decoding succeeded. |
| 74 bool decodeBMP(bool onlySize); | 79 bool decodeBMP(bool onlySize); |
| 75 | 80 |
| 76 // Helper for decodeBMP() which will call either processRLEData() or | 81 // Helper for decodeBMP() which will call either processRLEData() or |
| 77 // processNonRLEData(), depending on the value of |nonRLE|, call any | 82 // processNonRLEData(), depending on the value of |nonRLE|, call any |
| 78 // appropriate notifications to deal with the result, then return whether | 83 // appropriate notifications to deal with the result, then return whether |
| 79 // decoding succeeded. | 84 // decoding succeeded. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 uint16_t biBitCount; | 117 uint16_t biBitCount; |
| 113 CompressionType biCompression; | 118 CompressionType biCompression; |
| 114 uint32_t biClrUsed; | 119 uint32_t biClrUsed; |
| 115 }; | 120 }; |
| 116 struct RGBTriple { | 121 struct RGBTriple { |
| 117 uint8_t rgbBlue; | 122 uint8_t rgbBlue; |
| 118 uint8_t rgbGreen; | 123 uint8_t rgbGreen; |
| 119 uint8_t rgbRed; | 124 uint8_t rgbRed; |
| 120 }; | 125 }; |
| 121 | 126 |
| 122 inline uint8_t readUint8(size_t offset) const | |
| 123 { | |
| 124 return m_fastReader.getOneByte(m_decodedOffset + offset); | |
| 125 } | |
| 126 | |
| 127 inline uint16_t readUint16(int offset) const | 127 inline uint16_t readUint16(int offset) const |
| 128 { | 128 { |
| 129 char buffer[2]; | 129 return readUint16(m_data.get(), m_decodedOffset + offset); |
| 130 const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + off
set, 2, buffer); | |
| 131 return readUint16(data); | |
| 132 } | 130 } |
| 133 | 131 |
| 134 inline uint32_t readUint32(int offset) const | 132 inline uint32_t readUint32(int offset) const |
| 135 { | 133 { |
| 136 char buffer[4]; | 134 return readUint32(m_data.get(), m_decodedOffset + offset); |
| 137 const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + off
set, 4, buffer); | |
| 138 return readUint32(data); | |
| 139 } | 135 } |
| 140 | 136 |
| 141 // Determines the size of the BMP info header. Returns true if the size | 137 // Determines the size of the BMP info header. Returns true if the size |
| 142 // is valid. | 138 // is valid. |
| 143 bool readInfoHeaderSize(); | 139 bool readInfoHeaderSize(); |
| 144 | 140 |
| 145 // Processes the BMP info header. Returns true if the info header could | 141 // Processes the BMP info header. Returns true if the info header could |
| 146 // be decoded. | 142 // be decoded. |
| 147 bool processInfoHeader(); | 143 bool processInfoHeader(); |
| 148 | 144 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height
()) : ((m_coord.y() - numRows) < 0); | 191 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height
()) : ((m_coord.y() - numRows) < 0); |
| 196 } | 192 } |
| 197 | 193 |
| 198 // Returns the pixel data for the current X coordinate in a uint32_t. | 194 // Returns the pixel data for the current X coordinate in a uint32_t. |
| 199 // Assumes m_decodedOffset has been set to the beginning of the current | 195 // Assumes m_decodedOffset has been set to the beginning of the current |
| 200 // row. | 196 // row. |
| 201 // NOTE: Only as many bytes of the return value as are needed to hold | 197 // NOTE: Only as many bytes of the return value as are needed to hold |
| 202 // the pixel data will actually be set. | 198 // the pixel data will actually be set. |
| 203 inline uint32_t readCurrentPixel(int bytesPerPixel) const | 199 inline uint32_t readCurrentPixel(int bytesPerPixel) const |
| 204 { | 200 { |
| 205 // We need at most 4 bytes, starting at m_decodedOffset + offset. | |
| 206 char buffer[4]; | |
| 207 const int offset = m_coord.x() * bytesPerPixel; | 201 const int offset = m_coord.x() * bytesPerPixel; |
| 208 const char* encodedPixel = m_fastReader.getConsecutiveData(m_decodedOffs
et + offset, bytesPerPixel, buffer); | |
| 209 switch (bytesPerPixel) { | 202 switch (bytesPerPixel) { |
| 210 case 2: | 203 case 2: |
| 211 return readUint16(encodedPixel); | 204 return readUint16(offset); |
| 212 | 205 |
| 213 case 3: { | 206 case 3: { |
| 214 // It doesn't matter that we never set the most significant byte | 207 // It doesn't matter that we never set the most significant byte |
| 215 // of the return value, the caller won't read it. | 208 // of the return value here in little-endian mode, the caller |
| 209 // won't read it. |
| 216 uint32_t pixel; | 210 uint32_t pixel; |
| 217 memcpy(&pixel, encodedPixel, 3); | 211 memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3); |
| 212 #if CPU(BIG_ENDIAN) |
| 213 pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixe
l & 0xff000000) >> 24); |
| 214 #endif |
| 218 return pixel; | 215 return pixel; |
| 219 } | 216 } |
| 220 | 217 |
| 221 case 4: | 218 case 4: |
| 222 return readUint32(encodedPixel); | 219 return readUint32(offset); |
| 223 | 220 |
| 224 default: | 221 default: |
| 225 ASSERT_NOT_REACHED(); | 222 ASSERT_NOT_REACHED(); |
| 226 return 0; | 223 return 0; |
| 227 } | 224 } |
| 228 } | 225 } |
| 229 | 226 |
| 230 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) | 227 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) |
| 231 // in the given pixel data. | 228 // in the given pixel data. |
| 232 inline unsigned getComponent(uint32_t pixel, int component) const | 229 inline unsigned getComponent(uint32_t pixel, int component) const |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 void moveBufferToNextRow(); | 276 void moveBufferToNextRow(); |
| 280 | 277 |
| 281 // The decoder that owns us. | 278 // The decoder that owns us. |
| 282 ImageDecoder* m_parent; | 279 ImageDecoder* m_parent; |
| 283 | 280 |
| 284 // The destination for the pixel data. | 281 // The destination for the pixel data. |
| 285 ImageFrame* m_buffer; | 282 ImageFrame* m_buffer; |
| 286 | 283 |
| 287 // The file to decode. | 284 // The file to decode. |
| 288 RefPtr<SharedBuffer> m_data; | 285 RefPtr<SharedBuffer> m_data; |
| 289 FastSharedBufferReader m_fastReader; | |
| 290 | 286 |
| 291 // An index into |m_data| representing how much we've already decoded. | 287 // An index into |m_data| representing how much we've already decoded. |
| 292 size_t m_decodedOffset; | 288 size_t m_decodedOffset; |
| 293 | 289 |
| 294 // The file offset at which the BMP info header starts. | 290 // The file offset at which the BMP info header starts. |
| 295 size_t m_headerOffset; | 291 size_t m_headerOffset; |
| 296 | 292 |
| 297 // The file offset at which the actual image bits start. When decoding | 293 // The file offset at which the actual image bits start. When decoding |
| 298 // ICO files, this is set to 0, since it's not stored anywhere in a | 294 // ICO files, this is set to 0, since it's not stored anywhere in a |
| 299 // header; the reader functions expect the image data to start | 295 // header; the reader functions expect the image data to start |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 // ICOs store a 1bpp "mask" immediately after the main bitmap image data | 353 // ICOs store a 1bpp "mask" immediately after the main bitmap image data |
| 358 // (and, confusingly, add its height to the biHeight value in the info | 354 // (and, confusingly, add its height to the biHeight value in the info |
| 359 // header, thus doubling it). If |m_isInICO| is true, this variable tracks | 355 // header, thus doubling it). If |m_isInICO| is true, this variable tracks |
| 360 // whether we've begun decoding this mask yet. | 356 // whether we've begun decoding this mask yet. |
| 361 bool m_decodingAndMask; | 357 bool m_decodingAndMask; |
| 362 }; | 358 }; |
| 363 | 359 |
| 364 } // namespace blink | 360 } // namespace blink |
| 365 | 361 |
| 366 #endif | 362 #endif |
| OLD | NEW |