1/*
2 * Copyright (C) 2011 Apple 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "core/platform/graphics/filters/FilterOperation.h"
29
30#include "core/platform/animation/AnimationUtilities.h"
31
32namespace WebCore {
33
34PassRefPtr<FilterOperation> BasicColorMatrixFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
35{
36    if (from && !from->isSameType(*this))
37        return this;
38
39    if (blendToPassthrough)
40        return BasicColorMatrixFilterOperation::create(WebCore::blend(m_amount, passthroughAmount(), progress), m_type);
41
42    const BasicColorMatrixFilterOperation* fromOp = static_cast<const BasicColorMatrixFilterOperation*>(from);
43    double fromAmount = fromOp ? fromOp->amount() : passthroughAmount();
44    return BasicColorMatrixFilterOperation::create(WebCore::blend(fromAmount, m_amount, progress), m_type);
45}
46
47double BasicColorMatrixFilterOperation::passthroughAmount() const
48{
49    switch (m_type) {
50    case GRAYSCALE:
51    case SEPIA:
52    case HUE_ROTATE:
53        return 0;
54    case SATURATE:
55        return 1;
56    default:
57        ASSERT_NOT_REACHED();
58        return 0;
59    }
60}
61
62PassRefPtr<FilterOperation> BasicComponentTransferFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
63{
64    if (from && !from->isSameType(*this))
65        return this;
66
67    if (blendToPassthrough)
68        return BasicComponentTransferFilterOperation::create(WebCore::blend(m_amount, passthroughAmount(), progress), m_type);
69
70    const BasicComponentTransferFilterOperation* fromOp = static_cast<const BasicComponentTransferFilterOperation*>(from);
71    double fromAmount = fromOp ? fromOp->amount() : passthroughAmount();
72    return BasicComponentTransferFilterOperation::create(WebCore::blend(fromAmount, m_amount, progress), m_type);
73}
74
75double BasicComponentTransferFilterOperation::passthroughAmount() const
76{
77    switch (m_type) {
78    case OPACITY:
79        return 1;
80    case INVERT:
81        return 0;
82    case CONTRAST:
83        return 1;
84    case BRIGHTNESS:
85        return 1;
86    default:
87        ASSERT_NOT_REACHED();
88        return 0;
89    }
90}
91
92PassRefPtr<FilterOperation> GammaFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
93{
94    if (from && !from->isSameType(*this))
95        return this;
96
97    if (blendToPassthrough)
98        return GammaFilterOperation::create(
99            WebCore::blend(m_amplitude, 1.0, progress),
100            WebCore::blend(m_exponent, 1.0, progress),
101            WebCore::blend(m_offset, 0.0, progress), m_type);
102
103    const GammaFilterOperation* fromOp = static_cast<const GammaFilterOperation*>(from);
104    double fromAmplitude = fromOp ? fromOp->amplitude() : 1;
105    double fromExponent = fromOp ? fromOp->exponent() : 1;
106    double fromOffset = fromOp ? fromOp->offset() : 0;
107    return GammaFilterOperation::create(
108        WebCore::blend(fromAmplitude, m_amplitude, progress),
109        WebCore::blend(fromExponent, m_exponent, progress),
110        WebCore::blend(fromOffset, m_offset, progress), m_type);
111}
112
113PassRefPtr<FilterOperation> BlurFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
114{
115    if (from && !from->isSameType(*this))
116        return this;
117
118    LengthType lengthType = m_stdDeviation.type();
119
120    if (blendToPassthrough)
121        return BlurFilterOperation::create(Length(lengthType).blend(m_stdDeviation, progress), m_type);
122
123    const BlurFilterOperation* fromOp = static_cast<const BlurFilterOperation*>(from);
124    Length fromLength = fromOp ? fromOp->m_stdDeviation : Length(lengthType);
125    return BlurFilterOperation::create(m_stdDeviation.blend(fromLength, progress), m_type);
126}
127
128PassRefPtr<FilterOperation> DropShadowFilterOperation::blend(const FilterOperation* from, double progress, bool blendToPassthrough)
129{
130    if (from && !from->isSameType(*this))
131        return this;
132
133    if (blendToPassthrough)
134        return DropShadowFilterOperation::create(
135            WebCore::blend(m_location, IntPoint(), progress),
136            WebCore::blend(m_stdDeviation, 0, progress),
137            WebCore::blend(m_color, Color(Color::transparent), progress),
138            m_type);
139
140    const DropShadowFilterOperation* fromOp = static_cast<const DropShadowFilterOperation*>(from);
141    IntPoint fromLocation = fromOp ? fromOp->location() : IntPoint();
142    int fromStdDeviation = fromOp ? fromOp->stdDeviation() : 0;
143    Color fromColor = fromOp ? fromOp->color() : Color(Color::transparent);
144
145    return DropShadowFilterOperation::create(
146        WebCore::blend(fromLocation, m_location, progress),
147        WebCore::blend(fromStdDeviation, m_stdDeviation, progress),
148        WebCore::blend(fromColor, m_color, progress), m_type);
149}
150
151} // namespace WebCore
152
153