Commit 910636f4 authored by pkasting@chromium.org's avatar pkasting@chromium.org
Browse files

Code cleanup in ImageDecoders; no functional change.

https://bugs.webkit.org/show_bug.cgi?id=28751

Reviewed by Adam Barth.

* Make code mostly comply with the style guide (switch indenting, use of
  "!" vs. "== 0", don't unnecessarily wrap lines, etc.)
* Eliminate m_allDataReceived in ICO/BMP decoders since it's in the
  base ImageDecoder class
* Remove some useless or wrong comments, add others
* Wrap comments at 80 columns (most already were) for
  consistency/readability
* Avoid casts by using correct variable types
* Consistent naming of decode() function/args
* Shorter code in some places
* Make declaration and definition order match

* platform/graphics/qt/ImageDecoderQt.cpp:
(WebCore::ImageDecoderQt::frameCount):
(WebCore::ImageDecoderQt::frameBufferAtIndex):
(WebCore::ImageDecoderQt::forceLoadEverything):
* platform/image-decoders/ImageDecoder.cpp:
(WebCore::ImageDecoder::create):
(WebCore::RGBA32Buffer::clear):
(WebCore::RGBA32Buffer::setSize):
* platform/image-decoders/ImageDecoder.h:
(WebCore::RGBA32Buffer::):
(WebCore::ImageDecoder::ImageDecoder):
* platform/image-decoders/bmp/BMPImageDecoder.cpp:
(WebCore::BMPImageDecoder::BMPImageDecoder):
(WebCore::BMPImageDecoder::setData):
(WebCore::BMPImageDecoder::isSizeAvailable):
(WebCore::BMPImageDecoder::frameBufferAtIndex):
(WebCore::BMPImageDecoder::decode):
(WebCore::BMPImageDecoder::decodeHelper):
(WebCore::BMPImageDecoder::processFileHeader):
* platform/image-decoders/bmp/BMPImageDecoder.h:
(WebCore::BMPImageDecoder::readUint32):
* platform/image-decoders/bmp/BMPImageReader.cpp:
(WebCore::BMPImageReader::BMPImageReader):
(WebCore::BMPImageReader::decodeBMP):
(WebCore::BMPImageReader::readInfoHeaderSize):
(WebCore::BMPImageReader::processInfoHeader):
(WebCore::BMPImageReader::readInfoHeader):
(WebCore::BMPImageReader::isInfoHeaderValid):
(WebCore::BMPImageReader::processBitmasks):
(WebCore::BMPImageReader::processColorTable):
(WebCore::BMPImageReader::processRLEData):
(WebCore::BMPImageReader::processNonRLEData):
* platform/image-decoders/bmp/BMPImageReader.h:
(WebCore::BMPImageReader::readUint32):
(WebCore::BMPImageReader::pastEndOfImage):
(WebCore::BMPImageReader::readCurrentPixel):
(WebCore::BMPImageReader::getComponent):
(WebCore::BMPImageReader::getAlpha):
(WebCore::BMPImageReader::setI):
(WebCore::BMPImageReader::setRGBA):
* platform/image-decoders/gif/GIFImageDecoder.cpp:
(WebCore::GIFImageDecoder::GIFImageDecoder):
(WebCore::GIFImageDecoder::setData):
(WebCore::GIFImageDecoder::isSizeAvailable):
(WebCore::GIFImageDecoder::frameCount):
(WebCore::GIFImageDecoder::frameBufferAtIndex):
(WebCore::GIFImageDecoder::clearFrameBufferCache):
(WebCore::GIFImageDecoder::sizeNowAvailable):
(WebCore::GIFImageDecoder::haveDecodedRow):
(WebCore::GIFImageDecoder::frameComplete):
(WebCore::GIFImageDecoder::decode):
(WebCore::GIFImageDecoder::initFrameBuffer):
* platform/image-decoders/gif/GIFImageDecoder.h:
(WebCore::GIFImageDecoder::):
(WebCore::GIFImageDecoder::filenameExtension):
* platform/image-decoders/gif/GIFImageReader.cpp:
(GIFImageReader::do_lzw):
(GIFImageReader::read):
* platform/image-decoders/gif/GIFImageReader.h:
(GIFImageReader::~GIFImageReader):
* platform/image-decoders/ico/ICOImageDecoder.cpp:
(WebCore::ICOImageDecoder::ICOImageDecoder):
(WebCore::ICOImageDecoder::~ICOImageDecoder):
(WebCore::ICOImageDecoder::setData):
(WebCore::ICOImageDecoder::isSizeAvailable):
(WebCore::ICOImageDecoder::frameSizeAtIndex):
(WebCore::ICOImageDecoder::frameCount):
(WebCore::ICOImageDecoder::frameBufferAtIndex):
(WebCore::ICOImageDecoder::compareEntries):
(WebCore::ICOImageDecoder::setDataForPNGDecoderAtIndex):
(WebCore::ICOImageDecoder::decode):
(WebCore::ICOImageDecoder::decodeDirectory):
(WebCore::ICOImageDecoder::decodeAtIndex):
(WebCore::ICOImageDecoder::processDirectoryEntries):
(WebCore::ICOImageDecoder::readDirectoryEntry):
* platform/image-decoders/ico/ICOImageDecoder.h:
(WebCore::ICOImageDecoder::readUint16):
(WebCore::ICOImageDecoder::readUint32):
* platform/image-decoders/jpeg/JPEGImageDecoder.cpp:
(WebCore::):
(WebCore::JPEGImageReader::JPEGImageReader):
(WebCore::JPEGImageReader::skipBytes):
(WebCore::JPEGImageReader::decode):
(WebCore::error_exit):
(WebCore::fill_input_buffer):
(WebCore::term_source):
(WebCore::JPEGImageDecoder::setData):
(WebCore::JPEGImageDecoder::setSize):
(WebCore::JPEGImageDecoder::frameBufferAtIndex):
(WebCore::JPEGImageDecoder::jpegComplete):
(WebCore::JPEGImageDecoder::decode):
* platform/image-decoders/jpeg/JPEGImageDecoder.h:
(WebCore::JPEGImageDecoder::filenameExtension):
* platform/image-decoders/png/PNGImageDecoder.cpp:
(WebCore::decodingFailed):
(WebCore::decodingWarning):
(WebCore::headerAvailable):
(WebCore::rowAvailable):
(WebCore::pngComplete):
(WebCore::PNGImageReader::PNGImageReader):
(WebCore::PNGImageReader::close):
(WebCore::PNGImageReader::decode):
(WebCore::PNGImageReader::createInterlaceBuffer):
(WebCore::PNGImageDecoder::setData):
(WebCore::PNGImageDecoder::frameBufferAtIndex):
(WebCore::PNGImageDecoder::headerAvailable):
(WebCore::PNGImageDecoder::rowAvailable):
(WebCore::PNGImageDecoder::pngComplete):
(WebCore::PNGImageDecoder::decode):
* platform/image-decoders/png/PNGImageDecoder.h:
(WebCore::PNGImageDecoder::filenameExtension):
* platform/image-decoders/qt/RGBA32BufferQt.cpp:
(WebCore::RGBA32Buffer::setDecodedImage):



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@54823 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 5eb4f13a
2010-02-12 Peter Kasting <pkasting@google.com>
Reviewed by Adam Barth.
Code cleanup in ImageDecoders; no functional change.
https://bugs.webkit.org/show_bug.cgi?id=28751
* Make code mostly comply with the style guide (switch indenting, use of
"!" vs. "== 0", don't unnecessarily wrap lines, etc.)
* Eliminate m_allDataReceived in ICO/BMP decoders since it's in the
base ImageDecoder class
* Remove some useless or wrong comments, add others
* Wrap comments at 80 columns (most already were) for
consistency/readability
* Avoid casts by using correct variable types
* Consistent naming of decode() function/args
* Shorter code in some places
* Make declaration and definition order match
* platform/graphics/qt/ImageDecoderQt.cpp:
(WebCore::ImageDecoderQt::frameCount):
(WebCore::ImageDecoderQt::frameBufferAtIndex):
(WebCore::ImageDecoderQt::forceLoadEverything):
* platform/image-decoders/ImageDecoder.cpp:
(WebCore::ImageDecoder::create):
(WebCore::RGBA32Buffer::clear):
(WebCore::RGBA32Buffer::setSize):
* platform/image-decoders/ImageDecoder.h:
(WebCore::RGBA32Buffer::):
(WebCore::ImageDecoder::ImageDecoder):
* platform/image-decoders/bmp/BMPImageDecoder.cpp:
(WebCore::BMPImageDecoder::BMPImageDecoder):
(WebCore::BMPImageDecoder::setData):
(WebCore::BMPImageDecoder::isSizeAvailable):
(WebCore::BMPImageDecoder::frameBufferAtIndex):
(WebCore::BMPImageDecoder::decode):
(WebCore::BMPImageDecoder::decodeHelper):
(WebCore::BMPImageDecoder::processFileHeader):
* platform/image-decoders/bmp/BMPImageDecoder.h:
(WebCore::BMPImageDecoder::readUint32):
* platform/image-decoders/bmp/BMPImageReader.cpp:
(WebCore::BMPImageReader::BMPImageReader):
(WebCore::BMPImageReader::decodeBMP):
(WebCore::BMPImageReader::readInfoHeaderSize):
(WebCore::BMPImageReader::processInfoHeader):
(WebCore::BMPImageReader::readInfoHeader):
(WebCore::BMPImageReader::isInfoHeaderValid):
(WebCore::BMPImageReader::processBitmasks):
(WebCore::BMPImageReader::processColorTable):
(WebCore::BMPImageReader::processRLEData):
(WebCore::BMPImageReader::processNonRLEData):
* platform/image-decoders/bmp/BMPImageReader.h:
(WebCore::BMPImageReader::readUint32):
(WebCore::BMPImageReader::pastEndOfImage):
(WebCore::BMPImageReader::readCurrentPixel):
(WebCore::BMPImageReader::getComponent):
(WebCore::BMPImageReader::getAlpha):
(WebCore::BMPImageReader::setI):
(WebCore::BMPImageReader::setRGBA):
* platform/image-decoders/gif/GIFImageDecoder.cpp:
(WebCore::GIFImageDecoder::GIFImageDecoder):
(WebCore::GIFImageDecoder::setData):
(WebCore::GIFImageDecoder::isSizeAvailable):
(WebCore::GIFImageDecoder::frameCount):
(WebCore::GIFImageDecoder::frameBufferAtIndex):
(WebCore::GIFImageDecoder::clearFrameBufferCache):
(WebCore::GIFImageDecoder::sizeNowAvailable):
(WebCore::GIFImageDecoder::haveDecodedRow):
(WebCore::GIFImageDecoder::frameComplete):
(WebCore::GIFImageDecoder::decode):
(WebCore::GIFImageDecoder::initFrameBuffer):
* platform/image-decoders/gif/GIFImageDecoder.h:
(WebCore::GIFImageDecoder::):
(WebCore::GIFImageDecoder::filenameExtension):
* platform/image-decoders/gif/GIFImageReader.cpp:
(GIFImageReader::do_lzw):
(GIFImageReader::read):
* platform/image-decoders/gif/GIFImageReader.h:
(GIFImageReader::~GIFImageReader):
* platform/image-decoders/ico/ICOImageDecoder.cpp:
(WebCore::ICOImageDecoder::ICOImageDecoder):
(WebCore::ICOImageDecoder::~ICOImageDecoder):
(WebCore::ICOImageDecoder::setData):
(WebCore::ICOImageDecoder::isSizeAvailable):
(WebCore::ICOImageDecoder::frameSizeAtIndex):
(WebCore::ICOImageDecoder::frameCount):
(WebCore::ICOImageDecoder::frameBufferAtIndex):
(WebCore::ICOImageDecoder::compareEntries):
(WebCore::ICOImageDecoder::setDataForPNGDecoderAtIndex):
(WebCore::ICOImageDecoder::decode):
(WebCore::ICOImageDecoder::decodeDirectory):
(WebCore::ICOImageDecoder::decodeAtIndex):
(WebCore::ICOImageDecoder::processDirectoryEntries):
(WebCore::ICOImageDecoder::readDirectoryEntry):
* platform/image-decoders/ico/ICOImageDecoder.h:
(WebCore::ICOImageDecoder::readUint16):
(WebCore::ICOImageDecoder::readUint32):
* platform/image-decoders/jpeg/JPEGImageDecoder.cpp:
(WebCore::):
(WebCore::JPEGImageReader::JPEGImageReader):
(WebCore::JPEGImageReader::skipBytes):
(WebCore::JPEGImageReader::decode):
(WebCore::error_exit):
(WebCore::fill_input_buffer):
(WebCore::term_source):
(WebCore::JPEGImageDecoder::setData):
(WebCore::JPEGImageDecoder::setSize):
(WebCore::JPEGImageDecoder::frameBufferAtIndex):
(WebCore::JPEGImageDecoder::jpegComplete):
(WebCore::JPEGImageDecoder::decode):
* platform/image-decoders/jpeg/JPEGImageDecoder.h:
(WebCore::JPEGImageDecoder::filenameExtension):
* platform/image-decoders/png/PNGImageDecoder.cpp:
(WebCore::decodingFailed):
(WebCore::decodingWarning):
(WebCore::headerAvailable):
(WebCore::rowAvailable):
(WebCore::pngComplete):
(WebCore::PNGImageReader::PNGImageReader):
(WebCore::PNGImageReader::close):
(WebCore::PNGImageReader::decode):
(WebCore::PNGImageReader::createInterlaceBuffer):
(WebCore::PNGImageDecoder::setData):
(WebCore::PNGImageDecoder::frameBufferAtIndex):
(WebCore::PNGImageDecoder::headerAvailable):
(WebCore::PNGImageDecoder::rowAvailable):
(WebCore::PNGImageDecoder::pngComplete):
(WebCore::PNGImageDecoder::decode):
* platform/image-decoders/png/PNGImageDecoder.h:
(WebCore::PNGImageDecoder::filenameExtension):
* platform/image-decoders/qt/RGBA32BufferQt.cpp:
(WebCore::RGBA32Buffer::setDecodedImage):
2010-02-16 Csaba Osztrogonác <ossy@webkit.org>
[Qt] Unreviewed buildfix.
......
......@@ -102,7 +102,7 @@ size_t ImageDecoderQt::frameCount()
// Fixup for Qt decoders... imageCount() is wrong
// and jumpToNextImage does not work either... so
// we will have to parse everything...
if (imageCount == 0)
if (!imageCount)
forceLoadEverything();
else
m_frameBufferCache.resize(imageCount);
......@@ -132,13 +132,13 @@ RGBA32Buffer* ImageDecoderQt::frameBufferAtIndex(size_t index)
// In case the ImageDecoderQt got recreated we don't know
// yet how many images we are going to have and need to
// find that out now.
int count = m_frameBufferCache.size();
if (!m_failed && count == 0) {
size_t count = m_frameBufferCache.size();
if (!m_failed && !count) {
internalDecodeSize();
count = frameCount();
}
if (index >= static_cast<size_t>(count))
if (index >= count)
return 0;
RGBA32Buffer& frame = m_frameBufferCache[index];
......@@ -215,7 +215,7 @@ void ImageDecoderQt::forceLoadEverything()
do {
m_frameBufferCache.resize(++imageCount);
internalHandleCurrentImage(imageCount - 1);
} while(!m_failed);
} while (!m_failed);
// If we failed decoding the first image we actually
// have no images and need to keep m_failed set to
......
......@@ -54,7 +54,8 @@ static unsigned copyFromSharedBuffer(char* buffer, unsigned bufferLength, const
ImageDecoder* ImageDecoder::create(const SharedBuffer& data)
{
// We need at least 4 bytes to figure out what kind of image we're dealing with.
// We need at least 4 bytes to figure out what kind of image we're dealing
// with.
static const unsigned maxMarkerLength = 4;
char contents[maxMarkerLength];
unsigned length = copyFromSharedBuffer(contents, maxMarkerLength, data, 0);
......@@ -68,16 +69,11 @@ ImageDecoder* ImageDecoder::create(const SharedBuffer& data)
return new GIFImageDecoder();
// Test for PNG.
if (uContents[0]==0x89 &&
uContents[1]==0x50 &&
uContents[2]==0x4E &&
uContents[3]==0x47)
if (!memcmp(contents, "\x89\x50\x4E\x47", 4))
return new PNGImageDecoder();
// JPEG
if (uContents[0]==0xFF &&
uContents[1]==0xD8 &&
uContents[2]==0xFF)
if (!memcmp(contents, "\xFF\xD8\xFF", 3))
return new JPEGImageDecoder();
// BMP
......@@ -86,8 +82,7 @@ ImageDecoder* ImageDecoder::create(const SharedBuffer& data)
// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
// CURs begin with 2-byte 0 followed by 2-byte 2.
if (!memcmp(contents, "\000\000\001\000", 4) ||
!memcmp(contents, "\000\000\002\000", 4))
if (!memcmp(contents, "\x00\x00\x01\x00", 4) || !memcmp(contents, "\x00\x00\x02\x00", 4))
return new ICOImageDecoder();
// Give up. We don't know what the heck this is.
......@@ -108,10 +103,10 @@ void RGBA32Buffer::clear()
{
m_bytes.clear();
m_status = FrameEmpty;
// NOTE: Do not reset other members here; clearFrameBufferCache()
// calls this to free the bitmap data, but other functions like
// initFrameBuffer() and frameComplete() may still need to read
// other metadata out of this frame later.
// NOTE: Do not reset other members here; clearFrameBufferCache() calls this
// to free the bitmap data, but other functions like initFrameBuffer() and
// frameComplete() may still need to read other metadata out of this frame
// later.
}
void RGBA32Buffer::zeroFill()
......@@ -132,8 +127,8 @@ void RGBA32Buffer::copyBitmapData(const RGBA32Buffer& other)
bool RGBA32Buffer::setSize(int newWidth, int newHeight)
{
// NOTE: This has no way to check for allocation failure if the
// requested size was too big...
// NOTE: This has no way to check for allocation failure if the requested
// size was too big...
m_bytes.resize(newWidth * newHeight);
m_size = IntSize(newWidth, newHeight);
......@@ -195,13 +190,11 @@ inline void fillScaledValues(Vector<int>& scaledValues, double scaleRate, int le
{
double inflateRate = 1. / scaleRate;
scaledValues.reserveCapacity(static_cast<int>(length * scaleRate + 0.5));
for (int scaledIndex = 0;;) {
for (int scaledIndex = 0; ; ++scaledIndex) {
int index = static_cast<int>(scaledIndex * inflateRate + 0.5);
if (index < length) {
scaledValues.append(index);
++scaledIndex;
} else
if (index >= length)
break;
scaledValues.append(index);
}
}
......
......@@ -45,18 +45,21 @@
namespace WebCore {
// The RGBA32Buffer object represents the decoded image data in RGBA32 format. This buffer is what all
// decoders write a single frame into. Frames are then instantiated for drawing by being handed this buffer.
// The RGBA32Buffer object represents the decoded image data in RGBA32
// format. This buffer is what all decoders write a single frame into.
// Frames are then instantiated for drawing by being handed this buffer.
class RGBA32Buffer {
public:
enum FrameStatus { FrameEmpty, FramePartial, FrameComplete };
enum FrameDisposalMethod {
// If you change the numeric values of these, make sure you audit all
// users, as some users may cast raw values to/from these constants.
DisposeNotSpecified, // Leave frame in framebuffer
DisposeKeep, // Leave frame in framebuffer
DisposeOverwriteBgcolor, // Clear frame to transparent
DisposeOverwritePrevious, // Clear frame to previous framebuffer contents
// If you change the numeric values of these, make sure you audit
// all users, as some users may cast raw values to/from these
// constants.
DisposeNotSpecified, // Leave frame in framebuffer
DisposeKeep, // Leave frame in framebuffer
DisposeOverwriteBgcolor, // Clear frame to transparent
DisposeOverwritePrevious, // Clear frame to previous framebuffer
// contents
};
#if PLATFORM(SKIA) || PLATFORM(QT)
typedef uint32_t PixelData;
......@@ -178,26 +181,33 @@ namespace WebCore {
Vector<PixelData> m_bytes;
IntSize m_size; // The size of the buffer. This should be the
// same as ImageDecoder::m_size.
bool m_hasAlpha; // Whether or not any of the pixels in the buffer have transparency.
bool m_hasAlpha; // Whether or not any of the pixels in the buffer
// have transparency.
#endif
IntRect m_rect; // The rect of the original specified frame within the overall buffer.
// This will always just be the entire buffer except for GIF frames
// whose original rect was smaller than the overall image size.
FrameStatus m_status; // Whether or not this frame is completely finished decoding.
IntRect m_rect; // The rect of the original specified frame within
// the overall buffer. This will always just be
// the entire buffer except for GIF frames whose
// original rect was smaller than the overall
// image size.
FrameStatus m_status; // Whether or not this frame is completely
// finished decoding.
unsigned m_duration; // The animation delay.
FrameDisposalMethod m_disposalMethod;
// What to do with this frame's data when initializing the next frame.
// What to do with this frame's data when
// initializing the next frame.
};
// The ImageDecoder class represents a base class for specific image format decoders
// (e.g., GIF, JPG, PNG, ICO) to derive from. All decoders decode into RGBA32 format
// and the base class manages the RGBA32 frame cache.
// The ImageDecoder class represents a base class for specific image format
// decoders (e.g., GIF, JPG, PNG, ICO) to derive from. All decoders decode
// into RGBA32 format and the base class manages the RGBA32 frame cache.
//
// 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)
class ImageDecoder : public Noncopyable {
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_scaled(false)
, m_failed(false)
......@@ -214,21 +224,22 @@ namespace WebCore {
// needing to write a dedicated setData() implementation.
static ImageDecoder* create(const SharedBuffer& data);
// The the filename extension usually associated with an undecoded image of this type.
// The the filename extension usually associated with an undecoded image
// of this type.
virtual String filenameExtension() const = 0;
// All specific decoder plugins must do something with the data they are given.
bool isAllDataReceived() const { return m_isAllDataReceived; }
virtual void setData(SharedBuffer* data, bool allDataReceived)
{
m_data = data;
m_isAllDataReceived = allDataReceived;
}
// Whether or not the size information has been decoded yet. This default
// implementation just returns true if the size has been set and we have not
// seen a failure. Decoders may want to override this to lazily decode
// enough of the image to get the size.
// Whether or not the size information has been decoded yet. This
// default implementation just returns true if the size has been set and
// we have not seen a failure. Decoders may want to override this to
// lazily decode enough of the image to get the size.
virtual bool isSizeAvailable()
{
return !m_failed && m_sizeAvailable;
......@@ -256,10 +267,10 @@ namespace WebCore {
return size();
}
// Called by the image decoders to set their decoded size, this also check
// the size for validity. It will return true if the size was set, or false
// if there is an error. On error, the m_failed flag will be set and the
// caller should immediately stop decoding.
// Called by the image decoders to set their decoded size, this also
// checks the size for validity. It will return true if the size was
// set, or false if there is an error. On error, the m_failed flag will
// be set and the caller should immediately stop decoding.
virtual bool setSize(unsigned width, unsigned height)
{
if (isOverSize(width, height)) {
......@@ -271,29 +282,30 @@ namespace WebCore {
return true;
}
// The total number of frames for the image. Classes that support multiple frames
// will scan the image data for the answer if they need to (without necessarily
// decoding all of the individual frames).
// The total number of frames for the image. Classes that support
// multiple frames will scan the image data for the answer if they need
// to (without necessarily decoding all of the individual frames).
virtual size_t frameCount() { return 1; }
// The number of repetitions to perform for an animation loop.
virtual int repetitionCount() const { return cAnimationNone; }
// Called to obtain the RGBA32Buffer full of decoded data for rendering. The
// decoder plugin will decode as much of the frame as it can before handing
// back the buffer.
// Called to obtain the RGBA32Buffer full of decoded data for rendering.
// The decoder plugin will decode as much of the frame as it can before
// handing back the buffer.
virtual RGBA32Buffer* frameBufferAtIndex(size_t) = 0;
// Whether or not the underlying image format even supports alpha transparency.
// Whether or not the underlying image format even supports alpha
// transparency.
virtual bool supportsAlpha() const { return true; }
bool failed() const { return m_failed; }
void setFailed() { m_failed = true; }
// Wipe out frames in the frame buffer cache before |clearBeforeFrame|,
// assuming this can be done without breaking decoding. Different decoders
// place different restrictions on what frames are safe to destroy, so this
// is left to them to implement.
// assuming this can be done without breaking decoding. Different
// decoders place different restrictions on what frames are safe to
// destroy, so this is left to them to implement.
// For convenience's sake, we provide a default (empty) implementation,
// since in practice only GIFs will ever use this.
virtual void clearFrameBufferCache(size_t clearBeforeFrame) { }
......
......@@ -41,9 +41,7 @@ namespace WebCore {
static const size_t sizeOfFileHeader = 14;
BMPImageDecoder::BMPImageDecoder()
: ImageDecoder()
, m_allDataReceived(false)
, m_decodedOffset(0)
: m_decodedOffset(0)
{
}
......@@ -53,7 +51,6 @@ void BMPImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
return;
ImageDecoder::setData(data, allDataReceived);
m_allDataReceived = allDataReceived;
if (m_reader)
m_reader->setData(data);
}
......@@ -61,7 +58,7 @@ void BMPImageDecoder::setData(SharedBuffer* data, bool allDataReceived)
bool BMPImageDecoder::isSizeAvailable()
{
if (!ImageDecoder::isSizeAvailable() && !failed())
decodeWithCheckForDataEnded(true);
decode(true);
return ImageDecoder::isSizeAvailable();
}
......@@ -76,31 +73,29 @@ RGBA32Buffer* BMPImageDecoder::frameBufferAtIndex(size_t index)
RGBA32Buffer* buffer = &m_frameBufferCache.first();
if (buffer->status() != RGBA32Buffer::FrameComplete && !failed())
decodeWithCheckForDataEnded(false);
decode(false);
return buffer;
}
void BMPImageDecoder::decodeWithCheckForDataEnded(bool onlySize)
void BMPImageDecoder::decode(bool onlySize)
{
if (failed())
return;
// If we couldn't decode the image but we've received all the data, decoding
// has failed.
if (!decode(onlySize) && m_allDataReceived)
if (!decodeHelper(onlySize) && isAllDataReceived())
setFailed();
}
bool BMPImageDecoder::decode(bool onlySize)
bool BMPImageDecoder::decodeHelper(bool onlySize)
{
size_t imgDataOffset = 0;
if ((m_decodedOffset < sizeOfFileHeader)
&& !processFileHeader(&imgDataOffset))
if ((m_decodedOffset < sizeOfFileHeader) && !processFileHeader(&imgDataOffset))
return false;
if (!m_reader) {
m_reader.set(new BMPImageReader(this, m_decodedOffset, imgDataOffset,
false));
m_reader.set(new BMPImageReader(this, m_decodedOffset, imgDataOffset, false));
m_reader->setData(m_data.get());
}
......@@ -118,8 +113,7 @@ bool BMPImageDecoder::processFileHeader(size_t* imgDataOffset)
ASSERT(!m_decodedOffset);
if (m_data->size() < sizeOfFileHeader)
return false;
const uint16_t fileType =
(m_data->data()[0] << 8) | static_cast<uint8_t>(m_data->data()[1]);
const uint16_t fileType = (m_data->data()[0] << 8) | static_cast<uint8_t>(m_data->data()[1]);
*imgDataOffset = readUint32(10);
m_decodedOffset = sizeOfFileHeader;
......
......@@ -50,29 +50,23 @@ namespace WebCore {
private:
inline uint32_t readUint32(int offset) const
{
return BMPImageReader::readUint32(m_data.get(),
m_decodedOffset + offset);
return BMPImageReader::readUint32(m_data.get(), m_decodedOffset + offset);
}
// Decodes the image. If |onlySize| is true, stops decoding after
// calculating the image size. If decoding fails but there is no more
// data coming, sets the "decode failure" flag.
void decodeWithCheckForDataEnded(bool onlySize);
void decode(bool onlySize);
// Decodes the image. If |onlySize| is true, stops decoding after
// calculating the image size. Returns whether decoding succeeded.
// NOTE: Used internally by decodeWithCheckForDataEnded(). Other people
// should not call this.
bool decode(bool onlySize);
bool decodeHelper(bool onlySize);
// Processes the file header at the beginning of the data. Sets
// |*imgDataOffset| based on the header contents. Returns true if the
// file header could be decoded.
bool processFileHeader(size_t* imgDataOffset);
// True if we've seen all the data.
bool m_allDataReceived;
// An index into |m_data| representing how much we've already decoded.
// Note that this only tracks data _this_ class decodes; once the
// BMPImageReader takes over this will not be updated further.
......
......@@ -33,10 +33,7 @@
namespace WebCore {
BMPImageReader::BMPImageReader(ImageDecoder* parent,
size_t decodedAndHeaderOffset,
size_t imgDataOffset,
bool usesAndMask)
BMPImageReader::BMPImageReader(ImageDecoder* parent, size_t decodedAndHeaderOffset, size_t imgDataOffset, bool usesAndMask)
: m_parent(parent)
, m_buffer(0)
, m_decodedOffset(decodedAndHeaderOffset)
......@@ -63,8 +60,7 @@ bool BMPImageReader::decodeBMP(bool onlySize)
return false;
// Read and process info header.
if ((m_decodedOffset < (m_headerOffset + m_infoHeader.biSize))
&& !processInfoHeader())
if ((m_decodedOffset < (m_headerOffset + m_infoHeader.biSize)) && !processInfoHeader())
return false;
// processInfoHeader() set the size, so if that's all we needed, we're done.
......@@ -82,8 +78,7 @@ bool BMPImageReader::decodeBMP(bool onlySize)
// Initialize the framebuffer if needed.
ASSERT(m_buffer); // Parent should set this before asking us to decode!
if (m_buffer->status() == RGBA32Buffer::FrameEmpty) {
if (!m_buffer->setSize(m_parent->size().width(),
m_parent->size().height()))
if (!m_buffer->setSize(m_parent->size().width(), m_parent->size().height()))
return setFailed(); // Unable to allocate.
m_buffer->setStatus(RGBA32Buffer::FramePartial);
// setSize() calls eraseARGB(), which resets the alpha flag, so we force
......@@ -100,9 +95,7 @@ bool BMPImageReader::decodeBMP(bool onlySize)
// Decode the data.
if ((m_andMaskState != Decoding) && !pastEndOfImage(0)) {
if ((m_infoHeader.biCompression == RLE4)
|| (m_infoHeader.biCompression == RLE8)
|| (m_infoHeader.biCompression == RLE24)) {
if ((m_infoHeader.biCompression == RLE4) || (m_infoHeader.biCompression == RLE8) || (m_infoHeader.biCompression == RLE24)) {
if (!processRLEData())
return false;
} else if (!processNonRLEData(false, 0))
......@@ -133,8 +126,7 @@ bool BMPImageReader::readInfoHeaderSize()
{
// Get size of info header.
ASSERT(m_decodedOffset == m_headerOffset);
if ((m_decodedOffset > m_data->size())
|| ((m_data->size() - m_decodedOffset) < 4))
if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset) < 4))
return false;
m_infoHeader.biSize = readUint32(0);
// Don't increment m_decodedOffset here, it just makes the code in
......@@ -143,9 +135,7 @@ bool BMPImageReader::readInfoHeaderSize()
// Don't allow the header to overflow (which would be harmless here, but
// problematic or at least confusing in other places), or to overrun the
// image data.
if (((m_headerOffset + m_infoHeader.biSize) < m_headerOffset)
|| (m_imgDataOffset
&& (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize))))
if (((m_headerOffset + m_infoHeader.biSize) < m_headerOffset) || (m_imgDataOffset && (m_imgDataOffset < (m_headerOffset + m_infoHeader.biSize))))
return setFailed();
// See if this is a header size we understand:
......@@ -156,9 +146,7 @@ bool BMPImageReader::readInfoHeaderSize()
else if ((m_infoHeader.biSize == 40) || isWindowsV4Plus())
;
// OS/2 2.x: any multiple of 4 between 16 and 64, inclusive, or 42 or 46
else if ((m_infoHeader.biSize >= 16) && (m_infoHeader.biSize <= 64)
&& (((m_infoHeader.biSize & 3) == 0) || (m_infoHeader.biSize == 42)
|| (m_infoHeader.biSize == 46)))
else if ((m_infoHeader.biSize >= 16) && (m_infoHeader.biSize <= 64) && (!(m_infoHeader.biSize & 3) || (m_infoHeader.biSize == 42) || (m_infoHeader.biSize == 46)))
m_isOS22x = true;
else
return setFailed();
......@@ -170,9 +158,7 @@ bool BMPImageReader::processInfoHeader()
{
// Read info header.
ASSERT(m_decodedOffset == m_headerOffset);
if ((m_decodedOffset > m_data->size())
|| ((m_data->size() - m_decodedOffset) < m_infoHeader.biSize)
|| !readInfoHeader())
if ((m_decodedOffset > m_data->size()) || ((m_data->size() - m_decodedOffset) < m_infoHeader.biSize) || !readInfoHeader())
return false;
m_decodedOffset += m_infoHeader.biSize;
......@@ -188,11 +174,9 @@ bool BMPImageReader::processInfoHeader()
// colors", so set it to the maximum number of colors for this bit depth.