1/*
2 * Copyright (C) 2007, 2008 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 COMPUTER, 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 COMPUTER, 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 "DragImage.h"
28
29#include "BitmapInfo.h"
30#include "CachedImage.h"
31#include "GraphicsContextCG.h"
32#include "Image.h"
33#include "RetainPtr.h"
34
35#include <CoreGraphics/CoreGraphics.h>
36
37#include <windows.h>
38
39namespace WebCore {
40
41void deallocContext(CGContextRef target)
42{
43    CGContextRelease(target);
44}
45
46HBITMAP allocImage(HDC dc, IntSize size, CGContextRef *targetRef)
47{
48    BitmapInfo bmpInfo = BitmapInfo::create(size);
49
50    LPVOID bits;
51    HBITMAP hbmp = CreateDIBSection(dc, &bmpInfo, DIB_RGB_COLORS, &bits, 0, 0);
52
53    if (!targetRef)
54        return hbmp;
55
56    CGContextRef bitmapContext = CGBitmapContextCreate(bits, bmpInfo.bmiHeader.biWidth, bmpInfo.bmiHeader.biHeight, 8,
57                                                       bmpInfo.bmiHeader.biWidth * 4, deviceRGBColorSpaceRef(),
58                                                       kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
59    if (!bitmapContext) {
60        DeleteObject(hbmp);
61        return 0;
62    }
63
64    *targetRef = bitmapContext;
65    return hbmp;
66}
67
68static CGContextRef createCgContextFromBitmap(HBITMAP bitmap)
69{
70    BITMAP info;
71    GetObject(bitmap, sizeof(info), &info);
72    ASSERT(info.bmBitsPixel == 32);
73
74    CGContextRef bitmapContext = CGBitmapContextCreate(info.bmBits, info.bmWidth, info.bmHeight, 8,
75                                                       info.bmWidthBytes, deviceRGBColorSpaceRef(), kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
76    return bitmapContext;
77}
78
79DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
80{
81    // FIXME: due to the way drag images are done on windows we need
82    // to preprocess the alpha channel <rdar://problem/5015946>
83
84    if (!image)
85        return 0;
86    CGContextRef targetContext;
87    CGContextRef srcContext;
88    CGImageRef srcImage;
89    IntSize srcSize = dragImageSize(image);
90    IntSize dstSize(static_cast<int>(srcSize.width() * scale.width()), static_cast<int>(srcSize.height() * scale.height()));
91    HBITMAP hbmp = 0;
92    HDC dc = GetDC(0);
93    HDC dstDC = CreateCompatibleDC(dc);
94    if (!dstDC)
95        goto exit;
96
97    hbmp = allocImage(dstDC, dstSize, &targetContext);
98    if (!hbmp)
99        goto exit;
100
101    srcContext = createCgContextFromBitmap(image);
102    srcImage = CGBitmapContextCreateImage(srcContext);
103    CGRect rect;
104    rect.origin.x = 0;
105    rect.origin.y = 0;
106    rect.size = dstSize;
107    CGContextDrawImage(targetContext, rect, srcImage);
108    CGImageRelease(srcImage);
109    CGContextRelease(srcContext);
110    CGContextRelease(targetContext);
111    ::DeleteObject(image);
112    image = 0;
113
114exit:
115    if (!hbmp)
116        hbmp = image;
117    if (dstDC)
118        DeleteDC(dstDC);
119    ReleaseDC(0, dc);
120    return hbmp;
121}
122
123DragImageRef createDragImageFromImage(Image* img)
124{
125    HBITMAP hbmp = 0;
126    HDC dc = GetDC(0);
127    HDC workingDC = CreateCompatibleDC(dc);
128    CGContextRef drawContext = 0;
129    if (!workingDC)
130        goto exit;
131
132    hbmp = allocImage(workingDC, img->size(), &drawContext);
133
134    if (!hbmp)
135        goto exit;
136
137    if (!drawContext) {
138        ::DeleteObject(hbmp);
139        hbmp = 0;
140    }
141
142    CGImageRef srcImage = img->getCGImageRef();
143    CGRect rect;
144    rect.size = img->size();
145    rect.origin.x = 0;
146    rect.origin.y = -rect.size.height;
147    static const CGFloat white [] = {1.0, 1.0, 1.0, 1.0};
148    CGContextScaleCTM(drawContext, 1, -1);
149    CGContextSetFillColor(drawContext, white);
150    CGContextFillRect(drawContext, rect);
151    CGContextSetBlendMode(drawContext, kCGBlendModeNormal);
152    CGContextDrawImage(drawContext, rect, srcImage);
153    CGContextRelease(drawContext);
154
155exit:
156    if (workingDC)
157        DeleteDC(workingDC);
158    ReleaseDC(0, dc);
159    return hbmp;
160}
161
162}
163