Changeset 47868 in webkit


Ignore:
Timestamp:
Aug 28, 2009 11:08:38 AM (15 years ago)
Author:
pkasting@chromium.org
Message:

https://bugs.webkit.org/show_bug.cgi?id=27965
Move ImageDecoder creation function to a factory function on
ImageDecoder. This is arguably where it makes the most sense anyway,
and it will (soon) allow ImageSourceQt.cpp to have one less dedicated
function.

Reviewed by Eric Seidel.

  • platform/graphics/ImageSource.cpp:
  • platform/graphics/qt/ImageDecoderQt.cpp:

(WebCore::ImageDecoder::create):

  • platform/graphics/qt/ImageDecoderQt.h:
  • platform/graphics/qt/ImageSourceQt.cpp:

(WebCore::ImageSource::setData):

  • platform/image-decoders/ImageDecoder.cpp:
  • platform/image-decoders/ImageDecoder.h:
Location:
trunk/WebCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r47867 r47868  
     12009-08-28  Peter Kasting  <pkasting@google.com>
     2
     3        Reviewed by Eric Seidel.
     4
     5        https://bugs.webkit.org/show_bug.cgi?id=27965
     6        Move ImageDecoder creation function to a factory function on
     7        ImageDecoder.  This is arguably where it makes the most sense anyway,
     8        and it will (soon) allow ImageSourceQt.cpp to have one less dedicated
     9        function.
     10
     11        * platform/graphics/ImageSource.cpp:
     12        * platform/graphics/qt/ImageDecoderQt.cpp:
     13        (WebCore::ImageDecoder::create):
     14        * platform/graphics/qt/ImageDecoderQt.h:
     15        * platform/graphics/qt/ImageSourceQt.cpp:
     16        (WebCore::ImageSource::setData):
     17        * platform/image-decoders/ImageDecoder.cpp:
     18        * platform/image-decoders/ImageDecoder.h:
     19
    1202009-08-28  Peter Kasting  <pkasting@google.com>
    221
  • trunk/WebCore/platform/graphics/ImageSource.cpp

    r47854 r47868  
    3030#include "ImageSource.h"
    3131
    32 #include "BMPImageDecoder.h"
    33 #include "GIFImageDecoder.h"
    34 #include "ICOImageDecoder.h"
    35 #include "JPEGImageDecoder.h"
    36 #include "PNGImageDecoder.h"
    37 #include "SharedBuffer.h"
    38 #include "XBMImageDecoder.h"
     32#include "ImageDecoder.h"
    3933
    4034#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
     
    4539
    4640namespace WebCore {
    47 
    48 ImageDecoder* createDecoder(const Vector<char>& data)
    49 {
    50     // We need at least 4 bytes to figure out what kind of image we're dealing with.
    51     int length = data.size();
    52     if (length < 4)
    53         return 0;
    54 
    55     const unsigned char* uContents = (const unsigned char*)data.data();
    56     const char* contents = data.data();
    57 
    58     // GIFs begin with GIF8(7 or 9).
    59     if (strncmp(contents, "GIF8", 4) == 0)
    60         return new GIFImageDecoder();
    61 
    62     // Test for PNG.
    63     if (uContents[0]==0x89 &&
    64         uContents[1]==0x50 &&
    65         uContents[2]==0x4E &&
    66         uContents[3]==0x47)
    67         return new PNGImageDecoder();
    68 
    69     // JPEG
    70     if (uContents[0]==0xFF &&
    71         uContents[1]==0xD8 &&
    72         uContents[2]==0xFF)
    73         return new JPEGImageDecoder();
    74 
    75     // BMP
    76     if (strncmp(contents, "BM", 2) == 0)
    77         return new BMPImageDecoder();
    78 
    79     // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
    80     // CURs begin with 2-byte 0 followed by 2-byte 2.
    81     if (!memcmp(contents, "\000\000\001\000", 4) ||
    82         !memcmp(contents, "\000\000\002\000", 4))
    83         return new ICOImageDecoder();
    84 
    85     // XBMs require 8 bytes of info.
    86     if (length >= 8 && strncmp(contents, "#define ", 8) == 0)
    87         return new XBMImageDecoder();
    88 
    89     // Give up. We don't know what the heck this is.
    90     return 0;
    91 }
    9241
    9342ImageSource::ImageSource()
     
    12776    // made.
    12877    if (!m_decoder) {
    129         m_decoder = createDecoder(data->buffer());
     78        m_decoder = static_cast<NativeImageSourcePtr>(ImageDecoder::create(*data));
    13079#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
    13180        if (m_decoder)
  • trunk/WebCore/platform/graphics/qt/ImageDecoderQt.cpp

    r46738 r47868  
    4242
    4343namespace WebCore {
     44
     45  ImageDecoder* ImageDecoder::create(const SharedBuffer& data)
     46{
     47    // We need at least 4 bytes to figure out what kind of image we're dealing with.
     48    if (data.size() < 4)
     49        return 0;
     50
     51    QByteArray bytes = QByteArray::fromRawData(data.data(), data.size());
     52    QBuffer buffer(&bytes);
     53    if (!buffer.open(QBuffer::ReadOnly))
     54        return 0;
     55
     56    QString imageFormat = QString::fromLatin1(QImageReader::imageFormat(&buffer).toLower());
     57    if (imageFormat.isEmpty())
     58        return 0; // Image format not supported
     59
     60    return new ImageDecoderQt(imageFormat);
     61}
     62
    4463ImageDecoderQt::ImageData::ImageData(const QImage& image, ImageState imageState, int duration) :
    4564    m_image(image), m_imageState(imageState), m_duration(duration)
     
    179198}
    180199
    181 ImageDecoderQt* ImageDecoderQt::create(const SharedBuffer& data)
    182 {
    183     // We need at least 4 bytes to figure out what kind of image we're dealing with.
    184     if (data.size() < 4)
    185         return 0;
    186 
    187     QByteArray bytes = QByteArray::fromRawData(data.data(), data.size());
    188     QBuffer buffer(&bytes);
    189     if (!buffer.open(QBuffer::ReadOnly))
    190         return 0;
    191 
    192     QString imageFormat = QString::fromLatin1(QImageReader::imageFormat(&buffer).toLower());
    193     if (imageFormat.isEmpty())
    194         return 0; // Image format not supported
    195 
    196     return new ImageDecoderQt(imageFormat);
    197 }
    198 
    199200ImageDecoderQt::ImageDecoderQt(const QString &imageFormat)
    200201    : m_hasAlphaChannel(false)
  • trunk/WebCore/platform/graphics/qt/ImageDecoderQt.h

    r46738 r47868  
    4040{
    4141public:
    42     static ImageDecoderQt* create(const SharedBuffer& data);
    4342    ~ImageDecoderQt();
    4443
  • trunk/WebCore/platform/graphics/qt/ImageSourceQt.cpp

    r46738 r47868  
    6060    // made.
    6161    if (!m_decoder)
    62         m_decoder = ImageDecoderQt::create(*data);
     62        m_decoder = static_cast<NativeImageSourcePtr>(ImageDecoder::create(*data));
    6363
    6464    if (!m_decoder)
  • trunk/WebCore/platform/image-decoders/ImageDecoder.cpp

    r47867 r47868  
    2727#endif
    2828
     29#include "BMPImageDecoder.h"
     30#include "GIFImageDecoder.h"
     31#include "ICOImageDecoder.h"
     32#include "JPEGImageDecoder.h"
     33#include "PNGImageDecoder.h"
     34#include "SharedBuffer.h"
     35#include "XBMImageDecoder.h"
     36
    2937namespace WebCore {
     38
     39ImageDecoder* ImageDecoder::create(const SharedBuffer& data)
     40{
     41    // We need at least 4 bytes to figure out what kind of image we're dealing with.
     42    int length = data.size();
     43    if (length < 4)
     44        return 0;
     45
     46    const unsigned char* uContents = (const unsigned char*)data.data();
     47    const char* contents = data.data();
     48
     49    // GIFs begin with GIF8(7 or 9).
     50    if (strncmp(contents, "GIF8", 4) == 0)
     51        return new GIFImageDecoder();
     52
     53    // Test for PNG.
     54    if (uContents[0]==0x89 &&
     55        uContents[1]==0x50 &&
     56        uContents[2]==0x4E &&
     57        uContents[3]==0x47)
     58        return new PNGImageDecoder();
     59
     60    // JPEG
     61    if (uContents[0]==0xFF &&
     62        uContents[1]==0xD8 &&
     63        uContents[2]==0xFF)
     64        return new JPEGImageDecoder();
     65
     66    // BMP
     67    if (strncmp(contents, "BM", 2) == 0)
     68        return new BMPImageDecoder();
     69
     70    // ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
     71    // CURs begin with 2-byte 0 followed by 2-byte 2.
     72    if (!memcmp(contents, "\000\000\001\000", 4) ||
     73        !memcmp(contents, "\000\000\002\000", 4))
     74        return new ICOImageDecoder();
     75
     76    // XBMs require 8 bytes of info.
     77    if (length >= 8 && strncmp(contents, "#define ", 8) == 0)
     78        return new XBMImageDecoder();
     79
     80    // Give up. We don't know what the heck this is.
     81    return 0;
     82}
    3083
    3184#if !PLATFORM(SKIA)
  • trunk/WebCore/platform/image-decoders/ImageDecoder.h

    r47836 r47868  
    196196        virtual ~ImageDecoder() {}
    197197
     198        // Factory function to create an ImageDecoder.  Ports that subclass
     199        // ImageDecoder can provide their own implementation of this to avoid
     200        // needing to write a dedicated setData() implementation.
     201        static ImageDecoder* create(const SharedBuffer& data);
     202
    198203        // The the filename extension usually associated with an undecoded image of this type.
    199204        virtual String filenameExtension() const = 0;
Note: See TracChangeset for help on using the changeset viewer.