#pragma once
#include "IntRect.h"
#include "ImageSource.h"
#include "PlatformScreen.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 {
using ColorProfile = Vector<char>;
class ImageFrame {
public:
enum FrameStatus { FrameEmpty, FramePartial, FrameComplete };
enum FrameDisposalMethod {
DisposeNotSpecified, DisposeKeep, DisposeOverwriteBgcolor, DisposeOverwritePrevious };
typedef unsigned PixelData;
ImageFrame();
ImageFrame(const ImageFrame& other) { operator=(other); }
ImageFrame& operator=(const ImageFrame& other);
void clearPixelData();
void zeroFillPixelData();
void zeroFillFrameRect(const IntRect&);
bool copyBitmapData(const ImageFrame&);
void copyRowNTimes(int startX, int endX, int startY, int endY)
{
ASSERT(startX < width());
ASSERT(endX <= width());
ASSERT(startY < height());
ASSERT(endY <= height());
const int rowBytes = (endX - startX) * sizeof(PixelData);
const PixelData* const startAddr = getAddr(startX, startY);
for (int destY = startY + 1; destY < endY; ++destY)
memcpy(getAddr(startX, destY), startAddr, rowBytes);
}
bool setSize(int newWidth, int newHeight);
NativeImagePtr asNewNativeImage() const;
bool hasAlpha() const;
const IntRect& originalFrameRect() const { return m_originalFrameRect; }
FrameStatus status() const { return m_status; }
unsigned duration() const { return m_duration; }
FrameDisposalMethod disposalMethod() const { return m_disposalMethod; }
bool premultiplyAlpha() const { return m_premultiplyAlpha; }
void setHasAlpha(bool alpha);
void setColorProfile(const ColorProfile&);
void setOriginalFrameRect(const IntRect& r) { m_originalFrameRect = r; }
void setStatus(FrameStatus status);
void setDuration(unsigned duration) { m_duration = duration; }
void setDisposalMethod(FrameDisposalMethod method) { m_disposalMethod = method; }
void setPremultiplyAlpha(bool premultiplyAlpha) { m_premultiplyAlpha = premultiplyAlpha; }
inline void setRGBA(int x, int y, unsigned r, unsigned g, unsigned b, unsigned a)
{
setRGBA(getAddr(x, y), r, g, b, a);
}
inline PixelData* getAddr(int x, int y)
{
return m_bytes + (y * width()) + x;
}
inline bool hasPixelData() const
{
return m_bytes;
}
static const unsigned fixPointShift = 24;
static const unsigned fixPointMult = static_cast<unsigned>(1.0 / 255.0 * (1 << fixPointShift)) + 1;
static unsigned fixPointUnsignedMultiply(unsigned fixed, unsigned v)
{
return (fixed * v) >> fixPointShift;
}
inline void setRGBA(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
{
if (m_premultiplyAlpha && a < 255) {
if (!a) {
*dest = 0;
return;
}
unsigned alphaMult = a * fixPointMult;
r = fixPointUnsignedMultiply(r, alphaMult);
g = fixPointUnsignedMultiply(g, alphaMult);
b = fixPointUnsignedMultiply(b, alphaMult);
}
*dest = (a << 24 | r << 16 | g << 8 | b);
}
#if ENABLE(APNG)
static inline unsigned divide255(unsigned a)
{
return (a + (a >> 8) + 1) >> 8;
}
inline void overRGBA(PixelData* dest, unsigned r, unsigned g, unsigned b, unsigned a)
{
if (!a)
return;
if (a < 255) {
unsigned aDest = ((*dest) >> 24) & 255;
if (aDest) {
unsigned rDest = ((*dest) >> 16) & 255;
unsigned gDest = ((*dest) >> 8) & 255;
unsigned bDest = (*dest) & 255;
unsigned aAux = 255 - a;
if (!m_premultiplyAlpha) {
rDest = divide255(rDest * aDest);
gDest = divide255(gDest * aDest);
bDest = divide255(bDest * aDest);
}
r = divide255(r * a + rDest * aAux);
g = divide255(g * a + gDest * aAux);
b = divide255(b * a + bDest * aAux);
a += divide255(aDest * aAux);
if (!m_premultiplyAlpha) {
r = (r * 255 + a - 1) / a;
g = (g * 255 + a - 1) / a;
b = (b * 255 + a - 1) / a;
}
} else if (m_premultiplyAlpha) {
r = divide255(r * a);
g = divide255(g * a);
b = divide255(b * a);
}
}
*dest = (a << 24 | r << 16 | g << 8 | b);
}
#endif
private:
int width() const
{
return m_size.width();
}
int height() const
{
return m_size.height();
}
Vector<PixelData> m_backingStore;
PixelData* m_bytes; IntSize m_size;
ColorProfile m_colorProfile;
bool m_hasAlpha;
IntRect m_originalFrameRect; FrameStatus m_status;
unsigned m_duration;
FrameDisposalMethod m_disposalMethod;
bool m_premultiplyAlpha;
};
class ImageDecoder {
WTF_MAKE_NONCOPYABLE(ImageDecoder); WTF_MAKE_FAST_ALLOCATED;
public:
ImageDecoder(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
: m_premultiplyAlpha(alphaOption == ImageSource::AlphaPremultiplied)
, m_ignoreGammaAndColorProfile(gammaAndColorProfileOption == ImageSource::GammaAndColorProfileIgnored)
{
}
virtual ~ImageDecoder()
{
}
static std::unique_ptr<ImageDecoder> create(const SharedBuffer& data, ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption);
virtual String filenameExtension() const = 0;
bool isAllDataReceived() const { return m_isAllDataReceived; }
virtual void setData(SharedBuffer& data, bool allDataReceived)
{
if (m_failed)
return;
m_data = &data;
m_isAllDataReceived = allDataReceived;
}
virtual bool isSizeAvailable()
{
return !m_failed && m_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(unsigned width, unsigned height)
{
if (isOverSize(width, height))
return setFailed();
m_size = IntSize(width, height);
m_sizeAvailable = true;
return true;
}
virtual size_t frameCount() { return 1; }
virtual int repetitionCount() const { return cAnimationNone; }
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);
void setIgnoreGammaAndColorProfile(bool flag) { m_ignoreGammaAndColorProfile = flag; }
bool ignoresGammaAndColorProfile() const { return m_ignoreGammaAndColorProfile; }
ImageOrientation orientationAtIndex(size_t) const { return m_orientation; }
bool allowSubsamplingOfFrameAtIndex(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 0; }
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_failed = true;
return false;
}
bool failed() const { return m_failed; }
virtual void clearFrameBufferCache(size_t) { }
virtual Optional<IntPoint> hotSpot() const { return 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;
ColorProfile m_colorProfile;
bool m_scaled { false };
Vector<int> m_scaledColumns;
Vector<int> m_scaledRows;
bool m_premultiplyAlpha;
bool m_ignoreGammaAndColorProfile;
ImageOrientation m_orientation;
private:
static bool isOverSize(unsigned width, unsigned height)
{
unsigned long long total_size = static_cast<unsigned long long>(width)
* static_cast<unsigned long long>(height);
return total_size > ((1 << 29) - 1);
}
IntSize m_size;
bool m_sizeAvailable { false };
#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
static const int m_maxNumPixels { 1024 * 1024 };
#else
static const int m_maxNumPixels { -1 };
#endif
bool m_isAllDataReceived { false };
bool m_failed { false };
};
}