• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FoundationExtras.h"
31#import "GraphicsContext.h"
32#import "PlatformString.h"
33
34@interface WebCoreBundleFinder : NSObject
35@end
36
37@implementation WebCoreBundleFinder
38@end
39
40namespace WebCore {
41
42void BitmapImage::initPlatformData()
43{
44}
45
46void BitmapImage::invalidatePlatformData()
47{
48    if (m_frames.size() != 1)
49        return;
50
51    m_nsImage = 0;
52    m_tiffRep = 0;
53}
54
55PassRefPtr<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
73CFDataRef 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    CGImageDestinationRef destination = 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, images[i], 0);
104
105    CGImageDestinationFinalize(destination);
106    CFRelease(destination);
107
108    m_tiffRep = data;
109    return m_tiffRep.get();
110}
111
112NSImage* BitmapImage::getNSImage()
113{
114    if (m_nsImage)
115        return m_nsImage.get();
116
117    CFDataRef data = getTIFFRepresentation();
118    if (!data)
119        return 0;
120
121    m_nsImage.adoptNS([[NSImage alloc] initWithData:(NSData*)data]);
122    return m_nsImage.get();
123}
124
125}
126