2 * Copyright (C) 2008-2009 Torch Mobile, Inc.
3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #include "ImageDecoder.h"
29 #include "BMPImageDecoder.h"
30 #include "GIFImageDecoder.h"
31 #include "ICOImageDecoder.h"
32 #include "JPEGImageDecoder.h"
33 #include "PNGImageDecoder.h"
34 #include "WEBPImageDecoder.h"
35 #include "SharedBuffer.h"
43 unsigned copyFromSharedBuffer(char* buffer, unsigned bufferLength, const SharedBuffer& sharedBuffer, unsigned offset)
45 unsigned bytesExtracted = 0;
47 while (unsigned moreDataLength = sharedBuffer.getSomeData(moreData, offset)) {
48 unsigned bytesToCopy = min(bufferLength - bytesExtracted, moreDataLength);
49 memcpy(buffer + bytesExtracted, moreData, bytesToCopy);
50 bytesExtracted += bytesToCopy;
51 if (bytesExtracted == bufferLength)
53 offset += bytesToCopy;
55 return bytesExtracted;
58 bool matchesGIFSignature(char* contents)
60 return !memcmp(contents, "GIF87a", 6) || !memcmp(contents, "GIF89a", 6);
63 bool matchesPNGSignature(char* contents)
65 return !memcmp(contents, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
68 bool matchesJPEGSignature(char* contents)
70 return !memcmp(contents, "\xFF\xD8\xFF", 3);
74 bool matchesWebPSignature(char* contents)
76 return !memcmp(contents, "RIFF", 4) && !memcmp(contents + 8, "WEBPVP", 6);
80 bool matchesBMPSignature(char* contents)
82 return !memcmp(contents, "BM", 2);
85 bool matchesICOSignature(char* contents)
87 return !memcmp(contents, "\x00\x00\x01\x00", 4);
90 bool matchesCURSignature(char* contents)
92 return !memcmp(contents, "\x00\x00\x02\x00", 4);
97 ImageDecoder* ImageDecoder::create(const SharedBuffer& data, ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption)
99 static const unsigned lengthOfLongestSignature = 14; // To wit: "RIFF????WEBPVP"
100 char contents[lengthOfLongestSignature];
101 unsigned length = copyFromSharedBuffer(contents, lengthOfLongestSignature, data, 0);
102 if (length < lengthOfLongestSignature)
105 if (matchesGIFSignature(contents))
106 return new GIFImageDecoder(alphaOption, gammaAndColorProfileOption);
108 if (matchesPNGSignature(contents))
109 return new PNGImageDecoder(alphaOption, gammaAndColorProfileOption);
111 if (matchesJPEGSignature(contents))
112 return new JPEGImageDecoder(alphaOption, gammaAndColorProfileOption);
115 if (matchesWebPSignature(contents))
116 return new WEBPImageDecoder(alphaOption, gammaAndColorProfileOption);
119 if (matchesBMPSignature(contents))
120 return new BMPImageDecoder(alphaOption, gammaAndColorProfileOption);
122 if (matchesICOSignature(contents) || matchesCURSignature(contents))
123 return new ICOImageDecoder(alphaOption, gammaAndColorProfileOption);
130 ImageFrame::ImageFrame()
132 , m_status(FrameEmpty)
134 , m_disposalMethod(DisposeNotSpecified)
135 , m_premultiplyAlpha(true)
139 ImageFrame& ImageFrame::operator=(const ImageFrame& other)
144 copyReferenceToBitmapData(other);
145 setOriginalFrameRect(other.originalFrameRect());
146 setStatus(other.status());
147 setDuration(other.duration());
148 setDisposalMethod(other.disposalMethod());
149 setPremultiplyAlpha(other.premultiplyAlpha());
153 void ImageFrame::clearPixelData()
155 m_backingStore.clear();
157 m_status = FrameEmpty;
158 // NOTE: Do not reset other members here; clearFrameBufferCache() calls this
159 // to free the bitmap data, but other functions like initFrameBuffer() and
160 // frameComplete() may still need to read other metadata out of this frame
164 void ImageFrame::zeroFillPixelData()
166 memset(m_bytes, 0, m_size.width() * m_size.height() * sizeof(PixelData));
172 void ImageFrame::copyReferenceToBitmapData(const ImageFrame& other)
174 ASSERT(this != &other);
175 copyBitmapData(other);
178 bool ImageFrame::copyBitmapData(const ImageFrame& other)
183 m_backingStore = other.m_backingStore;
184 m_bytes = m_backingStore.data();
185 m_size = other.m_size;
186 setHasAlpha(other.m_hasAlpha);
190 bool ImageFrame::setSize(int newWidth, int newHeight)
192 // NOTE: This has no way to check for allocation failure if the requested
193 // size was too big...
194 m_backingStore.resize(newWidth * newHeight);
195 m_bytes = m_backingStore.data();
196 m_size = IntSize(newWidth, newHeight);
205 bool ImageFrame::hasAlpha() const
210 void ImageFrame::setHasAlpha(bool alpha)
215 void ImageFrame::setColorProfile(const ColorProfile& colorProfile)
217 m_colorProfile = colorProfile;
220 void ImageFrame::setStatus(FrameStatus status)
225 int ImageFrame::width() const
227 return m_size.width();
230 int ImageFrame::height() const
232 return m_size.height();
245 inline void fillScaledValues(Vector<int>& scaledValues, double scaleRate, int length)
247 double inflateRate = 1. / scaleRate;
248 scaledValues.reserveCapacity(static_cast<int>(length * scaleRate + 0.5));
249 for (int scaledIndex = 0; ; ++scaledIndex) {
250 int index = static_cast<int>(scaledIndex * inflateRate + 0.5);
253 scaledValues.append(index);
257 template <MatchType type> int getScaledValue(const Vector<int>& scaledValues, int valueToMatch, int searchStart)
259 if (scaledValues.isEmpty())
262 const int* dataStart = scaledValues.data();
263 const int* dataEnd = dataStart + scaledValues.size();
264 const int* matched = std::lower_bound(dataStart + searchStart, dataEnd, valueToMatch);
267 return matched != dataEnd && *matched == valueToMatch ? matched - dataStart : -1;
269 return matched != dataEnd && *matched == valueToMatch ? matched - dataStart : matched - dataStart - 1;
272 return matched != dataEnd ? matched - dataStart : -1;
278 void ImageDecoder::prepareScaleDataIfNecessary()
281 m_scaledColumns.clear();
282 m_scaledRows.clear();
284 int width = size().width();
285 int height = size().height();
286 int numPixels = height * width;
287 if (m_maxNumPixels <= 0 || numPixels <= m_maxNumPixels)
291 double scale = sqrt(m_maxNumPixels / (double)numPixels);
292 fillScaledValues(m_scaledColumns, scale, width);
293 fillScaledValues(m_scaledRows, scale, height);
296 int ImageDecoder::upperBoundScaledX(int origX, int searchStart)
298 return getScaledValue<UpperBound>(m_scaledColumns, origX, searchStart);
301 int ImageDecoder::lowerBoundScaledX(int origX, int searchStart)
303 return getScaledValue<LowerBound>(m_scaledColumns, origX, searchStart);
306 int ImageDecoder::upperBoundScaledY(int origY, int searchStart)
308 return getScaledValue<UpperBound>(m_scaledRows, origY, searchStart);
311 int ImageDecoder::lowerBoundScaledY(int origY, int searchStart)
313 return getScaledValue<LowerBound>(m_scaledRows, origY, searchStart);
316 int ImageDecoder::scaledY(int origY, int searchStart)
318 return getScaledValue<Exact>(m_scaledRows, origY, searchStart);