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

Side by Side Diff: Source/core/platform/image-decoders/webp/WEBPImageDecoder.cpp

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

Powered by Google App Engine
This is Rietveld 408576698