Home | History | Annotate | Download | only in mac
      1 /*
      2  * Copyright (C) 2004, 2005, 2006, 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 #import "config.h"
     27 #import "BitmapImage.h"
     28 
     29 #import "FloatRect.h"
     30 #import "GraphicsContext.h"
     31 #import "PlatformString.h"
     32 #import "SharedBuffer.h"
     33 
     34 @interface WebCoreBundleFinder : NSObject
     35 @end
     36 
     37 @implementation WebCoreBundleFinder
     38 @end
     39 
     40 namespace WebCore {
     41 
     42 void BitmapImage::initPlatformData()
     43 {
     44 }
     45 
     46 void BitmapImage::invalidatePlatformData()
     47 {
     48     if (m_frames.size() != 1)
     49         return;
     50 
     51     m_nsImage = 0;
     52     m_tiffRep = 0;
     53 }
     54 
     55 PassRefPtr<Image> Image::loadPlatformResource(const char *name)
     56 {
     57     NSBundle *bundle = [NSBundle bundleForClass:[WebCoreBundleFinder class]];
     58     NSString *imagePath = [bundle pathForResource:[NSString stringWithUTF8String:name] ofType:@"tiff"];
     59     NSData *namedImageData = [NSData dataWithContentsOfFile:imagePath];
     60     if (namedImageData) {
     61         RefPtr<Image> image = BitmapImage::create();
     62         image->setData(SharedBuffer::wrapNSData(namedImageData), true);
     63         return image.release();
     64     }
     65 
     66     // We have reports indicating resource loads are failing, but we don't yet know the root cause(s).
     67     // Two theories are bad installs (image files are missing), and too-many-open-files.
     68     // See rdar://5607381
     69     ASSERT_NOT_REACHED();
     70     return Image::nullImage();
     71 }
     72 
     73 CFDataRef BitmapImage::getTIFFRepresentation()
     74 {
     75     if (m_tiffRep)
     76         return m_tiffRep.get();
     77 
     78     unsigned numFrames = frameCount();
     79 
     80     // If numFrames is zero, we know for certain this image doesn't have valid data
     81     // Even though the call to CGImageDestinationCreateWithData will fail and we'll handle it gracefully,
     82     // in certain circumstances that call will spam the console with an error message
     83     if (!numFrames)
     84         return 0;
     85 
     86     Vector<CGImageRef> images;
     87     for (unsigned i = 0; i < numFrames; ++i ) {
     88         CGImageRef cgImage = frameAtIndex(i);
     89         if (cgImage)
     90             images.append(cgImage);
     91     }
     92 
     93     unsigned numValidFrames = images.size();
     94 
     95     RetainPtr<CFMutableDataRef> data(AdoptCF, CFDataCreateMutable(0, 0));
     96     // FIXME:  Use type kCGImageTypeIdentifierTIFF constant once is becomes available in the API
     97     RetainPtr<CGImageDestinationRef> destination(AdoptCF, CGImageDestinationCreateWithData(data.get(), CFSTR("public.tiff"), numValidFrames, 0));
     98 
     99     if (!destination)
    100         return 0;
    101 
    102     for (unsigned i = 0; i < numValidFrames; ++i)
    103         CGImageDestinationAddImage(destination.get(), images[i], 0);
    104 
    105     CGImageDestinationFinalize(destination.get());
    106 
    107     m_tiffRep = data;
    108     return m_tiffRep.get();
    109 }
    110 
    111 NSImage* BitmapImage::getNSImage()
    112 {
    113     if (m_nsImage)
    114         return m_nsImage.get();
    115 
    116     CFDataRef data = getTIFFRepresentation();
    117     if (!data)
    118         return 0;
    119 
    120     m_nsImage.adoptNS([[NSImage alloc] initWithData:(NSData*)data]);
    121     return m_nsImage.get();
    122 }
    123 
    124 }
    125