#pragma once
#include "GIFImageDecoder.h"
#include "SharedBuffer.h"
#include <wtf/Vector.h>
#define MAX_LZW_BITS 12
#define MAX_BYTES 4097
#define MAX_COLORS 256
#define GIF_COLORS 3
const int cLoopCountNotSeen = -2;
enum GIFState {
GIFType,
GIFGlobalHeader,
GIFGlobalColormap,
GIFImageStart,
GIFImageHeader,
GIFImageColormap,
GIFImageBody,
GIFLZWStart,
GIFLZW,
GIFSubBlock,
GIFExtension,
GIFControlExtension,
GIFConsumeBlock,
GIFSkipBlock,
GIFDone,
GIFCommentExtension,
GIFApplicationExtension,
GIFNetscapeExtensionBlock,
GIFConsumeNetscapeExtension,
GIFConsumeComment
};
struct GIFFrameContext;
class GIFLZWContext {
WTF_MAKE_FAST_ALLOCATED;
public:
GIFLZWContext(WebCore::GIFImageDecoder* client, const GIFFrameContext* frameContext)
: stackp(0)
, codesize(0)
, codemask(0)
, clearCode(0)
, avail(0)
, oldcode(0)
, firstchar(0)
, bits(0)
, datum(0)
, ipass(0)
, irow(0)
, rowPosition(0)
, rowsRemaining(0)
, m_client(client)
, m_frameContext(frameContext)
{ }
bool prepareToDecode();
bool outputRow();
bool doLZW(const unsigned char* block, size_t bytesInBlock);
bool hasRemainingRows() { return rowsRemaining; }
private:
size_t stackp; int codesize;
int codemask;
int clearCode; int avail; int oldcode;
unsigned char firstchar;
int bits; int datum; int ipass; size_t irow; size_t rowPosition;
size_t rowsRemaining;
Vector<unsigned short> prefix;
Vector<unsigned char> suffix;
Vector<unsigned char> stack;
Vector<unsigned char> rowBuffer;
WebCore::GIFImageDecoder* m_client;
const GIFFrameContext* m_frameContext;
};
struct GIFLZWBlock {
WTF_MAKE_FAST_ALLOCATED;
public:
GIFLZWBlock(size_t position, size_t size)
: blockPosition(position)
, blockSize(size)
{
}
size_t blockPosition;
size_t blockSize;
};
struct GIFFrameContext {
WTF_MAKE_FAST_ALLOCATED;
public:
int frameId;
unsigned xOffset;
unsigned yOffset; unsigned width;
unsigned height;
int tpixel; WebCore::ImageFrame::DisposalMethod disposalMethod; size_t localColormapPosition; int localColormapSize; int datasize;
bool isLocalColormapDefined : 1;
bool progressiveDisplay : 1; bool interlaced : 1; bool isTransparent : 1;
unsigned delayTime;
GIFFrameContext(int id)
: frameId(id)
, xOffset(0)
, yOffset(0)
, width(0)
, height(0)
, tpixel(0)
, disposalMethod(WebCore::ImageFrame::DisposalMethod::Unspecified)
, localColormapPosition(0)
, localColormapSize(0)
, datasize(0)
, isLocalColormapDefined(false)
, progressiveDisplay(false)
, interlaced(false)
, isTransparent(false)
, delayTime(0)
, m_currentLzwBlock(0)
, m_isComplete(false)
, m_isHeaderDefined(false)
, m_isDataSizeDefined(false)
{
}
void addLzwBlock(size_t position, size_t size)
{
m_lzwBlocks.append(GIFLZWBlock(position, size));
}
bool decode(const unsigned char* data, size_t length, WebCore::GIFImageDecoder* client, bool* frameDecoded);
bool isComplete() const { return m_isComplete; }
void setComplete() { m_isComplete = true; }
bool isHeaderDefined() const { return m_isHeaderDefined; }
void setHeaderDefined() { m_isHeaderDefined = true; }
bool isDataSizeDefined() const { return m_isDataSizeDefined; }
void setDataSize(int size)
{
datasize = size;
m_isDataSizeDefined = true;
}
private:
std::unique_ptr<GIFLZWContext> m_lzwContext;
Vector<GIFLZWBlock> m_lzwBlocks; size_t m_currentLzwBlock;
bool m_isComplete;
bool m_isHeaderDefined;
bool m_isDataSizeDefined;
};
class GIFImageReader {
WTF_MAKE_FAST_ALLOCATED;
public:
GIFImageReader(WebCore::GIFImageDecoder* client = 0)
: m_client(client)
, m_state(GIFType)
, m_bytesToConsume(6) , m_bytesRead(0)
, m_screenBgcolor(0)
, m_version(0)
, m_screenWidth(0)
, m_screenHeight(0)
, m_isGlobalColormapDefined(false)
, m_globalColormapPosition(0)
, m_globalColormapSize(0)
, m_loopCount(cLoopCountNotSeen)
, m_currentDecodingFrame(0)
, m_parseCompleted(false)
{
}
void setData(WebCore::SharedBuffer* data) { m_data = data; }
bool decode(WebCore::GIFImageDecoder::GIFQuery, unsigned haltAtFrame);
size_t imagesCount() const
{
if (m_frames.isEmpty())
return 0;
return m_frames.last()->isHeaderDefined() ? m_frames.size() : m_frames.size() - 1;
}
int loopCount() const { return m_loopCount; }
const unsigned char* globalColormap() const
{
return m_isGlobalColormapDefined ? data(m_globalColormapPosition) : 0;
}
int globalColormapSize() const
{
return m_isGlobalColormapDefined ? m_globalColormapSize : 0;
}
const unsigned char* localColormap(const GIFFrameContext* frame) const
{
return frame->isLocalColormapDefined ? data(frame->localColormapPosition) : 0;
}
int localColormapSize(const GIFFrameContext* frame) const
{
return frame->isLocalColormapDefined ? frame->localColormapSize : 0;
}
const GIFFrameContext* frameContext() const
{
return m_currentDecodingFrame < m_frames.size() ? m_frames[m_currentDecodingFrame].get() : 0;
}
const GIFFrameContext* frameContext(size_t frame) const
{
return frame < m_frames.size() ? m_frames[frame].get() : nullptr;
}
private:
bool parse(size_t dataPosition, size_t len, bool parseSizeOnly);
void setRemainingBytes(size_t);
const unsigned char* data(size_t dataPosition) const
{
return reinterpret_cast<const unsigned char*>(m_data->data()) + dataPosition;
}
void addFrameIfNecessary();
bool currentFrameIsFirstFrame() const
{
return m_frames.isEmpty() || (m_frames.size() == 1u && !m_frames[0]->isComplete());
}
WebCore::GIFImageDecoder* m_client;
GIFState m_state; size_t m_bytesToConsume; size_t m_bytesRead;
int m_screenBgcolor; int m_version; unsigned m_screenWidth; unsigned m_screenHeight;
bool m_isGlobalColormapDefined;
size_t m_globalColormapPosition; int m_globalColormapSize; int m_loopCount;
Vector<std::unique_ptr<GIFFrameContext> > m_frames;
size_t m_currentDecodingFrame;
RefPtr<WebCore::SharedBuffer> m_data;
bool m_parseCompleted;
};