This source file includes following definitions.
- m_repetitionCount
- setData
- isSizeAvailable
- frameCount
- repetitionCount
- frameBufferAtIndex
- frameIsCompleteAtIndex
- frameDurationAtIndex
- setFailed
- haveDecodedRow
- parseCompleted
- frameComplete
- clearCacheExceptFrame
- clearFrameBuffer
- parse
- decode
- initFrameBuffer
#include "config.h"
#include "platform/image-decoders/gif/GIFImageDecoder.h"
#include <limits>
#include "platform/PlatformInstrumentation.h"
#include "platform/image-decoders/gif/GIFImageReader.h"
#include "wtf/NotFound.h"
#include "wtf/PassOwnPtr.h"
namespace WebCore {
GIFImageDecoder::GIFImageDecoder(ImageSource::AlphaOption alphaOption,
ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption,
size_t maxDecodedBytes)
: ImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes)
, m_repetitionCount(cAnimationLoopOnce)
{
}
GIFImageDecoder::~GIFImageDecoder()
{
}
void GIFImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
{
if (failed())
return;
ImageDecoder::setData(data, allDataReceived);
if (m_reader)
m_reader->setData(data);
}
bool GIFImageDecoder::isSizeAvailable()
{
if (!ImageDecoder::isSizeAvailable())
parse(GIFSizeQuery);
return ImageDecoder::isSizeAvailable();
}
size_t GIFImageDecoder::frameCount()
{
parse(GIFFrameCountQuery);
return m_frameBufferCache.size();
}
int GIFImageDecoder::repetitionCount() const
{
if (isAllDataReceived() && parseCompleted() && m_reader->imagesCount() == 1)
m_repetitionCount = cAnimationNone;
else if (failed() || (m_reader && (!m_reader->imagesCount())))
m_repetitionCount = cAnimationLoopOnce;
else if (m_reader && m_reader->loopCount() != cLoopCountNotSeen)
m_repetitionCount = m_reader->loopCount();
return m_repetitionCount;
}
ImageFrame* GIFImageDecoder::frameBufferAtIndex(size_t index)
{
if (index >= frameCount())
return 0;
ImageFrame& frame = m_frameBufferCache[index];
if (frame.status() != ImageFrame::FrameComplete) {
PlatformInstrumentation::willDecodeImage("GIF");
decode(index);
PlatformInstrumentation::didDecodeImage();
}
frame.notifyBitmapIfPixelsChanged();
return &frame;
}
bool GIFImageDecoder::frameIsCompleteAtIndex(size_t index) const
{
return m_reader && (index < m_reader->imagesCount()) && m_reader->frameContext(index)->isComplete();
}
float GIFImageDecoder::frameDurationAtIndex(size_t index) const
{
return (m_reader && (index < m_reader->imagesCount()) &&
m_reader->frameContext(index)->isHeaderDefined()) ?
m_reader->frameContext(index)->delayTime() : 0;
}
bool GIFImageDecoder::setFailed()
{
m_reader.clear();
return ImageDecoder::setFailed();
}
bool GIFImageDecoder::haveDecodedRow(size_t frameIndex, GIFRow::const_iterator rowBegin, size_t width, size_t rowNumber, unsigned repeatCount, bool writeTransparentPixels)
{
const GIFFrameContext* frameContext = m_reader->frameContext(frameIndex);
const int xBegin = frameContext->xOffset();
const int yBegin = frameContext->yOffset() + rowNumber;
const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), size().width());
const int yEnd = std::min(static_cast<int>(frameContext->yOffset() + rowNumber + repeatCount), size().height());
if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
return true;
const GIFColorMap::Table& colorTable = frameContext->localColorMap().isDefined() ? frameContext->localColorMap().table() : m_reader->globalColorMap().table();
if (colorTable.isEmpty())
return true;
GIFColorMap::Table::const_iterator colorTableIter = colorTable.begin();
ImageFrame& buffer = m_frameBufferCache[frameIndex];
if ((buffer.status() == ImageFrame::FrameEmpty) && !initFrameBuffer(frameIndex))
return false;
const size_t transparentPixel = frameContext->transparentPixel();
GIFRow::const_iterator rowEnd = rowBegin + (xEnd - xBegin);
ImageFrame::PixelData* currentAddress = buffer.getAddr(xBegin, yBegin);
if (writeTransparentPixels) {
for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) {
const size_t sourceValue = *rowBegin;
if ((sourceValue != transparentPixel) && (sourceValue < colorTable.size())) {
*currentAddress = colorTableIter[sourceValue];
} else {
*currentAddress = 0;
m_currentBufferSawAlpha = true;
}
}
} else {
for (; rowBegin != rowEnd; ++rowBegin, ++currentAddress) {
const size_t sourceValue = *rowBegin;
if ((sourceValue != transparentPixel) && (sourceValue < colorTable.size()))
*currentAddress = colorTableIter[sourceValue];
else
m_currentBufferSawAlpha = true;
}
}
if (repeatCount > 1)
buffer.copyRowNTimes(xBegin, xEnd, yBegin, yEnd);
buffer.setPixelsChanged(true);
return true;
}
bool GIFImageDecoder::parseCompleted() const
{
return m_reader && m_reader->parseCompleted();
}
bool GIFImageDecoder::frameComplete(size_t frameIndex)
{
ImageFrame& buffer = m_frameBufferCache[frameIndex];
if ((buffer.status() == ImageFrame::FrameEmpty) && !initFrameBuffer(frameIndex))
return false;
buffer.setStatus(ImageFrame::FrameComplete);
if (!m_currentBufferSawAlpha) {
if (buffer.originalFrameRect().contains(IntRect(IntPoint(), size()))) {
buffer.setHasAlpha(false);
buffer.setRequiredPreviousFrameIndex(kNotFound);
} else if (buffer.requiredPreviousFrameIndex() != kNotFound) {
const ImageFrame* prevBuffer = &m_frameBufferCache[buffer.requiredPreviousFrameIndex()];
ASSERT(prevBuffer->disposalMethod() != ImageFrame::DisposeOverwritePrevious);
if ((prevBuffer->disposalMethod() == ImageFrame::DisposeOverwriteBgcolor) && !prevBuffer->hasAlpha() && buffer.originalFrameRect().contains(prevBuffer->originalFrameRect()))
buffer.setHasAlpha(false);
}
}
return true;
}
size_t GIFImageDecoder::clearCacheExceptFrame(size_t clearExceptFrame)
{
while ((clearExceptFrame < m_frameBufferCache.size()) && (m_frameBufferCache[clearExceptFrame].status() == ImageFrame::FrameEmpty))
clearExceptFrame = m_frameBufferCache[clearExceptFrame].requiredPreviousFrameIndex();
return ImageDecoder::clearCacheExceptFrame(clearExceptFrame);
}
void GIFImageDecoder::clearFrameBuffer(size_t frameIndex)
{
if (m_reader && m_frameBufferCache[frameIndex].status() == ImageFrame::FramePartial) {
m_reader->clearDecodeState(frameIndex);
}
ImageDecoder::clearFrameBuffer(frameIndex);
}
void GIFImageDecoder::parse(GIFParseQuery query)
{
if (failed())
return;
if (!m_reader) {
m_reader = adoptPtr(new GIFImageReader(this));
m_reader->setData(m_data);
}
if (!m_reader->parse(query)) {
setFailed();
return;
}
const size_t oldSize = m_frameBufferCache.size();
m_frameBufferCache.resize(m_reader->imagesCount());
for (size_t i = oldSize; i < m_reader->imagesCount(); ++i) {
ImageFrame& buffer = m_frameBufferCache[i];
const GIFFrameContext* frameContext = m_reader->frameContext(i);
buffer.setPremultiplyAlpha(m_premultiplyAlpha);
buffer.setRequiredPreviousFrameIndex(findRequiredPreviousFrame(i, false));
buffer.setDuration(frameContext->delayTime());
buffer.setDisposalMethod(frameContext->disposalMethod());
IntRect frameRect = frameContext->frameRect();
if (frameRect.maxX() > size().width())
frameRect.setWidth(size().width() - frameRect.x());
if (frameRect.maxY() > size().height())
frameRect.setHeight(size().height() - frameRect.y());
buffer.setOriginalFrameRect(frameRect);
}
}
void GIFImageDecoder::decode(size_t frameIndex)
{
parse(GIFFrameCountQuery);
if (failed())
return;
Vector<size_t> framesToDecode;
size_t frameToDecode = frameIndex;
do {
framesToDecode.append(frameToDecode);
frameToDecode = m_frameBufferCache[frameToDecode].requiredPreviousFrameIndex();
} while (frameToDecode != kNotFound && m_frameBufferCache[frameToDecode].status() != ImageFrame::FrameComplete);
for (size_t i = framesToDecode.size(); i > 0; --i) {
size_t frameIndex = framesToDecode[i - 1];
if (!m_reader->decode(frameIndex)) {
setFailed();
return;
}
if (m_frameBufferCache[frameIndex].status() != ImageFrame::FrameComplete)
break;
}
if (frameIndex >= m_frameBufferCache.size() - 1 && isAllDataReceived() && m_reader && !m_reader->parseCompleted())
setFailed();
}
bool GIFImageDecoder::initFrameBuffer(size_t frameIndex)
{
ImageFrame* const buffer = &m_frameBufferCache[frameIndex];
size_t requiredPreviousFrameIndex = buffer->requiredPreviousFrameIndex();
if (requiredPreviousFrameIndex == kNotFound) {
if (!buffer->setSize(size().width(), size().height()))
return setFailed();
} else {
const ImageFrame* prevBuffer = &m_frameBufferCache[requiredPreviousFrameIndex];
ASSERT(prevBuffer->status() == ImageFrame::FrameComplete);
if (!buffer->copyBitmapData(*prevBuffer))
return setFailed();
if (prevBuffer->disposalMethod() == ImageFrame::DisposeOverwriteBgcolor) {
const IntRect& prevRect = prevBuffer->originalFrameRect();
ASSERT(!prevRect.contains(IntRect(IntPoint(), size())));
buffer->zeroFillFrameRect(prevRect);
}
}
buffer->setStatus(ImageFrame::FramePartial);
m_currentBufferSawAlpha = false;
return true;
}
}