Changeset 46958 in webkit


Ignore:
Timestamp:
Aug 8, 2009 1:34:40 PM (15 years ago)
Author:
krit@webkit.org
Message:

2009-08-08 Dirk Schulze <krit@webkit.org>

Reviewed by Eric Seidel.

SVG Filter need feColorMatrix implementation
https://bugs.webkit.org/show_bug.cgi?id=27711

Adds the filter effect feColorMatrix to SVG filters.

There is already a test in the w3c directory.
Test: svg/W3C-SVG-1.1/filters-color-01-b.svg

  • platform/graphics/filters/FEColorMatrix.cpp: (WebCore::matrix): (WebCore::saturate): (WebCore::huerotate): (WebCore::luminance): (WebCore::FEColorMatrix::apply):
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r46957 r46958  
     12009-08-08  Dirk Schulze  <krit@webkit.org>
     2
     3        Reviewed by Eric Seidel.
     4
     5        SVG Filter need feColorMatrix implementation
     6        [https://bugs.webkit.org/show_bug.cgi?id=27711]
     7
     8        Adds the filter effect feColorMatrix to SVG filters.
     9
     10        There is already a test in the w3c directory.
     11        Test: svg/W3C-SVG-1.1/filters-color-01-b.svg
     12
     13        * platform/graphics/filters/FEColorMatrix.cpp:
     14        (WebCore::matrix):
     15        (WebCore::saturate):
     16        (WebCore::huerotate):
     17        (WebCore::luminance):
     18        (WebCore::FEColorMatrix::apply):
     19
    1202009-08-08  Dirk Schulze  <krit@webkit.org>
    221
  • trunk/WebCore/platform/graphics/filters/FEColorMatrix.cpp

    r44296 r46958  
    33                  2004, 2005 Rob Buis <buis@kde.org>
    44                  2005 Eric Seidel <eric@webkit.org>
     5                  2009 Dirk Schulze <krit@webkit.org>
    56
    67    This library is free software; you can redistribute it and/or
     
    2526#include "FEColorMatrix.h"
    2627
     28#include "CanvasPixelArray.h"
    2729#include "Filter.h"
     30#include "GraphicsContext.h"
     31#include "ImageData.h"
     32#include <math.h>
    2833
    2934namespace WebCore {
     
    6267}
    6368
    64 void FEColorMatrix::apply(Filter*)
     69inline void matrix(double& red, double& green, double& blue, double& alpha, const Vector<float>& values)
    6570{
     71    double r = values[0]  * red + values[1]  * green + values[2]  * blue + values[3]  * alpha;
     72    double g = values[5]  * red + values[6]  * green + values[7]  * blue + values[8]  * alpha;
     73    double b = values[10]  * red + values[11]  * green + values[12] * blue + values[13] * alpha;
     74    double a = values[15] * red + values[16] * green + values[17] * blue + values[18] * alpha;
     75
     76    red = r;
     77    green = g;
     78    blue = b;
     79    alpha = a;
     80}
     81
     82inline void saturate(double& red, double& green, double& blue, const float& s)
     83{
     84    double r = (0.213 + 0.787 * s) * red + (0.715 - 0.715 * s) * green + (0.072 - 0.072 * s) * blue;
     85    double g = (0.213 - 0.213 * s) * red + (0.715 + 0.285 * s) * green + (0.072 - 0.072 * s) * blue;
     86    double b = (0.213 - 0.213 * s) * red + (0.715 - 0.715 * s) * green + (0.072 + 0.928 * s) * blue;
     87
     88    red = r;
     89    green = g;
     90    blue = b;
     91}
     92
     93inline void huerotate(double& red, double& green, double& blue, const float& hue)
     94{
     95    double cosHue = cos(hue * M_PI / 180);
     96    double sinHue = sin(hue * M_PI / 180);
     97    double r = red   * (0.213 + cosHue * 0.787 - sinHue * 0.213) +
     98               green * (0.715 - cosHue * 0.715 - sinHue * 0.715) +
     99               blue  * (0.072 - cosHue * 0.072 + sinHue * 0.928);
     100    double g = red   * (0.213 - cosHue * 0.213 + sinHue * 0.143) +
     101               green * (0.715 + cosHue * 0.285 + sinHue * 0.140) +
     102               blue  * (0.072 - cosHue * 0.072 - sinHue * 0.283);
     103    double b = red   * (0.213 - cosHue * 0.213 - sinHue * 0.787) +
     104               green * (0.715 - cosHue * 0.715 + sinHue * 0.715) +
     105               blue  * (0.072 + cosHue * 0.928 + sinHue * 0.072);
     106
     107    red = r;
     108    green = g;
     109    blue = b;
     110}
     111
     112inline void luminance(double& red, double& green, double& blue, double& alpha)
     113{
     114    alpha = 0.2125 * red + 0.7154 * green + 0.0721 * blue;
     115    red = 0.;
     116    green = 0.;
     117    blue = 0.;
     118}
     119
     120template<ColorMatrixType filterType>
     121void effectType(const PassRefPtr<CanvasPixelArray>& srcPixelArray, PassRefPtr<ImageData>& imageData, const Vector<float>& values)
     122{
     123    for (unsigned pixelOffset = 0; pixelOffset < srcPixelArray->length(); pixelOffset++) {
     124        unsigned pixelByteOffset = pixelOffset * 4;
     125
     126        unsigned char r = 0, g = 0, b = 0, a = 0;
     127        srcPixelArray->get(pixelByteOffset, r);
     128        srcPixelArray->get(pixelByteOffset + 1, g);
     129        srcPixelArray->get(pixelByteOffset + 2, b);
     130        srcPixelArray->get(pixelByteOffset + 3, a);
     131
     132        double red = r, green = g, blue = b, alpha = a;
     133       
     134        switch (filterType) {
     135            case FECOLORMATRIX_TYPE_MATRIX:
     136                matrix(red, green, blue, alpha, values);
     137                break;
     138            case FECOLORMATRIX_TYPE_SATURATE:
     139                saturate(red, green, blue, values[0]);
     140                break;
     141            case FECOLORMATRIX_TYPE_HUEROTATE:
     142                huerotate(red, green, blue, values[0]);
     143                break;
     144            case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
     145                luminance(red, green, blue, alpha);
     146                break;
     147        }
     148
     149        imageData->data()->set(pixelByteOffset, red);
     150        imageData->data()->set(pixelByteOffset + 1, green);
     151        imageData->data()->set(pixelByteOffset + 2, blue);
     152        imageData->data()->set(pixelByteOffset + 3, alpha);
     153    }
     154}
     155
     156void FEColorMatrix::apply(Filter* filter)
     157{
     158    m_in->apply(filter);
     159    if (!m_in->resultImage())
     160        return;
     161
     162    GraphicsContext* filterContext = getEffectContext();
     163    if (!filterContext)
     164        return;
     165
     166    filterContext->drawImage(m_in->resultImage()->image(), calculateDrawingRect(m_in->subRegion()));
     167
     168    IntRect imageRect(IntPoint(), resultImage()->size());
     169    PassRefPtr<ImageData> imageData(resultImage()->getImageData(imageRect));
     170    PassRefPtr<CanvasPixelArray> srcPixelArray(imageData->data());
     171
     172    switch (m_type) {
     173        case FECOLORMATRIX_TYPE_UNKNOWN:
     174            break;
     175        case FECOLORMATRIX_TYPE_MATRIX:
     176            effectType<FECOLORMATRIX_TYPE_MATRIX>(srcPixelArray, imageData, m_values);
     177            break;
     178        case FECOLORMATRIX_TYPE_SATURATE:
     179            effectType<FECOLORMATRIX_TYPE_SATURATE>(srcPixelArray, imageData, m_values);
     180            break;
     181        case FECOLORMATRIX_TYPE_HUEROTATE:
     182            effectType<FECOLORMATRIX_TYPE_HUEROTATE>(srcPixelArray, imageData, m_values);
     183            break;
     184        case FECOLORMATRIX_TYPE_LUMINANCETOALPHA:
     185            effectType<FECOLORMATRIX_TYPE_LUMINANCETOALPHA>(srcPixelArray, imageData, m_values);
     186            break;
     187    }
     188
     189    resultImage()->putImageData(imageData.get(), imageRect, IntPoint());
    66190}
    67191
Note: See TracChangeset for help on using the changeset viewer.