mrowe@apple.com | e452c87 | 2008-02-07 01:52:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006, 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 "Image.h" |
| 28 | #include "BitmapImage.h" |
jhoneycutt@apple.com | 6e17426 | 2009-10-03 02:51:49 +0000 | [diff] [blame] | 29 | #include "BitmapInfo.h" |
mrowe@apple.com | e452c87 | 2008-02-07 01:52:05 +0000 | [diff] [blame] | 30 | #include "GraphicsContext.h" |
| 31 | #include <ApplicationServices/ApplicationServices.h> |
| 32 | |
| 33 | #include <windows.h> |
| 34 | #include "PlatformString.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
jhoneycutt@apple.com | 6e17426 | 2009-10-03 02:51:49 +0000 | [diff] [blame] | 38 | PassRefPtr<BitmapImage> BitmapImage::create(HBITMAP hBitmap) |
| 39 | { |
| 40 | DIBSECTION dibSection; |
| 41 | if (!GetObject(hBitmap, sizeof(DIBSECTION), &dibSection)) |
| 42 | return 0; |
| 43 | |
| 44 | ASSERT(dibSection.dsBm.bmBitsPixel == 32); |
| 45 | if (dibSection.dsBm.bmBitsPixel != 32) |
| 46 | return 0; |
| 47 | |
| 48 | ASSERT(dibSection.dsBm.bmBits); |
| 49 | if (!dibSection.dsBm.bmBits) |
| 50 | return 0; |
| 51 | |
| 52 | RetainPtr<CGColorSpaceRef> deviceRGB(AdoptCF, CGColorSpaceCreateDeviceRGB()); |
| 53 | RetainPtr<CGContextRef> bitmapContext(AdoptCF, CGBitmapContextCreate(dibSection.dsBm.bmBits, dibSection.dsBm.bmWidth, dibSection.dsBm.bmHeight, 8, |
| 54 | dibSection.dsBm.bmWidthBytes, deviceRGB.get(), kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst)); |
| 55 | |
| 56 | // The BitmapImage takes ownership of this. |
| 57 | CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext.get()); |
| 58 | |
| 59 | return adoptRef(new BitmapImage(cgImage)); |
| 60 | } |
| 61 | |
mrowe@apple.com | e452c87 | 2008-02-07 01:52:05 +0000 | [diff] [blame] | 62 | bool BitmapImage::getHBITMAPOfSize(HBITMAP bmp, LPSIZE size) |
| 63 | { |
| 64 | ASSERT(bmp); |
| 65 | |
| 66 | BITMAP bmpInfo; |
| 67 | GetObject(bmp, sizeof(BITMAP), &bmpInfo); |
| 68 | |
| 69 | ASSERT(bmpInfo.bmBitsPixel == 32); |
| 70 | int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight; |
| 71 | |
| 72 | CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB(); |
| 73 | CGContextRef cgContext = CGBitmapContextCreate(bmpInfo.bmBits, bmpInfo.bmWidth, bmpInfo.bmHeight, |
| 74 | 8, bmpInfo.bmWidthBytes, deviceRGB, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); |
| 75 | |
| 76 | GraphicsContext gc(cgContext); |
| 77 | |
| 78 | IntSize imageSize = BitmapImage::size(); |
| 79 | if (size) |
bdakin@apple.com | 62711a6 | 2009-11-19 23:36:58 +0000 | [diff] [blame^] | 80 | drawFrameMatchingSourceSize(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), IntSize(*size), DeviceColorSpace, CompositeCopy); |
mrowe@apple.com | e452c87 | 2008-02-07 01:52:05 +0000 | [diff] [blame] | 81 | else |
| 82 | draw(&gc, FloatRect(0.0f, 0.0f, bmpInfo.bmWidth, bmpInfo.bmHeight), FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), CompositeCopy); |
| 83 | |
| 84 | // Do cleanup |
| 85 | CGContextRelease(cgContext); |
| 86 | CGColorSpaceRelease(deviceRGB); |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
bdakin@apple.com | 62711a6 | 2009-11-19 23:36:58 +0000 | [diff] [blame^] | 91 | void BitmapImage::drawFrameMatchingSourceSize(GraphicsContext* ctxt, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator compositeOp) |
mrowe@apple.com | e452c87 | 2008-02-07 01:52:05 +0000 | [diff] [blame] | 92 | { |
pkasting@chromium.org | b3c84c8 | 2009-08-03 23:48:26 +0000 | [diff] [blame] | 93 | size_t frames = frameCount(); |
| 94 | for (size_t i = 0; i < frames; ++i) { |
mrowe@apple.com | e452c87 | 2008-02-07 01:52:05 +0000 | [diff] [blame] | 95 | CGImageRef image = frameAtIndex(i); |
| 96 | if (CGImageGetHeight(image) == static_cast<size_t>(srcSize.height()) && CGImageGetWidth(image) == static_cast<size_t>(srcSize.width())) { |
| 97 | size_t currentFrame = m_currentFrame; |
| 98 | m_currentFrame = i; |
| 99 | draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, srcSize.width(), srcSize.height()), compositeOp); |
| 100 | m_currentFrame = currentFrame; |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // No image of the correct size was found, fallback to drawing the current frame |
| 106 | IntSize imageSize = BitmapImage::size(); |
| 107 | draw(ctxt, dstRect, FloatRect(0.0f, 0.0f, imageSize.width(), imageSize.height()), compositeOp); |
| 108 | } |
| 109 | |
| 110 | } // namespace WebCore |