OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 15 matching lines...) Expand all Loading... | |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include "config.h" | 29 #include "config.h" |
30 #include "core/platform/image-decoders/webp/WEBPImageDecoder.h" | 30 #include "core/platform/image-decoders/webp/WEBPImageDecoder.h" |
31 | 31 |
32 #include "core/platform/PlatformInstrumentation.h" | 32 #include "core/platform/PlatformInstrumentation.h" |
33 | 33 |
34 #ifdef QCMS_WEBP_COLOR_CORRECTION | 34 #ifdef QCMS_WEBP_COLOR_CORRECTION |
35 #include "qcms.h" | 35 #include "qcms.h" |
36 #include "webp/demux.h" | |
37 #else | |
38 #undef ICCP_FLAG | |
39 #define ICCP_FLAG 0 | |
40 #endif | 36 #endif |
41 | 37 |
42 // Backward emulation for earlier versions than 0.1.99. | 38 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
39 #include "RuntimeEnabledFeatures.h" | |
40 #include "webp/format_constants.h" | |
41 #endif | |
42 | |
43 #if (WEBP_DECODER_ABI_VERSION < 0x0163) | 43 #if (WEBP_DECODER_ABI_VERSION < 0x0163) |
44 // Backward emulation for versions earlier than 0.1.99. | |
44 #define MODE_rgbA MODE_RGBA | 45 #define MODE_rgbA MODE_RGBA |
45 #define MODE_bgrA MODE_BGRA | 46 #define MODE_bgrA MODE_BGRA |
47 #define ALPHA_FLAG 0 | |
48 #elif (WEBP_DECODER_ABI_VERSION <= 0x0200) | |
49 // Backward emulation for versions earlier than 0.3.0. | |
50 #define ALPHA_FLAG 0x000010 | |
46 #endif | 51 #endif |
47 | 52 |
48 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN) | 53 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN) |
49 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } | 54 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } |
50 #elif SK_B32_SHIFT | 55 #elif SK_B32_SHIFT |
51 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } | 56 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } |
52 #else // LITTLE_ENDIAN, output BGRA pixels. | 57 #else // LITTLE_ENDIAN, output BGRA pixels. |
53 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_bgrA : M ODE_BGRA; } | 58 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_bgrA : M ODE_BGRA; } |
54 #endif | 59 #endif |
55 | 60 |
56 namespace WebCore { | 61 namespace WebCore { |
57 | 62 |
58 WEBPImageDecoder::WEBPImageDecoder(ImageSource::AlphaOption alphaOption, | 63 WEBPImageDecoder::WEBPImageDecoder(ImageSource::AlphaOption alphaOption, |
59 ImageSource::GammaAndColorProfileOption gamma AndColorProfileOption) | 64 ImageSource::GammaAndColorProfileOption gamma AndColorProfileOption) |
60 : ImageDecoder(alphaOption, gammaAndColorProfileOption) | 65 : ImageDecoder(alphaOption, gammaAndColorProfileOption) |
61 , m_decoder(0) | 66 , m_decoder(0) |
62 , m_hasAlpha(false) | |
63 , m_formatFlags(0) | 67 , m_formatFlags(0) |
64 #ifdef QCMS_WEBP_COLOR_CORRECTION | 68 #ifdef QCMS_WEBP_COLOR_CORRECTION |
65 , m_haveReadProfile(false) | 69 , m_haveReadProfile(false) |
66 , m_transform(0) | 70 , m_transform(0) |
71 #endif | |
72 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
67 , m_decodedHeight(0) | 73 , m_decodedHeight(0) |
74 , m_haveAlreadyParsedThisData(false) | |
75 , m_demux(0) | |
76 , m_demuxState(WEBP_DEMUX_PARSING_HEADER) | |
77 , m_haveReadAnimParams(false) | |
78 , m_repetitionCount(cAnimationLoopOnce) | |
68 #endif | 79 #endif |
69 { | 80 { |
70 WebPInitDecBuffer(&m_decoderBuffer); | |
71 } | 81 } |
72 | 82 |
73 WEBPImageDecoder::~WEBPImageDecoder() | 83 WEBPImageDecoder::~WEBPImageDecoder() |
74 { | 84 { |
75 clear(); | 85 clear(); |
76 } | 86 } |
77 | 87 |
78 void WEBPImageDecoder::clear() | 88 void WEBPImageDecoder::clear() |
79 { | 89 { |
80 #ifdef QCMS_WEBP_COLOR_CORRECTION | 90 #ifdef QCMS_WEBP_COLOR_CORRECTION |
81 if (m_transform) | 91 if (m_transform) |
82 qcms_transform_release(m_transform); | 92 qcms_transform_release(m_transform); |
83 m_transform = 0; | 93 m_transform = 0; |
84 #endif | 94 #endif |
85 WebPFreeDecBuffer(&m_decoderBuffer); | 95 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
86 if (m_decoder) | 96 WebPDemuxDelete(m_demux); |
87 WebPIDelete(m_decoder); | 97 m_demux = 0; |
98 #endif | |
99 clearDecoder(); | |
100 } | |
101 | |
102 void WEBPImageDecoder::clearDecoder() | |
103 { | |
104 WebPIDelete(m_decoder); | |
88 m_decoder = 0; | 105 m_decoder = 0; |
106 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
107 m_decodedHeight = 0; | |
108 #endif | |
89 } | 109 } |
90 | 110 |
91 bool WEBPImageDecoder::isSizeAvailable() | 111 bool WEBPImageDecoder::isSizeAvailable() |
92 { | 112 { |
93 if (!ImageDecoder::isSizeAvailable()) | 113 if (!ImageDecoder::isSizeAvailable()) { |
94 decode(true); | 114 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
95 | 115 if (!updateDemuxer()) |
116 return 0; | |
117 #else | |
118 decode(reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size(), true, 0); | |
119 #endif | |
120 } | |
96 return ImageDecoder::isSizeAvailable(); | 121 return ImageDecoder::isSizeAvailable(); |
97 } | 122 } |
98 | 123 |
99 ImageFrame* WEBPImageDecoder::frameBufferAtIndex(size_t index) | 124 size_t WEBPImageDecoder::frameCount() |
100 { | 125 { |
101 if (index) | 126 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
127 if (!updateDemuxer()) | |
102 return 0; | 128 return 0; |
103 | 129 #else |
104 if (m_frameBufferCache.isEmpty()) { | 130 if (m_frameBufferCache.isEmpty()) { |
105 m_frameBufferCache.resize(1); | 131 m_frameBufferCache.resize(1); |
106 m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha); | 132 m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha); |
107 } | 133 } |
108 | 134 #endif |
109 ImageFrame& frame = m_frameBufferCache[0]; | 135 return m_frameBufferCache.size(); |
110 if (frame.status() != ImageFrame::FrameComplete) { | 136 } |
137 | |
138 ImageFrame* WEBPImageDecoder::frameBufferAtIndex(size_t index) | |
139 { | |
140 if (index >= frameCount()) | |
141 return 0; | |
142 | |
143 ImageFrame& frame = m_frameBufferCache[index]; | |
144 if (frame.status() == ImageFrame::FrameComplete) | |
145 return &frame; | |
146 | |
147 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
148 if (RuntimeEnabledFeatures::animatedWebPEnabled()) { | |
149 if (index && (m_frameBufferCache[index - 1].status() != ImageFrame::Fram eComplete)) | |
150 return 0; // We haven't fully decoded the previous frame yet. | |
151 ASSERT(m_demux); | |
152 WebPIterator webpFrame; | |
153 if (!WebPDemuxGetFrame(m_demux, index + 1, &webpFrame)) | |
154 return 0; | |
155 if ((m_formatFlags & ANIMATION_FLAG) && !initFrameBuffer(webpFrame, inde x)) | |
156 return 0; | |
111 PlatformInstrumentation::willDecodeImage("WEBP"); | 157 PlatformInstrumentation::willDecodeImage("WEBP"); |
112 decode(false); | 158 decode(webpFrame.fragment.bytes, webpFrame.fragment.size, false, index); |
113 PlatformInstrumentation::didDecodeImage(); | 159 PlatformInstrumentation::didDecodeImage(); |
114 } | 160 WebPDemuxReleaseIterator(&webpFrame); |
161 return &frame; | |
162 } | |
163 #endif | |
164 | |
165 ASSERT(!index); | |
166 PlatformInstrumentation::willDecodeImage("WEBP"); | |
167 decode(reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size(), fal se, index); | |
168 PlatformInstrumentation::didDecodeImage(); | |
115 return &frame; | 169 return &frame; |
116 } | 170 } |
117 | 171 |
172 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
173 | |
174 void WEBPImageDecoder::setData(SharedBuffer* data, bool allDataReceived) | |
175 { | |
176 if (failed()) | |
177 return; | |
178 | |
179 ImageDecoder::setData(data, allDataReceived); | |
180 | |
181 // Mark that we have new data. | |
182 if (m_demuxState != WEBP_DEMUX_DONE) | |
183 m_haveAlreadyParsedThisData = false; | |
184 } | |
185 | |
186 bool WEBPImageDecoder::updateDemuxer() | |
187 { | |
188 if (m_haveAlreadyParsedThisData) | |
189 return true; | |
190 | |
191 m_haveAlreadyParsedThisData = true; | |
192 | |
193 static const size_t minSizeForDemux = RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE; | |
194 if (m_data->size() < minSizeForDemux) | |
195 return false; // Wait for headers so that WebPDemuxPartial doesn't retur n null. | |
196 | |
197 WebPDemuxDelete(m_demux); | |
198 WebPData inputData = { reinterpret_cast<const uint8_t*>(m_data->data()), m_d ata->size() }; | |
199 m_demux = WebPDemuxPartial(&inputData, &m_demuxState); | |
200 if (!m_demux) | |
201 return setFailed(); // Must be a failure as we have at least 'minSizeFor Demux' bytes. | |
202 | |
203 if (m_demuxState <= WEBP_DEMUX_PARSING_HEADER) | |
204 return false; // Not enough data for parsing canvas width/height yet. | |
205 | |
206 bool hasAnimation = (m_formatFlags & ANIMATION_FLAG); | |
207 if (!ImageDecoder::isSizeAvailable()) { | |
208 m_formatFlags = WebPDemuxGetI(m_demux, WEBP_FF_FORMAT_FLAGS); | |
209 hasAnimation = (m_formatFlags & ANIMATION_FLAG); | |
210 if (!RuntimeEnabledFeatures::animatedWebPEnabled() && hasAnimation) | |
211 return setFailed(); | |
212 if (!setSize(WebPDemuxGetI(m_demux, WEBP_FF_CANVAS_WIDTH), WebPDemuxGetI (m_demux, WEBP_FF_CANVAS_HEIGHT))) | |
213 return setFailed(); | |
214 } | |
215 ASSERT(ImageDecoder::isSizeAvailable()); | |
216 const size_t newFrameCount = WebPDemuxGetI(m_demux, WEBP_FF_FRAME_COUNT); | |
217 if (hasAnimation && !m_haveReadAnimParams && (newFrameCount >= 1)) { | |
218 // As we have parsed at least one frame (even if partially), | |
219 // we must already have parsed the animation properties. | |
220 // This is because ANIM chunk always precedes ANMF chunks. | |
221 m_repetitionCount = WebPDemuxGetI(m_demux, WEBP_FF_LOOP_COUNT); | |
222 ASSERT(m_repetitionCount == (m_repetitionCount & 0xffff)); // Loop count is always <= 16 bits. | |
223 if (!m_repetitionCount) | |
224 m_repetitionCount = cAnimationLoopInfinite; | |
225 m_haveReadAnimParams = true; | |
226 } | |
227 if (newFrameCount > m_frameBufferCache.size()) { | |
228 m_frameBufferCache.resize(newFrameCount); | |
229 for (size_t i = 0; i < newFrameCount; ++i) | |
230 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha); | |
231 } | |
232 return true; | |
233 } | |
234 | |
235 // FIXME: This method is very similar to the one in GIFImageDecoder.cpp and shou ld be refactored. | |
Noel Gordon
2013/05/03 18:15:46
I'm not sure we should hitch our wagon to the GIFI
urvang (Google)
2013/05/03 21:55:20
Well, the scaledSize() returns size() essentially,
| |
236 bool WEBPImageDecoder::initFrameBuffer(const WebPIterator& frame, size_t frameIn dex) | |
237 { | |
238 ImageFrame& buffer = m_frameBufferCache[frameIndex]; | |
239 if (buffer.status() != ImageFrame::FrameEmpty) // Already initialized. | |
240 return true; | |
241 | |
242 // Initialize the frame rect in our buffer. | |
243 IntRect frameRect(frame.x_offset, frame.y_offset, frame.width, frame.height) ; | |
244 | |
245 // Make sure the frameRect doesn't extend outside the buffer. | |
246 if (frameRect.maxX() > size().width()) | |
247 frameRect.setWidth(size().width() - frame.x_offset); | |
248 if (frameRect.maxY() > size().height()) | |
249 frameRect.setHeight(size().height() - frame.y_offset); | |
250 | |
251 const int left = upperBoundScaledX(frameRect.x()); | |
Noel Gordon
2013/05/03 18:15:46
This block of code is an example of the down sampl
urvang (Google)
2013/05/03 21:55:20
Done.
| |
252 const int right = lowerBoundScaledX(frameRect.maxX(), left); | |
253 const int top = upperBoundScaledY(frameRect.y()); | |
254 const int bottom = lowerBoundScaledY(frameRect.maxY(), top); | |
255 buffer.setOriginalFrameRect(IntRect(left, top, right - left, bottom - top)); | |
256 | |
257 buffer.setDisposalMethod(frame.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ? ImageFrame::DisposeOverwriteBgcolor : ImageFrame::DisposeKeep); | |
258 buffer.setDuration(frame.duration); | |
259 buffer.setHasAlpha(m_formatFlags & ALPHA_FLAG); | |
260 | |
261 if (!frameIndex) { | |
262 // This is the first frame, so we're not relying on any previous data. | |
263 if (!buffer.setSize(scaledSize().width(), scaledSize().height())) | |
Noel Gordon
2013/05/03 18:15:46
Here is another example: scaledSize().width() etc.
urvang (Google)
2013/05/03 21:55:20
Just using size() now
| |
264 return setFailed(); | |
265 } else { | |
266 // The starting state for this frame depends on the previous frame's | |
267 // disposal method. | |
268 const ImageFrame& prevBuffer = m_frameBufferCache[frameIndex - 1]; | |
269 ASSERT(prevBuffer.status() == ImageFrame::FrameComplete); | |
270 const IntRect& prevRect = prevBuffer.originalFrameRect(); | |
271 const ImageFrame::FrameDisposalMethod prevMethod = prevBuffer.disposalMe thod(); | |
272 if ((prevMethod == ImageFrame::DisposeKeep) || (prevMethod == ImageFrame ::DisposeNotSpecified)) { | |
273 // Preserve the last frame as the starting state for this frame. | |
274 if (!buffer.copyBitmapData(prevBuffer)) | |
275 return setFailed(); | |
276 } else { | |
277 ASSERT(prevMethod == ImageFrame::DisposeOverwriteBgcolor); | |
278 // We want to clear the previous frame to transparent, without | |
279 // affecting pixels in the image outside of the frame. | |
280 // So, we copy the whole previous buffer, then clear just its frame. | |
281 // FIXME: !frameIndex is always false here in case of WebP. But coul d | |
282 // be true in case of GIF. So it is kept here for ease of refactorin g. | |
283 if (!frameIndex || prevRect.contains(IntRect(IntPoint(), scaledSize( )))) { | |
284 // Clearing the first frame, or a frame the size of the whole | |
285 // image, results in a completely empty image. | |
286 if (!buffer.setSize(scaledSize().width(), scaledSize().height()) ) | |
287 return setFailed(); | |
288 } else { | |
289 // Copy the whole previous buffer, then clear just its frame. | |
290 if (!buffer.copyBitmapData(prevBuffer)) | |
291 return setFailed(); | |
292 for (int y = prevRect.y(); y < prevRect.maxY(); ++y) { | |
293 for (int x = prevRect.x(); x < prevRect.maxX(); ++x) | |
294 buffer.setRGBA(x, y, 0, 0, 0, 0); | |
295 } | |
296 } | |
297 } | |
298 } | |
299 | |
300 // Update frame status to be partially complete. | |
301 buffer.setStatus(ImageFrame::FramePartial); | |
302 return true; | |
303 } | |
304 | |
305 void WEBPImageDecoder::clearFrameBufferCache(size_t clearBeforeFrame) | |
306 { | |
307 // We always preserve at least one frame. | |
308 if (m_frameBufferCache.size() <= 1) | |
309 return; | |
310 | |
311 // Find the last frame we need to preserve in the cache to facilitate | |
312 // the construction of next frames (needed by initFrame() and | |
313 // applyPostProcessing()) . This frame is either: | |
314 // * The last decoded frame in cache, OR | |
315 // * The first frame (if cache doesn't contain any decoded frames). | |
316 const int lastFrame = std::min(clearBeforeFrame, m_frameBufferCache.size() - 1); | |
317 Vector<ImageFrame>::iterator i(m_frameBufferCache.begin() + lastFrame); | |
318 while ((i != m_frameBufferCache.begin()) && (i->status() != ImageFrame::Fram eComplete)) | |
319 --i; | |
320 | |
321 // Now |i| holds the last frame we need to preserve; clear prior frames. | |
322 for (Vector<ImageFrame>::iterator j(m_frameBufferCache.begin()); j != i; ++j ) { | |
323 ASSERT(j->status() != ImageFrame::FramePartial); | |
324 if (j->status() != ImageFrame::FrameEmpty) | |
325 j->clearPixelData(); | |
326 } | |
327 } | |
328 | |
329 #endif // WEBP_ICC_ANIMATION_SUPPORT | |
330 | |
118 #ifdef QCMS_WEBP_COLOR_CORRECTION | 331 #ifdef QCMS_WEBP_COLOR_CORRECTION |
119 | 332 |
120 void WEBPImageDecoder::createColorTransform(const char* data, size_t size) | 333 void WEBPImageDecoder::createColorTransform(const char* data, size_t size) |
121 { | 334 { |
122 if (m_transform) | 335 if (m_transform) |
123 qcms_transform_release(m_transform); | 336 qcms_transform_release(m_transform); |
124 m_transform = 0; | 337 m_transform = 0; |
125 | 338 |
126 qcms_profile* deviceProfile = ImageDecoder::qcmsOutputDeviceProfile(); | 339 qcms_profile* deviceProfile = ImageDecoder::qcmsOutputDeviceProfile(); |
127 if (!deviceProfile) | 340 if (!deviceProfile) |
128 return; | 341 return; |
129 qcms_profile* inputProfile = qcms_profile_from_memory(data, size); | 342 qcms_profile* inputProfile = qcms_profile_from_memory(data, size); |
130 if (!inputProfile) | 343 if (!inputProfile) |
131 return; | 344 return; |
132 | 345 |
133 // We currently only support color profiles for RGB profiled images. | 346 // We currently only support color profiles for RGB profiled images. |
134 ASSERT(icSigRgbData == qcms_profile_get_color_space(inputProfile)); | 347 ASSERT(icSigRgbData == qcms_profile_get_color_space(inputProfile)); |
135 // The input image pixels are RGBA format. | 348 // The input image pixels are RGBA format. |
136 qcms_data_type format = QCMS_DATA_RGBA_8; | 349 qcms_data_type format = QCMS_DATA_RGBA_8; |
137 // FIXME: Don't force perceptual intent if the image profile contains an int ent. | 350 // FIXME: Don't force perceptual intent if the image profile contains an int ent. |
138 m_transform = qcms_transform_create(inputProfile, format, deviceProfile, QCM S_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL); | 351 m_transform = qcms_transform_create(inputProfile, format, deviceProfile, QCM S_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL); |
139 | 352 |
140 qcms_profile_release(inputProfile); | 353 qcms_profile_release(inputProfile); |
141 } | 354 } |
142 | 355 |
143 void WEBPImageDecoder::readColorProfile(const uint8_t* data, size_t size) | 356 void WEBPImageDecoder::readColorProfile() |
144 { | 357 { |
145 WebPChunkIterator chunkIterator; | 358 WebPChunkIterator chunkIterator; |
146 WebPData inputData = { data, size }; | 359 if (!WebPDemuxGetChunk(m_demux, "ICCP", 1, &chunkIterator)) { |
147 WebPDemuxState state; | |
148 | |
149 WebPDemuxer* demuxer = WebPDemuxPartial(&inputData, &state); | |
150 if (!WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunkIterator)) { | |
151 WebPDemuxReleaseChunkIterator(&chunkIterator); | 360 WebPDemuxReleaseChunkIterator(&chunkIterator); |
152 WebPDemuxDelete(demuxer); | |
153 return; | 361 return; |
154 } | 362 } |
155 | 363 |
156 const char* profileData = reinterpret_cast<const char*>(chunkIterator.chunk. bytes); | 364 const char* profileData = reinterpret_cast<const char*>(chunkIterator.chunk. bytes); |
157 size_t profileSize = chunkIterator.chunk.size; | 365 size_t profileSize = chunkIterator.chunk.size; |
158 | 366 |
159 // Only accept RGB color profiles from input class devices. | 367 // Only accept RGB color profiles from input class devices. |
160 bool ignoreProfile = false; | 368 bool ignoreProfile = false; |
161 if (profileSize < ImageDecoder::iccColorProfileHeaderLength) | 369 if (profileSize < ImageDecoder::iccColorProfileHeaderLength) |
162 ignoreProfile = true; | 370 ignoreProfile = true; |
163 else if (!ImageDecoder::rgbColorProfile(profileData, profileSize)) | 371 else if (!ImageDecoder::rgbColorProfile(profileData, profileSize)) |
164 ignoreProfile = true; | 372 ignoreProfile = true; |
165 else if (!ImageDecoder::inputDeviceColorProfile(profileData, profileSize)) | 373 else if (!ImageDecoder::inputDeviceColorProfile(profileData, profileSize)) |
166 ignoreProfile = true; | 374 ignoreProfile = true; |
167 | 375 |
168 if (!ignoreProfile) | 376 if (!ignoreProfile) |
169 createColorTransform(profileData, profileSize); | 377 createColorTransform(profileData, profileSize); |
170 | 378 |
171 WebPDemuxReleaseChunkIterator(&chunkIterator); | 379 WebPDemuxReleaseChunkIterator(&chunkIterator); |
172 WebPDemuxDelete(demuxer); | |
173 } | 380 } |
174 | 381 |
175 void WEBPImageDecoder::applyColorProfile(const uint8_t* data, size_t size, Image Frame& buffer) | 382 #endif // QCMS_WEBP_COLOR_CORRECTION |
383 | |
384 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
385 void WEBPImageDecoder::applyPostProcessing(size_t frameIndex) | |
176 { | 386 { |
387 ImageFrame& buffer = m_frameBufferCache[frameIndex]; | |
177 int width; | 388 int width; |
178 int decodedHeight; | 389 int decodedHeight; |
179 if (!WebPIDecGetRGB(m_decoder, &decodedHeight, &width, 0, 0)) | 390 if (!WebPIDecGetRGB(m_decoder, &decodedHeight, &width, 0, 0)) |
180 return; // See also https://bugs.webkit.org/show_bug.cgi?id=74062 | 391 return; // See also https://bugs.webkit.org/show_bug.cgi?id=74062 |
181 if (decodedHeight <= 0) | 392 if (decodedHeight <= 0) |
182 return; | 393 return; |
394 ASSERT(width == buffer.originalFrameRect().width()); | |
Noel Gordon
2013/05/03 18:15:46
Space before this line. And the width assert is n
urvang (Google)
2013/05/03 21:55:20
Done.
| |
395 ASSERT(decodedHeight <= scaledSize().height()); | |
Noel Gordon
2013/05/03 18:15:46
The one could be better. scaledSize().height() =
urvang (Google)
2013/05/03 21:55:20
Done.
Noel Gordon
2013/05/04 17:23:43
I don't see the ASSERT_WITH_SECURITY_IMPLICATIONs.
| |
396 const int left = buffer.originalFrameRect().x(); | |
397 const int top = buffer.originalFrameRect().y(); | |
183 | 398 |
184 if (!m_haveReadProfile) { | 399 #ifdef QCMS_WEBP_COLOR_CORRECTION |
185 readColorProfile(data, size); | 400 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) { |
186 m_haveReadProfile = true; | 401 if (!m_haveReadProfile) { |
402 readColorProfile(); | |
403 m_haveReadProfile = true; | |
404 } | |
405 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | |
406 const int canvasY = top + y; | |
407 uint8_t* row = reinterpret_cast<uint8_t*>(buffer.getAddr(left, canva sY)); | |
408 if (qcms_transform* transform = colorTransform()) | |
409 qcms_transform_data_type(transform, row, row, width, QCMS_OUTPUT _RGBX); | |
410 uint8_t* pixel = row; | |
411 for (int x = 0; x < width; ++x, pixel += 4) { | |
412 const int canvasX = left + x; | |
413 buffer.setRGBA(canvasX, canvasY, pixel[0], pixel[1], pixel[2], p ixel[3]); | |
414 } | |
415 } | |
187 } | 416 } |
417 #endif // QCMS_WEBP_COLOR_CORRECTION | |
188 | 418 |
189 ASSERT(width == scaledSize().width()); | 419 // During the decoding of current frame, we may have set some pixels to be t ransparent (i.e. alpha < 255). |
190 ASSERT(decodedHeight <= scaledSize().height()); | 420 // However, the value of each of these pixels should have been determined by blending it against the value |
191 | 421 // of that pixel in the previous frame. So, we correct these pixels based on disposal method of the previous |
192 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | 422 // frame and the previous frame buffer. |
193 uint8_t* row = reinterpret_cast<uint8_t*>(buffer.getAddr(0, y)); | 423 if ((m_formatFlags & ANIMATION_FLAG) && frameIndex) { |
194 if (qcms_transform* transform = colorTransform()) | 424 ImageFrame& prevBuffer = m_frameBufferCache[frameIndex - 1]; |
195 qcms_transform_data_type(transform, row, row, width, QCMS_OUTPUT_RGB X); | 425 ImageFrame::FrameDisposalMethod prevMethod = prevBuffer.disposalMethod() ; |
196 uint8_t* pixel = row; | 426 ASSERT(prevBuffer.status() == ImageFrame::FrameComplete); |
197 for (int x = 0; x < width; ++x, pixel += 4) | 427 if (prevMethod == ImageFrame::DisposeKeep) { // Restore transparent pixe ls to pixels in previous canvas. |
198 buffer.setRGBA(x, y, pixel[0], pixel[1], pixel[2], pixel[3]); | 428 for (int y = m_decodedHeight; y < decodedHeight; ++y) { |
429 const int canvasY = top + y; | |
430 for (int x = 0; x < width; ++x) { | |
431 const int canvasX = left + x; | |
432 ImageFrame::PixelData& pixel = *buffer.getAddr(canvasX, canv asY); | |
433 // FIXME: Use alpha-blending when alpha is between 0 and 255 . | |
434 // Alpha-blending is being implemented in: https://bugs.webk it.org/show_bug.cgi?id=17022 | |
435 if (!((pixel >> SK_A32_SHIFT) & 0xff)) { // Need to restore. | |
436 ImageFrame::PixelData prevPixel = *prevBuffer.getAddr(ca nvasX, canvasY); | |
437 pixel = prevPixel; | |
438 } | |
439 } | |
440 } | |
441 } else if (prevMethod == ImageFrame::DisposeOverwriteBgcolor) { | |
442 const IntRect& prevRect = prevBuffer.originalFrameRect(); | |
443 // We need to restore transparent pixels to as they were just after initFrame() call. That is: | |
444 // * Transparent if it belongs to prevRect <-- This is a no-op. | |
445 // * Pixel in the previous canvas otherwise <-- Need to restore. | |
446 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | |
447 const int canvasY = top + y; | |
448 for (int x = 0; x < width; ++x) { | |
449 const int canvasX = left + x; | |
450 ImageFrame::PixelData& pixel = *buffer.getAddr(canvasX, canv asY); | |
451 // FIXME: Use alpha-blending when alpha is between 0 and 255 . | |
452 if (!((pixel >> SK_A32_SHIFT) & 0xff) && !prevRect.contains( IntPoint(canvasX, canvasY))) { // Need to restore. | |
453 ImageFrame::PixelData prevPixel = *prevBuffer.getAddr(ca nvasX, canvasY); | |
454 pixel = prevPixel; | |
455 } | |
456 } | |
457 } | |
458 } | |
199 } | 459 } |
200 | 460 |
201 m_decodedHeight = decodedHeight; | 461 m_decodedHeight = decodedHeight; |
202 } | 462 } |
463 #endif // WEBP_ICC_ANIMATION_SUPPORT | |
203 | 464 |
204 #endif // QCMS_WEBP_COLOR_CORRECTION | 465 bool WEBPImageDecoder::decode(const uint8_t* dataBytes, size_t dataSize, bool on lySize, size_t frameIndex) |
205 | |
206 bool WEBPImageDecoder::decode(bool onlySize) | |
207 { | 466 { |
208 if (failed()) | 467 if (failed()) |
209 return false; | 468 return false; |
210 | 469 |
211 const uint8_t* dataBytes = reinterpret_cast<const uint8_t*>(m_data->data()); | |
212 const size_t dataSize = m_data->size(); | |
213 | |
214 if (!ImageDecoder::isSizeAvailable()) { | 470 if (!ImageDecoder::isSizeAvailable()) { |
215 static const size_t imageHeaderSize = 30; | 471 static const size_t imageHeaderSize = 30; |
216 if (dataSize < imageHeaderSize) | 472 if (dataSize < imageHeaderSize) |
217 return false; | 473 return false; |
218 int width, height; | 474 int width, height; |
219 #ifdef QCMS_WEBP_COLOR_CORRECTION | 475 #if (WEBP_DECODER_ABI_VERSION >= 0x0163) |
220 WebPData inputData = { dataBytes, dataSize }; | |
221 WebPDemuxState state; | |
222 WebPDemuxer* demuxer = WebPDemuxPartial(&inputData, &state); | |
223 if (!demuxer) | |
224 return setFailed(); | |
225 | |
226 width = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); | |
227 height = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); | |
228 m_formatFlags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); | |
229 m_hasAlpha = !!(m_formatFlags & ALPHA_FLAG); | |
230 | |
231 WebPDemuxDelete(demuxer); | |
232 if (state <= WEBP_DEMUX_PARSING_HEADER) | |
233 return false; | |
234 #elif (WEBP_DECODER_ABI_VERSION >= 0x0163) | |
235 WebPBitstreamFeatures features; | 476 WebPBitstreamFeatures features; |
236 if (WebPGetFeatures(dataBytes, dataSize, &features) != VP8_STATUS_OK) | 477 if (WebPGetFeatures(dataBytes, dataSize, &features) != VP8_STATUS_OK) |
237 return setFailed(); | 478 return setFailed(); |
238 width = features.width; | 479 width = features.width; |
239 height = features.height; | 480 height = features.height; |
240 m_hasAlpha = features.has_alpha; | 481 m_formatFlags = features.has_alpha ? ALPHA_FLAG : 0; |
241 #else | 482 #else |
242 // Earlier version won't be able to display WebP files with alpha. | 483 // Earlier version won't be able to display WebP files with alpha. |
243 if (!WebPGetInfo(dataBytes, dataSize, &width, &height)) | 484 if (!WebPGetInfo(dataBytes, dataSize, &width, &height)) |
244 return setFailed(); | 485 return setFailed(); |
245 m_hasAlpha = false; | |
246 #endif | 486 #endif |
247 if (!setSize(width, height)) | 487 if (!setSize(width, height)) |
248 return setFailed(); | 488 return setFailed(); |
249 } | 489 } |
250 | 490 |
251 ASSERT(ImageDecoder::isSizeAvailable()); | 491 ASSERT(ImageDecoder::isSizeAvailable()); |
252 if (onlySize) | 492 if (onlySize) |
253 return true; | 493 return true; |
254 | 494 |
255 ASSERT(!m_frameBufferCache.isEmpty()); | 495 ASSERT(m_frameBufferCache.size() > frameIndex); |
256 ImageFrame& buffer = m_frameBufferCache[0]; | 496 ImageFrame& buffer = m_frameBufferCache[frameIndex]; |
257 ASSERT(buffer.status() != ImageFrame::FrameComplete); | 497 ASSERT(buffer.status() != ImageFrame::FrameComplete); |
258 | 498 |
259 if (buffer.status() == ImageFrame::FrameEmpty) { | 499 if (buffer.status() == ImageFrame::FrameEmpty) { |
260 if (!buffer.setSize(size().width(), size().height())) | 500 if (!buffer.setSize(scaledSize().width(), scaledSize().height())) |
261 return setFailed(); | 501 return setFailed(); |
262 buffer.setStatus(ImageFrame::FramePartial); | 502 buffer.setStatus(ImageFrame::FramePartial); |
263 buffer.setHasAlpha(m_hasAlpha); | 503 buffer.setHasAlpha(m_formatFlags & ALPHA_FLAG); |
264 buffer.setOriginalFrameRect(IntRect(IntPoint(), size())); | 504 buffer.setOriginalFrameRect(IntRect(IntPoint(), size())); |
265 } | 505 } |
266 | 506 |
507 const IntRect& frameRect = buffer.originalFrameRect(); | |
267 if (!m_decoder) { | 508 if (!m_decoder) { |
268 WEBP_CSP_MODE mode = outputMode(m_hasAlpha); | 509 WEBP_CSP_MODE mode = outputMode(m_formatFlags & ALPHA_FLAG); |
269 if (!m_premultiplyAlpha) | 510 if (!m_premultiplyAlpha) |
270 mode = outputMode(false); | 511 mode = outputMode(false); |
512 #ifdef QCMS_WEBP_COLOR_CORRECTION | |
271 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) | 513 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) |
272 mode = MODE_RGBA; // Decode to RGBA for input to libqcms. | 514 mode = MODE_RGBA; // Decode to RGBA for input to libqcms. |
515 #endif | |
516 WebPInitDecBuffer(&m_decoderBuffer); | |
273 m_decoderBuffer.colorspace = mode; | 517 m_decoderBuffer.colorspace = mode; |
274 m_decoderBuffer.u.RGBA.stride = size().width() * sizeof(ImageFrame::Pixe lData); | 518 m_decoderBuffer.u.RGBA.stride = size().width() * sizeof(ImageFrame::Pixe lData); |
275 m_decoderBuffer.u.RGBA.size = m_decoderBuffer.u.RGBA.stride * size().hei ght(); | 519 m_decoderBuffer.u.RGBA.size = m_decoderBuffer.u.RGBA.stride * frameRect. height(); |
276 m_decoderBuffer.is_external_memory = 1; | 520 m_decoderBuffer.is_external_memory = 1; |
277 m_decoder = WebPINewDecoder(&m_decoderBuffer); | 521 m_decoder = WebPINewDecoder(&m_decoderBuffer); |
278 if (!m_decoder) | 522 if (!m_decoder) |
279 return setFailed(); | 523 return setFailed(); |
280 } | 524 } |
281 | 525 |
282 m_decoderBuffer.u.RGBA.rgba = reinterpret_cast<uint8_t*>(buffer.getAddr(0, 0 )); | 526 m_decoderBuffer.u.RGBA.rgba = reinterpret_cast<uint8_t*>(buffer.getAddr(fram eRect.x(), frameRect.y())); |
283 | 527 |
284 switch (WebPIUpdate(m_decoder, dataBytes, dataSize)) { | 528 switch (WebPIUpdate(m_decoder, dataBytes, dataSize)) { |
285 case VP8_STATUS_OK: | 529 case VP8_STATUS_OK: |
286 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) | 530 applyPostProcessing(frameIndex); |
287 applyColorProfile(dataBytes, dataSize, buffer); | |
288 buffer.setStatus(ImageFrame::FrameComplete); | 531 buffer.setStatus(ImageFrame::FrameComplete); |
289 clear(); | 532 clearDecoder(); |
290 return true; | 533 return true; |
291 case VP8_STATUS_SUSPENDED: | 534 case VP8_STATUS_SUSPENDED: |
292 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) | 535 applyPostProcessing(frameIndex); |
293 applyColorProfile(dataBytes, dataSize, buffer); | |
294 return false; | 536 return false; |
295 default: | 537 default: |
296 clear(); | 538 clear(); |
297 return setFailed(); | 539 return setFailed(); |
298 } | 540 } |
299 } | 541 } |
300 | 542 |
301 } // namespace WebCore | 543 } // namespace WebCore |
OLD | NEW |