summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/image-decoders
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2009-08-18 15:36:45 +0100
committerBen Murdoch <benm@google.com>2009-08-18 19:20:06 +0100
commitd227fc870c7a697500a3c900c31baf05fb9a8524 (patch)
treea3fa109aa5bf52fef562ac49d97a2f723889cc71 /WebCore/platform/image-decoders
parentf2c627513266faa73f7669058d98c60769fb3524 (diff)
downloadexternal_webkit-d227fc870c7a697500a3c900c31baf05fb9a8524.zip
external_webkit-d227fc870c7a697500a3c900c31baf05fb9a8524.tar.gz
external_webkit-d227fc870c7a697500a3c900c31baf05fb9a8524.tar.bz2
Merge WebKit r47420
Diffstat (limited to 'WebCore/platform/image-decoders')
-rw-r--r--WebCore/platform/image-decoders/ImageDecoder.cpp103
-rw-r--r--WebCore/platform/image-decoders/ImageDecoder.h30
-rw-r--r--WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp17
-rw-r--r--WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.cpp86
-rw-r--r--WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.h11
-rw-r--r--WebCore/platform/image-decoders/png/PNGImageDecoder.cpp36
6 files changed, 253 insertions, 30 deletions
diff --git a/WebCore/platform/image-decoders/ImageDecoder.cpp b/WebCore/platform/image-decoders/ImageDecoder.cpp
new file mode 100644
index 0000000..9c723cc
--- /dev/null
+++ b/WebCore/platform/image-decoders/ImageDecoder.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2008-2009 Torch Mobile, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+
+#include "config.h"
+#include "ImageDecoder.h"
+
+#include <algorithm>
+
+namespace WebCore {
+
+namespace {
+
+enum MatchType {
+ Exact,
+ UpperBound,
+ LowerBound
+};
+
+}
+
+template <MatchType type> static int getScaledValue(const Vector<int>& scaledValues, int valueToMatch, int searchStart)
+{
+ const int* dataStart = scaledValues.data();
+ const int* dataEnd = dataStart + scaledValues.size();
+ const int* matched = std::lower_bound(dataStart + searchStart, dataEnd, valueToMatch);
+ switch (type) {
+ case Exact:
+ return matched != dataEnd && *matched == valueToMatch ? matched - dataStart : -1;
+ case LowerBound:
+ return matched != dataEnd && *matched == valueToMatch ? matched - dataStart : matched - dataStart - 1;
+ case UpperBound:
+ default:
+ return matched != dataEnd ? matched - dataStart : -1;
+ }
+}
+
+int ImageDecoder::upperBoundScaledX(int origX, int searchStart)
+{
+ return getScaledValue<UpperBound>(m_scaledColumns, origX, searchStart);
+}
+
+int ImageDecoder::lowerBoundScaledX(int origX, int searchStart)
+{
+ return getScaledValue<LowerBound>(m_scaledColumns, origX, searchStart);
+}
+
+int ImageDecoder::scaledY(int origY, int searchStart)
+{
+ return getScaledValue<Exact>(m_scaledRows, origY, searchStart);
+}
+
+static inline void fillScaledValues(Vector<int>& scaledValues, double scaleRate, int length)
+{
+ double inflateRate = 1. / scaleRate;
+ scaledValues.reserveCapacity(static_cast<int>(length * scaleRate + 0.5));
+ for (int scaledIndex = 0;;) {
+ int index = static_cast<int>(scaledIndex * inflateRate + 0.5);
+ if (index < length) {
+ scaledValues.append(index);
+ ++scaledIndex;
+ } else
+ break;
+ }
+}
+
+void ImageDecoder::prepareScaleDataIfNecessary()
+{
+ int width = m_size.width();
+ int height = m_size.height();
+ int numPixels = height * width;
+ if (m_maxNumPixels <= 0 || numPixels <= m_maxNumPixels) {
+ m_scaled = false;
+ return;
+ }
+
+ m_scaled = true;
+ double scale = sqrt(m_maxNumPixels / (double)numPixels);
+ fillScaledValues(m_scaledColumns, scale, width);
+ fillScaledValues(m_scaledRows, scale, height);
+}
+
+}
+
+#endif // ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
diff --git a/WebCore/platform/image-decoders/ImageDecoder.h b/WebCore/platform/image-decoders/ImageDecoder.h
index 57f8735..9256afe 100644
--- a/WebCore/platform/image-decoders/ImageDecoder.h
+++ b/WebCore/platform/image-decoders/ImageDecoder.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (C) 2008-2009 Torch Mobile, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -140,7 +141,7 @@ namespace WebCore {
inline PixelData* getAddr(int x, int y)
{
-#if PLATFORM(CAIRO) || PLATFORM(WX)
+#if PLATFORM(CAIRO) || PLATFORM(WX) || PLATFORM(HAIKU)
return m_bytes.data() + (y * width()) + x;
#elif (PLATFORM(SKIA) || PLATFORM(SGL))
return m_bitmap.getAddr32(x, y);
@@ -166,7 +167,7 @@ namespace WebCore {
}
}
-#if PLATFORM(CAIRO) || PLATFORM(WX)
+#if PLATFORM(CAIRO) || PLATFORM(WX) || PLATFORM(HAIKU)
Vector<PixelData> m_bytes;
IntSize m_size; // The size of the buffer. This should be the
// same as ImageDecoder::m_size.
@@ -188,9 +189,17 @@ namespace WebCore {
// and the base class manages the RGBA32 frame cache.
class ImageDecoder {
public:
+ // ENABLE(IMAGE_DECODER_DOWN_SAMPLING) allows image decoders to write directly to
+ // scaled output buffers by down sampling. Call setMaxNumPixels() to specify the
+ // biggest size that decoded images can have. Image decoders will deflate those
+ // images that are bigger than m_maxNumPixels. (Not supported by all image decoders yet)
ImageDecoder()
: m_failed(false)
, m_sizeAvailable(false)
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ , m_maxNumPixels(-1)
+ , m_scaled(false)
+#endif
{
}
@@ -272,8 +281,25 @@ namespace WebCore {
// since in practice only GIFs will ever use this.
virtual void clearFrameBufferCache(size_t clearBeforeFrame) { }
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ void setMaxNumPixels(int m) { m_maxNumPixels = m; }
+#endif
+
protected:
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ void prepareScaleDataIfNecessary();
+ int upperBoundScaledX(int origX, int searchStart = 0);
+ int lowerBoundScaledX(int origX, int searchStart = 0);
+ int scaledY(int origY, int searchStart = 0);
+#endif
+
RefPtr<SharedBuffer> m_data; // The encoded data.
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ int m_maxNumPixels;
+ Vector<int> m_scaledColumns;
+ Vector<int> m_scaledRows;
+ bool m_scaled;
+#endif
Vector<RGBA32Buffer> m_frameBufferCache;
bool m_failed;
diff --git a/WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp b/WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp
index e9296ad..bc4f357 100644
--- a/WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp
+++ b/WebCore/platform/image-decoders/ico/ICOImageDecoder.cpp
@@ -249,7 +249,7 @@ bool ICOImageDecoder::processDirectory()
ICON = 1,
CURSOR = 2,
};
- if (((fileType != ICON) && (fileType != CURSOR)) || (idCount == 0)) {
+ if (((fileType != ICON) && (fileType != CURSOR)) || (!idCount)) {
setFailed();
return false;
}
@@ -303,10 +303,10 @@ ICOImageDecoder::IconDirectoryEntry ICOImageDecoder::readDirectoryEntry()
// matching uint8_ts) is so we can record dimensions of size 256 (which is
// what a zero byte really means).
int width = static_cast<uint8_t>(m_data->data()[m_decodedOffset]);
- if (width == 0)
+ if (!width)
width = 256;
int height = static_cast<uint8_t>(m_data->data()[m_decodedOffset + 1]);
- if (height == 0)
+ if (!height)
height = 256;
IconDirectoryEntry entry;
entry.m_size = IntSize(width, height);
@@ -318,11 +318,12 @@ ICOImageDecoder::IconDirectoryEntry ICOImageDecoder::readDirectoryEntry()
// this isn't quite what the bitmap info header says later, as we only use
// this value to determine which icon entry is best.
if (!entry.m_bitCount) {
- uint8_t colorCount = m_data->data()[m_decodedOffset + 2];
- if (colorCount) {
- for (--colorCount; colorCount; colorCount >>= 1)
- ++entry.m_bitCount;
- }
+ int colorCount =
+ static_cast<uint8_t>(m_data->data()[m_decodedOffset + 2]);
+ if (!colorCount)
+ colorCount = 256; // Vague in the spec, needed by real-world icons.
+ for (--colorCount; colorCount; colorCount >>= 1)
+ ++entry.m_bitCount;
}
m_decodedOffset += sizeOfDirEntry;
diff --git a/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.cpp b/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.cpp
index ae09586..2565ea6 100644
--- a/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.cpp
+++ b/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.cpp
@@ -461,11 +461,27 @@ void JPEGImageDecoder::decode(bool sizeOnly)
}
}
-static void convertCMYKToRGBA(RGBA32Buffer& dest, JSAMPROW src, jpeg_decompress_struct* info)
+static void convertCMYKToRGBA(RGBA32Buffer& dest, int destY, JSAMPROW src, int srcWidth
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ , bool scaled, const Vector<int>& scaledColumns
+#endif
+ )
{
- ASSERT(info->out_color_space == JCS_CMYK);
-
- for (unsigned x = 0; x < info->output_width; ++x) {
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ if (scaled) {
+ int numColumns = scaledColumns.size();
+ for (int x = 0; x < numColumns; ++x) {
+ JSAMPLE* jsample = src + scaledColumns[x] * 3;
+ unsigned c = jsample[0];
+ unsigned m = jsample[1];
+ unsigned y = jsample[2];
+ unsigned k = jsample[3];
+ dest.setRGBA(x, destY, c * k / 255, m * k / 255, y * k / 255, 0xFF);
+ }
+ return;
+ }
+#endif
+ for (unsigned x = 0; x < srcWidth; ++x) {
unsigned c = *src++;
unsigned m = *src++;
unsigned y = *src++;
@@ -489,23 +505,31 @@ static void convertCMYKToRGBA(RGBA32Buffer& dest, JSAMPROW src, jpeg_decompress_
// G = 1 - M => 1 - (1 - iM*iK) => iM*iK
// B = 1 - Y => 1 - (1 - iY*iK) => iY*iK
- // read_scanlines has increased the scanline counter, so we
- // actually mean the previous one.
- dest.setRGBA(x, info->output_scanline - 1, c * k / 255, m * k / 255, y * k / 255, 0xFF);
+ dest.setRGBA(x, destY, c * k / 255, m * k / 255, y * k / 255, 0xFF);
}
}
-static void convertRGBToRGBA(RGBA32Buffer& dest, JSAMPROW src, jpeg_decompress_struct* info)
+static void convertRGBToRGBA(RGBA32Buffer& dest, int destY, JSAMPROW src, int srcWidth
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ , bool scaled, const Vector<int>& scaledColumns
+#endif
+ )
{
- ASSERT(info->out_color_space == JCS_RGB);
-
- for (unsigned x = 0; x < info->output_width; ++x) {
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ if (scaled) {
+ int numColumns = scaledColumns.size();
+ for (int x = 0; x < numColumns; ++x) {
+ JSAMPLE* jsample = src + scaledColumns[x] * 3;
+ dest.setRGBA(x, destY, jsample[0], jsample[1], jsample[2], 0xFF);
+ }
+ return;
+ }
+#endif
+ for (unsigned x = 0; x < srcWidth; ++x) {
unsigned r = *src++;
unsigned g = *src++;
unsigned b = *src++;
- // read_scanlines has increased the scanline counter, so we
- // actually mean the previous one.
- dest.setRGBA(x, info->output_scanline - 1, r, g, b, 0xFF);
+ dest.setRGBA(x, destY, r, g, b, 0xFF);
}
}
@@ -517,7 +541,17 @@ bool JPEGImageDecoder::outputScanlines()
// Initialize the framebuffer if needed.
RGBA32Buffer& buffer = m_frameBufferCache[0];
if (buffer.status() == RGBA32Buffer::FrameEmpty) {
- if (!buffer.setSize(size().width(), size().height())) {
+ int bufferWidth = size().width();
+ int bufferHeight = size().height();
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ // Let's resize our buffer now to the correct width/height.
+ if (m_scaled) {
+ bufferWidth = m_scaledColumns.size();
+ bufferHeight = m_scaledRows.size();
+ }
+#endif
+
+ if (!buffer.setSize(bufferWidth, bufferHeight)) {
m_failed = true;
return false;
}
@@ -532,16 +566,34 @@ bool JPEGImageDecoder::outputScanlines()
JSAMPARRAY samples = m_reader->samples();
while (info->output_scanline < info->output_height) {
+ // jpeg_read_scanlines will increase the scanline counter, so we
+ // save the scanline before calling it.
+ int sourceY = info->output_scanline;
/* Request one scanline. Returns 0 or 1 scanlines. */
if (jpeg_read_scanlines(info, samples, 1) != 1)
return false;
+ int destY = sourceY;
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ if (m_scaled) {
+ destY = scaledY(sourceY);
+ if (destY < 0)
+ continue;
+ }
if (info->out_color_space == JCS_RGB)
- convertRGBToRGBA(buffer, *samples, info);
+ convertRGBToRGBA(buffer, destY, *samples, info->output_width, m_scaled, m_scaledColumns);
else if (info->out_color_space == JCS_CMYK)
- convertCMYKToRGBA(buffer, *samples, info);
+ convertCMYKToRGBA(buffer, destY, *samples, info->output_width, m_scaled, m_scaledColumns);
else
return false;
+#else
+ if (info->out_color_space == JCS_RGB)
+ convertRGBToRGBA(buffer, destY, *samples, info->output_width);
+ else if (info->out_color_space == JCS_CMYK)
+ convertCMYKToRGBA(buffer, destY, *samples, info->output_width);
+ else
+ return false;
+#endif
}
return true;
diff --git a/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.h b/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.h
index 56e007d..4a822d7 100644
--- a/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.h
+++ b/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (C) 2008-2009 Torch Mobile, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -57,6 +58,16 @@ namespace WebCore {
bool outputScanlines();
void jpegComplete();
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ bool setSize(int width, int height)
+ {
+ if (!ImageDecoder::setSize(width, height))
+ return false;
+ prepareScaleDataIfNecessary();
+ return true;
+ }
+#endif
+
private:
JPEGImageReader* m_reader;
};
diff --git a/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp b/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp
index d14333f..ad79fc8 100644
--- a/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp
+++ b/WebCore/platform/image-decoders/png/PNGImageDecoder.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2006 Apple Computer, Inc.
+ * Copyright (C) 2007-2009 Torch Mobile, Inc.
*
* Portions are Copyright (C) 2001 mozilla.org
*
@@ -242,6 +243,9 @@ void PNGImageDecoder::headerAvailable()
longjmp(png->jmpbuf, 1);
return;
}
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ prepareScaleDataIfNecessary();
+#endif
}
int bitDepth, colorType, interlaceType, compressionType, filterType, channels;
@@ -313,7 +317,14 @@ void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex,
// Initialize the framebuffer if needed.
RGBA32Buffer& buffer = m_frameBufferCache[0];
if (buffer.status() == RGBA32Buffer::FrameEmpty) {
- if (!buffer.setSize(size().width(), size().height())) {
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ int width = m_scaled ? m_scaledColumns.size() : size().width();
+ int height = m_scaled ? m_scaledRows.size() : size().height();
+#else
+ int width = size().width();
+ int height = size().height();
+#endif
+ if (!buffer.setSize(width, height)) {
static_cast<PNGImageDecoder*>(png_get_progressive_ptr(reader()->pngPtr()))->decodingFailed();
longjmp(reader()->pngPtr()->jmpbuf, 1);
return;
@@ -358,7 +369,7 @@ void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex,
* to pass the current row, and the function will combine the
* old row and the new row.
*/
-
+
png_structp png = reader()->pngPtr();
bool hasAlpha = reader()->hasAlpha();
unsigned colorChannels = hasAlpha ? 4 : 3;
@@ -372,8 +383,27 @@ void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex,
row = rowBuffer;
// Copy the data into our buffer.
+#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
+ if (m_scaled) {
+ int destY = scaledY(rowIndex);
+ if (destY < 0)
+ return;
+ int columns = m_scaledColumns.size();
+ bool sawAlpha = buffer.hasAlpha();
+ for (int x = 0; x < columns; ++x) {
+ png_bytep pixel = row + m_scaledColumns[x] * 4;
+ unsigned alpha = pixel[3];
+ buffer.setRGBA(x, destY, pixel[0], pixel[1], pixel[2], alpha);
+ if (!sawAlpha && alpha < 255) {
+ sawAlpha = true;
+ buffer.setHasAlpha(true);
+ }
+ }
+ return;
+ }
+#endif
int width = size().width();
- bool sawAlpha = false;
+ bool sawAlpha = buffer.hasAlpha();
for (int x = 0; x < width; x++) {
unsigned red = *row++;
unsigned green = *row++;