Chromium Code Reviews| 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 <stdint.h> | 34 #include "platform/image-decoders/FastSharedBufferReader.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> | |
| 37 | 38 |
| 38 namespace blink { | 39 namespace blink { |
| 39 | 40 |
| 40 // This class decodes a BMP image. It is used in the BMP and ICO decoders, | 41 // This class decodes a BMP image. It is used in the BMP and ICO decoders, |
| 41 // which wrap it in the appropriate code to read file headers, etc. | 42 // which wrap it in the appropriate code to read file headers, etc. |
| 42 class PLATFORM_EXPORT BMPImageReader { | 43 class PLATFORM_EXPORT BMPImageReader { |
| 43 WTF_MAKE_FAST_ALLOCATED(BMPImageReader); | 44 WTF_MAKE_FAST_ALLOCATED(BMPImageReader); |
| 44 public: | 45 public: |
| 45 // Read a value from |data[offset]|, converting from little to native | 46 // Read a value from |buffer|, converting to an int assuming little |
| 46 // endianness. | 47 // endianness |
| 47 static inline uint16_t readUint16(SharedBuffer* data, int offset) | 48 static inline uint16_t readUint16(const char* buffer) |
| 48 { | 49 { |
| 49 uint16_t result; | 50 return *reinterpret_cast<const uint16_t*>(buffer); |
| 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; | |
| 55 } | 51 } |
| 56 | 52 |
| 57 static inline uint32_t readUint32(SharedBuffer* data, int offset) | 53 static inline uint32_t readUint32(const char* buffer) |
| 58 { | 54 { |
| 59 uint32_t result; | 55 return *reinterpret_cast<const uint32_t*>(buffer); |
| 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; | |
| 65 } | 56 } |
| 66 | 57 |
| 67 // |parent| is the decoder that owns us. | 58 // |parent| is the decoder that owns us. |
| 68 // |startOffset| points to the start of the BMP within the file. | 59 // |startOffset| points to the start of the BMP within the file. |
| 69 // |buffer| points at an empty ImageFrame that we'll initialize and | 60 // |buffer| points at an empty ImageFrame that we'll initialize and |
| 70 // fill with decoded data. | 61 // fill with decoded data. |
| 71 BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t i mgDataOffset, bool isInICO); | 62 BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t i mgDataOffset, bool isInICO); |
| 72 | 63 |
| 73 void setBuffer(ImageFrame* buffer) { m_buffer = buffer; } | 64 void setBuffer(ImageFrame* buffer) { m_buffer = buffer; } |
| 74 void setData(SharedBuffer* data) { m_data = data; } | 65 void setData(SharedBuffer* data) |
| 66 { | |
| 67 m_data = data; | |
| 68 m_fastReader.setData(data); | |
| 69 } | |
| 75 | 70 |
| 76 // Does the actual decoding. If |onlySize| is true, decoding only | 71 // Does the actual decoding. If |onlySize| is true, decoding only |
| 77 // progresses as far as necessary to get the image size. Returns | 72 // progresses as far as necessary to get the image size. Returns |
| 78 // whether decoding succeeded. | 73 // whether decoding succeeded. |
| 79 bool decodeBMP(bool onlySize); | 74 bool decodeBMP(bool onlySize); |
| 80 | 75 |
| 81 // Helper for decodeBMP() which will call either processRLEData() or | 76 // Helper for decodeBMP() which will call either processRLEData() or |
| 82 // processNonRLEData(), depending on the value of |nonRLE|, call any | 77 // processNonRLEData(), depending on the value of |nonRLE|, call any |
| 83 // appropriate notifications to deal with the result, then return whether | 78 // appropriate notifications to deal with the result, then return whether |
| 84 // decoding succeeded. | 79 // decoding succeeded. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 uint16_t biBitCount; | 112 uint16_t biBitCount; |
| 118 CompressionType biCompression; | 113 CompressionType biCompression; |
| 119 uint32_t biClrUsed; | 114 uint32_t biClrUsed; |
| 120 }; | 115 }; |
| 121 struct RGBTriple { | 116 struct RGBTriple { |
| 122 uint8_t rgbBlue; | 117 uint8_t rgbBlue; |
| 123 uint8_t rgbGreen; | 118 uint8_t rgbGreen; |
| 124 uint8_t rgbRed; | 119 uint8_t rgbRed; |
| 125 }; | 120 }; |
| 126 | 121 |
| 127 inline uint16_t readUint16(int offset) const | 122 inline uint16_t readUint16(int offset) |
| 128 { | 123 { |
| 129 return readUint16(m_data.get(), m_decodedOffset + offset); | 124 char buffer[2]; |
| 125 const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + off set, 2, buffer); | |
| 126 return readUint16(data); | |
| 130 } | 127 } |
| 131 | 128 |
| 132 inline uint32_t readUint32(int offset) const | 129 inline uint32_t readUint32(int offset) |
| 133 { | 130 { |
| 134 return readUint32(m_data.get(), m_decodedOffset + offset); | 131 char buffer[4]; |
| 132 const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + off set, 4, buffer); | |
| 133 return readUint32(data); | |
| 135 } | 134 } |
| 136 | 135 |
| 137 // Determines the size of the BMP info header. Returns true if the size | 136 // Determines the size of the BMP info header. Returns true if the size |
| 138 // is valid. | 137 // is valid. |
| 139 bool readInfoHeaderSize(); | 138 bool readInfoHeaderSize(); |
| 140 | 139 |
| 141 // Processes the BMP info header. Returns true if the info header could | 140 // Processes the BMP info header. Returns true if the info header could |
| 142 // be decoded. | 141 // be decoded. |
| 143 bool processInfoHeader(); | 142 bool processInfoHeader(); |
| 144 | 143 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 inline bool pastEndOfImage(int numRows) | 188 inline bool pastEndOfImage(int numRows) |
| 190 { | 189 { |
| 191 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height ()) : ((m_coord.y() - numRows) < 0); | 190 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height ()) : ((m_coord.y() - numRows) < 0); |
| 192 } | 191 } |
| 193 | 192 |
| 194 // Returns the pixel data for the current X coordinate in a uint32_t. | 193 // Returns the pixel data for the current X coordinate in a uint32_t. |
| 195 // Assumes m_decodedOffset has been set to the beginning of the current | 194 // Assumes m_decodedOffset has been set to the beginning of the current |
| 196 // row. | 195 // row. |
| 197 // NOTE: Only as many bytes of the return value as are needed to hold | 196 // NOTE: Only as many bytes of the return value as are needed to hold |
| 198 // the pixel data will actually be set. | 197 // the pixel data will actually be set. |
| 199 inline uint32_t readCurrentPixel(int bytesPerPixel) const | 198 inline uint32_t readCurrentPixel(int bytesPerPixel) |
| 200 { | 199 { |
| 200 // We need at most 4 bytes, starting at m_decodedOffset + offset. | |
| 201 char buffer[4]; | |
| 201 const int offset = m_coord.x() * bytesPerPixel; | 202 const int offset = m_coord.x() * bytesPerPixel; |
| 203 const char* encodedPixel = m_fastReader.getConsecutiveData(m_decodedOffs et + offset, bytesPerPixel, buffer); | |
| 202 switch (bytesPerPixel) { | 204 switch (bytesPerPixel) { |
| 203 case 2: | 205 case 2: |
| 204 return readUint16(offset); | 206 return readUint16(encodedPixel); |
| 205 | 207 |
| 206 case 3: { | 208 case 3: { |
| 207 // It doesn't matter that we never set the most significant byte | 209 // It doesn't matter that we never set the most significant byte |
| 208 // of the return value here in little-endian mode, the caller | 210 // of the return value, the caller won't read it. |
| 209 // won't read it. | |
| 210 uint32_t pixel; | 211 uint32_t pixel; |
| 211 memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3); | 212 memcpy(&pixel, encodedPixel, 3); |
| 212 #if CPU(BIG_ENDIAN) | |
| 213 pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixe l & 0xff000000) >> 24); | |
| 214 #endif | |
| 215 return pixel; | 213 return pixel; |
| 216 } | 214 } |
| 217 | 215 |
| 218 case 4: | 216 case 4: |
| 219 return readUint32(offset); | 217 return readUint32(encodedPixel); |
| 220 | 218 |
| 221 default: | 219 default: |
| 222 ASSERT_NOT_REACHED(); | 220 ASSERT_NOT_REACHED(); |
| 223 return 0; | 221 return 0; |
| 224 } | 222 } |
| 225 } | 223 } |
| 226 | 224 |
| 227 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) | 225 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) |
| 228 // in the given pixel data. | 226 // in the given pixel data. |
| 229 inline unsigned getComponent(uint32_t pixel, int component) const | 227 inline unsigned getComponent(uint32_t pixel, int component) const |
| 230 { | 228 { |
| 231 uint8_t value = (pixel & m_bitMasks[component]) >> m_bitShiftsRight[comp onent]; | 229 uint8_t value = (pixel & m_bitMasks[component]) >> m_bitShiftsRight[comp onent]; |
| 232 return m_lookupTableAddresses[component] ? m_lookupTableAddresses[compon ent][value] : value; | 230 return m_lookupTableAddresses[component] ? m_lookupTableAddresses[compon ent][value] : value; |
| 233 } | 231 } |
| 234 | 232 |
| 235 inline unsigned getAlpha(uint32_t pixel) const | 233 inline unsigned getAlpha(uint32_t pixel) const |
| 236 { | 234 { |
| 237 // For images without alpha, return alpha of 0xff. | 235 // For images without alpha, return alpha of 0xff. |
| 238 return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff; | 236 return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff; |
| 239 } | 237 } |
| 240 | 238 |
| 239 // Retrieve one byte from |m_data| at |offset|. | |
| 240 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
| |
| 241 { | |
| 242 return m_fastReader.getOneByte(offset); | |
| 243 } | |
| 244 | |
| 245 | |
| 241 // Sets the current pixel to the color given by |colorIndex|. This also | 246 // Sets the current pixel to the color given by |colorIndex|. This also |
| 242 // increments the relevant local variables to move the current pixel | 247 // increments the relevant local variables to move the current pixel |
| 243 // right by one. | 248 // right by one. |
| 244 inline void setI(size_t colorIndex) | 249 inline void setI(size_t colorIndex) |
| 245 { | 250 { |
| 246 setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGre en, m_colorTable[colorIndex].rgbBlue, 0xff); | 251 setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGre en, m_colorTable[colorIndex].rgbBlue, 0xff); |
| 247 } | 252 } |
| 248 | 253 |
| 249 // Like setI(), but with the individual component values specified. | 254 // Like setI(), but with the individual component values specified. |
| 250 inline void setRGBA(unsigned red, | 255 inline void setRGBA(unsigned red, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 276 void moveBufferToNextRow(); | 281 void moveBufferToNextRow(); |
| 277 | 282 |
| 278 // The decoder that owns us. | 283 // The decoder that owns us. |
| 279 ImageDecoder* m_parent; | 284 ImageDecoder* m_parent; |
| 280 | 285 |
| 281 // The destination for the pixel data. | 286 // The destination for the pixel data. |
| 282 ImageFrame* m_buffer; | 287 ImageFrame* m_buffer; |
| 283 | 288 |
| 284 // The file to decode. | 289 // The file to decode. |
| 285 RefPtr<SharedBuffer> m_data; | 290 RefPtr<SharedBuffer> m_data; |
| 291 FastSharedBufferReader m_fastReader; | |
| 286 | 292 |
| 287 // An index into |m_data| representing how much we've already decoded. | 293 // An index into |m_data| representing how much we've already decoded. |
| 288 size_t m_decodedOffset; | 294 size_t m_decodedOffset; |
| 289 | 295 |
| 290 // The file offset at which the BMP info header starts. | 296 // The file offset at which the BMP info header starts. |
| 291 size_t m_headerOffset; | 297 size_t m_headerOffset; |
| 292 | 298 |
| 293 // The file offset at which the actual image bits start. When decoding | 299 // The file offset at which the actual image bits start. When decoding |
| 294 // ICO files, this is set to 0, since it's not stored anywhere in a | 300 // ICO files, this is set to 0, since it's not stored anywhere in a |
| 295 // header; the reader functions expect the image data to start | 301 // header; the reader functions expect the image data to start |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 // ICOs store a 1bpp "mask" immediately after the main bitmap image data | 359 // ICOs store a 1bpp "mask" immediately after the main bitmap image data |
| 354 // (and, confusingly, add its height to the biHeight value in the info | 360 // (and, confusingly, add its height to the biHeight value in the info |
| 355 // header, thus doubling it). If |m_isInICO| is true, this variable tracks | 361 // header, thus doubling it). If |m_isInICO| is true, this variable tracks |
| 356 // whether we've begun decoding this mask yet. | 362 // whether we've begun decoding this mask yet. |
| 357 bool m_decodingAndMask; | 363 bool m_decodingAndMask; |
| 358 }; | 364 }; |
| 359 | 365 |
| 360 } // namespace blink | 366 } // namespace blink |
| 361 | 367 |
| 362 #endif | 368 #endif |
| OLD | NEW |