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