OLD | NEW |
| (Empty) |
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 "core/platform/DragImage.h" | |
33 | |
34 #include "SkBitmap.h" | |
35 #include "SkCanvas.h" | |
36 #include "SkMatrix.h" | |
37 #include "core/platform/NotImplemented.h" | |
38 #include "core/platform/graphics/BitmapImage.h" | |
39 #include "core/platform/graphics/FloatRect.h" | |
40 #include "core/platform/graphics/Image.h" | |
41 #include "core/platform/graphics/skia/NativeImageSkia.h" | |
42 #include "core/platform/graphics/transforms/AffineTransform.h" | |
43 | |
44 #include "skia/ext/image_operations.h" | |
45 | |
46 #include <wtf/RefPtr.h> | |
47 | |
48 namespace WebCore { | |
49 | |
50 IntSize dragImageSize(DragImageRef image) | |
51 { | |
52 if (!image) | |
53 return IntSize(); | |
54 | |
55 return IntSize(image->bitmap->width(), image->bitmap->height()); | |
56 } | |
57 | |
58 void deleteDragImage(DragImageRef image) | |
59 { | |
60 if (image) | |
61 delete image->bitmap; | |
62 delete image; | |
63 } | |
64 | |
65 DragImageRef scaleDragImage(DragImageRef image, FloatSize scale) | |
66 { | |
67 if (!image) | |
68 return 0; | |
69 | |
70 int imageWidth = scale.width() * image->bitmap->width(); | |
71 int imageHeight = scale.height() * image->bitmap->height(); | |
72 SkBitmap* scaledImage = new SkBitmap( | |
73 skia::ImageOperations::Resize(*image->bitmap, skia::ImageOperations::RES
IZE_LANCZOS3, | |
74 imageWidth, imageHeight)); | |
75 delete image->bitmap; | |
76 image->bitmap = scaledImage; | |
77 return image; | |
78 } | |
79 | |
80 DragImageRef dissolveDragImageToFraction(DragImageRef image, float fraction) | |
81 { | |
82 if (!image) | |
83 return 0; | |
84 | |
85 image->bitmap->setIsOpaque(false); | |
86 image->bitmap->lockPixels(); | |
87 | |
88 for (int row = 0; row < image->bitmap->height(); ++row) { | |
89 for (int column = 0; column < image->bitmap->width(); ++column) { | |
90 uint32_t* pixel = image->bitmap->getAddr32(column, row); | |
91 *pixel = SkPreMultiplyARGB(SkColorGetA(*pixel) * fraction, | |
92 SkColorGetR(*pixel), | |
93 SkColorGetG(*pixel), | |
94 SkColorGetB(*pixel)); | |
95 } | |
96 } | |
97 | |
98 image->bitmap->unlockPixels(); | |
99 | |
100 return image; | |
101 } | |
102 | |
103 DragImageRef createDragImageFromImage(Image* image, RespectImageOrientationEnum
shouldRespectImageOrientation) | |
104 { | |
105 if (!image) | |
106 return 0; | |
107 | |
108 RefPtr<NativeImageSkia> bitmap = image->nativeImageForCurrentFrame(); | |
109 if (!bitmap) | |
110 return 0; | |
111 | |
112 DragImageChromium* dragImageChromium = new DragImageChromium; | |
113 dragImageChromium->bitmap = new SkBitmap(); | |
114 dragImageChromium->resolutionScale = bitmap->resolutionScale(); | |
115 | |
116 if (image->isBitmapImage()) { | |
117 ImageOrientation orientation = DefaultImageOrientation; | |
118 BitmapImage* bitmapImage = static_cast<BitmapImage*>(image); | |
119 IntSize sizeRespectingOrientation = bitmapImage->sizeRespectingOrientati
on(); | |
120 | |
121 if (shouldRespectImageOrientation == RespectImageOrientation) | |
122 orientation = bitmapImage->currentFrameOrientation(); | |
123 | |
124 if (orientation != DefaultImageOrientation) { | |
125 // Construct a correctly-rotated copy of the image to use as the dra
g image. | |
126 dragImageChromium->bitmap->setConfig( | |
127 SkBitmap::kARGB_8888_Config, sizeRespectingOrientation.width(),
sizeRespectingOrientation.height()); | |
128 dragImageChromium->bitmap->allocPixels(); | |
129 | |
130 FloatRect destRect(FloatPoint(), sizeRespectingOrientation); | |
131 SkCanvas canvas(*dragImageChromium->bitmap); | |
132 | |
133 canvas.concat(orientation.transformFromDefault(sizeRespectingOrienta
tion)); | |
134 | |
135 if (orientation.usesWidthAsHeight()) | |
136 destRect = FloatRect(destRect.x(), destRect.y(), destRect.height
(), destRect.width()); | |
137 | |
138 canvas.drawBitmapRect(bitmap->bitmap(), 0, destRect); | |
139 return dragImageChromium; | |
140 } | |
141 } | |
142 | |
143 bitmap->bitmap().copyTo(dragImageChromium->bitmap, SkBitmap::kARGB_8888_Conf
ig); | |
144 return dragImageChromium; | |
145 } | |
146 | |
147 DragImageRef createDragImageIconForCachedImage(CachedImage*) | |
148 { | |
149 notImplemented(); | |
150 return 0; | |
151 } | |
152 | |
153 } // namespace WebCore | |
OLD | NEW |