#ifndef WebGLImageConversion_h
#define WebGLImageConversion_h
#include "platform/PlatformExport.h"
#include "platform/graphics/Image.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "wtf/RefPtr.h"
namespace WebCore {
class Image;
class IntSize;
class PLATFORM_EXPORT WebGLImageConversion {
public:
enum DataFormat {
DataFormatRGBA8 = 0,
DataFormatRGBA16F,
DataFormatRGBA32F,
DataFormatRGB8,
DataFormatRGB16F,
DataFormatRGB32F,
DataFormatBGR8,
DataFormatBGRA8,
DataFormatARGB8,
DataFormatABGR8,
DataFormatRGBA5551,
DataFormatRGBA4444,
DataFormatRGB565,
DataFormatR8,
DataFormatR16F,
DataFormatR32F,
DataFormatRA8,
DataFormatRA16F,
DataFormatRA32F,
DataFormatAR8,
DataFormatA8,
DataFormatA16F,
DataFormatA32F,
DataFormatNumFormats
};
enum ChannelBits {
ChannelRed = 1,
ChannelGreen = 2,
ChannelBlue = 4,
ChannelAlpha = 8,
ChannelDepth = 16,
ChannelStencil = 32,
ChannelRGB = ChannelRed | ChannelGreen | ChannelBlue,
ChannelRGBA = ChannelRGB | ChannelAlpha,
};
enum AlphaOp {
AlphaDoNothing = 0,
AlphaDoPremultiply = 1,
AlphaDoUnmultiply = 2
};
enum ImageHtmlDomSource {
HtmlDomImage = 0,
HtmlDomCanvas = 1,
HtmlDomVideo = 2,
HtmlDomNone = 3
};
class PLATFORM_EXPORT ImageExtractor {
public:
ImageExtractor(Image*, ImageHtmlDomSource, bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
~ImageExtractor();
bool extractSucceeded() { return m_extractSucceeded; }
const void* imagePixelData() { return m_imagePixelData; }
unsigned imageWidth() { return m_imageWidth; }
unsigned imageHeight() { return m_imageHeight; }
DataFormat imageSourceFormat() { return m_imageSourceFormat; }
AlphaOp imageAlphaOp() { return m_alphaOp; }
unsigned imageSourceUnpackAlignment() { return m_imageSourceUnpackAlignment; }
ImageHtmlDomSource imageHtmlDomSource() { return m_imageHtmlDomSource; }
private:
bool extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile);
RefPtr<NativeImageSkia> m_nativeImage;
RefPtr<NativeImageSkia> m_skiaImage;
Image* m_image;
ImageHtmlDomSource m_imageHtmlDomSource;
bool m_extractSucceeded;
const void* m_imagePixelData;
unsigned m_imageWidth;
unsigned m_imageHeight;
DataFormat m_imageSourceFormat;
AlphaOp m_alphaOp;
unsigned m_imageSourceUnpackAlignment;
};
static bool computeFormatAndTypeParameters(GLenum format, GLenum type, unsigned* componentsPerPixel, unsigned* bytesPerComponent);
static GLenum computeImageSizeInBytes(GLenum format, GLenum type, GLsizei width, GLsizei height, GLint alignment, unsigned* imageSizeInBytes, unsigned* paddingInBytes);
static ALWAYS_INLINE bool srcFormatComeFromDOMElementOrImageData(DataFormat SrcFormat)
{
return SrcFormat == DataFormatBGRA8 || SrcFormat == DataFormatRGBA8;
}
static unsigned getClearBitsByFormat(GLenum);
static unsigned getChannelBitsByFormat(GLenum);
static bool packImageData(Image*, const void* pixels, GLenum format, GLenum type, bool flipY, AlphaOp, DataFormat sourceFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, Vector<uint8_t>& data);
static bool extractImageData(const uint8_t*, const IntSize&, GLenum format, GLenum type, bool flipY, bool premultiplyAlpha, Vector<uint8_t>& data);
static bool extractTextureData(unsigned width, unsigned height, GLenum format, GLenum type, unsigned unpackAlignment, bool flipY, bool premultiplyAlpha, const void* pixels, Vector<uint8_t>& data);
private:
static bool packPixels(const uint8_t* sourceData, DataFormat sourceDataFormat, unsigned width, unsigned height, unsigned sourceUnpackAlignment, unsigned destinationFormat, unsigned destinationType, AlphaOp, void* destinationData, bool flipY);
};
}
#endif