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

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

Issue 872683008: Small cleanup on BMPImageReader.cpp (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 10 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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 memset(&m_infoHeader, 0, sizeof(m_infoHeader)); 89 memset(&m_infoHeader, 0, sizeof(m_infoHeader));
90 } 90 }
91 91
92 bool BMPImageReader::decodeBMP(bool onlySize) 92 bool BMPImageReader::decodeBMP(bool onlySize)
93 { 93 {
94 // Calculate size of info header. 94 // Calculate size of info header.
95 if (!m_infoHeader.biSize && !readInfoHeaderSize()) 95 if (!m_infoHeader.biSize && !readInfoHeaderSize())
96 return false; 96 return false;
97 97
98 // Read and process info header. 98 // Read and process info header.
99 if ((m_decodedOffset < (m_headerOffset + m_infoHeader.biSize)) && !processIn foHeader()) 99 if ((m_decodedOffset < sizeOfHeaderAndInfoHeader()) && !processInfoHeader())
100 return false; 100 return false;
101 101
102 // processInfoHeader() set the size, so if that's all we needed, we're done. 102 // processInfoHeader() set the size, so if that's all we needed, we're done.
103 if (onlySize) 103 if (onlySize)
104 return true; 104 return true;
105 105
106 // Read and process the bitmasks, if needed. 106 // Read and process the bitmasks, if needed.
107 if (m_needToProcessBitmasks && !processBitmasks()) 107 if (m_needToProcessBitmasks && !processBitmasks())
108 return false; 108 return false;
109 109
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 ASSERT(m_decodedOffset == m_headerOffset); 168 ASSERT(m_decodedOffset == m_headerOffset);
169 if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset ) < 4)) 169 if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset ) < 4))
170 return false; 170 return false;
171 m_infoHeader.biSize = readUint32(0); 171 m_infoHeader.biSize = readUint32(0);
172 // Don't increment m_decodedOffset here, it just makes the code in 172 // Don't increment m_decodedOffset here, it just makes the code in
173 // processInfoHeader() more confusing. 173 // processInfoHeader() more confusing.
174 174
175 // Don't allow the header to overflow (which would be harmless here, but 175 // Don't allow the header to overflow (which would be harmless here, but
176 // problematic or at least confusing in other places), or to overrun the 176 // problematic or at least confusing in other places), or to overrun the
177 // image data. 177 // image data.
178 if (((m_headerOffset + m_infoHeader.biSize) < m_headerOffset) || (m_imgDataO ffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize)))) 178 const size_t headerAndInfoHeaderSize = sizeOfHeaderAndInfoHeader();
Peter Kasting 2015/02/03 23:56:43 Nit: Call this |headerEnd|.
changseok 2015/02/04 04:10:32 Done.
179 if ((headerAndInfoHeaderSize < m_headerOffset) || (m_imgDataOffset && (m_img DataOffset < headerAndInfoHeaderSize)))
179 return m_parent->setFailed(); 180 return m_parent->setFailed();
180 181
181 // See if this is a header size we understand: 182 // See if this is a header size we understand:
182 // OS/2 1.x: 12 183 // OS/2 1.x: 12
183 if (m_infoHeader.biSize == 12) 184 if (m_infoHeader.biSize == 12)
184 m_isOS21x = true; 185 m_isOS21x = true;
185 // Windows V3: 40 186 // Windows V3: 40
186 else if ((m_infoHeader.biSize == 40) || isWindowsV4Plus()) 187 else if ((m_infoHeader.biSize == 40) || isWindowsV4Plus())
187 ; 188 ;
188 // OS/2 2.x: any multiple of 4 between 16 and 64, inclusive, or 42 or 46 189 // OS/2 2.x: any multiple of 4 between 16 and 64, inclusive, or 42 or 46
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 // 16 bits: MSB <- xRRRRRGG GGGBBBBB -> LSB 416 // 16 bits: MSB <- xRRRRRGG GGGBBBBB -> LSB
416 // 24/32 bits: MSB <- [AAAAAAAA] RRRRRRRR GGGGGGGG BBBBBBBB -> LSB 417 // 24/32 bits: MSB <- [AAAAAAAA] RRRRRRRR GGGGGGGG BBBBBBBB -> LSB
417 const int numBits = (m_infoHeader.biBitCount == 16) ? 5 : 8; 418 const int numBits = (m_infoHeader.biBitCount == 16) ? 5 : 8;
418 for (int i = 0; i <= 2; ++i) 419 for (int i = 0; i <= 2; ++i)
419 m_bitMasks[i] = ((static_cast<uint32_t>(1) << (numBits * (3 - i))) - 1) ^ ((static_cast<uint32_t>(1) << (numBits * (2 - i))) - 1); 420 m_bitMasks[i] = ((static_cast<uint32_t>(1) << (numBits * (3 - i))) - 1) ^ ((static_cast<uint32_t>(1) << (numBits * (2 - i))) - 1);
420 } else if (!isWindowsV4Plus()) { 421 } else if (!isWindowsV4Plus()) {
421 // For Windows V4+ BITFIELDS mode bitmaps, this was already done when 422 // For Windows V4+ BITFIELDS mode bitmaps, this was already done when
422 // we read the info header. 423 // we read the info header.
423 424
424 // Fail if we don't have enough file space for the bitmasks. 425 // Fail if we don't have enough file space for the bitmasks.
425 static const size_t SIZEOF_BITMASKS = 12; 426 static const size_t SIZEOF_BITMASKS = 12;
Peter Kasting 2015/02/03 23:56:43 Nit: While here, change to: const size_t
changseok 2015/02/04 04:10:32 Done.
426 if (((m_headerOffset + m_infoHeader.biSize + SIZEOF_BITMASKS) < (m_heade rOffset + m_infoHeader.biSize)) || (m_imgDataOffset && (m_imgDataOffset < (m_hea derOffset + m_infoHeader.biSize + SIZEOF_BITMASKS)))) 427 const size_t headerAndInfoHeaderSize = sizeOfHeaderAndInfoHeader();
428 const size_t headerInfoHeaderAndBitmasksSize = headerAndInfoHeaderSize + SIZEOF_BITMASKS;
Peter Kasting 2015/02/03 23:56:43 Nit: Call these |headerEnd| and |bitmasksEnd|.
changseok 2015/02/04 04:10:32 Done.
429 if ((headerInfoHeaderAndBitmasksSize < headerAndInfoHeaderSize) || (m_im gDataOffset && (m_imgDataOffset < headerInfoHeaderAndBitmasksSize)))
427 return m_parent->setFailed(); 430 return m_parent->setFailed();
428 431
429 // Read bitmasks. 432 // Read bitmasks.
430 if ((m_data->size() - m_decodedOffset) < SIZEOF_BITMASKS) 433 if ((m_data->size() - m_decodedOffset) < SIZEOF_BITMASKS)
431 return false; 434 return false;
432 m_bitMasks[0] = readUint32(0); 435 m_bitMasks[0] = readUint32(0);
433 m_bitMasks[1] = readUint32(4); 436 m_bitMasks[1] = readUint32(4);
434 m_bitMasks[2] = readUint32(8); 437 m_bitMasks[2] = readUint32(8);
435 438
436 m_decodedOffset += SIZEOF_BITMASKS; 439 m_decodedOffset += SIZEOF_BITMASKS;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 // Calculate LUT address. 526 // Calculate LUT address.
524 m_lookupTableAddresses[i] = numBits ? (nBitTo8BitlookupTable + (1 << num Bits) - 2) : 0; 527 m_lookupTableAddresses[i] = numBits ? (nBitTo8BitlookupTable + (1 << num Bits) - 2) : 0;
525 } 528 }
526 529
527 return true; 530 return true;
528 } 531 }
529 532
530 bool BMPImageReader::processColorTable() 533 bool BMPImageReader::processColorTable()
531 { 534 {
532 size_t tableSizeInBytes = m_infoHeader.biClrUsed * (m_isOS21x ? 3 : 4); 535 size_t tableSizeInBytes = m_infoHeader.biClrUsed * (m_isOS21x ? 3 : 4);
536 size_t headerAndInfoHeaderSize = sizeOfHeaderAndInfoHeader();
537 size_t headerInfoHeaderAndTableSize = headerAndInfoHeaderSize + tableSizeInB ytes;
Peter Kasting 2015/02/03 23:56:43 Nit: Make all these const. Call the new variables
changseok 2015/02/04 04:10:32 Done.
533 538
534 // Fail if we don't have enough file space for the color table. 539 // Fail if we don't have enough file space for the color table.
535 if (((m_headerOffset + m_infoHeader.biSize + tableSizeInBytes) < (m_headerOf fset + m_infoHeader.biSize)) || (m_imgDataOffset && (m_imgDataOffset < (m_header Offset + m_infoHeader.biSize + tableSizeInBytes)))) 540 if ((headerInfoHeaderAndTableSize < headerAndInfoHeaderSize) || (m_imgDataOf fset && (m_imgDataOffset < headerInfoHeaderAndTableSize)))
536 return m_parent->setFailed(); 541 return m_parent->setFailed();
537 542
538 // Read color table. 543 // Read color table.
539 if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset ) < tableSizeInBytes)) 544 if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset ) < tableSizeInBytes))
540 return false; 545 return false;
541 m_colorTable.resize(m_infoHeader.biClrUsed); 546 m_colorTable.resize(m_infoHeader.biClrUsed);
542 for (size_t i = 0; i < m_infoHeader.biClrUsed; ++i) { 547 for (size_t i = 0; i < m_infoHeader.biClrUsed; ++i) {
543 m_colorTable[i].rgbBlue = m_data->data()[m_decodedOffset++]; 548 m_colorTable[i].rgbBlue = m_data->data()[m_decodedOffset++];
544 m_colorTable[i].rgbGreen = m_data->data()[m_decodedOffset++]; 549 m_colorTable[i].rgbGreen = m_data->data()[m_decodedOffset++];
545 m_colorTable[i].rgbRed = m_data->data()[m_decodedOffset++]; 550 m_colorTable[i].rgbRed = m_data->data()[m_decodedOffset++];
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 // Finished decoding whole image. 800 // Finished decoding whole image.
796 return Success; 801 return Success;
797 } 802 }
798 803
799 void BMPImageReader::moveBufferToNextRow() 804 void BMPImageReader::moveBufferToNextRow()
800 { 805 {
801 m_coord.move(-m_coord.x(), m_isTopDown ? 1 : -1); 806 m_coord.move(-m_coord.x(), m_isTopDown ? 1 : -1);
802 } 807 }
803 808
804 } // namespace blink 809 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698