1 | /* |
2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "GIFImageDecoder.h" |
28 | |
29 | #include "GIFImageReader.h" |
30 | #include <limits> |
31 | |
32 | namespace WebCore { |
33 | |
34 | GIFImageDecoder::GIFImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption) |
35 | : ScalableImageDecoder(alphaOption, gammaAndColorProfileOption) |
36 | { |
37 | } |
38 | |
39 | GIFImageDecoder::~GIFImageDecoder() = default; |
40 | |
41 | void GIFImageDecoder::setData(SharedBuffer& data, bool allDataReceived) |
42 | { |
43 | if (failed()) |
44 | return; |
45 | |
46 | ScalableImageDecoder::setData(data, allDataReceived); |
47 | if (m_reader) |
48 | m_reader->setData(&data); |
49 | } |
50 | |
51 | bool GIFImageDecoder::setSize(const IntSize& size) |
52 | { |
53 | if (ScalableImageDecoder::encodedDataStatus() >= EncodedDataStatus::SizeAvailable && this->size() == size) |
54 | return true; |
55 | |
56 | if (!ScalableImageDecoder::setSize(size)) |
57 | return false; |
58 | |
59 | prepareScaleDataIfNecessary(); |
60 | return true; |
61 | } |
62 | |
63 | size_t GIFImageDecoder::frameCount() const |
64 | { |
65 | const_cast<GIFImageDecoder*>(this)->decode(std::numeric_limits<unsigned>::max(), GIFFrameCountQuery, isAllDataReceived()); |
66 | return m_frameBufferCache.size(); |
67 | } |
68 | |
69 | RepetitionCount GIFImageDecoder::repetitionCount() const |
70 | { |
71 | // This value can arrive at any point in the image data stream. Most GIFs |
72 | // in the wild declare it near the beginning of the file, so it usually is |
73 | // set by the time we've decoded the size, but (depending on the GIF and the |
74 | // packets sent back by the webserver) not always. If the reader hasn't |
75 | // seen a loop count yet, it will return cLoopCountNotSeen, in which case we |
76 | // should default to looping once (the initial value for |
77 | // |m_repetitionCount|). |
78 | // |
79 | // There are some additional wrinkles here. First, ImageSource::clear() |
80 | // may destroy the reader, making the result from the reader _less_ |
81 | // authoritative on future calls if the recreated reader hasn't seen the |
82 | // loop count. We don't need to special-case this because in this case the |
83 | // new reader will once again return cLoopCountNotSeen, and we won't |
84 | // overwrite the cached correct value. |
85 | // |
86 | // Second, a GIF might never set a loop count at all, in which case we |
87 | // should continue to treat it as a "loop once" animation. We don't need |
88 | // special code here either, because in this case we'll never change |
89 | // |m_repetitionCount| from its default value. |
90 | // |
91 | // Third, we use the same GIFImageReader for counting frames and we might |
92 | // see the loop count and then encounter a decoding error which happens |
93 | // later in the stream. It is also possible that no frames are in the |
94 | // stream. In these cases we should just loop once. |
95 | if (failed() || (m_reader && (!m_reader->imagesCount()))) |
96 | m_repetitionCount = RepetitionCountOnce; |
97 | else if (m_reader && m_reader->loopCount() != cLoopCountNotSeen) |
98 | m_repetitionCount = m_reader->loopCount() > 0 ? m_reader->loopCount() + 1 : m_reader->loopCount(); |
99 | return m_repetitionCount; |
100 | } |
101 | |
102 | size_t GIFImageDecoder::findFirstRequiredFrameToDecode(size_t frameIndex) |
103 | { |
104 | // The first frame doesn't depend on any other. |
105 | if (!frameIndex) |
106 | return 0; |
107 | |
108 | for (size_t i = frameIndex; i > 0; --i) { |
109 | auto& frame = m_frameBufferCache[i - 1]; |
110 | |
111 | // Frames with disposal method RestoreToPrevious are useless, skip them. |
112 | if (frame.disposalMethod() == ScalableImageDecoderFrame::DisposalMethod::RestoreToPrevious) |
113 | continue; |
114 | |
115 | // At this point the disposal method can be Unspecified, DoNotDispose or RestoreToBackground. |
116 | // In every case, if the frame is complete we can start decoding the next one. |
117 | if (frame.isComplete()) |
118 | return i; |
119 | |
120 | // If the disposal method of this frame is RestoreToBackground and it fills the whole area, |
121 | // the next frame's backing store is initialized to transparent, so we start decoding with it. |
122 | if (frame.disposalMethod() == ScalableImageDecoderFrame::DisposalMethod::RestoreToBackground) { |
123 | // We cannot use frame.backingStore()->frameRect() here, because it has been cleared |
124 | // when the frame was removed from the cache. We need to get the values from the |
125 | // reader context. |
126 | const auto* frameContext = m_reader->frameContext(i - 1); |
127 | ASSERT(frameContext); |
128 | IntRect frameRect(frameContext->xOffset, frameContext->yOffset, frameContext->width, frameContext->height); |
129 | // We would need to scale frameRect and check whether it fills the whole scaledSize(). But |
130 | // can check whether the original frameRect fills size() instead. If the frame fills the |
131 | // whole area then it can be decoded without dependencies. |
132 | if (frameRect.contains({ { }, size() })) |
133 | return i; |
134 | } |
135 | } |
136 | |
137 | return 0; |
138 | } |
139 | |
140 | ScalableImageDecoderFrame* GIFImageDecoder::frameBufferAtIndex(size_t index) |
141 | { |
142 | if (index >= frameCount()) |
143 | return 0; |
144 | |
145 | auto& frame = m_frameBufferCache[index]; |
146 | if (!frame.isComplete()) { |
147 | for (auto i = findFirstRequiredFrameToDecode(index); i <= index; i++) |
148 | decode(i + 1, GIFFullQuery, isAllDataReceived()); |
149 | } |
150 | |
151 | return &frame; |
152 | } |
153 | |
154 | bool GIFImageDecoder::setFailed() |
155 | { |
156 | m_reader = nullptr; |
157 | return ScalableImageDecoder::setFailed(); |
158 | } |
159 | |
160 | void GIFImageDecoder::clearFrameBufferCache(size_t clearBeforeFrame) |
161 | { |
162 | // In some cases, like if the decoder was destroyed while animating, we |
163 | // can be asked to clear more frames than we currently have. |
164 | if (m_frameBufferCache.isEmpty()) |
165 | return; // Nothing to do. |
166 | |
167 | // The "-1" here is tricky. It does not mean that |clearBeforeFrame| is the |
168 | // last frame we wish to preserve, but rather that we never want to clear |
169 | // the very last frame in the cache: it's empty (so clearing it is |
170 | // pointless), it's partial (so we don't want to clear it anyway), or the |
171 | // cache could be enlarged with a future setData() call and it could be |
172 | // needed to construct the next frame (see comments below). Callers can |
173 | // always use ImageSource::clear(true, ...) to completely free the memory in |
174 | // this case. |
175 | clearBeforeFrame = std::min(clearBeforeFrame, m_frameBufferCache.size() - 1); |
176 | const Vector<ScalableImageDecoderFrame>::iterator end(m_frameBufferCache.begin() + clearBeforeFrame); |
177 | |
178 | // We need to preserve frames such that: |
179 | // * We don't clear |end| |
180 | // * We don't clear the frame we're currently decoding |
181 | // * We don't clear any frame from which a future initFrameBuffer() call |
182 | // will copy bitmap data |
183 | // All other frames can be cleared. Because of the constraints on when |
184 | // ImageSource::clear() can be called (see ImageSource.h), we're guaranteed |
185 | // not to have non-empty frames after the frame we're currently decoding. |
186 | // So, scan backwards from |end| as follows: |
187 | // * If the frame is empty, we're still past any frames we care about. |
188 | // * If the frame is complete, but is DisposalMethod::RestoreToPrevious, we'll |
189 | // skip over it in future initFrameBuffer() calls. We can clear it |
190 | // unless it's |end|, and keep scanning. For any other disposal method, |
191 | // stop scanning, as we've found the frame initFrameBuffer() will need |
192 | // next. |
193 | // * If the frame is partial, we're decoding it, so don't clear it; if it |
194 | // has a disposal method other than DisposalMethod::RestoreToPrevious, stop |
195 | // scanning, as we'll only need this frame when decoding the next one. |
196 | Vector<ScalableImageDecoderFrame>::iterator i(end); |
197 | for (; (i != m_frameBufferCache.begin()) && (i->isInvalid() || (i->disposalMethod() == ScalableImageDecoderFrame::DisposalMethod::RestoreToPrevious)); --i) { |
198 | if (i->isComplete() && (i != end)) |
199 | i->clear(); |
200 | } |
201 | |
202 | // Now |i| holds the last frame we need to preserve; clear prior frames. |
203 | for (Vector<ScalableImageDecoderFrame>::iterator j(m_frameBufferCache.begin()); j != i; ++j) { |
204 | ASSERT(!j->isPartial()); |
205 | if (!j->isInvalid()) |
206 | j->clear(); |
207 | } |
208 | } |
209 | |
210 | bool GIFImageDecoder::haveDecodedRow(unsigned frameIndex, const Vector<unsigned char>& rowBuffer, size_t width, size_t rowNumber, unsigned repeatCount, bool writeTransparentPixels) |
211 | { |
212 | const GIFFrameContext* frameContext = m_reader->frameContext(); |
213 | // The pixel data and coordinates supplied to us are relative to the frame's |
214 | // origin within the entire image size, i.e. |
215 | // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
216 | // that width == (size().width() - frameContext->xOffset), so |
217 | // we must ensure we don't run off the end of either the source data or the |
218 | // row's X-coordinates. |
219 | int xBegin = upperBoundScaledX(frameContext->xOffset); |
220 | int yBegin = upperBoundScaledY(frameContext->yOffset + rowNumber); |
221 | int xEnd = lowerBoundScaledX(std::min(static_cast<int>(frameContext->xOffset + width), size().width()) - 1, xBegin + 1) + 1; |
222 | int yEnd = lowerBoundScaledY(std::min(static_cast<int>(frameContext->yOffset + rowNumber + repeatCount), size().height()) - 1, yBegin + 1) + 1; |
223 | if (rowBuffer.isEmpty() || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin)) |
224 | return true; |
225 | |
226 | // Get the colormap. |
227 | const unsigned char* colorMap; |
228 | unsigned colorMapSize; |
229 | if (frameContext->isLocalColormapDefined) { |
230 | colorMap = m_reader->localColormap(frameContext); |
231 | colorMapSize = m_reader->localColormapSize(frameContext); |
232 | } else { |
233 | colorMap = m_reader->globalColormap(); |
234 | colorMapSize = m_reader->globalColormapSize(); |
235 | } |
236 | if (!colorMap) |
237 | return true; |
238 | |
239 | // Initialize the frame if necessary. |
240 | auto& buffer = m_frameBufferCache[frameIndex]; |
241 | if ((buffer.isInvalid() && !initFrameBuffer(frameIndex)) || !buffer.hasBackingStore()) |
242 | return false; |
243 | |
244 | auto* currentAddress = buffer.backingStore()->pixelAt(xBegin, yBegin); |
245 | // Write one row's worth of data into the frame. |
246 | for (int x = xBegin; x < xEnd; ++x) { |
247 | const unsigned char sourceValue = rowBuffer[(m_scaled ? m_scaledColumns[x] : x) - frameContext->xOffset]; |
248 | if ((!frameContext->isTransparent || (sourceValue != frameContext->tpixel)) && (sourceValue < colorMapSize)) { |
249 | const size_t colorIndex = static_cast<size_t>(sourceValue) * 3; |
250 | buffer.backingStore()->setPixel(currentAddress, colorMap[colorIndex], colorMap[colorIndex + 1], colorMap[colorIndex + 2], 255); |
251 | } else { |
252 | m_currentBufferSawAlpha = true; |
253 | // We may or may not need to write transparent pixels to the buffer. |
254 | // If we're compositing against a previous image, it's wrong, and if |
255 | // we're writing atop a cleared, fully transparent buffer, it's |
256 | // unnecessary; but if we're decoding an interlaced gif and |
257 | // displaying it "Haeberli"-style, we must write these for passes |
258 | // beyond the first, or the initial passes will "show through" the |
259 | // later ones. |
260 | if (writeTransparentPixels) |
261 | buffer.backingStore()->setPixel(currentAddress, 0, 0, 0, 0); |
262 | } |
263 | ++currentAddress; |
264 | } |
265 | |
266 | // Tell the frame to copy the row data if need be. |
267 | if (repeatCount > 1) |
268 | buffer.backingStore()->repeatFirstRow(IntRect(xBegin, yBegin, xEnd - xBegin , yEnd - yBegin)); |
269 | |
270 | return true; |
271 | } |
272 | |
273 | bool GIFImageDecoder::frameComplete(unsigned frameIndex, unsigned frameDuration, ScalableImageDecoderFrame::DisposalMethod disposalMethod) |
274 | { |
275 | // Initialize the frame if necessary. Some GIFs insert do-nothing frames, |
276 | // in which case we never reach haveDecodedRow() before getting here. |
277 | auto& buffer = m_frameBufferCache[frameIndex]; |
278 | if (buffer.isInvalid() && !initFrameBuffer(frameIndex)) |
279 | return false; // initFrameBuffer() has already called setFailed(). |
280 | |
281 | buffer.setDecodingStatus(DecodingStatus::Complete); |
282 | buffer.setDuration(Seconds::fromMilliseconds(frameDuration)); |
283 | buffer.setDisposalMethod(disposalMethod); |
284 | |
285 | if (!m_currentBufferSawAlpha) { |
286 | IntRect rect = buffer.backingStore()->frameRect(); |
287 | |
288 | // The whole frame was non-transparent, so it's possible that the entire |
289 | // resulting buffer was non-transparent, and we can setHasAlpha(false). |
290 | if (rect.contains(IntRect(IntPoint(), scaledSize()))) |
291 | buffer.setHasAlpha(false); |
292 | else if (frameIndex) { |
293 | // Tricky case. This frame does not have alpha only if everywhere |
294 | // outside its rect doesn't have alpha. To know whether this is |
295 | // true, we check the start state of the frame -- if it doesn't have |
296 | // alpha, we're safe. |
297 | // |
298 | // First skip over prior DisposalMethod::RestoreToPrevious frames (since they |
299 | // don't affect the start state of this frame) the same way we do in |
300 | // initFrameBuffer(). |
301 | const auto* prevBuffer = &m_frameBufferCache[--frameIndex]; |
302 | while (frameIndex && (prevBuffer->disposalMethod() == ScalableImageDecoderFrame::DisposalMethod::RestoreToPrevious)) |
303 | prevBuffer = &m_frameBufferCache[--frameIndex]; |
304 | |
305 | // Now, if we're at a DisposalMethod::Unspecified or DisposalMethod::DoNotDispose frame, then |
306 | // we can say we have no alpha if that frame had no alpha. But |
307 | // since in initFrameBuffer() we already copied that frame's alpha |
308 | // state into the current frame's, we need do nothing at all here. |
309 | // |
310 | // The only remaining case is a DisposalMethod::RestoreToBackground frame. If |
311 | // it had no alpha, and its rect is contained in the current frame's |
312 | // rect, we know the current frame has no alpha. |
313 | IntRect prevRect = prevBuffer->backingStore()->frameRect(); |
314 | if ((prevBuffer->disposalMethod() == ScalableImageDecoderFrame::DisposalMethod::RestoreToBackground) && !prevBuffer->hasAlpha() && rect.contains(prevRect)) |
315 | buffer.setHasAlpha(false); |
316 | } |
317 | } |
318 | |
319 | return true; |
320 | } |
321 | |
322 | void GIFImageDecoder::gifComplete() |
323 | { |
324 | // Cache the repetition count, which is now as authoritative as it's ever |
325 | // going to be. |
326 | repetitionCount(); |
327 | |
328 | m_reader = nullptr; |
329 | } |
330 | |
331 | void GIFImageDecoder::decode(unsigned haltAtFrame, GIFQuery query, bool allDataReceived) |
332 | { |
333 | if (failed()) |
334 | return; |
335 | |
336 | if (!m_reader) { |
337 | m_reader = std::make_unique<GIFImageReader>(this); |
338 | m_reader->setData(m_data.get()); |
339 | } |
340 | |
341 | if (query == GIFSizeQuery) { |
342 | if (!m_reader->decode(GIFSizeQuery, haltAtFrame)) |
343 | setFailed(); |
344 | return; |
345 | } |
346 | |
347 | if (!m_reader->decode(GIFFrameCountQuery, haltAtFrame)) { |
348 | setFailed(); |
349 | return; |
350 | } |
351 | |
352 | m_frameBufferCache.resize(m_reader->imagesCount()); |
353 | |
354 | if (query == GIFFrameCountQuery) |
355 | return; |
356 | |
357 | if (!m_reader->decode(GIFFullQuery, haltAtFrame)) { |
358 | setFailed(); |
359 | return; |
360 | } |
361 | |
362 | // It is also a fatal error if all data is received but we failed to decode |
363 | // all frames completely. |
364 | if (allDataReceived && haltAtFrame >= m_frameBufferCache.size() && m_reader) |
365 | setFailed(); |
366 | } |
367 | |
368 | bool GIFImageDecoder::initFrameBuffer(unsigned frameIndex) |
369 | { |
370 | // Initialize the frame rect in our buffer. |
371 | const GIFFrameContext* frameContext = m_reader->frameContext(); |
372 | IntRect frameRect(frameContext->xOffset, frameContext->yOffset, frameContext->width, frameContext->height); |
373 | auto* const buffer = &m_frameBufferCache[frameIndex]; |
374 | |
375 | if (!frameIndex) { |
376 | // This is the first frame, so we're not relying on any previous data. |
377 | if (!buffer->initialize(scaledSize(), m_premultiplyAlpha)) |
378 | return setFailed(); |
379 | } else { |
380 | // The starting state for this frame depends on the previous frame's |
381 | // disposal method. |
382 | // |
383 | // Frames that use the DisposalMethod::RestoreToPrevious method are effectively |
384 | // no-ops in terms of changing the starting state of a frame compared to |
385 | // the starting state of the previous frame, so skip over them. (If the |
386 | // first frame specifies this method, it will get treated like |
387 | // DisposalMethod::RestoreToBackground below and reset to a completely empty image.) |
388 | const auto* prevBuffer = &m_frameBufferCache[--frameIndex]; |
389 | auto prevMethod = prevBuffer->disposalMethod(); |
390 | while (frameIndex && (prevMethod == ScalableImageDecoderFrame::DisposalMethod::RestoreToPrevious)) { |
391 | prevBuffer = &m_frameBufferCache[--frameIndex]; |
392 | prevMethod = prevBuffer->disposalMethod(); |
393 | } |
394 | |
395 | ASSERT(prevBuffer->isComplete()); |
396 | |
397 | if ((prevMethod == ScalableImageDecoderFrame::DisposalMethod::Unspecified) || (prevMethod == ScalableImageDecoderFrame::DisposalMethod::DoNotDispose)) { |
398 | // Preserve the last frame as the starting state for this frame. |
399 | if (!prevBuffer->backingStore() || !buffer->initialize(*prevBuffer->backingStore())) |
400 | return setFailed(); |
401 | } else { |
402 | // We want to clear the previous frame to transparent, without |
403 | // affecting pixels in the image outside of the frame. |
404 | IntRect prevRect = prevBuffer->backingStore()->frameRect(); |
405 | const IntSize& bufferSize = scaledSize(); |
406 | if (!frameIndex || prevRect.contains(IntRect(IntPoint(), scaledSize()))) { |
407 | // Clearing the first frame, or a frame the size of the whole |
408 | // image, results in a completely empty image. |
409 | if (!buffer->initialize(bufferSize, m_premultiplyAlpha)) |
410 | return setFailed(); |
411 | } else { |
412 | // Copy the whole previous buffer, then clear just its frame. |
413 | if (!prevBuffer->backingStore() || !buffer->initialize(*prevBuffer->backingStore())) |
414 | return setFailed(); |
415 | buffer->backingStore()->clearRect(prevRect); |
416 | buffer->setHasAlpha(true); |
417 | } |
418 | } |
419 | } |
420 | |
421 | // Make sure the frameRect doesn't extend outside the buffer. |
422 | if (frameRect.maxX() > size().width()) |
423 | frameRect.setWidth(size().width() - frameContext->xOffset); |
424 | if (frameRect.maxY() > size().height()) |
425 | frameRect.setHeight(size().height() - frameContext->yOffset); |
426 | |
427 | int left = upperBoundScaledX(frameRect.x()); |
428 | int right = lowerBoundScaledX(frameRect.maxX(), left); |
429 | int top = upperBoundScaledY(frameRect.y()); |
430 | int bottom = lowerBoundScaledY(frameRect.maxY(), top); |
431 | buffer->backingStore()->setFrameRect(IntRect(left, top, right - left, bottom - top)); |
432 | |
433 | // Update our status to be partially complete. |
434 | buffer->setDecodingStatus(DecodingStatus::Partial); |
435 | |
436 | // Reset the alpha pixel tracker for this frame. |
437 | m_currentBufferSawAlpha = false; |
438 | return true; |
439 | } |
440 | |
441 | } // namespace WebCore |
442 | |