| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 return (index && (index < m_dirEntries.size())) ? m_dirEntries[index].m_size
: size(); | 76 return (index && (index < m_dirEntries.size())) ? m_dirEntries[index].m_size
: size(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 bool ICOImageDecoder::setSize(unsigned width, unsigned height) | 79 bool ICOImageDecoder::setSize(unsigned width, unsigned height) |
| 80 { | 80 { |
| 81 // The size calculated inside the BMPImageReader had better match the one in | 81 // The size calculated inside the BMPImageReader had better match the one in |
| 82 // the icon directory. | 82 // the icon directory. |
| 83 return m_frameSize.isEmpty() ? ImageDecoder::setSize(width, height) : ((IntS
ize(width, height) == m_frameSize) || setFailed()); | 83 return m_frameSize.isEmpty() ? ImageDecoder::setSize(width, height) : ((IntS
ize(width, height) == m_frameSize) || setFailed()); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool ICOImageDecoder::frameIsCompleteAtIndex(size_t index) const |
| 87 { |
| 88 if (index >= m_dirEntries.size()) |
| 89 return false; |
| 90 const IconDirectoryEntry& dirEntry = m_dirEntries[index]; |
| 91 return (dirEntry.m_imageOffset + dirEntry.m_byteSize) <= m_data->size(); |
| 92 } |
| 93 |
| 86 bool ICOImageDecoder::setFailed() | 94 bool ICOImageDecoder::setFailed() |
| 87 { | 95 { |
| 88 m_bmpReaders.clear(); | 96 m_bmpReaders.clear(); |
| 89 m_pngDecoders.clear(); | 97 m_pngDecoders.clear(); |
| 90 return ImageDecoder::setFailed(); | 98 return ImageDecoder::setFailed(); |
| 91 } | 99 } |
| 92 | 100 |
| 93 bool ICOImageDecoder::hotSpot(IntPoint& hotSpot) const | 101 bool ICOImageDecoder::hotSpot(IntPoint& hotSpot) const |
| 94 { | 102 { |
| 95 // When unspecified, the default frame is always frame 0. This is consistent
with | 103 // When unspecified, the default frame is always frame 0. This is consistent
with |
| 96 // BitmapImage where currentFrame() starts at 0 and only increases when anim
ation is | 104 // BitmapImage where currentFrame() starts at 0 and only increases when anim
ation is |
| 97 // requested. | 105 // requested. |
| 98 return hotSpotAtIndex(0, hotSpot); | 106 return hotSpotAtIndex(0, hotSpot); |
| 99 } | 107 } |
| 100 | 108 |
| 101 bool ICOImageDecoder::hotSpotAtIndex(size_t index, IntPoint& hotSpot) const | 109 bool ICOImageDecoder::hotSpotAtIndex(size_t index, IntPoint& hotSpot) const |
| 102 { | 110 { |
| 103 if (index >= m_dirEntries.size() || m_fileType != CURSOR) | 111 if (index >= m_dirEntries.size() || m_fileType != CURSOR) |
| 104 return false; | 112 return false; |
| 105 | 113 |
| 106 hotSpot = m_dirEntries[index].m_hotSpot; | 114 hotSpot = m_dirEntries[index].m_hotSpot; |
| 107 return true; | 115 return true; |
| 108 } | 116 } |
| 109 | 117 |
| 110 | |
| 111 // static | 118 // static |
| 112 bool ICOImageDecoder::compareEntries(const IconDirectoryEntry& a, const IconDire
ctoryEntry& b) | 119 bool ICOImageDecoder::compareEntries(const IconDirectoryEntry& a, const IconDire
ctoryEntry& b) |
| 113 { | 120 { |
| 114 // Larger icons are better. After that, higher bit-depth icons are better. | 121 // Larger icons are better. After that, higher bit-depth icons are better. |
| 115 const int aEntryArea = a.m_size.width() * a.m_size.height(); | 122 const int aEntryArea = a.m_size.width() * a.m_size.height(); |
| 116 const int bEntryArea = b.m_size.width() * b.m_size.height(); | 123 const int bEntryArea = b.m_size.width() * b.m_size.height(); |
| 117 return (aEntryArea == bEntryArea) ? (a.m_bitCount > b.m_bitCount) : (aEntryA
rea > bEntryArea); | 124 return (aEntryArea == bEntryArea) ? (a.m_bitCount > b.m_bitCount) : (aEntryA
rea > bEntryArea); |
| 118 } | 125 } |
| 119 | 126 |
| 120 size_t ICOImageDecoder::decodeFrameCount() | 127 size_t ICOImageDecoder::decodeFrameCount() |
| 121 { | 128 { |
| 122 decodeSize(); | 129 decodeSize(); |
| 130 |
| 131 for (size_t i = 0; i < m_dirEntries.size(); ++i) { |
| 132 const IconDirectoryEntry& dirEntry = m_dirEntries[i]; |
| 133 if ((dirEntry.m_imageOffset + dirEntry.m_byteSize) > m_data->size()) |
| 134 return i + 1; |
| 135 } |
| 123 return m_dirEntries.size(); | 136 return m_dirEntries.size(); |
| 124 } | 137 } |
| 125 | 138 |
| 126 void ICOImageDecoder::setDataForPNGDecoderAtIndex(size_t index) | 139 void ICOImageDecoder::setDataForPNGDecoderAtIndex(size_t index) |
| 127 { | 140 { |
| 128 if (!m_pngDecoders[index]) | 141 if (!m_pngDecoders[index]) |
| 129 return; | 142 return; |
| 130 | 143 |
| 131 m_pngDecoders[index]->setData(m_data.get(), isAllDataReceived()); | 144 m_pngDecoders[index]->setData(m_data.get(), isAllDataReceived()); |
| 132 } | 145 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 bool ICOImageDecoder::decodeAtIndex(size_t index) | 179 bool ICOImageDecoder::decodeAtIndex(size_t index) |
| 167 { | 180 { |
| 168 ASSERT_WITH_SECURITY_IMPLICATION(index < m_dirEntries.size()); | 181 ASSERT_WITH_SECURITY_IMPLICATION(index < m_dirEntries.size()); |
| 169 const IconDirectoryEntry& dirEntry = m_dirEntries[index]; | 182 const IconDirectoryEntry& dirEntry = m_dirEntries[index]; |
| 170 const ImageType imageType = imageTypeAtIndex(index); | 183 const ImageType imageType = imageTypeAtIndex(index); |
| 171 if (imageType == Unknown) | 184 if (imageType == Unknown) |
| 172 return false; // Not enough data to determine image type yet. | 185 return false; // Not enough data to determine image type yet. |
| 173 | 186 |
| 174 if (imageType == BMP) { | 187 if (imageType == BMP) { |
| 175 if (!m_bmpReaders[index]) { | 188 if (!m_bmpReaders[index]) { |
| 176 // We need to have already sized m_frameBufferCache before this, and | |
| 177 // we must not resize it again later (see caution in frameCount()). | |
| 178 ASSERT(m_frameBufferCache.size() == m_dirEntries.size()); | |
| 179 m_bmpReaders[index] = adoptPtr(new BMPImageReader(this, dirEntry.m_i
mageOffset, 0, true)); | 189 m_bmpReaders[index] = adoptPtr(new BMPImageReader(this, dirEntry.m_i
mageOffset, 0, true)); |
| 180 m_bmpReaders[index]->setData(m_data.get()); | 190 m_bmpReaders[index]->setData(m_data.get()); |
| 181 m_bmpReaders[index]->setBuffer(&m_frameBufferCache[index]); | |
| 182 } | 191 } |
| 192 // Update the pointer to the buffer as it could change after |
| 193 // m_frameBufferCache.resize(). |
| 194 m_bmpReaders[index]->setBuffer(&m_frameBufferCache[index]); |
| 183 m_frameSize = dirEntry.m_size; | 195 m_frameSize = dirEntry.m_size; |
| 184 bool result = m_bmpReaders[index]->decodeBMP(false); | 196 bool result = m_bmpReaders[index]->decodeBMP(false); |
| 185 m_frameSize = IntSize(); | 197 m_frameSize = IntSize(); |
| 186 return result; | 198 return result; |
| 187 } | 199 } |
| 188 | 200 |
| 189 if (!m_pngDecoders[index]) { | 201 if (!m_pngDecoders[index]) { |
| 190 AlphaOption alphaOption = m_premultiplyAlpha ? AlphaPremultiplied : Alph
aNotPremultiplied; | 202 AlphaOption alphaOption = m_premultiplyAlpha ? AlphaPremultiplied : Alph
aNotPremultiplied; |
| 191 GammaAndColorProfileOption colorOptions = m_ignoreGammaAndColorProfile ?
GammaAndColorProfileIgnored : GammaAndColorProfileApplied; | 203 GammaAndColorProfileOption colorOptions = m_ignoreGammaAndColorProfile ?
GammaAndColorProfileIgnored : GammaAndColorProfileApplied; |
| 192 m_pngDecoders[index] = adoptPtr(new PNGImageDecoder(alphaOption, colorOp
tions, m_maxDecodedBytes, dirEntry.m_imageOffset)); | 204 m_pngDecoders[index] = adoptPtr(new PNGImageDecoder(alphaOption, colorOp
tions, m_maxDecodedBytes, dirEntry.m_imageOffset)); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 height = 256; | 278 height = 256; |
| 267 IconDirectoryEntry entry; | 279 IconDirectoryEntry entry; |
| 268 entry.m_size = IntSize(width, height); | 280 entry.m_size = IntSize(width, height); |
| 269 if (m_fileType == CURSOR) { | 281 if (m_fileType == CURSOR) { |
| 270 entry.m_bitCount = 0; | 282 entry.m_bitCount = 0; |
| 271 entry.m_hotSpot = IntPoint(readUint16(4), readUint16(6)); | 283 entry.m_hotSpot = IntPoint(readUint16(4), readUint16(6)); |
| 272 } else { | 284 } else { |
| 273 entry.m_bitCount = readUint16(6); | 285 entry.m_bitCount = readUint16(6); |
| 274 entry.m_hotSpot = IntPoint(); | 286 entry.m_hotSpot = IntPoint(); |
| 275 } | 287 } |
| 288 entry.m_byteSize = readUint32(8); |
| 276 entry.m_imageOffset = readUint32(12); | 289 entry.m_imageOffset = readUint32(12); |
| 277 | 290 |
| 278 // Some icons don't have a bit depth, only a color count. Convert the | 291 // Some icons don't have a bit depth, only a color count. Convert the |
| 279 // color count to the minimum necessary bit depth. It doesn't matter if | 292 // color count to the minimum necessary bit depth. It doesn't matter if |
| 280 // this isn't quite what the bitmap info header says later, as we only use | 293 // this isn't quite what the bitmap info header says later, as we only use |
| 281 // this value to determine which icon entry is best. | 294 // this value to determine which icon entry is best. |
| 282 if (!entry.m_bitCount) { | 295 if (!entry.m_bitCount) { |
| 283 int colorCount = readUint8(2); | 296 int colorCount = readUint8(2); |
| 284 if (!colorCount) | 297 if (!colorCount) |
| 285 colorCount = 256; // Vague in the spec, needed by real-world icons. | 298 colorCount = 256; // Vague in the spec, needed by real-world icons. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 298 ASSERT_WITH_SECURITY_IMPLICATION(index < m_dirEntries.size()); | 311 ASSERT_WITH_SECURITY_IMPLICATION(index < m_dirEntries.size()); |
| 299 const uint32_t imageOffset = m_dirEntries[index].m_imageOffset; | 312 const uint32_t imageOffset = m_dirEntries[index].m_imageOffset; |
| 300 if ((imageOffset > m_data->size()) || ((m_data->size() - imageOffset) < 4)) | 313 if ((imageOffset > m_data->size()) || ((m_data->size() - imageOffset) < 4)) |
| 301 return Unknown; | 314 return Unknown; |
| 302 char buffer[4]; | 315 char buffer[4]; |
| 303 const char* data = m_fastReader.getConsecutiveData(imageOffset, 4, buffer); | 316 const char* data = m_fastReader.getConsecutiveData(imageOffset, 4, buffer); |
| 304 return strncmp(data, "\x89PNG", 4) ? BMP : PNG; | 317 return strncmp(data, "\x89PNG", 4) ? BMP : PNG; |
| 305 } | 318 } |
| 306 | 319 |
| 307 } // namespace blink | 320 } // namespace blink |
| OLD | NEW |