Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: Source/platform/image-decoders/bmp/BMPImageReader.h

Issue 1259083003: Do not consolidate data in BMPImageDecoder (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@SegmentedBuffer
Patch Set: Read exactly the amount needed in getConsecutiveData Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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>
Peter Kasting 2015/08/31 19:53:42 Why was this header moved down? I think it should
scroggo_chromium 2015/09/01 22:50:30 The style guide disagrees (https://www.chromium.or
Peter Kasting 2015/09/02 22:08:54 It is SO DUMB that Blink has its own style guide t
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 from little to native
46 // endianness. 47 // endianness.
Peter Kasting 2015/08/31 19:53:42 Do we actually compile on any big-endian targets?
scroggo_chromium 2015/09/01 22:50:30 According to [1], you are correct! From that same
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 uint16_t result;
50 memcpy(&result, &data->data()[offset], 2); 51 memcpy(&result, buffer, 2);
51 #if CPU(BIG_ENDIAN) 52 #if CPU(BIG_ENDIAN)
52 result = ((result & 0xff) << 8) | ((result & 0xff00) >> 8); 53 result = ((result & 0xff) << 8) | ((result & 0xff00) >> 8);
53 #endif 54 #endif
54 return result; 55 return result;
55 } 56 }
56 57
57 static inline uint32_t readUint32(SharedBuffer* data, int offset) 58 static inline uint32_t readUint32(const char* buffer)
58 { 59 {
59 uint32_t result; 60 uint32_t result;
60 memcpy(&result, &data->data()[offset], 4); 61 memcpy(&result, buffer, 4);
61 #if CPU(BIG_ENDIAN) 62 #if CPU(BIG_ENDIAN)
62 result = ((result & 0xff) << 24) | ((result & 0xff00) << 8) | ((result & 0xff0000) >> 8) | ((result & 0xff000000) >> 24); 63 result = ((result & 0xff) << 24) | ((result & 0xff00) << 8) | ((result & 0xff0000) >> 8) | ((result & 0xff000000) >> 24);
63 #endif 64 #endif
64 return result; 65 return result;
65 } 66 }
66 67
67 // |parent| is the decoder that owns us. 68 // |parent| is the decoder that owns us.
68 // |startOffset| points to the start of the BMP within the file. 69 // |startOffset| points to the start of the BMP within the file.
69 // |buffer| points at an empty ImageFrame that we'll initialize and 70 // |buffer| points at an empty ImageFrame that we'll initialize and
70 // fill with decoded data. 71 // fill with decoded data.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 uint16_t biBitCount; 118 uint16_t biBitCount;
118 CompressionType biCompression; 119 CompressionType biCompression;
119 uint32_t biClrUsed; 120 uint32_t biClrUsed;
120 }; 121 };
121 struct RGBTriple { 122 struct RGBTriple {
122 uint8_t rgbBlue; 123 uint8_t rgbBlue;
123 uint8_t rgbGreen; 124 uint8_t rgbGreen;
124 uint8_t rgbRed; 125 uint8_t rgbRed;
125 }; 126 };
126 127
127 inline uint16_t readUint16(int offset) const 128 inline uint16_t readUint16(FastSharedBufferReader* reader, int offset) const
128 { 129 {
129 return readUint16(m_data.get(), m_decodedOffset + offset); 130 char buffer[2];
131 const char* data = reader->getConsecutiveData(m_decodedOffset + offset, 2, buffer);
132 return readUint16(data);
130 } 133 }
131 134
132 inline uint32_t readUint32(int offset) const 135 inline uint32_t readUint32(FastSharedBufferReader* reader, int offset) const
133 { 136 {
134 return readUint32(m_data.get(), m_decodedOffset + offset); 137 char buffer[4];
138 const char* data = reader->getConsecutiveData(m_decodedOffset + offset, 4, buffer);
139 return readUint32(data);
135 } 140 }
136 141
137 // Determines the size of the BMP info header. Returns true if the size 142 // Determines the size of the BMP info header. Returns true if the size
138 // is valid. 143 // is valid.
139 bool readInfoHeaderSize(); 144 bool readInfoHeaderSize();
140 145
141 // Processes the BMP info header. Returns true if the info header could 146 // Processes the BMP info header. Returns true if the info header could
142 // be decoded. 147 // be decoded.
143 bool processInfoHeader(); 148 bool processInfoHeader();
144 149
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 inline bool pastEndOfImage(int numRows) 194 inline bool pastEndOfImage(int numRows)
190 { 195 {
191 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height ()) : ((m_coord.y() - numRows) < 0); 196 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height ()) : ((m_coord.y() - numRows) < 0);
192 } 197 }
193 198
194 // Returns the pixel data for the current X coordinate in a uint32_t. 199 // 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 200 // Assumes m_decodedOffset has been set to the beginning of the current
196 // row. 201 // row.
197 // NOTE: Only as many bytes of the return value as are needed to hold 202 // NOTE: Only as many bytes of the return value as are needed to hold
198 // the pixel data will actually be set. 203 // the pixel data will actually be set.
199 inline uint32_t readCurrentPixel(int bytesPerPixel) const 204 inline uint32_t readCurrentPixel(FastSharedBufferReader* fastReader, int byt esPerPixel) const
scroggo_chromium 2015/08/31 19:07:00 This seems like an odd API to me - initially I cre
Peter Kasting 2015/08/31 19:53:42 I think it would make sense to have a member FastS
scroggo_chromium 2015/09/01 22:50:30 I've made the FastSharedBufferReader to the BMPIma
200 { 205 {
206 // We need at most 4 bytes, starting at m_decodedOffset + offset.
207 char buffer[4];
201 const int offset = m_coord.x() * bytesPerPixel; 208 const int offset = m_coord.x() * bytesPerPixel;
209 const char* encodedPixel = fastReader->getConsecutiveData(m_decodedOffse t + offset, bytesPerPixel, buffer);
202 switch (bytesPerPixel) { 210 switch (bytesPerPixel) {
203 case 2: 211 case 2:
204 return readUint16(offset); 212 return readUint16(encodedPixel);
205 213
206 case 3: { 214 case 3: {
207 // It doesn't matter that we never set the most significant byte 215 // It doesn't matter that we never set the most significant byte
208 // of the return value here in little-endian mode, the caller 216 // of the return value here in little-endian mode, the caller
209 // won't read it. 217 // won't read it.
210 uint32_t pixel; 218 uint32_t pixel;
211 memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3); 219 memcpy(&pixel, encodedPixel, 3);
212 #if CPU(BIG_ENDIAN) 220 #if CPU(BIG_ENDIAN)
213 pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixe l & 0xff000000) >> 24); 221 pixel = ((pixel & 0xff00) << 8) | ((pixel & 0xff0000) >> 8) | ((pixe l & 0xff000000) >> 24);
214 #endif 222 #endif
215 return pixel; 223 return pixel;
216 } 224 }
217 225
218 case 4: 226 case 4:
219 return readUint32(offset); 227 return readUint32(encodedPixel);
220 228
221 default: 229 default:
222 ASSERT_NOT_REACHED(); 230 ASSERT_NOT_REACHED();
223 return 0; 231 return 0;
224 } 232 }
225 } 233 }
226 234
227 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) 235 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A)
228 // in the given pixel data. 236 // in the given pixel data.
229 inline unsigned getComponent(uint32_t pixel, int component) const 237 inline unsigned getComponent(uint32_t pixel, int component) const
230 { 238 {
231 uint8_t value = (pixel & m_bitMasks[component]) >> m_bitShiftsRight[comp onent]; 239 uint8_t value = (pixel & m_bitMasks[component]) >> m_bitShiftsRight[comp onent];
232 return m_lookupTableAddresses[component] ? m_lookupTableAddresses[compon ent][value] : value; 240 return m_lookupTableAddresses[component] ? m_lookupTableAddresses[compon ent][value] : value;
233 } 241 }
234 242
235 inline unsigned getAlpha(uint32_t pixel) const 243 inline unsigned getAlpha(uint32_t pixel) const
236 { 244 {
237 // For images without alpha, return alpha of 0xff. 245 // For images without alpha, return alpha of 0xff.
238 return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff; 246 return m_bitMasks[3] ? getComponent(pixel, 3) : 0xff;
239 } 247 }
240 248
249 // Retrieve one byte from |m_data| at |offset|
250 uint8_t getByte(size_t offset)
251 {
252 const char* segment;
253 #if ENABLE(ASSERT)
254 unsigned bytes =
Peter Kasting 2015/08/31 19:53:42 If you really want this assert, I'd do this: #if
scroggo_chromium 2015/09/01 22:50:30 Removed.
255 #endif
256 m_data->getSomeData(segment, offset);
scroggo_chromium 2015/08/31 19:07:00 I think there might be a tradeoff between speed an
Peter Kasting 2015/08/31 19:53:42 It does kind of bug me that we have a method like
257 ASSERT(bytes >= 1);
258 return segment[0];
259 }
260
261
241 // Sets the current pixel to the color given by |colorIndex|. This also 262 // Sets the current pixel to the color given by |colorIndex|. This also
242 // increments the relevant local variables to move the current pixel 263 // increments the relevant local variables to move the current pixel
243 // right by one. 264 // right by one.
244 inline void setI(size_t colorIndex) 265 inline void setI(size_t colorIndex)
245 { 266 {
246 setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGre en, m_colorTable[colorIndex].rgbBlue, 0xff); 267 setRGBA(m_colorTable[colorIndex].rgbRed, m_colorTable[colorIndex].rgbGre en, m_colorTable[colorIndex].rgbBlue, 0xff);
247 } 268 }
248 269
249 // Like setI(), but with the individual component values specified. 270 // Like setI(), but with the individual component values specified.
250 inline void setRGBA(unsigned red, 271 inline void setRGBA(unsigned red,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 // ICOs store a 1bpp "mask" immediately after the main bitmap image data 374 // 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 375 // (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 376 // header, thus doubling it). If |m_isInICO| is true, this variable tracks
356 // whether we've begun decoding this mask yet. 377 // whether we've begun decoding this mask yet.
357 bool m_decodingAndMask; 378 bool m_decodingAndMask;
358 }; 379 };
359 380
360 } // namespace blink 381 } // namespace blink
361 382
362 #endif 383 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698