1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp.toker@collabora.co.uk>
4 * Copyright (C) 2008, Google Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29 #include "platform/graphics/ImageSource.h"
30
31 #include "platform/graphics/DeferredImageDecoder.h"
32 #include "platform/image-decoders/ImageDecoder.h"
33 #include "wtf/PassOwnPtr.h"
34 #include "wtf/PassRefPtr.h"
35
36 namespace WebCore {
37
ImageSource(ImageSource::AlphaOption alphaOption,ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)38 ImageSource::ImageSource(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
39 : m_alphaOption(alphaOption)
40 , m_gammaAndColorProfileOption(gammaAndColorProfileOption)
41 {
42 }
43
~ImageSource()44 ImageSource::~ImageSource()
45 {
46 }
47
clearCacheExceptFrame(size_t clearExceptFrame)48 size_t ImageSource::clearCacheExceptFrame(size_t clearExceptFrame)
49 {
50 return m_decoder ? m_decoder->clearCacheExceptFrame(clearExceptFrame) : 0;
51 }
52
initialized() const53 bool ImageSource::initialized() const
54 {
55 return m_decoder;
56 }
57
setData(SharedBuffer * data,bool allDataReceived)58 void ImageSource::setData(SharedBuffer* data, bool allDataReceived)
59 {
60 // Make the decoder by sniffing the bytes.
61 // This method will examine the data and instantiate an instance of the appropriate decoder plugin.
62 // If insufficient bytes are available to determine the image type, no decoder plugin will be
63 // made.
64 if (!m_decoder)
65 m_decoder = DeferredImageDecoder::create(*data, m_alphaOption, m_gammaAndColorProfileOption);
66
67 if (m_decoder)
68 m_decoder->setData(data, allDataReceived);
69 }
70
filenameExtension() const71 String ImageSource::filenameExtension() const
72 {
73 return m_decoder ? m_decoder->filenameExtension() : String();
74 }
75
isSizeAvailable()76 bool ImageSource::isSizeAvailable()
77 {
78 return m_decoder && m_decoder->isSizeAvailable();
79 }
80
size(RespectImageOrientationEnum shouldRespectOrientation) const81 IntSize ImageSource::size(RespectImageOrientationEnum shouldRespectOrientation) const
82 {
83 return frameSizeAtIndex(0, shouldRespectOrientation);
84 }
85
frameSizeAtIndex(size_t index,RespectImageOrientationEnum shouldRespectOrientation) const86 IntSize ImageSource::frameSizeAtIndex(size_t index, RespectImageOrientationEnum shouldRespectOrientation) const
87 {
88 if (!m_decoder)
89 return IntSize();
90
91 IntSize size = m_decoder->frameSizeAtIndex(index);
92 if ((shouldRespectOrientation == RespectImageOrientation) && m_decoder->orientation().usesWidthAsHeight())
93 return IntSize(size.height(), size.width());
94
95 return size;
96 }
97
getHotSpot(IntPoint & hotSpot) const98 bool ImageSource::getHotSpot(IntPoint& hotSpot) const
99 {
100 return m_decoder ? m_decoder->hotSpot(hotSpot) : false;
101 }
102
repetitionCount()103 int ImageSource::repetitionCount()
104 {
105 return m_decoder ? m_decoder->repetitionCount() : cAnimationNone;
106 }
107
frameCount() const108 size_t ImageSource::frameCount() const
109 {
110 return m_decoder ? m_decoder->frameCount() : 0;
111 }
112
createFrameAtIndex(size_t index)113 PassRefPtr<NativeImageSkia> ImageSource::createFrameAtIndex(size_t index)
114 {
115 if (!m_decoder)
116 return 0;
117
118 ImageFrame* buffer = m_decoder->frameBufferAtIndex(index);
119 if (!buffer || buffer->status() == ImageFrame::FrameEmpty)
120 return 0;
121
122 // Zero-height images can cause problems for some ports. If we have an
123 // empty image dimension, just bail.
124 if (size().isEmpty())
125 return 0;
126
127 // Return the buffer contents as a native image. For some ports, the data
128 // is already in a native container, and this just increments its refcount.
129 return buffer->asNewNativeImage();
130 }
131
frameDurationAtIndex(size_t index) const132 float ImageSource::frameDurationAtIndex(size_t index) const
133 {
134 if (!m_decoder)
135 return 0;
136
137 // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
138 // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
139 // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
140 // for more information.
141 const float duration = m_decoder->frameDurationAtIndex(index) / 1000.0f;
142 if (duration < 0.011f)
143 return 0.100f;
144 return duration;
145 }
146
orientationAtIndex(size_t) const147 ImageOrientation ImageSource::orientationAtIndex(size_t) const
148 {
149 return m_decoder ? m_decoder->orientation() : DefaultImageOrientation;
150 }
151
frameHasAlphaAtIndex(size_t index) const152 bool ImageSource::frameHasAlphaAtIndex(size_t index) const
153 {
154 return !m_decoder || m_decoder->frameHasAlphaAtIndex(index);
155 }
156
frameIsCompleteAtIndex(size_t index) const157 bool ImageSource::frameIsCompleteAtIndex(size_t index) const
158 {
159 return m_decoder && m_decoder->frameIsCompleteAtIndex(index);
160 }
161
frameBytesAtIndex(size_t index) const162 unsigned ImageSource::frameBytesAtIndex(size_t index) const
163 {
164 if (!m_decoder)
165 return 0;
166 return m_decoder->frameBytesAtIndex(index);
167 }
168
169 }
170