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

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: Do not use a raw pointer 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>
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
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 uint8_t readUint8(size_t offset)
128 { 123 {
129 return readUint16(m_data.get(), m_decodedOffset + offset); 124 return m_fastReader.getOneByte(m_decodedOffset + offset);
130 } 125 }
131 126
132 inline uint32_t readUint32(int offset) const 127 inline uint16_t readUint16(int offset)
133 { 128 {
134 return readUint32(m_data.get(), m_decodedOffset + offset); 129 char buffer[2];
130 const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + off set, 2, buffer);
131 return readUint16(data);
132 }
133
134 inline uint32_t readUint32(int offset)
135 {
136 char buffer[4];
137 const char* data = m_fastReader.getConsecutiveData(m_decodedOffset + off set, 4, buffer);
138 return readUint32(data);
135 } 139 }
136 140
137 // Determines the size of the BMP info header. Returns true if the size 141 // Determines the size of the BMP info header. Returns true if the size
138 // is valid. 142 // is valid.
139 bool readInfoHeaderSize(); 143 bool readInfoHeaderSize();
140 144
141 // Processes the BMP info header. Returns true if the info header could 145 // Processes the BMP info header. Returns true if the info header could
142 // be decoded. 146 // be decoded.
143 bool processInfoHeader(); 147 bool processInfoHeader();
144 148
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 inline bool pastEndOfImage(int numRows) 193 inline bool pastEndOfImage(int numRows)
190 { 194 {
191 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height ()) : ((m_coord.y() - numRows) < 0); 195 return m_isTopDown ? ((m_coord.y() + numRows) >= m_parent->size().height ()) : ((m_coord.y() - numRows) < 0);
192 } 196 }
193 197
194 // Returns the pixel data for the current X coordinate in a uint32_t. 198 // 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 199 // Assumes m_decodedOffset has been set to the beginning of the current
196 // row. 200 // row.
197 // NOTE: Only as many bytes of the return value as are needed to hold 201 // NOTE: Only as many bytes of the return value as are needed to hold
198 // the pixel data will actually be set. 202 // the pixel data will actually be set.
199 inline uint32_t readCurrentPixel(int bytesPerPixel) const 203 inline uint32_t readCurrentPixel(int bytesPerPixel)
200 { 204 {
205 // We need at most 4 bytes, starting at m_decodedOffset + offset.
206 char buffer[4];
201 const int offset = m_coord.x() * bytesPerPixel; 207 const int offset = m_coord.x() * bytesPerPixel;
208 const char* encodedPixel = m_fastReader.getConsecutiveData(m_decodedOffs et + offset, bytesPerPixel, buffer);
202 switch (bytesPerPixel) { 209 switch (bytesPerPixel) {
203 case 2: 210 case 2:
204 return readUint16(offset); 211 return readUint16(encodedPixel);
205 212
206 case 3: { 213 case 3: {
207 // It doesn't matter that we never set the most significant byte 214 // It doesn't matter that we never set the most significant byte
208 // of the return value here in little-endian mode, the caller 215 // of the return value, the caller won't read it.
209 // won't read it.
210 uint32_t pixel; 216 uint32_t pixel;
211 memcpy(&pixel, &m_data->data()[m_decodedOffset + offset], 3); 217 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; 218 return pixel;
216 } 219 }
217 220
218 case 4: 221 case 4:
219 return readUint32(offset); 222 return readUint32(encodedPixel);
220 223
221 default: 224 default:
222 ASSERT_NOT_REACHED(); 225 ASSERT_NOT_REACHED();
223 return 0; 226 return 0;
224 } 227 }
225 } 228 }
226 229
227 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A) 230 // Returns the value of the desired component (0, 1, 2, 3 == R, G, B, A)
228 // in the given pixel data. 231 // in the given pixel data.
229 inline unsigned getComponent(uint32_t pixel, int component) const 232 inline unsigned getComponent(uint32_t pixel, int component) const
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 void moveBufferToNextRow(); 279 void moveBufferToNextRow();
277 280
278 // The decoder that owns us. 281 // The decoder that owns us.
279 ImageDecoder* m_parent; 282 ImageDecoder* m_parent;
280 283
281 // The destination for the pixel data. 284 // The destination for the pixel data.
282 ImageFrame* m_buffer; 285 ImageFrame* m_buffer;
283 286
284 // The file to decode. 287 // The file to decode.
285 RefPtr<SharedBuffer> m_data; 288 RefPtr<SharedBuffer> m_data;
289 FastSharedBufferReader m_fastReader;
286 290
287 // An index into |m_data| representing how much we've already decoded. 291 // An index into |m_data| representing how much we've already decoded.
288 size_t m_decodedOffset; 292 size_t m_decodedOffset;
289 293
290 // The file offset at which the BMP info header starts. 294 // The file offset at which the BMP info header starts.
291 size_t m_headerOffset; 295 size_t m_headerOffset;
292 296
293 // The file offset at which the actual image bits start. When decoding 297 // 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 298 // 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 299 // header; the reader functions expect the image data to start
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 // ICOs store a 1bpp "mask" immediately after the main bitmap image data 357 // 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 358 // (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 359 // header, thus doubling it). If |m_isInICO| is true, this variable tracks
356 // whether we've begun decoding this mask yet. 360 // whether we've begun decoding this mask yet.
357 bool m_decodingAndMask; 361 bool m_decodingAndMask;
358 }; 362 };
359 363
360 } // namespace blink 364 } // namespace blink
361 365
362 #endif 366 #endif
OLDNEW
« no previous file with comments | « Source/platform/image-decoders/bmp/BMPImageDecoder.cpp ('k') | Source/platform/image-decoders/bmp/BMPImageReader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698