#ifndef ImageDecoder_h
#define ImageDecoder_h
#include "SkColorPriv.h"
#include "platform/PlatformExport.h"
#include "platform/PlatformScreen.h"
#include "platform/SharedBuffer.h"
#include "platform/graphics/ImageSource.h"
#include "platform/image-decoders/ImageFrame.h"
#include "public/platform/Platform.h"
#include "wtf/Assertions.h"
#include "wtf/RefPtr.h"
#include "wtf/Threading.h"
#include "wtf/text/WTFString.h"
#include "wtf/Vector.h"
#if USE(QCMSLIB)
#include "qcms.h"
#if OS(MACOSX)
#include <ApplicationServices/ApplicationServices.h>
#include "platform/graphics/cg/GraphicsContextCG.h"
#include "wtf/RetainPtr.h"
#endif
#endif
namespace WebCore {
class PLATFORM_EXPORT ImageDecoder {
WTF_MAKE_NONCOPYABLE(ImageDecoder); WTF_MAKE_FAST_ALLOCATED;
public:
static const size_t noDecodedImageByteLimit = blink::Platform::noDecodedImageByteLimit;
ImageDecoder(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption, size_t maxDecodedBytes)
: m_premultiplyAlpha(alphaOption == ImageSource::AlphaPremultiplied)
, m_ignoreGammaAndColorProfile(gammaAndColorProfileOption == ImageSource::GammaAndColorProfileIgnored)
, m_maxDecodedBytes(maxDecodedBytes)
, m_sizeAvailable(false)
, m_isAllDataReceived(false)
, m_failed(false) { }
virtual ~ImageDecoder() { }
static PassOwnPtr<ImageDecoder> create(const SharedBuffer& data, ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption);
static PassOwnPtr<ImageDecoder> create(const SharedBuffer& data, ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption, size_t maxDecodedSize);
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() const { return m_size; }
virtual IntSize decodedSize() const { return size(); }
virtual IntSize frameSizeAtIndex(size_t) const
{
return size();
}
virtual bool setSize(unsigned width, unsigned height)
{
if (sizeCalculationMayOverflow(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;
virtual bool frameHasAlphaAtIndex(size_t) const;
virtual bool frameIsCompleteAtIndex(size_t) const;
virtual float frameDurationAtIndex(size_t) const { return 0; }
virtual unsigned frameBytesAtIndex(size_t) const;
void setIgnoreGammaAndColorProfile(bool flag) { m_ignoreGammaAndColorProfile = flag; }
bool ignoresGammaAndColorProfile() const { return m_ignoreGammaAndColorProfile; }
ImageOrientation orientation() const { return m_orientation; }
enum { iccColorProfileHeaderLength = 128 };
static bool rgbColorProfile(const char* profileData, unsigned profileLength)
{
ASSERT_UNUSED(profileLength, profileLength >= iccColorProfileHeaderLength);
return !memcmp(&profileData[16], "RGB ", 4);
}
static bool inputDeviceColorProfile(const char* profileData, unsigned profileLength)
{
ASSERT_UNUSED(profileLength, profileLength >= iccColorProfileHeaderLength);
return !memcmp(&profileData[12], "mntr", 4) || !memcmp(&profileData[12], "scnr", 4);
}
#if USE(QCMSLIB)
class OutputDeviceProfile {
public:
OutputDeviceProfile()
: m_outputDeviceProfile(0)
{
#if OS(MACOSX)
RetainPtr<CGColorSpaceRef> monitorColorSpace(AdoptCF, CGDisplayCopyColorSpace(CGMainDisplayID()));
CFDataRef iccProfile(CGColorSpaceCopyICCProfile(monitorColorSpace.get()));
if (iccProfile) {
size_t length = CFDataGetLength(iccProfile);
const unsigned char* systemProfile = CFDataGetBytePtr(iccProfile);
m_outputDeviceProfile = qcms_profile_from_memory(systemProfile, length);
}
#else
ColorProfile profile;
screenColorProfile(profile);
if (!profile.isEmpty())
m_outputDeviceProfile = qcms_profile_from_memory(profile.data(), profile.size());
#endif
if (m_outputDeviceProfile && qcms_profile_is_bogus(m_outputDeviceProfile)) {
qcms_profile_release(m_outputDeviceProfile);
m_outputDeviceProfile = 0;
}
if (!m_outputDeviceProfile)
m_outputDeviceProfile = qcms_profile_sRGB();
if (m_outputDeviceProfile)
qcms_profile_precache_output_transform(m_outputDeviceProfile);
}
qcms_profile* profile() const { return m_outputDeviceProfile; }
private:
qcms_profile* m_outputDeviceProfile;
};
static qcms_profile* qcmsOutputDeviceProfile()
{
AtomicallyInitializedStatic(OutputDeviceProfile*, outputDeviceProfile = new OutputDeviceProfile());
return outputDeviceProfile->profile();
}
#endif
virtual bool setFailed()
{
m_failed = true;
return false;
}
bool failed() const { return m_failed; }
virtual size_t clearCacheExceptFrame(size_t);
virtual bool hotSpot(IntPoint&) const { return false; }
virtual void setMemoryAllocator(SkBitmap::Allocator* allocator)
{
if (m_frameBufferCache.isEmpty()) {
m_frameBufferCache.resize(1);
m_frameBufferCache[0].setRequiredPreviousFrameIndex(
findRequiredPreviousFrame(0, false));
}
m_frameBufferCache[0].setMemoryAllocator(allocator);
}
protected:
size_t findRequiredPreviousFrame(size_t frameIndex, bool frameRectIsOpaque);
virtual void clearFrameBuffer(size_t frameIndex);
RefPtr<SharedBuffer> m_data;
Vector<ImageFrame, 1> m_frameBufferCache;
bool m_premultiplyAlpha;
bool m_ignoreGammaAndColorProfile;
ImageOrientation m_orientation;
size_t m_maxDecodedBytes;
private:
static bool sizeCalculationMayOverflow(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;
bool m_isAllDataReceived;
bool m_failed;
};
}
#endif