#include "config.h"
#include "NativeImage.h"
#if USE(CG)
#include "Color.h"
#include "FloatRect.h"
#include "GeometryUtilities.h"
#include "GraphicsContextCG.h"
#include "IntSize.h"
#include "SubimageCacheWithTimer.h"
#include <pal/spi/cg/CoreGraphicsSPI.h>
namespace WebCore {
IntSize nativeImageSize(const NativeImagePtr& image)
{
return image ? IntSize(CGImageGetWidth(image.get()), CGImageGetHeight(image.get())) : IntSize();
}
bool nativeImageHasAlpha(const NativeImagePtr& image)
{
CGImageAlphaInfo info = CGImageGetAlphaInfo(image.get());
return (info >= kCGImageAlphaPremultipliedLast) && (info <= kCGImageAlphaFirst);
}
Color nativeImageSinglePixelSolidColor(const NativeImagePtr& image)
{
if (!image || nativeImageSize(image) != IntSize(1, 1))
return Color();
unsigned char pixel[4]; auto bitmapContext = adoptCF(CGBitmapContextCreate(pixel, 1, 1, 8, sizeof(pixel), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big));
if (!bitmapContext)
return Color();
CGContextSetBlendMode(bitmapContext.get(), kCGBlendModeCopy);
CGContextDrawImage(bitmapContext.get(), CGRectMake(0, 0, 1, 1), image.get());
if (!pixel[3])
return Color(0, 0, 0, 0);
return Color(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]);
}
void drawNativeImage(const NativeImagePtr& image, GraphicsContext& context, const FloatRect& destRect, const FloatRect& srcRect, const IntSize& srcSize, CompositeOperator op, BlendMode mode, const ImageOrientation& orientation)
{
IntSize subsampledImageSize = nativeImageSize(image);
FloatRect adjustedSrcRect = srcRect;
if (subsampledImageSize != srcSize)
adjustedSrcRect = mapRect(srcRect, FloatRect({ }, srcSize), FloatRect({ }, subsampledImageSize));
context.drawNativeImage(image, subsampledImageSize, destRect, adjustedSrcRect, op, mode, orientation);
}
void clearNativeImageSubimages(const NativeImagePtr& image)
{
#if CACHE_SUBIMAGES
if (image)
SubimageCacheWithTimer::clearImage(image.get());
#endif
}
}
#endif // USE(CG)