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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include "platform/graphics/ImageSource.h"
31 #include "platform/graphics/DeferredImageDecoder.h"
32 #include "platform/image-decoders/ImageDecoder.h"
33 #include "wtf/PassOwnPtr.h"
34 #include "wtf/PassRefPtr.h"
38 ImageSource::ImageSource(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
39 : m_alphaOption(alphaOption)
40 , m_gammaAndColorProfileOption(gammaAndColorProfileOption)
44 ImageSource::~ImageSource()
48 size_t ImageSource::clearCacheExceptFrame(size_t clearExceptFrame)
50 return m_decoder ? m_decoder->clearCacheExceptFrame(clearExceptFrame) : 0;
53 bool ImageSource::initialized() const
58 void ImageSource::setData(SharedBuffer& data, bool allDataReceived)
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
65 m_decoder = DeferredImageDecoder::create(data, m_alphaOption, m_gammaAndColorProfileOption);
68 m_decoder->setData(data, allDataReceived);
71 String ImageSource::filenameExtension() const
73 return m_decoder ? m_decoder->filenameExtension() : String();
76 bool ImageSource::isSizeAvailable()
78 return m_decoder && m_decoder->isSizeAvailable();
81 bool ImageSource::hasColorProfile() const
83 return m_decoder && m_decoder->hasColorProfile();
86 IntSize ImageSource::size(RespectImageOrientationEnum shouldRespectOrientation) const
88 return frameSizeAtIndex(0, shouldRespectOrientation);
91 IntSize ImageSource::frameSizeAtIndex(size_t index, RespectImageOrientationEnum shouldRespectOrientation) const
96 IntSize size = m_decoder->frameSizeAtIndex(index);
97 if ((shouldRespectOrientation == RespectImageOrientation) && m_decoder->orientation().usesWidthAsHeight())
98 return IntSize(size.height(), size.width());
103 bool ImageSource::getHotSpot(IntPoint& hotSpot) const
105 return m_decoder ? m_decoder->hotSpot(hotSpot) : false;
108 int ImageSource::repetitionCount()
110 return m_decoder ? m_decoder->repetitionCount() : cAnimationNone;
113 size_t ImageSource::frameCount() const
115 return m_decoder ? m_decoder->frameCount() : 0;
118 PassRefPtr<NativeImageSkia> ImageSource::createFrameAtIndex(size_t index)
123 ImageFrame* buffer = m_decoder->frameBufferAtIndex(index);
124 if (!buffer || buffer->status() == ImageFrame::FrameEmpty)
127 // Zero-height images can cause problems for some ports. If we have an
128 // empty image dimension, just bail.
129 if (size().isEmpty())
132 // Return the buffer contents as a native image. For some ports, the data
133 // is already in a native container, and this just increments its refcount.
134 return buffer->asNewNativeImage();
137 float ImageSource::frameDurationAtIndex(size_t index) const
142 // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
143 // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
144 // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
145 // for more information.
146 const float duration = m_decoder->frameDurationAtIndex(index) / 1000.0f;
147 if (duration < 0.011f)
152 ImageOrientation ImageSource::orientationAtIndex(size_t) const
154 return m_decoder ? m_decoder->orientation() : DefaultImageOrientation;
157 bool ImageSource::frameHasAlphaAtIndex(size_t index) const
159 return !m_decoder || m_decoder->frameHasAlphaAtIndex(index);
162 bool ImageSource::frameIsCompleteAtIndex(size_t index) const
164 return m_decoder && m_decoder->frameIsCompleteAtIndex(index);
167 unsigned ImageSource::frameBytesAtIndex(size_t index) const
171 return m_decoder->frameBytesAtIndex(index);