[chromium] Fix crash in DragImageTest caused by r122996
[WebKit-https.git] / Source / WebCore / platform / chromium / DragImageChromiumSkia.cpp
1 /*
2  * Copyright (c) 2008, 2009, Google 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 are
6  * met:
7  * 
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  * 
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "DragImage.h"
33
34 #include "Image.h"
35 #include "NativeImageSkia.h"
36 #include "NotImplemented.h"
37 #include "SkBitmap.h"
38
39 #include "skia/ext/image_operations.h"
40
41 #include <wtf/RefPtr.h>
42
43 namespace WebCore {
44
45 IntSize dragImageSize(DragImageRef image)
46 {
47     if (!image)
48         return IntSize();
49
50     return IntSize(image->bitmap->width(), image->bitmap->height());
51 }
52
53 void deleteDragImage(DragImageRef image)
54 {
55     if (image)
56         delete image->bitmap;
57     delete image;
58 }
59
60 DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
61 {
62     if (!image)
63         return 0;
64
65     int imageWidth = scale.width() * image->bitmap->width();
66     int imageHeight = scale.height() * image->bitmap->height();
67     SkBitmap* scaledImage = new SkBitmap(
68         skia::ImageOperations::Resize(*image->bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
69                                       imageWidth, imageHeight));
70     delete image->bitmap;
71     image->bitmap = scaledImage;
72     return image;
73 }
74
75 DragImageRef dissolveDragImageToFraction(DragImageRef image, float fraction)
76 {
77     if (!image)
78         return 0;
79
80     image->bitmap->setIsOpaque(false);
81     image->bitmap->lockPixels();
82
83     for (int row = 0; row < image->bitmap->height(); ++row) {
84         for (int column = 0; column < image->bitmap->width(); ++column) {
85             uint32_t* pixel = image->bitmap->getAddr32(column, row);
86             *pixel = SkPreMultiplyARGB(SkColorGetA(*pixel) * fraction,
87                                        SkColorGetR(*pixel),
88                                        SkColorGetG(*pixel),
89                                        SkColorGetB(*pixel));
90         }
91     }
92
93     image->bitmap->unlockPixels();
94
95     return image;
96 }
97
98 DragImageRef createDragImageFromImage(Image* image, RespectImageOrientationEnum)
99 {
100     if (!image)
101         return 0;
102
103     NativeImageSkia* bitmap = image->nativeImageForCurrentFrame();
104     if (!bitmap)
105         return 0;
106
107     SkBitmap* dragImage = new SkBitmap();
108     bitmap->bitmap().copyTo(dragImage, SkBitmap::kARGB_8888_Config);
109     DragImageChromium* dragImageChromium = new DragImageChromium;
110     dragImageChromium->bitmap = dragImage;
111     dragImageChromium->resolutionScale = bitmap->resolutionScale();
112     return dragImageChromium;
113 }
114
115 DragImageRef createDragImageIconForCachedImage(CachedImage*)
116 {
117     notImplemented();
118     return 0;
119 }
120
121 } // namespace WebCore