Chromium Code Reviews| Index: Source/platform/image-decoders/bmp/BMPImageReader.h |
| diff --git a/Source/platform/image-decoders/bmp/BMPImageReader.h b/Source/platform/image-decoders/bmp/BMPImageReader.h |
| index 83d70caf4a5bc5cbc5559ae760cc74958289e1f1..63c87707e44ba6b6f4e26b6b3bd1a7d4491f61f2 100644 |
| --- a/Source/platform/image-decoders/bmp/BMPImageReader.h |
| +++ b/Source/platform/image-decoders/bmp/BMPImageReader.h |
| @@ -31,9 +31,10 @@ |
| #ifndef BMPImageReader_h |
| #define BMPImageReader_h |
| -#include <stdint.h> |
| +#include "platform/image-decoders/FastSharedBufferReader.h" |
| #include "platform/image-decoders/ImageDecoder.h" |
| #include "wtf/CPU.h" |
| +#include <stdint.h> |
| namespace blink { |
| @@ -42,26 +43,16 @@ namespace blink { |
| class PLATFORM_EXPORT BMPImageReader { |
| WTF_MAKE_FAST_ALLOCATED(BMPImageReader); |
| public: |
| - // Read a value from |data[offset]|, converting from little to native |
| - // endianness. |
| - static inline uint16_t readUint16(SharedBuffer* data, int offset) |
| + // Read a value from |buffer|, converting to an int assuming little |
| + // endianness |
| + static inline uint16_t readUint16(const char* buffer) |
| { |
| - uint16_t result; |
| - memcpy(&result, &data->data()[offset], 2); |
| - #if CPU(BIG_ENDIAN) |
| - result = ((result & 0xff) << 8) | ((result & 0xff00) >> 8); |
| - #endif |
| - return result; |
| + return *reinterpret_cast<const uint16_t*>(buffer); |
| } |
| - static inline uint32_t readUint32(SharedBuffer* data, int offset) |
| + static inline uint32_t readUint32(const char* buffer) |
| { |
| - uint32_t result; |
| - memcpy(&result, &data->data()[offset], 4); |
| - #if CPU(BIG_ENDIAN) |
| - result = ((result & 0xff) << 24) | ((result & 0xff00) << 8) | ((result & 0xff0000) >> 8) | ((result & 0xff000000) >> 24); |
| - #endif |
| - return result; |
| + return *reinterpret_cast<const uint32_t*>(buffer); |
| } |
| // |parent| is the decoder that owns us. |
| @@ -71,7 +62,11 @@ public: |
| BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t imgDataOffset, bool isInICO); |
| void setBuffer(ImageFrame* buffer) { m_buffer = buffer; } |
| - void setData(SharedBuffer* data) { m_data = data; } |
| + void setData(SharedBuffer* data) |
| + { |
| + m_data = data; |
| + m_fastReader.setData(data); |
| + } |
| // Does the actual decoding. If |onlySize| is true, decoding only |
| // progresses as far as necessary to get the image size. Returns |
| @@ -124,14 +119,18 @@ private: |
| uint8_t rgbRed; |
| }; |
| - inline uint16_t readUint16(int offset) const |
| + inline uint16_t readUint16(int offset) |
| { |
| - return readUint16(m_data.get(), m_decodedOffset + offset); |
| + char buffer[2]; |
| + const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + offset, 2, buffer); |
| + return readUint16(data); |
| } |
| - inline uint32_t readUint32(int offset) const |
| + inline uint32_t readUint32(int offset) |
| { |
| - return readUint32(m_data.get(), m_decodedOffset + offset); |
| + char buffer[4]; |
| + const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + offset, 4, buffer); |
| + return readUint32(data); |
| } |
| // Determines the size of the BMP info header. Returns true if the size |
| @@ -196,27 +195,26 @@ private: |
| // row. |
| // NOTE: Only as many bytes of the return value as are needed to hold |
| // the pixel data will actually be set. |
| - inline uint32_t readCurrentPixel(int bytesPerPixel) const |
| + inline uint32_t readCurrentPixel(int bytesPerPixel) |
| { |
| + // We need at most 4 bytes, starting at m_decodedOffset + offset. |
| + char buffer[4]; |
| const int offset = m_coord.x() * bytesPerPixel; |
| + const char* encodedPixel = m_fastReader.getConsecutiveData(m_decodedOffset + offset, bytesPerPixel, buffer); |
| switch (bytesPerPixel) { |
| case 2: |
| - return readUint16(offset); |
| + return readUint16(encodedPixel); |
| case 3: { |
| // It doesn't matter that we never set the most significant byte |
| - // of the return value here in little-endian mode, the caller |
| - // won't read it. |
| + // of the return value, the caller won't read it. |
| uint32_t pixel; |
| - memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3); |
| - #if CPU(BIG_ENDIAN) |
| - pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixel & 0xff000000) >> 24); |
| - #endif |
| + memcpy(&pixel, encodedPixel, 3); |
| return pixel; |
| } |
| case 4: |
| - return readUint32(offset); |
| + return readUint32(encodedPixel); |
| default: |
| ASSERT_NOT_REACHED(); |
| @@ -238,6 +236,13 @@ private: |
| return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff; |
| } |
| + // Retrieve one byte from |m_data| at |offset|. |
| + uint8_t getByte(size_t offset) |
|
Peter Kasting
2015/09/02 23:48:44
Nit: Maybe this should be readUint8() and be up by
scroggo_chromium
2015/09/03 15:28:36
Good idea!
For consistency, it now adds m_decoded
|
| + { |
| + return m_fastReader.getOneByte(offset); |
| + } |
| + |
| + |
| // Sets the current pixel to the color given by |colorIndex|. This also |
| // increments the relevant local variables to move the current pixel |
| // right by one. |
| @@ -283,6 +288,7 @@ private: |
| // The file to decode. |
| RefPtr<SharedBuffer> m_data; |
| + FastSharedBufferReader m_fastReader; |
| // An index into |m_data| representing how much we've already decoded. |
| size_t m_decodedOffset; |