#include "config.h"
#include "Image.h"
#include "BitmapImage.h"
#include "BitmapInfo.h"
#include "GraphicsContextCG.h"
#include <ApplicationServices/ApplicationServices.h>
#include <windows.h>
#include <wtf/RetainPtr.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
PassRefPtr<BitmapImage> BitmapImage::create(HBITMAP hBitmap)
{
DIBSECTION dibSection;
if (!GetObject(hBitmap, sizeof(DIBSECTION), &dibSection))
return 0;
ASSERT(dibSection.dsBm.bmBitsPixel == 32);
if (dibSection.dsBm.bmBitsPixel != 32)
return 0;
ASSERT(dibSection.dsBm.bmBits);
if (!dibSection.dsBm.bmBits)
return 0;
RetainPtr<CGContextRef> bitmapContext = adoptCF(CGBitmapContextCreate(dibSection.dsBm.bmBits, dibSection.dsBm.bmWidth, dibSection.dsBm.bmHeight, 8,
dibSection.dsBm.bmWidthBytes, deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext.get());
return adoptRef(new BitmapImage(cgImage));
}
bool BitmapImage::getHBITMAPOfSize(HBITMAP bmp, const IntSize* size)
{
ASSERT(bmp);
BITMAP bmpInfo;
GetObject(bmp, sizeof(BITMAP), &bmpInfo);
ASSERT(bmpInfo.bmBitsPixel == 32);
int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight;
CGContextRef cgContext = CGBitmapContextCreate(bmpInfo.bmBits, bmpInfo.bmWidth, bmpInfo.bmHeight,
8, bmpInfo.bmWidthBytes, deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
GraphicsContext gc(cgContext);
FloatSize imageSize = BitmapImage::size();
if (size)
drawFrameMatchingSourceSize(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), *size, ColorSpaceDeviceRGB, CompositeCopy);
else
draw(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), ColorSpaceDeviceRGB, CompositeCopy, BlendModeNormal, ImageOrientationDescription());
CGContextRelease(cgContext);
return true;
}
void BitmapImage::drawFrameMatchingSourceSize(GraphicsContext* ctxt, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator compositeOp)
{
size_t frames = frameCount();
for (size_t i = 0; i < frames; ++i) {
CGImageRef image = frameAtIndex(i);
if (image && CGImageGetHeight(image) == static_cast<size_t>(srcSize.height()) && CGImageGetWidth(image) == static_cast<size_t>(srcSize.width())) {
size_t currentFrame = m_currentFrame;
m_currentFrame = i;
draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, srcSize.width(), srcSize.height()), styleColorSpace, compositeOp, BlendModeNormal, ImageOrientationDescription());
m_currentFrame = currentFrame;
return;
}
}
FloatSize imageSize = BitmapImage::size();
draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), styleColorSpace, compositeOp, BlendModeNormal, ImageOrientationDescription());
}
}