#pragma once
#include "ImageFrame.h"
#include "IntRect.h"
#include "IntSize.h"
#include "SharedBuffer.h"
#include <wtf/Assertions.h>
#include <wtf/Optional.h>
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class ImageDecoder : public ThreadSafeRefCounted<ImageDecoder> {
WTF_MAKE_NONCOPYABLE(ImageDecoder); WTF_MAKE_FAST_ALLOCATED;
public:
ImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption)
: m_premultiplyAlpha(alphaOption == AlphaOption::Premultiplied)
, m_ignoreGammaAndColorProfile(gammaAndColorProfileOption == GammaAndColorProfileOption::Ignored)
{
}
virtual ~ImageDecoder()
{
}
static RefPtr<ImageDecoder> create(SharedBuffer& data, AlphaOption, GammaAndColorProfileOption);
virtual String filenameExtension() const = 0;
bool premultiplyAlpha() const { return m_premultiplyAlpha; }
bool isAllDataReceived() const
{
ASSERT(!m_decodingSizeFromSetData);
return m_encodedDataStatus == EncodedDataStatus::Complete;
}
virtual void setData(SharedBuffer& data, bool allDataReceived)
{
if (m_encodedDataStatus == EncodedDataStatus::Error)
return;
m_data = &data;
if (m_encodedDataStatus == EncodedDataStatus::TypeAvailable) {
m_decodingSizeFromSetData = true;
tryDecodeSize(allDataReceived);
m_decodingSizeFromSetData = false;
}
if (m_encodedDataStatus == EncodedDataStatus::Error)
return;
if (allDataReceived) {
ASSERT(m_encodedDataStatus == EncodedDataStatus::SizeAvailable);
m_encodedDataStatus = EncodedDataStatus::Complete;
}
}
EncodedDataStatus encodedDataStatus() const { return m_encodedDataStatus; }
bool isSizeAvailable() { return m_encodedDataStatus >= EncodedDataStatus::SizeAvailable; }
virtual IntSize size() { return isSizeAvailable() ? m_size : IntSize(); }
IntSize scaledSize()
{
return m_scaled ? IntSize(m_scaledColumns.size(), m_scaledRows.size()) : size();
}
virtual IntSize frameSizeAtIndex(size_t, SubsamplingLevel)
{
return size();
}
virtual bool setSize(const IntSize& size)
{
if (ImageBackingStore::isOverSize(size))
return setFailed();
m_size = size;
m_encodedDataStatus = EncodedDataStatus::SizeAvailable;
return true;
}
virtual size_t frameCount() const { return 1; }
virtual RepetitionCount repetitionCount() const { return RepetitionCountNone; }
virtual ImageFrame* frameBufferAtIndex(size_t) = 0;
bool frameIsCompleteAtIndex(size_t);
bool frameHasAlphaAtIndex(size_t) const;
unsigned frameBytesAtIndex(size_t) const;
float frameDurationAtIndex(size_t);
NativeImagePtr createFrameImageAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default, const DecodingOptions& = DecodingMode::Synchronous);
void setIgnoreGammaAndColorProfile(bool flag) { m_ignoreGammaAndColorProfile = flag; }
bool ignoresGammaAndColorProfile() const { return m_ignoreGammaAndColorProfile; }
ImageOrientation frameOrientationAtIndex(size_t) const { return m_orientation; }
bool frameAllowSubsamplingAtIndex(size_t) const { return false; }
enum { ICCColorProfileHeaderLength = 128 };
static bool rgbColorProfile(const char* profileData, unsigned profileLength)
{
ASSERT_UNUSED(profileLength, profileLength >= ICCColorProfileHeaderLength);
return !memcmp(&profileData[16], "RGB ", 4);
}
static size_t bytesDecodedToDetermineProperties() { return 0; }
static SubsamplingLevel subsamplingLevelForScale(float, SubsamplingLevel) { return SubsamplingLevel::Default; }
static bool inputDeviceColorProfile(const char* profileData, unsigned profileLength)
{
ASSERT_UNUSED(profileLength, profileLength >= ICCColorProfileHeaderLength);
return !memcmp(&profileData[12], "mntr", 4) || !memcmp(&profileData[12], "scnr", 4);
}
virtual bool setFailed()
{
m_encodedDataStatus = EncodedDataStatus::Error;
return false;
}
bool failed() const { return m_encodedDataStatus == EncodedDataStatus::Error; }
virtual void clearFrameBufferCache(size_t) { }
virtual std::optional<IntPoint> hotSpot() const { return std::nullopt; }
protected:
void prepareScaleDataIfNecessary();
int upperBoundScaledX(int origX, int searchStart = 0);
int lowerBoundScaledX(int origX, int searchStart = 0);
int upperBoundScaledY(int origY, int searchStart = 0);
int lowerBoundScaledY(int origY, int searchStart = 0);
int scaledY(int origY, int searchStart = 0);
RefPtr<SharedBuffer> m_data; Vector<ImageFrame, 1> m_frameBufferCache;
bool m_scaled { false };
Vector<int> m_scaledColumns;
Vector<int> m_scaledRows;
bool m_premultiplyAlpha;
bool m_ignoreGammaAndColorProfile;
ImageOrientation m_orientation;
private:
virtual void tryDecodeSize(bool) = 0;
IntSize m_size;
EncodedDataStatus m_encodedDataStatus { EncodedDataStatus::TypeAvailable };
bool m_decodingSizeFromSetData { false };
#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
static const int m_maxNumPixels { 1024 * 1024 };
#else
static const int m_maxNumPixels { -1 };
#endif
};
}