#include "config.h"
#include "ImageSource.h"
#include "ImageDecoderQt.h"
#include "SharedBuffer.h"
#include <QImage>
#include <qdebug.h>
namespace WebCore {
enum ImageFormat { ImageFormat_None, ImageFormat_GIF, ImageFormat_PNG, ImageFormat_JPEG,
ImageFormat_BMP, ImageFormat_ICO, ImageFormat_XBM };
ImageFormat detectImageFormat(const SharedBuffer& data)
{
int length = data.size();
if (length < 4)
return ImageFormat_None;
const unsigned char* uContents = (const unsigned char*) data.data();
const char* contents = data.data();
if (strncmp(contents, "GIF8", 4) == 0)
return ImageFormat_GIF;
if (uContents[0] == 0x89 &&
uContents[1] == 0x50 &&
uContents[2] == 0x4E &&
uContents[3] == 0x47)
return ImageFormat_PNG;
if (uContents[0] == 0xFF &&
uContents[1] == 0xD8 &&
uContents[2] == 0xFF)
return ImageFormat_JPEG;
if (strncmp(contents, "BM", 2) == 0)
return ImageFormat_BMP;
if (!memcmp(contents, "\000\000\001\000", 4) ||
!memcmp(contents, "\000\000\002\000", 4))
return ImageFormat_ICO;
if (length >= 8 && strncmp(contents, "#define ", 8) == 0)
return ImageFormat_XBM;
return ImageFormat_None;
}
ImageDecoderQt* createDecoder(const SharedBuffer& data) {
if (detectImageFormat(data) != ImageFormat_None)
return new ImageDecoderQt();
return 0;
}
ImageSource::ImageSource()
: m_decoder(0)
{
}
ImageSource::~ImageSource()
{
delete m_decoder;
}
bool ImageSource::initialized() const
{
return m_decoder;
}
void ImageSource::setData(SharedBuffer* data, bool allDataReceived)
{
if (!m_decoder)
m_decoder = createDecoder(*data);
if (!m_decoder)
return;
m_decoder->setData(data->buffer(), allDataReceived);
}
bool ImageSource::isSizeAvailable()
{
if (!m_decoder)
return false;
return m_decoder->isSizeAvailable();
}
IntSize ImageSource::size() const
{
if (!m_decoder)
return IntSize();
return m_decoder->size();
}
int ImageSource::repetitionCount()
{
if (!m_decoder)
return cAnimationNone;
return m_decoder->repetitionCount();
}
size_t ImageSource::frameCount() const
{
if (!m_decoder)
return 0;
return m_decoder->frameCount();
}
NativeImagePtr ImageSource::createFrameAtIndex(size_t index)
{
if (!m_decoder)
return 0;
const QPixmap* source = m_decoder->imageAtIndex(index);
if (!source)
return 0;
return new QPixmap(*source);
}
float ImageSource::frameDurationAtIndex(size_t index)
{
if (!m_decoder)
return 0;
const float duration = m_decoder->duration(index) / 1000.0f;
return (duration < 0.051f) ? 0.100f : duration;
}
bool ImageSource::frameHasAlphaAtIndex(size_t index)
{
if (!m_decoder || !m_decoder->supportsAlpha())
return false;
const QPixmap* source = m_decoder->imageAtIndex( index);
if (!source)
return false;
return source->hasAlphaChannel();
}
bool ImageSource::frameIsCompleteAtIndex(size_t index)
{
return (m_decoder && m_decoder->imageAtIndex(index) != 0);
}
void ImageSource::clear()
{
delete m_decoder;
m_decoder = 0;
}
}