1/*
2 * Copyright (C) 2008, 2013 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "Image.h"
28#include "BitmapImage.h"
29#include "GraphicsContext.h"
30#include "RefPtrCairo.h"
31#include <cairo.h>
32#include <cairo-win32.h>
33
34#include <windows.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39PassRefPtr<BitmapImage> BitmapImage::create(HBITMAP hBitmap)
40{
41    DIBSECTION dibSection;
42    if (!GetObject(hBitmap, sizeof(DIBSECTION), &dibSection))
43        return 0;
44
45    ASSERT(dibSection.dsBm.bmBitsPixel == 32);
46    if (dibSection.dsBm.bmBitsPixel != 32)
47        return 0;
48
49    ASSERT(dibSection.dsBm.bmBits);
50    if (!dibSection.dsBm.bmBits)
51        return 0;
52
53    RefPtr<cairo_surface_t> surface = adoptRef(cairo_win32_surface_create_with_dib(CAIRO_FORMAT_ARGB32, dibSection.dsBm.bmWidth, dibSection.dsBm.bmHeight));
54
55    return BitmapImage::create(surface.release());
56}
57
58bool BitmapImage::getHBITMAPOfSize(HBITMAP bmp, const IntSize* size)
59{
60    ASSERT(bmp);
61
62    BITMAP bmpInfo;
63    GetObject(bmp, sizeof(BITMAP), &bmpInfo);
64
65    // If this is a 32bpp bitmap, which it always should be, we'll clear it so alpha-wise it will be visible
66    if (bmpInfo.bmBitsPixel == 32 && bmpInfo.bmBits) {
67        int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight;
68        memset(bmpInfo.bmBits, 255, bufferSize);
69    }
70
71    unsigned char* bmpdata = (unsigned char*)bmpInfo.bmBits + bmpInfo.bmWidthBytes*(bmpInfo.bmHeight-1);
72
73    cairo_surface_t* image = cairo_image_surface_create_for_data(bmpdata, CAIRO_FORMAT_ARGB32, bmpInfo.bmWidth, bmpInfo.bmHeight, -bmpInfo.bmWidthBytes);
74
75    cairo_t* targetRef = cairo_create(image);
76    cairo_surface_destroy(image);
77
78    GraphicsContext gc(targetRef);
79
80    FloatSize imageSize = BitmapImage::size();
81    if (size)
82        drawFrameMatchingSourceSize(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), *size, ColorSpaceDeviceRGB, CompositeCopy);
83    else
84        draw(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), ColorSpaceDeviceRGB, CompositeCopy, BlendModeNormal, ImageOrientationDescription());
85
86    // Do cleanup
87    cairo_destroy(targetRef);
88
89    return true;
90}
91
92void BitmapImage::drawFrameMatchingSourceSize(GraphicsContext* ctxt, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator compositeOp)
93{
94    size_t frames = frameCount();
95    for (size_t i = 0; i < frames; ++i) {
96        RefPtr<cairo_surface_t> surface = frameAtIndex(i);
97        if (!surface)
98            continue;
99
100        if (cairo_image_surface_get_height(surface.get()) == static_cast<size_t>(srcSize.height()) && cairo_image_surface_get_width(surface.get()) == static_cast<size_t>(srcSize.width())) {
101            size_t currentFrame = m_currentFrame;
102            m_currentFrame = i;
103            draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, srcSize.width(), srcSize.height()), ColorSpaceDeviceRGB, compositeOp, BlendModeNormal, ImageOrientationDescription());
104            m_currentFrame = currentFrame;
105            return;
106        }
107    }
108
109    // No image of the correct size was found, fallback to drawing the current frame
110    FloatSize imageSize = BitmapImage::size();
111    draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), ColorSpaceDeviceRGB, compositeOp, BlendModeNormal, ImageOrientationDescription());
112}
113
114} // namespace WebCore
115