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 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 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 "SharedBuffer.h"
32 #import <wtf/text/WTFString.h>
35 #import <CoreGraphics/CoreGraphics.h>
36 #import <ImageIO/ImageIO.h>
37 #import <MobileCoreServices/MobileCoreServices.h>
40 @interface WebCoreBundleFinder : NSObject
43 @implementation WebCoreBundleFinder
48 void BitmapImage::invalidatePlatformData()
50 if (m_frames.size() != 1)
59 PassRefPtr<Image> Image::loadPlatformResource(const char *name)
61 NSBundle *bundle = [NSBundle bundleForClass:[WebCoreBundleFinder class]];
62 NSString *imagePath = [bundle pathForResource:[NSString stringWithUTF8String:name] ofType:@"png"];
63 NSData *namedImageData = [NSData dataWithContentsOfFile:imagePath];
65 auto image = BitmapImage::create();
66 image->setData(SharedBuffer::wrapNSData(namedImageData), true);
67 return WTFMove(image);
70 // We have reports indicating resource loads are failing, but we don't yet know the root cause(s).
71 // Two theories are bad installs (image files are missing), and too-many-open-files.
74 return Image::nullImage();
77 CFDataRef BitmapImage::getTIFFRepresentation()
80 return m_tiffRep.get();
82 unsigned numFrames = frameCount();
84 // If numFrames is zero, we know for certain this image doesn't have valid data
85 // Even though the call to CGImageDestinationCreateWithData will fail and we'll handle it gracefully,
86 // in certain circumstances that call will spam the console with an error message
90 Vector<CGImageRef> images;
91 for (unsigned i = 0; i < numFrames; ++i ) {
92 CGImageRef cgImage = frameImageAtIndex(i).get();
94 images.append(cgImage);
97 unsigned numValidFrames = images.size();
99 RetainPtr<CFMutableDataRef> data = adoptCF(CFDataCreateMutable(0, 0));
100 RetainPtr<CGImageDestinationRef> destination = adoptCF(CGImageDestinationCreateWithData(data.get(), kUTTypeTIFF, numValidFrames, 0));
105 for (unsigned i = 0; i < numValidFrames; ++i)
106 CGImageDestinationAddImage(destination.get(), images[i], 0);
108 CGImageDestinationFinalize(destination.get());
111 return m_tiffRep.get();
115 NSImage* BitmapImage::getNSImage()
118 return m_nsImage.get();
120 CFDataRef data = getTIFFRepresentation();
124 m_nsImage = adoptNS([[NSImage alloc] initWithData:(NSData*)data]);
125 return m_nsImage.get();