2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "platform/image-decoders/ImageDecoder.h"
24 #include "platform/graphics/DeferredImageDecoder.h"
25 #include "platform/image-decoders/bmp/BMPImageDecoder.h"
26 #include "platform/image-decoders/gif/GIFImageDecoder.h"
27 #include "platform/image-decoders/ico/ICOImageDecoder.h"
28 #include "platform/image-decoders/jpeg/JPEGImageDecoder.h"
29 #include "platform/image-decoders/png/PNGImageDecoder.h"
30 #include "platform/image-decoders/webp/WEBPImageDecoder.h"
31 #include "wtf/PassOwnPtr.h"
35 static unsigned copyFromSharedBuffer(char* buffer, unsigned bufferLength, const SharedBuffer& sharedBuffer, unsigned offset)
37 unsigned bytesExtracted = 0;
39 while (unsigned moreDataLength = sharedBuffer.getSomeData(moreData, offset)) {
40 unsigned bytesToCopy = std::min(bufferLength - bytesExtracted, moreDataLength);
41 memcpy(buffer + bytesExtracted, moreData, bytesToCopy);
42 bytesExtracted += bytesToCopy;
43 if (bytesExtracted == bufferLength)
45 offset += bytesToCopy;
47 return bytesExtracted;
50 inline bool matchesGIFSignature(char* contents)
52 return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6);
55 inline bool matchesPNGSignature(char* contents)
57 return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
60 inline bool matchesJPEGSignature(char* contents)
62 return !memcmp(contents, "\xFF\xD8\xFF", 3);
65 inline bool matchesWebPSignature(char* contents)
67 return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6);
70 inline bool matchesBMPSignature(char* contents)
72 return !memcmp(contents, "BM", 2);
75 inline bool matchesICOSignature(char* contents)
77 return !memcmp(contents, "\x00\x00\x01\x00", 4);
80 inline bool matchesCURSignature(char* contents)
82 return !memcmp(contents, "\x00\x00\x02\x00", 4);
85 PassOwnPtr<ImageDecoder> ImageDecoder::create(const SharedBuffer& data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
87 static const unsigned longestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
88 ASSERT(longestSignatureLength == 14);
90 size_t maxDecodedBytes = blink::Platform::current()->maxDecodedImageBytes();
92 char contents[longestSignatureLength];
93 if (copyFromSharedBuffer(contents, longestSignatureLength, data, 0) < longestSignatureLength)
96 if (matchesJPEGSignature(contents))
97 return adoptPtr(new JPEGImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
99 if (matchesPNGSignature(contents))
100 return adoptPtr(new PNGImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
102 if (matchesGIFSignature(contents))
103 return adoptPtr(new GIFImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
105 if (matchesICOSignature(contents) || matchesCURSignature(contents))
106 return adoptPtr(new ICOImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
108 if (matchesWebPSignature(contents))
109 return adoptPtr(new WEBPImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
111 if (matchesBMPSignature(contents))
112 return adoptPtr(new BMPImageDecoder(alphaOption, gammaAndColorProfileOption, maxDecodedBytes));
117 bool ImageDecoder::frameHasAlphaAtIndex(size_t index) const
119 return !frameIsCompleteAtIndex(index) || m_frameBufferCache[index].hasAlpha();
122 bool ImageDecoder::frameIsCompleteAtIndex(size_t index) const
124 return (index < m_frameBufferCache.size()) &&
125 (m_frameBufferCache[index].status() == ImageFrame::FrameComplete);
128 unsigned ImageDecoder::frameBytesAtIndex(size_t index) const
130 if (m_frameBufferCache.size() <= index || m_frameBufferCache[index].status() == ImageFrame::FrameEmpty)
132 // FIXME: Use the dimension of the requested frame.
133 return m_size.area() * sizeof(ImageFrame::PixelData);
136 bool ImageDecoder::deferredImageDecodingEnabled()
138 return DeferredImageDecoder::enabled();
141 size_t ImageDecoder::clearCacheExceptFrame(size_t clearExceptFrame)
143 // Don't clear if there are no frames or only one frame.
144 if (m_frameBufferCache.size() <= 1)
147 size_t frameBytesCleared = 0;
148 for (size_t i = 0; i < m_frameBufferCache.size(); ++i) {
149 if (i != clearExceptFrame) {
150 frameBytesCleared += frameBytesAtIndex(i);
154 return frameBytesCleared;
157 void ImageDecoder::clearFrameBuffer(size_t frameIndex)
159 m_frameBufferCache[frameIndex].clearPixelData();
162 size_t ImageDecoder::findRequiredPreviousFrame(size_t frameIndex, bool frameRectIsOpaque)
164 ASSERT(frameIndex <= m_frameBufferCache.size());
166 // The first frame doesn't rely on any previous data.
170 const ImageFrame* currBuffer = &m_frameBufferCache[frameIndex];
171 if ((frameRectIsOpaque || currBuffer->alphaBlendSource() == ImageFrame::BlendAtopBgcolor)
172 && currBuffer->originalFrameRect().contains(IntRect(IntPoint(), size())))
175 // The starting state for this frame depends on the previous frame's
177 size_t prevFrame = frameIndex - 1;
178 const ImageFrame* prevBuffer = &m_frameBufferCache[prevFrame];
179 ASSERT(prevBuffer->requiredPreviousFrameIndexValid());
181 switch (prevBuffer->disposalMethod()) {
182 case ImageFrame::DisposeNotSpecified:
183 case ImageFrame::DisposeKeep:
184 // prevFrame will be used as the starting state for this frame.
185 // FIXME: Be even smarter by checking the frame sizes and/or alpha-containing regions.
187 case ImageFrame::DisposeOverwritePrevious:
188 // Frames that use the DisposeOverwritePrevious method are effectively
189 // no-ops in terms of changing the starting state of a frame compared to
190 // the starting state of the previous frame, so skip over them and
191 // return the required previous frame of it.
192 return prevBuffer->requiredPreviousFrameIndex();
193 case ImageFrame::DisposeOverwriteBgcolor:
194 // If the previous frame fills the whole image, then the current frame
195 // can be decoded alone. Likewise, if the previous frame could be
196 // decoded without reference to any prior frame, the starting state for
197 // this frame is a blank frame, so it can again be decoded alone.
198 // Otherwise, the previous frame contributes to this frame.
199 return (prevBuffer->originalFrameRect().contains(IntRect(IntPoint(), size()))
200 || (prevBuffer->requiredPreviousFrameIndex() == kNotFound)) ? kNotFound : prevFrame;
202 ASSERT_NOT_REACHED();
207 } // namespace WebCore