This source file includes following definitions.
- m_decodedOffset
- setData
- isSizeAvailable
- frameBufferAtIndex
- setFailed
- decode
- decodeHelper
- processFileHeader
#include "config.h"
#include "platform/image-decoders/bmp/BMPImageDecoder.h"
#include "platform/PlatformInstrumentation.h"
#include "wtf/PassOwnPtr.h"
namespace WebCore {
static const size_t sizeOfFileHeader = 14;
BMPImageDecoder::BMPImageDecoder(ImageSource::AlphaOption alphaOption,
ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption,
size_t maxDecodedBytes)
: ImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes)
, m_decodedOffset(0)
{
}
void BMPImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
{
if (failed())
return;
ImageDecoder::setData(data, allDataReceived);
if (m_reader)
m_reader->setData(data);
}
bool BMPImageDecoder::isSizeAvailable()
{
if (!ImageDecoder::isSizeAvailable())
decode(true);
return ImageDecoder::isSizeAvailable();
}
ImageFrame* BMPImageDecoder::frameBufferAtIndex(size_t index)
{
if (index)
return 0;
if (m_frameBufferCache.isEmpty()) {
m_frameBufferCache.resize(1);
m_frameBufferCache.first().setPremultiplyAlpha(m_premultiplyAlpha);
}
ImageFrame* buffer = &m_frameBufferCache.first();
if (buffer->status() != ImageFrame::FrameComplete) {
PlatformInstrumentation::willDecodeImage("BMP");
decode(false);
PlatformInstrumentation::didDecodeImage();
}
return buffer;
}
bool BMPImageDecoder::setFailed()
{
m_reader.clear();
return ImageDecoder::setFailed();
}
void BMPImageDecoder::decode(bool onlySize)
{
if (failed())
return;
if (!decodeHelper(onlySize) && isAllDataReceived())
setFailed();
else if (!m_frameBufferCache.isEmpty() && (m_frameBufferCache.first().status() == ImageFrame::FrameComplete))
m_reader.clear();
}
bool BMPImageDecoder::decodeHelper(bool onlySize)
{
size_t imgDataOffset = 0;
if ((m_decodedOffset < sizeOfFileHeader) && !processFileHeader(&imgDataOffset))
return false;
if (!m_reader) {
m_reader = adoptPtr(new BMPImageReader(this, m_decodedOffset, imgDataOffset, false));
m_reader->setData(m_data.get());
}
if (!m_frameBufferCache.isEmpty())
m_reader->setBuffer(&m_frameBufferCache.first());
return m_reader->decodeBMP(onlySize);
}
bool BMPImageDecoder::processFileHeader(size_t* imgDataOffset)
{
ASSERT(imgDataOffset);
ASSERT(!m_decodedOffset);
if (m_data->size() < sizeOfFileHeader)
return false;
const uint16_t fileType = (m_data->data()[0] << 8) | static_cast<uint8_t>(m_data->data()[1]);
*imgDataOffset = readUint32(10);
m_decodedOffset = sizeOfFileHeader;
enum {
BMAP = 0x424D,
};
return (fileType == BMAP) || setFailed();
}
}