Changeset 87032 in webkit


Ignore:
Timestamp:
May 22, 2011 9:56:55 AM (13 years ago)
Author:
commit-queue@webkit.org
Message:

2011-05-22 Andrew Wason <rectalogic@rectalogic.com>

Reviewed by Benjamin Poulain.

[Qt] Implement toImageData() in QtWebKit Bridge
https://bugs.webkit.org/show_bug.cgi?id=60897

Tests are in WebKit/qt/tests/hybridPixmap

  • bridge/qt/qt_pixmapruntime.cpp: (JSC::Bindings::QtPixmapToImageDataMethod::name): (JSC::Bindings::QtPixmapToImageDataMethod::invoke): (JSC::Bindings::QtPixmapToImageDataMethod::copyPixels): (JSC::Bindings::QtPixmapClass::methodsNamed): (JSC::Bindings::QtPixmapInstance::getPropertyNames): Add toImageData() to Qt Bridge, alternative to assignToHTMLImageElement().

2011-05-22 Andrew Wason <rectalogic@rectalogic.com>

Reviewed by Benjamin Poulain.

[Qt] Implement toImageData() in QtWebKit Bridge
https://bugs.webkit.org/show_bug.cgi?id=60897

  • docs/qtwebkit-bridge.qdoc:
  • docs/webkitsnippets/qtwebkit_bridge_snippets.cpp: (wrapInFunction): Document Qt bridge toImageData() feature.
  • tests/hybridPixmap/test.html:
  • tests/hybridPixmap/widget.cpp: (Widget::Widget): (Widget::abcImage):
  • tests/hybridPixmap/widget.h: Add tests for Qt bridge toImageData() feature.
