2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "BitmapImage.h"
30 #import "GraphicsContext.h"
31 #import "PlatformString.h"
32 #import "SharedBuffer.h"
34 @interface WebCoreBundleFinder : NSObject
37 @implementation WebCoreBundleFinder
42 void BitmapImage::initPlatformData()
46 void BitmapImage::invalidatePlatformData()
48 if (m_frames.size() != 1)
55 PassRefPtr<Image> Image::loadPlatformResource(const char *name)
57 NSBundle *bundle = [NSBundle bundleForClass:[WebCoreBundleFinder class]];
58 NSString *imagePath = [bundle pathForResource:[NSString stringWithUTF8String:name] ofType:@"png"];
59 NSData *namedImageData = [NSData dataWithContentsOfFile:imagePath];
61 RefPtr<Image> image = BitmapImage::create();
62 image->setData(SharedBuffer::wrapNSData(namedImageData), true);
63 return image.release();
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.
70 return Image::nullImage();
73 CFDataRef BitmapImage::getTIFFRepresentation()
76 return m_tiffRep.get();
78 unsigned numFrames = frameCount();
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
86 Vector<CGImageRef> images;
87 for (unsigned i = 0; i < numFrames; ++i ) {
88 CGImageRef cgImage = frameAtIndex(i);
90 images.append(cgImage);
93 unsigned numValidFrames = images.size();
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));
102 for (unsigned i = 0; i < numValidFrames; ++i)
103 CGImageDestinationAddImage(destination.get(), images[i], 0);
105 CGImageDestinationFinalize(destination.get());
108 return m_tiffRep.get();
111 NSImage* BitmapImage::getNSImage()
114 return m_nsImage.get();
116 CFDataRef data = getTIFFRepresentation();
120 m_nsImage.adoptNS([[NSImage alloc] initWithData:(NSData*)data]);
121 return m_nsImage.get();