This source file includes following definitions.
- m_decodedOffset
- setData
- isSizeAvailable
- size
- frameSizeAtIndex
- setSize
- frameCount
- frameBufferAtIndex
- setFailed
- hotSpot
- hotSpotAtIndex
- compareEntries
- setDataForPNGDecoderAtIndex
- decode
- decodeDirectory
- decodeAtIndex
- processDirectory
- processDirectoryEntries
- readDirectoryEntry
- imageTypeAtIndex
#include "config.h"
#include "platform/image-decoders/ico/ICOImageDecoder.h"
#include <algorithm>
#include "platform/PlatformInstrumentation.h"
#include "platform/image-decoders/png/PNGImageDecoder.h"
#include "wtf/PassOwnPtr.h"
namespace WebCore {
static const size_t sizeOfDirectory = 6;
static const size_t sizeOfDirEntry = 16;
ICOImageDecoder::ICOImageDecoder(ImageSource::AlphaOption alphaOption,
ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption,
size_t maxDecodedBytes)
: ImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes)
, m_decodedOffset(0)
{
}
ICOImageDecoder::~ICOImageDecoder()
{
}
void ICOImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
{
if (failed())
return;
ImageDecoder::setData(data, allDataReceived);
for (BMPReaders::iterator i(m_bmpReaders.begin()); i != m_bmpReaders.end(); ++i) {
if (*i)
(*i)->setData(data);
}
for (size_t i = 0; i < m_pngDecoders.size(); ++i)
setDataForPNGDecoderAtIndex(i);
}
bool ICOImageDecoder::isSizeAvailable()
{
if (!ImageDecoder::isSizeAvailable())
decode(0, true);
return ImageDecoder::isSizeAvailable();
}
IntSize ICOImageDecoder::size() const
{
return m_frameSize.isEmpty() ? ImageDecoder::size() : m_frameSize;
}
IntSize ICOImageDecoder::frameSizeAtIndex(size_t index) const
{
return (index && (index < m_dirEntries.size())) ? m_dirEntries[index].m_size : size();
}
bool ICOImageDecoder::setSize(unsigned width, unsigned height)
{
return m_frameSize.isEmpty() ? ImageDecoder::setSize(width, height) : ((IntSize(width, height) == m_frameSize) || setFailed());
}
size_t ICOImageDecoder::frameCount()
{
decode(0, true);
if (m_frameBufferCache.isEmpty()) {
m_frameBufferCache.resize(m_dirEntries.size());
for (size_t i = 0; i < m_dirEntries.size(); ++i) {
m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha);
m_frameBufferCache[i].setRequiredPreviousFrameIndex(kNotFound);
}
}
return m_frameBufferCache.size();
}
ImageFrame* ICOImageDecoder::frameBufferAtIndex(size_t index)
{
if (index >= frameCount())
return 0;
ImageFrame* buffer = &m_frameBufferCache[index];
if (buffer->status() != ImageFrame::FrameComplete) {
PlatformInstrumentation::willDecodeImage("ICO");
decode(index, false);
PlatformInstrumentation::didDecodeImage();
}
return buffer;
}
bool ICOImageDecoder::setFailed()
{
m_bmpReaders.clear();
m_pngDecoders.clear();
return ImageDecoder::setFailed();
}
bool ICOImageDecoder::hotSpot(IntPoint& hotSpot) const
{
return hotSpotAtIndex(0, hotSpot);
}
bool ICOImageDecoder::hotSpotAtIndex(size_t index, IntPoint& hotSpot) const
{
if (index >= m_dirEntries.size() || m_fileType != CURSOR)
return false;
hotSpot = m_dirEntries[index].m_hotSpot;
return true;
}
bool ICOImageDecoder::compareEntries(const IconDirectoryEntry& a, const IconDirectoryEntry& b)
{
const int aEntryArea = a.m_size.width() * a.m_size.height();
const int bEntryArea = b.m_size.width() * b.m_size.height();
return (aEntryArea == bEntryArea) ? (a.m_bitCount > b.m_bitCount) : (aEntryArea > bEntryArea);
}
void ICOImageDecoder::setDataForPNGDecoderAtIndex(size_t index)
{
if (!m_pngDecoders[index])
return;
const IconDirectoryEntry& dirEntry = m_dirEntries[index];
RefPtr<SharedBuffer> pngData(SharedBuffer::create(&m_data->data()[dirEntry.m_imageOffset], m_data->size() - dirEntry.m_imageOffset));
m_pngDecoders[index]->setData(pngData.get(), isAllDataReceived());
}
void ICOImageDecoder::decode(size_t index, bool onlySize)
{
if (failed())
return;
if ((!decodeDirectory() || (!onlySize && !decodeAtIndex(index))) && isAllDataReceived())
setFailed();
else if ((m_frameBufferCache.size() > index) && (m_frameBufferCache[index].status() == ImageFrame::FrameComplete)) {
m_bmpReaders[index].clear();
m_pngDecoders[index].clear();
}
}
bool ICOImageDecoder::decodeDirectory()
{
if ((m_decodedOffset < sizeOfDirectory) && !processDirectory())
return false;
return (m_decodedOffset >= (sizeOfDirectory + (m_dirEntries.size() * sizeOfDirEntry))) || processDirectoryEntries();
}
bool ICOImageDecoder::decodeAtIndex(size_t index)
{
ASSERT_WITH_SECURITY_IMPLICATION(index < m_dirEntries.size());
const IconDirectoryEntry& dirEntry = m_dirEntries[index];
const ImageType imageType = imageTypeAtIndex(index);
if (imageType == Unknown)
return false;
if (imageType == BMP) {
if (!m_bmpReaders[index]) {
ASSERT(m_frameBufferCache.size() == m_dirEntries.size());
m_bmpReaders[index] = adoptPtr(new BMPImageReader(this, dirEntry.m_imageOffset, 0, true));
m_bmpReaders[index]->setData(m_data.get());
m_bmpReaders[index]->setBuffer(&m_frameBufferCache[index]);
}
m_frameSize = dirEntry.m_size;
bool result = m_bmpReaders[index]->decodeBMP(false);
m_frameSize = IntSize();
return result;
}
if (!m_pngDecoders[index]) {
m_pngDecoders[index] = adoptPtr(
new PNGImageDecoder(m_premultiplyAlpha ? ImageSource::AlphaPremultiplied : ImageSource::AlphaNotPremultiplied,
m_ignoreGammaAndColorProfile ? ImageSource::GammaAndColorProfileIgnored : ImageSource::GammaAndColorProfileApplied, m_maxDecodedBytes));
setDataForPNGDecoderAtIndex(index);
}
if (m_pngDecoders[index]->isSizeAvailable() && (m_pngDecoders[index]->size() != dirEntry.m_size))
return setFailed();
m_frameBufferCache[index] = *m_pngDecoders[index]->frameBufferAtIndex(0);
m_frameBufferCache[index].setPremultiplyAlpha(m_premultiplyAlpha);
m_frameBufferCache[index].setRequiredPreviousFrameIndex(kNotFound);
return !m_pngDecoders[index]->failed() || setFailed();
}
bool ICOImageDecoder::processDirectory()
{
ASSERT(!m_decodedOffset);
if (m_data->size() < sizeOfDirectory)
return false;
const uint16_t fileType = readUint16(2);
const uint16_t idCount = readUint16(4);
m_decodedOffset = sizeOfDirectory;
if (((fileType != ICON) && (fileType != CURSOR)) || (!idCount))
return setFailed();
m_fileType = static_cast<FileType>(fileType);
m_dirEntries.resize(idCount);
m_bmpReaders.resize(idCount);
m_pngDecoders.resize(idCount);
return true;
}
bool ICOImageDecoder::processDirectoryEntries()
{
ASSERT(m_decodedOffset == sizeOfDirectory);
if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset) < (m_dirEntries.size() * sizeOfDirEntry)))
return false;
for (IconDirectoryEntries::iterator i(m_dirEntries.begin()); i != m_dirEntries.end(); ++i)
*i = readDirectoryEntry();
for (IconDirectoryEntries::iterator i(m_dirEntries.begin()); i != m_dirEntries.end(); ++i) {
if (i->m_imageOffset < m_decodedOffset)
return setFailed();
}
std::sort(m_dirEntries.begin(), m_dirEntries.end(), compareEntries);
const IconDirectoryEntry& dirEntry = m_dirEntries.first();
return setSize(dirEntry.m_size.width(), dirEntry.m_size.height());
}
ICOImageDecoder::IconDirectoryEntry ICOImageDecoder::readDirectoryEntry()
{
int width = static_cast<uint8_t>(m_data->data()[m_decodedOffset]);
if (!width)
width = 256;
int height = static_cast<uint8_t>(m_data->data()[m_decodedOffset + 1]);
if (!height)
height = 256;
IconDirectoryEntry entry;
entry.m_size = IntSize(width, height);
if (m_fileType == CURSOR) {
entry.m_bitCount = 0;
entry.m_hotSpot = IntPoint(readUint16(4), readUint16(6));
} else {
entry.m_bitCount = readUint16(6);
entry.m_hotSpot = IntPoint();
}
entry.m_imageOffset = readUint32(12);
if (!entry.m_bitCount) {
int colorCount = static_cast<uint8_t>(m_data->data()[m_decodedOffset + 2]);
if (!colorCount)
colorCount = 256;
for (--colorCount; colorCount; colorCount >>= 1)
++entry.m_bitCount;
}
m_decodedOffset += sizeOfDirEntry;
return entry;
}
ICOImageDecoder::ImageType ICOImageDecoder::imageTypeAtIndex(size_t index)
{
ASSERT_WITH_SECURITY_IMPLICATION(index < m_dirEntries.size());
const uint32_t imageOffset = m_dirEntries[index].m_imageOffset;
if ((imageOffset > m_data->size()) || ((m_data->size() - imageOffset) < 4))
return Unknown;
return strncmp(&m_data->data()[imageOffset], "\x89PNG", 4) ? BMP : PNG;
}
}