Home | History | Annotate | Download | only in cg
      1 /*
      2  * Copyright (C) 2010 Google, Inc.
      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 "ImageDecoder.h"
     28 
     29 #include "GraphicsContextCG.h"
     30 
     31 #include <CoreGraphics/CGColorSpace.h>
     32 #include <CoreGraphics/CGImage.h>
     33 
     34 namespace WebCore {
     35 
     36 static ImageFrame::PixelData* getPtrAsPixelData(CFMutableDataRef data)
     37 {
     38     return data ? reinterpret_cast<ImageFrame::PixelData*>(CFDataGetMutableBytePtr(data)) : 0;
     39 }
     40 
     41 void ImageFrame::copyReferenceToBitmapData(const ImageFrame& other)
     42 {
     43     ASSERT(this != &other);
     44     m_backingStore = other.m_backingStore;
     45     m_bytes = getPtrAsPixelData(m_backingStore.get());
     46     // FIXME: The rest of this function seems redundant with ImageFrame::copyBitmapData.
     47     m_size = other.m_size;
     48     setHasAlpha(other.m_hasAlpha);
     49 }
     50 
     51 bool ImageFrame::copyBitmapData(const ImageFrame& other)
     52 {
     53     if (this == &other)
     54         return true;
     55 
     56     m_backingStore.adoptCF(CFDataCreateMutableCopy(kCFAllocatorDefault, 0, other.m_backingStore.get()));
     57     m_bytes = getPtrAsPixelData(m_backingStore.get());
     58     m_size = other.m_size;
     59     setHasAlpha(other.m_hasAlpha);
     60     return true;
     61 }
     62 
     63 bool ImageFrame::setSize(int newWidth, int newHeight)
     64 {
     65     ASSERT(!m_backingStore);
     66     size_t backingStoreSize = newWidth * newHeight * sizeof(PixelData);
     67     CFMutableDataRef backingStoreRef = CFDataCreateMutable(kCFAllocatorDefault, backingStoreSize);
     68     if (!backingStoreRef)
     69         return false;
     70     m_backingStore.adoptCF(backingStoreRef);
     71     CFDataSetLength(backingStoreRef, backingStoreSize);
     72     m_bytes = reinterpret_cast<PixelData*>(CFDataGetMutableBytePtr(m_backingStore.get()));
     73     m_size = IntSize(newWidth, newHeight);
     74 
     75     zeroFillPixelData();
     76     return true;
     77 }
     78 
     79 static CGColorSpaceRef createColorSpace(const ColorProfile& colorProfile)
     80 {
     81     if (colorProfile.isEmpty())
     82         return CGColorSpaceCreateDeviceRGB();
     83 
     84     RetainPtr<CFDataRef> data(AdoptCF, CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(colorProfile.data()), colorProfile.size()));
     85 #if !defined(TARGETING_TIGER) && !defined(TARGETING_LEOPARD)
     86     return CGColorSpaceCreateWithICCProfile(data.get());
     87 #else
     88     RetainPtr<CGDataProviderRef> profileDataProvider(AdoptCF, CGDataProviderCreateWithCFData(data.get()));
     89     CGFloat ranges[] = {0.0, 255.0, 0.0, 255.0, 0.0, 255.0};
     90     return CGColorSpaceCreateICCBased(3, ranges, profileDataProvider.get(), deviceRGBColorSpaceRef());
     91 #endif
     92 }
     93 
     94 NativeImagePtr ImageFrame::asNewNativeImage() const
     95 {
     96     RetainPtr<CGColorSpaceRef> colorSpace(AdoptCF, createColorSpace(m_colorProfile));
     97     RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(m_backingStore.get()));
     98 
     99     CGImageAlphaInfo alphaInfo = m_premultiplyAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaFirst;
    100 
    101     return CGImageCreate(width(), height(), 8, 32, width() * sizeof(PixelData), colorSpace.get(),
    102         alphaInfo | kCGBitmapByteOrder32Host, dataProvider.get(), 0, /*shouldInterpolate=*/true, kCGRenderingIntentDefault);
    103 }
    104 
    105 } // namespace WebCore
    106