Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (c) 2010, 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 "NotImplemented.h"
     36 #include <wtf/RetainPtr.h>
     37 
     38 #include <CoreGraphics/CGBitmapContext.h>
     39 #include <CoreGraphics/CGImage.h>
     40 
     41 namespace WebCore {
     42 
     43 IntSize dragImageSize(DragImageRef image)
     44 {
     45     if (!image)
     46         return IntSize();
     47     return IntSize(CGImageGetWidth(image), CGImageGetHeight(image));
     48 }
     49 
     50 void deleteDragImage(DragImageRef image)
     51 {
     52     CGImageRelease(image);
     53 }
     54 
     55 DragImageRef scaleDragImage(DragImageRef image, FloatSize scale)
     56 {
     57     if (!image)
     58         return 0;
     59     size_t width = roundf(CGImageGetWidth(image) * scale.width());
     60     size_t height = roundf(CGImageGetHeight(image) * scale.height());
     61 
     62     RetainPtr<CGColorSpaceRef> deviceRGB(WTF::AdoptCF, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
     63     CGContextRef context = CGBitmapContextCreate(0, width, height, 8, width * 4, deviceRGB.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
     64 
     65     if (!context)
     66         return 0;
     67     CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
     68     CGImageRelease(image);
     69 
     70     CGImageRef scaledImage = CGBitmapContextCreateImage(context);
     71     CGContextRelease(context);
     72     return scaledImage;
     73 }
     74 
     75 DragImageRef dissolveDragImageToFraction(DragImageRef image, float delta)
     76 {
     77     if (!image)
     78         return 0;
     79     size_t width = CGImageGetWidth(image);
     80     size_t height = CGImageGetHeight(image);
     81 
     82     RetainPtr<CGColorSpaceRef> deviceRGB(WTF::AdoptCF, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
     83     CGContextRef context = CGBitmapContextCreate(0, width, height, 8, width * 4, deviceRGB.get(), kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
     84 
     85     if (!context)
     86         return 0;
     87     // From CGContext.h:
     88     //     The Porter-Duff "source over" mode is called `kCGBlendModeNormal':
     89     //       R = S + D*(1 - Sa)
     90     //  This is the same as NSCompositeSourceOver, which is what -[NSImage dissolveToPoint:fraction:] uses.
     91     CGContextSetAlpha(context, delta);
     92     CGContextSetBlendMode(context, kCGBlendModeNormal);
     93     CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
     94     CGImageRelease(image);
     95 
     96     CGImageRef dissolvedImage = CGBitmapContextCreateImage(context);
     97     CGContextRelease(context);
     98     return dissolvedImage;
     99 }
    100 
    101 DragImageRef createDragImageFromImage(Image* image)
    102 {
    103     if (!image)
    104         return 0;
    105     return CGImageCreateCopy(image->nativeImageForCurrentFrame());
    106 }
    107 
    108 DragImageRef createDragImageIconForCachedImage(CachedImage*)
    109 {
    110     notImplemented();
    111     return 0;
    112 }
    113 
    114 } // namespace WebCore
    115