Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1.  Redistributions of source code must retain the above copyright
      8  *     notice, this list of conditions and the following disclaimer.
      9  * 2.  Redistributions in binary form must reproduce the above copyright
     10  *     notice, this list of conditions and the following disclaimer in the
     11  *     documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23  */
     24 
     25 #include "config.h"
     26 
     27 #include "SkImageFilter.h"
     28 #include "platform/graphics/filters/FEBlend.h"
     29 #include "platform/graphics/filters/FEGaussianBlur.h"
     30 #include "platform/graphics/filters/FEMerge.h"
     31 #include "platform/graphics/filters/FilterOperations.h"
     32 #include "platform/graphics/filters/ReferenceFilter.h"
     33 #include "platform/graphics/filters/SkiaImageFilterBuilder.h"
     34 #include "platform/graphics/filters/SourceGraphic.h"
     35 #include <gtest/gtest.h>
     36 
     37 using testing::Test;
     38 using namespace blink;
     39 
     40 class ImageFilterBuilderTest : public Test {
     41 protected:
     42     void colorSpaceTest()
     43     {
     44         // Build filter tree
     45         RefPtr<ReferenceFilter> referenceFilter = ReferenceFilter::create();
     46 
     47         // Add a dummy source graphic input
     48         RefPtr<FilterEffect> sourceEffect = referenceFilter->sourceGraphic();
     49         sourceEffect->setOperatingColorSpace(ColorSpaceDeviceRGB);
     50 
     51         // Add a blur effect (with input : source)
     52         RefPtr<FilterEffect> blurEffect =
     53             FEGaussianBlur::create(referenceFilter.get(), 3.0f, 3.0f);
     54         blurEffect->setOperatingColorSpace(ColorSpaceLinearRGB);
     55         blurEffect->inputEffects().append(sourceEffect);
     56 
     57         // Add a blend effect (with inputs : blur, source)
     58         RefPtr<FilterEffect> blendEffect =
     59             FEBlend::create(referenceFilter.get(), WebBlendModeNormal);
     60         blendEffect->setOperatingColorSpace(ColorSpaceDeviceRGB);
     61         FilterEffectVector& blendInputs = blendEffect->inputEffects();
     62         blendInputs.reserveCapacity(2);
     63         blendInputs.append(sourceEffect);
     64         blendInputs.append(blurEffect);
     65 
     66         // Add a merge effect (with inputs : blur, blend)
     67         RefPtr<FilterEffect> mergeEffect = FEMerge::create(referenceFilter.get());
     68         mergeEffect->setOperatingColorSpace(ColorSpaceLinearRGB);
     69         FilterEffectVector& mergeInputs = mergeEffect->inputEffects();
     70         mergeInputs.reserveCapacity(2);
     71         mergeInputs.append(blurEffect);
     72         mergeInputs.append(blendEffect);
     73         referenceFilter->setLastEffect(mergeEffect);
     74 
     75         // Get SkImageFilter resulting tree
     76         SkiaImageFilterBuilder builder;
     77         RefPtr<SkImageFilter> filter = builder.build(referenceFilter->lastEffect(), ColorSpaceDeviceRGB);
     78 
     79         // Let's check that the resulting tree looks like this :
     80         //      ColorSpace (Linear->Device) : CS (L->D)
     81         //                |
     82         //             Merge (L)
     83         //              |     |
     84         //              |    CS (D->L)
     85         //              |          |
     86         //              |      Blend (D)
     87         //              |       /    |
     88         //              |  CS (L->D) |
     89         //              |  /         |
     90         //             Blur (L)      |
     91         //                 \         |
     92         //               CS (D->L)   |
     93         //                   \       |
     94         //                 Source Graphic (D)
     95 
     96         EXPECT_EQ(filter->countInputs(), 1); // Should be CS (L->D)
     97         SkImageFilter* child = filter->getInput(0); // Should be Merge
     98         EXPECT_EQ(child->asColorFilter(0), false);
     99         EXPECT_EQ(child->countInputs(), 2);
    100         child = child->getInput(1); // Should be CS (D->L)
    101         EXPECT_EQ(child->asColorFilter(0), true);
    102         EXPECT_EQ(child->countInputs(), 1);
    103         child = child->getInput(0); // Should be Blend
    104         EXPECT_EQ(child->asColorFilter(0), false);
    105         EXPECT_EQ(child->countInputs(), 2);
    106         child = child->getInput(0); // Should be CS (L->D)
    107         EXPECT_EQ(child->asColorFilter(0), true);
    108         EXPECT_EQ(child->countInputs(), 1);
    109         child = child->getInput(0); // Should be Blur
    110         EXPECT_EQ(child->asColorFilter(0), false);
    111         EXPECT_EQ(child->countInputs(), 1);
    112         child = child->getInput(0); // Should be CS (D->L)
    113         EXPECT_EQ(child->asColorFilter(0), true);
    114         EXPECT_EQ(child->countInputs(), 1);
    115     }
    116 };
    117 
    118 namespace {
    119 
    120 TEST_F(ImageFilterBuilderTest, testColorSpace)
    121 {
    122     colorSpaceTest();
    123 }
    124 
    125 } // namespace
    126