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) |
scroggo_chromium
2015/09/01 22:50:30
Note that these are no longer const, since FastSha
Peter Kasting
2015/09/02 22:08:55
I wonder if FastSharedBufferReader's getters shoul
scroggo_chromium
2015/09/02 23:01:07
That is the way I would have written; I was surpri
| |
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); | |
Peter Kasting
2015/09/02 22:08:55
I'm not convinced this is right. If we're only as
scroggo_chromium
2015/09/02 23:01:07
Wait, what's the problem? I originally called getC
Peter Kasting
2015/09/02 23:48:44
Ah, my bad, I didn't read the code correctly.
I s
| |
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 here in little-endian mode, the caller |
Peter Kasting
2015/09/02 22:08:55
Nit: Might as well remove "in little-endian mode"
scroggo_chromium
2015/09/02 23:01:07
Done.
| |
209 // won't read it. | 211 // won't read it. |
210 uint32_t pixel; | 212 uint32_t pixel; |
211 memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3); | 213 memcpy(&pixel, encodedPixel, 3); |
212 #if CPU(BIG_ENDIAN) | 214 #if CPU(BIG_ENDIAN) |
213 pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixe l & 0xff000000) >> 24); | 215 pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixe l & 0xff000000) >> 24); |
214 #endif | 216 #endif |
215 return pixel; | 217 return pixel; |
216 } | 218 } |
217 | 219 |
218 case 4: | 220 case 4: |
219 return readUint32(offset); | 221 return readUint32(encodedPixel); |
220 | 222 |
221 default: | 223 default: |
222 ASSERT_NOT_REACHED(); | 224 ASSERT_NOT_REACHED(); |
223 return 0; | 225 return 0; |
224 } | 226 } |
225 } | 227 } |
226 | 228 |
227 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) | 229 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) |
228 // in the given pixel data. | 230 // in the given pixel data. |
229 inline unsigned getComponent(uint32_t pixel, int component) const | 231 inline unsigned getComponent(uint32_t pixel, int component) const |
230 { | 232 { |
231 uint8_t value = (pixel & m_bitMasks[component]) >> m_bitShiftsRight[comp onent]; | 233 uint8_t value = (pixel & m_bitMasks[component]) >> m_bitShiftsRight[comp onent]; |
232 return m_lookupTableAddresses[component] ? m_lookupTableAddresses[compon ent][value] : value; | 234 return m_lookupTableAddresses[component] ? m_lookupTableAddresses[compon ent][value] : value; |
233 } | 235 } |
234 | 236 |
235 inline unsigned getAlpha(uint32_t pixel) const | 237 inline unsigned getAlpha(uint32_t pixel) const |
236 { | 238 { |
237 // For images without alpha, return alpha of 0xff. | 239 // For images without alpha, return alpha of 0xff. |
238 return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff; | 240 return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff; |
239 } | 241 } |
240 | 242 |
243 // Retrieve one byte from |m_data| at |offset| | |
Peter Kasting
2015/09/02 22:08:55
Nit: Trailing period
scroggo_chromium
2015/09/02 23:01:07
Done.
| |
244 uint8_t getByte(size_t offset) | |
245 { | |
246 const char* segment; | |
Peter Kasting
2015/09/02 22:08:55
Why is this not just a call to m_fastReader.getOne
scroggo_chromium
2015/09/02 23:01:07
Wrote this function before I had a fastReader. Don
| |
247 m_data->getSomeData(segment, offset); | |
248 return segment[0]; | |
249 } | |
250 | |
251 | |
241 // Sets the current pixel to the color given by |colorIndex|. This also | 252 // Sets the current pixel to the color given by |colorIndex|. This also |
242 // increments the relevant local variables to move the current pixel | 253 // increments the relevant local variables to move the current pixel |
243 // right by one. | 254 // right by one. |
244 inline void setI(size_t colorIndex) | 255 inline void setI(size_t colorIndex) |
245 { | 256 { |
246 setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGre en, m_colorTable[colorIndex].rgbBlue, 0xff); | 257 setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGre en, m_colorTable[colorIndex].rgbBlue, 0xff); |
247 } | 258 } |
248 | 259 |
249 // Like setI(), but with the individual component values specified. | 260 // Like setI(), but with the individual component values specified. |
250 inline void setRGBA(unsigned red, | 261 inline void setRGBA(unsigned red, |
(...skipping 25 matching lines...) Expand all Loading... | |
276 void moveBufferToNextRow(); | 287 void moveBufferToNextRow(); |
277 | 288 |
278 // The decoder that owns us. | 289 // The decoder that owns us. |
279 ImageDecoder* m_parent; | 290 ImageDecoder* m_parent; |
280 | 291 |
281 // The destination for the pixel data. | 292 // The destination for the pixel data. |
282 ImageFrame* m_buffer; | 293 ImageFrame* m_buffer; |
283 | 294 |
284 // The file to decode. | 295 // The file to decode. |
285 RefPtr<SharedBuffer> m_data; | 296 RefPtr<SharedBuffer> m_data; |
297 FastSharedBufferReader m_fastReader; | |
286 | 298 |
287 // An index into |m_data| representing how much we've already decoded. | 299 // An index into |m_data| representing how much we've already decoded. |
288 size_t m_decodedOffset; | 300 size_t m_decodedOffset; |
289 | 301 |
290 // The file offset at which the BMP info header starts. | 302 // The file offset at which the BMP info header starts. |
291 size_t m_headerOffset; | 303 size_t m_headerOffset; |
292 | 304 |
293 // The file offset at which the actual image bits start. When decoding | 305 // 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 | 306 // 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 | 307 // 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 | 365 // 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 | 366 // (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 | 367 // header, thus doubling it). If |m_isInICO| is true, this variable tracks |
356 // whether we've begun decoding this mask yet. | 368 // whether we've begun decoding this mask yet. |
357 bool m_decodingAndMask; | 369 bool m_decodingAndMask; |
358 }; | 370 }; |
359 | 371 |
360 } // namespace blink | 372 } // namespace blink |
361 | 373 |
362 #endif | 374 #endif |
OLD | NEW |