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