OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. | 2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 case SniffResult::BMP: | 100 case SniffResult::BMP: |
101 return wrapUnique(new BMPImageDecoder(alphaOption, colorOptions, maxDeco dedBytes)); | 101 return wrapUnique(new BMPImageDecoder(alphaOption, colorOptions, maxDeco dedBytes)); |
102 case SniffResult::InsufficientData: | 102 case SniffResult::InsufficientData: |
103 case SniffResult::Invalid: | 103 case SniffResult::Invalid: |
104 return nullptr; | 104 return nullptr; |
105 } | 105 } |
106 NOTREACHED(); | 106 NOTREACHED(); |
107 return nullptr; | 107 return nullptr; |
108 } | 108 } |
109 | 109 |
110 namespace { | |
Peter Kasting
2016/08/16 20:12:25
Instead of adding all this, why not just use FastS
f(malita)
2016/08/16 21:54:03
I wasn't familiar with FastSharedBufferReader, goo
| |
111 | |
112 constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1; | |
113 static_assert(kLongestSignatureLength == 14, "kLongestSignatureLength mismatch") ; | |
114 | |
115 // Offers contiguous access to the first LEN bytes in the buffer. | |
116 // If the first segment is large enough to hold all the requested data, we use a pointer to | |
117 // the actual segment data. | |
118 // Otherwise, we copy/consolidate the data in a local buffer. | |
119 template <typename T, size_t LEN> | |
120 class SpeculativeSegmentReader { | |
121 STACK_ALLOCATED(); | |
122 public: | |
123 SpeculativeSegmentReader(const T& data, size_t (*accessSegment)(const T&, co nst char*&, size_t)) | |
124 { | |
125 m_dataLen = accessSegment(data, m_data, 0); | |
126 if (!m_dataLen || m_dataLen >= LEN) { | |
127 // We either don't have any data, or the first segment is large enou gh. | |
128 // Either way, we don't need a copy: just use the actual/contiguous segment data. | |
129 return; | |
130 } | |
131 | |
132 // The signature is fragmented: we need to make a copy. | |
133 std::copy(m_data, m_data + m_dataLen, m_buffer); | |
134 m_data = m_buffer; | |
135 | |
136 do { | |
137 const char* segmentData; | |
138 const size_t segmentLen = accessSegment(data, segmentData, m_dataLen ); | |
139 if (!segmentLen) | |
140 break; | |
141 const size_t usableLen = std::min(segmentLen, LEN - m_dataLen); | |
142 std::copy(segmentData, segmentData + usableLen, m_buffer + m_dataLen ); | |
143 m_dataLen += usableLen; | |
144 } while (m_dataLen < LEN); | |
145 } | |
146 | |
147 const char* data() const { return m_data; } | |
148 size_t dataLen() const { return m_dataLen; } | |
149 | |
150 private: | |
151 const char* m_data; | |
152 size_t m_dataLen; | |
153 char m_buffer[LEN]; | |
154 }; | |
155 | |
156 } // anonymous ns | |
157 | |
110 ImageDecoder::SniffResult ImageDecoder::determineImageType(const char* contents, size_t length) | 158 ImageDecoder::SniffResult ImageDecoder::determineImageType(const char* contents, size_t length) |
111 { | 159 { |
112 const size_t longestSignatureLength = sizeof("RIFF????WEBPVP") - 1; | 160 if (length < kLongestSignatureLength) |
113 DCHECK_EQ(14u, longestSignatureLength); | |
114 | |
115 if (length < longestSignatureLength) | |
116 return SniffResult::InsufficientData; | 161 return SniffResult::InsufficientData; |
117 if (matchesJPEGSignature(contents)) | 162 if (matchesJPEGSignature(contents)) |
118 return SniffResult::JPEG; | 163 return SniffResult::JPEG; |
119 if (matchesPNGSignature(contents)) | 164 if (matchesPNGSignature(contents)) |
120 return SniffResult::PNG; | 165 return SniffResult::PNG; |
121 if (matchesGIFSignature(contents)) | 166 if (matchesGIFSignature(contents)) |
122 return SniffResult::GIF; | 167 return SniffResult::GIF; |
123 if (matchesWebPSignature(contents)) | 168 if (matchesWebPSignature(contents)) |
124 return SniffResult::WEBP; | 169 return SniffResult::WEBP; |
125 if (matchesICOSignature(contents) || matchesCURSignature(contents)) | 170 if (matchesICOSignature(contents) || matchesCURSignature(contents)) |
126 return SniffResult::ICO; | 171 return SniffResult::ICO; |
127 if (matchesBMPSignature(contents)) | 172 if (matchesBMPSignature(contents)) |
128 return SniffResult::BMP; | 173 return SniffResult::BMP; |
129 return SniffResult::Invalid; | 174 return SniffResult::Invalid; |
130 } | 175 } |
131 | 176 |
132 ImageDecoder::SniffResult ImageDecoder::determineImageType(const SharedBuffer& d ata) | 177 ImageDecoder::SniffResult ImageDecoder::determineImageType(const SharedBuffer& d ata) |
133 { | 178 { |
134 const char* contents; | 179 const SpeculativeSegmentReader<SharedBuffer, kLongestSignatureLength> sniffe r(data, |
135 const size_t length = data.getSomeData<size_t>(contents); | 180 [] (const SharedBuffer& data, const char*& segmentData, size_t offset) - > size_t { |
136 return determineImageType(contents, length); | 181 return data.getSomeData<size_t>(segmentData, offset); |
182 }); | |
183 return determineImageType(sniffer.data(), sniffer.dataLen()); | |
137 } | 184 } |
138 | 185 |
139 ImageDecoder::SniffResult ImageDecoder::determineImageType(const SegmentReader& data) | 186 ImageDecoder::SniffResult ImageDecoder::determineImageType(const SegmentReader& data) |
140 { | 187 { |
141 const char* contents; | 188 const SpeculativeSegmentReader<SegmentReader, kLongestSignatureLength> sniff er(data, |
142 const size_t length = data.getSomeData(contents, 0); | 189 [] (const SegmentReader& data, const char*& segmentData, size_t offset) -> size_t { |
143 return determineImageType(contents, length); | 190 return data.getSomeData(segmentData, offset); |
191 }); | |
192 return determineImageType(sniffer.data(), sniffer.dataLen()); | |
144 } | 193 } |
145 | 194 |
146 size_t ImageDecoder::frameCount() | 195 size_t ImageDecoder::frameCount() |
147 { | 196 { |
148 const size_t oldSize = m_frameBufferCache.size(); | 197 const size_t oldSize = m_frameBufferCache.size(); |
149 const size_t newSize = decodeFrameCount(); | 198 const size_t newSize = decodeFrameCount(); |
150 if (oldSize != newSize) { | 199 if (oldSize != newSize) { |
151 m_frameBufferCache.resize(newSize); | 200 m_frameBufferCache.resize(newSize); |
152 for (size_t i = oldSize; i < newSize; ++i) { | 201 for (size_t i = oldSize; i < newSize; ++i) { |
153 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha); | 202 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha); |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 return; | 454 return; |
406 | 455 |
407 qcms_data_type dataFormat = hasAlpha ? QCMS_DATA_RGBA_8 : QCMS_DATA_RGB_8; | 456 qcms_data_type dataFormat = hasAlpha ? QCMS_DATA_RGBA_8 : QCMS_DATA_RGB_8; |
408 | 457 |
409 // FIXME: Don't force perceptual intent if the image profile contains an int ent. | 458 // FIXME: Don't force perceptual intent if the image profile contains an int ent. |
410 m_sourceToOutputDeviceColorTransform.reset(qcms_transform_create(inputProfil e.get(), dataFormat, gTargetColorProfile, QCMS_DATA_RGBA_8, QCMS_INTENT_PERCEPTU AL)); | 459 m_sourceToOutputDeviceColorTransform.reset(qcms_transform_create(inputProfil e.get(), dataFormat, gTargetColorProfile, QCMS_DATA_RGBA_8, QCMS_INTENT_PERCEPTU AL)); |
411 #endif // USE(QCMSLIB) | 460 #endif // USE(QCMSLIB) |
412 } | 461 } |
413 | 462 |
414 } // namespace blink | 463 } // namespace blink |
OLD | NEW |