Location:
trunk/Source
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r87029 r87032  
     12011-05-22  Andrew Wason  <rectalogic@rectalogic.com>
     2
     3        Reviewed by Benjamin Poulain.
     4
     5        [Qt] Implement toImageData() in QtWebKit Bridge
     6        https://bugs.webkit.org/show_bug.cgi?id=60897
     7
     8        Tests are in WebKit/qt/tests/hybridPixmap
     9
     10        * bridge/qt/qt_pixmapruntime.cpp:
     11        (JSC::Bindings::QtPixmapToImageDataMethod::name):
     12        (JSC::Bindings::QtPixmapToImageDataMethod::invoke):
     13        (JSC::Bindings::QtPixmapToImageDataMethod::copyPixels):
     14        (JSC::Bindings::QtPixmapClass::methodsNamed):
     15        (JSC::Bindings::QtPixmapInstance::getPropertyNames):
     16         Add toImageData() to Qt Bridge, alternative to
     17         assignToHTMLImageElement().
     18
    1192011-05-22  Dominic Cooney  <dominicc@chromium.org>
    220
  • trunk/Source/WebCore/bridge/qt/qt_pixmapruntime.cpp

    r84556 r87032  
    2222#include "CachedImage.h"
    2323#include "HTMLImageElement.h"
     24#include "ImageData.h"
     25#include "IntSize.h"
    2426#include "JSGlobalObject.h"
    2527#include "JSHTMLImageElement.h"
     28#include "JSImageData.h"
    2629#include "JSLock.h"
    2730#include "ObjectPrototype.h"
    2831#include "StillImageQt.h"
     32#include <QtEndian>
    2933#include <QBuffer>
    3034#include <QByteArray>
     35#include <QColor>
    3136#include <QImage>
    3237#include <QPixmap>
     
    8085};
    8186
     87class QtPixmapToImageDataMethod : public QtPixmapRuntimeMethod {
     88public:
     89    static const char* name() { return "toImageData"; }
     90    JSValue invoke(ExecState* exec, QtPixmapInstance* instance)
     91    {
     92        int width = instance->width();
     93        int height = instance->height();
     94        RefPtr<ByteArray> byteArray = ByteArray::create(width * height * 4);
     95        copyPixels(instance->toImage(), width, height, byteArray->data());
     96        RefPtr<ImageData> imageData = ImageData::create(IntSize(width, height), byteArray);
     97        return toJS(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), imageData.get());
     98    }
     99private:
     100    void copyPixels(const QImage& sourceImage, int width, int height, unsigned char* destPixels)
     101    {
     102        QImage image(sourceImage);
     103        switch (image.format()) {
     104        case QImage::Format_RGB888:
     105            for (int y = 0; y < height; y++) {
     106                const uchar* scanLine = image.scanLine(y);
     107                for (int x = 0; x < width; x++) {
     108                    *(destPixels++) = *(scanLine++);
     109                    *(destPixels++) = *(scanLine++);
     110                    *(destPixels++) = *(scanLine++);
     111                    *(destPixels++) = 0xFF;
     112                }
     113            }
     114            break;
     115        default:
     116            image = image.convertToFormat(QImage::Format_ARGB32);
     117            // Fall through
     118        case QImage::Format_RGB32:
     119        case QImage::Format_ARGB32:
     120            for (int y = 0; y < height; y++) {
     121                const quint32* scanLine = reinterpret_cast_ptr<const quint32*>(image.scanLine(y));
     122                for (int x = 0; x < width; x++) {
     123                    QRgb pixel = scanLine[x];
     124                    qToBigEndian<quint32>((pixel << 8) | qAlpha(pixel), destPixels);
     125                    destPixels += 4;
     126                }
     127            }
     128            break;
     129        }
     130    }
     131};
     132
    82133// this function receives an HTML image element as a parameter, makes it display the pixmap/image from Qt
    83134class QtPixmapAssignToElementMethod : public QtPixmapRuntimeMethod {
     
    137188struct QtPixmapMetaData {
    138189    QtPixmapToDataUrlMethod toDataUrlMethod;
     190    QtPixmapToImageDataMethod toImageDataMethod;
    139191    QtPixmapAssignToElementMethod assignToElementMethod;
    140192    QtPixmapToStringMethod toStringMethod;
     
    199251    if (identifier == QtPixmapToDataUrlMethod::name())
    200252        methods.append(&qt_pixmap_metaData.toDataUrlMethod);
     253    else if (identifier == QtPixmapToImageDataMethod::name())
     254        methods.append(&qt_pixmap_metaData.toImageDataMethod);
    201255    else if (identifier == QtPixmapAssignToElementMethod::name())
    202256        methods.append(&qt_pixmap_metaData.assignToElementMethod);
     
    218272{
    219273    arr.add(Identifier(exec, UString(QtPixmapToDataUrlMethod::name())));
     274    arr.add(Identifier(exec, UString(QtPixmapToImageDataMethod::name())));
    220275    arr.add(Identifier(exec, UString(QtPixmapAssignToElementMethod::name())));
    221276    arr.add(Identifier(exec, UString(QtPixmapToStringMethod::name())));
  • trunk/Source/WebKit/qt/ChangeLog

    r86981 r87032  
     12011-05-22  Andrew Wason  <rectalogic@rectalogic.com>
     2
     3        Reviewed by Benjamin Poulain.
     4
     5        [Qt] Implement toImageData() in QtWebKit Bridge
     6        https://bugs.webkit.org/show_bug.cgi?id=60897
     7
     8        * docs/qtwebkit-bridge.qdoc:
     9        * docs/webkitsnippets/qtwebkit_bridge_snippets.cpp:
     10        (wrapInFunction):
     11         Document Qt bridge toImageData() feature.
     12        * tests/hybridPixmap/test.html:
     13        * tests/hybridPixmap/widget.cpp:
     14        (Widget::Widget):
     15        (Widget::abcImage):
     16        * tests/hybridPixmap/widget.h:
     17         Add tests for Qt bridge toImageData() feature.
     18
    1192011-05-20  Simon Fraser  <simon.fraser@apple.com>
    220
  • trunk/Source/WebKit/qt/docs/qtwebkit-bridge.qdoc

    r64621 r87032  
    364364    which allows using the pixmap as the \c{src} attribute of an image or as a \c{background-image} URL. Note that the \c{toDataURL()}
    365365    function is costly and should be used with caution.
     366    It can also use the \c{toImageData()} function to convert the pixmap to a JavaScript \c{ImageData} object.
    366367
    367368    Example code:
  • trunk/Source/WebKit/qt/docs/webkitsnippets/qtwebkit_bridge_snippets.cpp

    r64621 r87032  
    1616        toDataURL: function() { ... },
    1717        assignToHTMLImageElement: function(element) { ... }
     18        toImageData: function() { ... }
    1819    }
    1920    //! [1]
     
    4243                {
    4344                    myObject.myPixmap.assignToHTMLImageElement(document.getElementById("imageElement"));
     45                    var context = document.getElementById("canvasElement").getContext("2d");
     46                    context.putImageData(myObject.myPixmap.toImageData());
    4447                }
    4548            </script>
     
    4750        <body onload="loadImage()">
    4851            <img id="imageElement" width="300" height="200" />
     52            <canvas id="canvasElement" width="300" height="200" />
    4953        </body>
    5054    </html>
  • trunk/Source/WebKit/qt/tests/hybridPixmap/test.html

    r54986 r87032  
    55        </style>
    66        <script>
     7            function testImageData() {
     8                var obj = myWidget.image;
     9                var pxm = myWidget.pixmap;
     10
     11                function compareImageDataSize(o, imageData) {
     12                    myWidget.compare(imageData.height, o.height);
     13                    myWidget.compare(imageData.width, o.width);
     14                }
     15                compareImageDataSize(obj, obj.toImageData());
     16                compareImageDataSize(pxm, pxm.toImageData());
     17
     18                function compareImageDataPixel(o, imageData) {
     19                    compareImageDataSize(o, imageData);
     20                    // Make sure pixels are 0xAABBCCFF
     21                    var data = imageData.data;
     22                    for (var i = 0; i < data.length; i += 4) {
     23                        myWidget.compare(data[i], 0xaa); // R
     24                        myWidget.compare(data[i+1], 0xbb); // G
     25                        myWidget.compare(data[i+2], 0xcc); // B
     26                        myWidget.compare(data[i+3], 0xff); // A
     27                    }
     28                }
     29                var objARGB32 = myWidget.abcImage(5);
     30                compareImageDataPixel(objARGB32, objARGB32.toImageData());
     31                var objRGB32 = myWidget.abcImage(4);
     32                compareImageDataPixel(objRGB32, objRGB32.toImageData());
     33                var objRGB888 = myWidget.abcImage(13);
     34                compareImageDataPixel(objRGB888, objRGB888.toImageData());
     35                var objRGB444 = myWidget.abcImage(14);
     36                compareImageDataPixel(objRGB444, objRGB444.toImageData());
     37            }
     38
    739            function startTest()
    840            {
     41                testImageData();
     42
    943                var obj = myWidget.image;
    1044                var pxm = myWidget.pixmap;
  • trunk/Source/WebKit/qt/tests/hybridPixmap/widget.cpp

    r53611 r87032  
    2828Widget::Widget(QWidget* parent) :
    2929    QWidget(parent),
    30     ui(new Ui::Widget)
     30    ui(new Ui::Widget),
     31    abcFilledImage(32, 32, QImage::Format_ARGB32)
    3132{
    3233    ui->setupUi(this);
     34    abcFilledImage.fill(qRgba(0xaa, 0xbb, 0xcc, 0xff));
    3335}
    3436
     
    8183}
    8284
     85QImage Widget::abcImage(int format)
     86{
     87    return abcFilledImage.convertToFormat(static_cast<QImage::Format>(format));
     88}
     89
    8390Widget::~Widget()
    8491{
  • trunk/Source/WebKit/qt/tests/hybridPixmap/widget.h

    r74741 r87032  
    5757    void pixmapSlot(const QPixmap&);
    5858    void randomSlot(const QPixmap&);
     59    QImage abcImage(int format);
    5960
    6061signals:
     
    6869private:
    6970    Ui::Widget* ui;
     71    QImage abcFilledImage;
    7072};
    7173
Note: See TracChangeset for help on using the changeset viewer.