#ifndef GIFImageReader_h
#define GIFImageReader_h
#include "platform/SharedBuffer.h"
#include "platform/image-decoders/gif/GIFImageDecoder.h"
#include "wtf/Noncopyable.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/Vector.h"
#define MAX_DICTIONARY_ENTRY_BITS 12
#define MAX_DICTIONARY_ENTRIES 4096
#define MAX_COLORS 256
#define BYTES_PER_COLORMAP_ENTRY 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)
: codesize(0)
, codemask(0)
, clearCode(0)
, avail(0)
, oldcode(0)
, firstchar(0)
, bits(0)
, datum(0)
, ipass(0)
, irow(0)
, rowsRemaining(0)
, rowIter(0)
, m_client(client)
, m_frameContext(frameContext)
{ }
bool prepareToDecode();
bool outputRow(GIFRow::const_iterator rowBegin);
bool doLZW(const unsigned char* block, size_t bytesInBlock);
bool hasRemainingRows() { return rowsRemaining; }
private:
int codesize;
int codemask;
int clearCode;
int avail;
int oldcode;
unsigned char firstchar;
int bits;
int datum;
int ipass;
size_t irow;
size_t rowsRemaining;
unsigned short prefix[MAX_DICTIONARY_ENTRIES];
unsigned char suffix[MAX_DICTIONARY_ENTRIES];
unsigned short suffixLength[MAX_DICTIONARY_ENTRIES];
GIFRow rowBuffer;
GIFRow::iterator rowIter;
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;
};
class GIFColorMap {
WTF_MAKE_FAST_ALLOCATED;
public:
typedef Vector<WebCore::ImageFrame::PixelData> Table;
GIFColorMap()
: m_isDefined(false)
, m_position(0)
, m_colors(0)
{
}
void setTablePositionAndSize(size_t position, size_t colors)
{
m_position = position;
m_colors = colors;
}
void setDefined() { m_isDefined = true; }
bool isDefined() const { return m_isDefined; }
void buildTable(const unsigned char* data, size_t length);
const Table& table() const { return m_table; }
private:
bool m_isDefined;
size_t m_position;
size_t m_colors;
Table m_table;
};
struct GIFFrameContext {
WTF_MAKE_FAST_ALLOCATED; WTF_MAKE_NONCOPYABLE(GIFFrameContext);
public:
GIFFrameContext(int id)
: m_frameId(id)
, m_xOffset(0)
, m_yOffset(0)
, m_width(0)
, m_height(0)
, m_transparentPixel(kNotFound)
, m_disposalMethod(WebCore::ImageFrame::DisposeNotSpecified)
, m_dataSize(0)
, m_progressiveDisplay(false)
, m_interlaced(false)
, m_delayTime(0)
, m_currentLzwBlock(0)
, m_isComplete(false)
, m_isHeaderDefined(false)
, m_isDataSizeDefined(false)
{
}
~GIFFrameContext()
{
}
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);
int frameId() const { return m_frameId; }
void setRect(unsigned x, unsigned y, unsigned width, unsigned height)
{
m_xOffset = x;
m_yOffset = y;
m_width = width;
m_height = height;
}
WebCore::IntRect frameRect() const { return WebCore::IntRect(m_xOffset, m_yOffset, m_width, m_height); }
unsigned xOffset() const { return m_xOffset; }
unsigned yOffset() const { return m_yOffset; }
unsigned width() const { return m_width; }
unsigned height() const { return m_height; }
size_t transparentPixel() const { return m_transparentPixel; }
void setTransparentPixel(size_t pixel) { m_transparentPixel = pixel; }
WebCore::ImageFrame::DisposalMethod disposalMethod() const { return m_disposalMethod; }
void setDisposalMethod(WebCore::ImageFrame::DisposalMethod disposalMethod) { m_disposalMethod = disposalMethod; }
unsigned delayTime() const { return m_delayTime; }
void setDelayTime(unsigned delay) { m_delayTime = delay; }
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; }
int dataSize() const { return m_dataSize; }
void setDataSize(int size)
{
m_dataSize = size;
m_isDataSizeDefined = true;
}
bool progressiveDisplay() const { return m_progressiveDisplay; }
void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay = progressiveDisplay; }
bool interlaced() const { return m_interlaced; }
void setInterlaced(bool interlaced) { m_interlaced = interlaced; }
void clearDecodeState() { m_lzwContext.clear(); }
const GIFColorMap& localColorMap() const { return m_localColorMap; }
GIFColorMap& localColorMap() { return m_localColorMap; }
private:
int m_frameId;
unsigned m_xOffset;
unsigned m_yOffset;
unsigned m_width;
unsigned m_height;
size_t m_transparentPixel;
WebCore::ImageFrame::DisposalMethod m_disposalMethod;
int m_dataSize;
bool m_progressiveDisplay;
bool m_interlaced;
unsigned m_delayTime;
OwnPtr<GIFLZWContext> m_lzwContext;
Vector<GIFLZWBlock> m_lzwBlocks;
GIFColorMap m_localColorMap;
size_t m_currentLzwBlock;
bool m_isComplete;
bool m_isHeaderDefined;
bool m_isDataSizeDefined;
};
class PLATFORM_EXPORT GIFImageReader {
WTF_MAKE_FAST_ALLOCATED; WTF_MAKE_NONCOPYABLE(GIFImageReader);
public:
GIFImageReader(WebCore::GIFImageDecoder* client = 0)
: m_client(client)
, m_state(GIFType)
, m_bytesToConsume(6)
, m_bytesRead(0)
, m_version(0)
, m_screenWidth(0)
, m_screenHeight(0)
, m_loopCount(cLoopCountNotSeen)
, m_parseCompleted(false)
{
}
~GIFImageReader()
{
}
void setData(PassRefPtr<WebCore::SharedBuffer> data) { m_data = data; }
bool parse(WebCore::GIFImageDecoder::GIFParseQuery);
bool decode(size_t frameIndex);
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 GIFColorMap& globalColorMap() const
{
return m_globalColorMap;
}
const GIFFrameContext* frameContext(size_t index) const
{
return index < m_frames.size() ? m_frames[index].get() : 0;
}
bool parseCompleted() const { return m_parseCompleted; }
void clearDecodeState(size_t index) { m_frames[index]->clearDecodeState(); }
private:
bool parseData(size_t dataPosition, size_t len, WebCore::GIFImageDecoder::GIFParseQuery);
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_version;
unsigned m_screenWidth;
unsigned m_screenHeight;
GIFColorMap m_globalColorMap;
int m_loopCount;
Vector<OwnPtr<GIFFrameContext> > m_frames;
RefPtr<WebCore::SharedBuffer> m_data;
bool m_parseCompleted;
};
#endif