Source code: com/arranger/jarl/filter/base/Water.java
1 package com.arranger.jarl.filter.base;
2
3 import com.arranger.jarl.base.IContext;
4 import com.arranger.jarl.base.IJarlObjectInfo;
5 import com.arranger.jarl.filter.BaseSegmentFilter;
6 import com.arranger.jarl.util.InterpolateUtil;
7 import com.arranger.jarl.util.ObjectUtil;
8 import com.arranger.jarl.util.WidgetConfigSegment;
9 import com.arranger.jarl.util.JarlInfoUtil;
10 import com.jhlabs.image.WaterFilter;
11 import org.w3c.dom.Element;
12
13 /**
14 * Water generates water ripples through the image
15 *
16 * @filterAttribute frequency ## xs:float ## the frequency of the water ripples (between 1 & 100)
17 * @filterAttribute amplitude ## xs:float ## the amplitude of the water ripples (between 1 & 100)
18 * @filterAttribute phase ## xs:float ## the phase in degrees of the oscilator (between 1 & 100)
19 *
20 * @filterAttribute startFrequency ## xs:float ## the starting frequency of the water ripples (between 1 & 100)
21 * @filterAttribute startAmplitude ## xs:float ## the starting amplitude of the water ripples (between 1 & 100)
22 * @filterAttribute startPhase ## xs:float ## the starting phase in degrees of the oscilator (between 1 & 100)
23 * @filterAttribute endFrequency ## xs:float ## the ending frequency of the water ripples (between 1 & 100)
24 * @filterAttribute endAmplitude ## xs:float ## the ending amplitude of the water ripples (between 1 & 100)
25 * @filterAttribute endPhase ## xs:float ## the ending phase in degrees of the oscilator (between 1 & 100)
26 */
27 public class Water extends BaseSegmentFilter {
28
29 protected float m_frequency = Float.NEGATIVE_INFINITY;
30 protected float m_amplitude = Float.NEGATIVE_INFINITY;
31 protected float m_phase = Float.NEGATIVE_INFINITY;
32
33 protected float m_startFrequency = Float.NEGATIVE_INFINITY;
34 protected float m_startAmplitude = Float.NEGATIVE_INFINITY;
35 protected float m_startPhase = Float.NEGATIVE_INFINITY;
36 protected float m_endFrequency = Float.NEGATIVE_INFINITY;
37 protected float m_endAmplitude = Float.NEGATIVE_INFINITY;
38 protected float m_endPhase = Float.NEGATIVE_INFINITY;
39
40 /**
41 * Prepare the filter with any changes as necessary
42 * @param context the current context
43 */
44 protected void prepareFilter(IContext context) {
45 float frequency, amplitude, phase;
46
47 if (m_frequency != Float.NEGATIVE_INFINITY) {
48 frequency = m_frequency;
49 amplitude = m_amplitude;
50 phase = m_phase;
51 } else if (m_startFrequency != Float.NEGATIVE_INFINITY && m_endFrequency != Float.NEGATIVE_INFINITY) {
52 frequency = (float) InterpolateUtil.interpolate(getStartTime(),
53 getEndTime(),
54 m_startFrequency,
55 m_endFrequency,
56 context.getTime());
57
58 amplitude = (float) InterpolateUtil.interpolate(getStartTime(),
59 getEndTime(),
60 m_startAmplitude,
61 m_endAmplitude,
62 context.getTime());
63
64 phase = (float) InterpolateUtil.interpolate(getStartTime(),
65 getEndTime(),
66 m_startPhase,
67 m_endPhase,
68 context.getTime());
69 } else {
70 WaterConfigSegment waterConfigSegment = (WaterConfigSegment) getCurrentSegment(context);
71 double currentTimePct = getCurrentSegmentTimePct(context, waterConfigSegment);
72
73 frequency = (float) InterpolateUtil.interpolate(0,
74 1,
75 waterConfigSegment.getStartFrequency(),
76 waterConfigSegment.getEndFrequency(),
77 currentTimePct);
78
79 amplitude = (float) InterpolateUtil.interpolate(0,
80 1,
81 waterConfigSegment.getStartAmplitude(),
82 waterConfigSegment.getEndAmplitude(),
83 currentTimePct);
84
85 phase = (float) InterpolateUtil.interpolate(0,
86 1,
87 waterConfigSegment.getStartPhase(),
88 waterConfigSegment.getEndPhase(),
89 currentTimePct);
90 }
91
92 WaterFilter waterFilter = (WaterFilter) getImageFilter();
93 waterFilter.setWavelength(frequency);
94 waterFilter.setPhase((float) Math.toRadians(phase));
95 waterFilter.setAmplitude(amplitude);
96 waterFilter.setAntialias(true);
97 }
98
99 /**
100 * @return the name of the underlying filter class
101 */
102 protected String getImageFilterClassName() {
103 return WaterFilter.class.getName();
104 }
105
106 /**
107 * Always remember some attrs might not be there
108 * @param context
109 */
110 protected void initAttributes(IContext context) {
111 super.initAttributes(context);
112
113 ObjectUtil.initializeField("frequency", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
114 ObjectUtil.initializeField("amplitude", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
115 ObjectUtil.initializeField("phase", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
116
117 ObjectUtil.initializeField("startFrequency", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
118 ObjectUtil.initializeField("startAmplitude", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
119 ObjectUtil.initializeField("startPhase", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
120 ObjectUtil.initializeField("endFrequency", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
121 ObjectUtil.initializeField("endAmplitude", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
122 ObjectUtil.initializeField("endPhase", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
123 }
124
125 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
126 super.addJarlObjectInfo(jarlObjectInfo);
127 populateInfo(jarlObjectInfo, "frequency", "Frequency", JarlInfoUtil.PRIMITIVE_DISPLAY);
128 populateInfo(jarlObjectInfo, "amplitude", "Amplitude", JarlInfoUtil.PRIMITIVE_DISPLAY);
129 populateInfo(jarlObjectInfo, "phase", "Phase", JarlInfoUtil.PRIMITIVE_DISPLAY);
130
131 populateInfo(jarlObjectInfo, "startFrequency", "Start Frequency", JarlInfoUtil.PRIMITIVE_DISPLAY);
132 populateInfo(jarlObjectInfo, "startAmplitude", "Start Amplitude", JarlInfoUtil.PRIMITIVE_DISPLAY);
133 populateInfo(jarlObjectInfo, "startPhase", "Start Phase", JarlInfoUtil.PRIMITIVE_DISPLAY);
134 populateInfo(jarlObjectInfo, "endFrequency", "End Frequency", JarlInfoUtil.PRIMITIVE_DISPLAY);
135 populateInfo(jarlObjectInfo, "endAmplitude", "End Amplitude", JarlInfoUtil.PRIMITIVE_DISPLAY);
136 populateInfo(jarlObjectInfo, "endPhase", "End Phase", JarlInfoUtil.PRIMITIVE_DISPLAY);
137 }
138
139 /**
140 * Create a concrete {@link WidgetConfigSegment} based on this element
141 * @param element
142 * @return a specific {@link WidgetConfigSegment}
143 */
144 public WidgetConfigSegment createSegment(Element element) {
145 return new WaterConfigSegment(element);
146 }
147
148 protected static class WaterConfigSegment extends WidgetConfigSegment {
149
150 protected float m_startFrequency = Float.NEGATIVE_INFINITY;
151 protected float m_startAmplitude = Float.NEGATIVE_INFINITY;
152 protected float m_startPhase = Float.NEGATIVE_INFINITY;
153 protected float m_endFrequency = Float.NEGATIVE_INFINITY;
154 protected float m_endAmplitude = Float.NEGATIVE_INFINITY;
155 protected float m_endPhase = Float.NEGATIVE_INFINITY;
156
157 public WaterConfigSegment(Element element) {
158 super(element);
159
160 ObjectUtil.initializeFieldStrict("startFrequency", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
161 ObjectUtil.initializeFieldStrict("startAmplitude", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
162 ObjectUtil.initializeFieldStrict("startPhase", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
163 ObjectUtil.initializeFieldStrict("endFrequency", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
164 ObjectUtil.initializeFieldStrict("endAmplitude", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
165 ObjectUtil.initializeFieldStrict("endPhase", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
166 }
167
168 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
169 super.addJarlObjectInfo(jarlObjectInfo);
170 populateInfo(jarlObjectInfo, "startFrequency", "Start Frequency", JarlInfoUtil.PRIMITIVE_DISPLAY);
171 populateInfo(jarlObjectInfo, "startAmplitude", "Start Amplitude", JarlInfoUtil.PRIMITIVE_DISPLAY);
172 populateInfo(jarlObjectInfo, "startPhase", "Start Phase", JarlInfoUtil.PRIMITIVE_DISPLAY);
173 populateInfo(jarlObjectInfo, "endFrequency", "End Frequency", JarlInfoUtil.PRIMITIVE_DISPLAY);
174 populateInfo(jarlObjectInfo, "endAmplitude", "End Amplitude", JarlInfoUtil.PRIMITIVE_DISPLAY);
175 populateInfo(jarlObjectInfo, "endPhase", "End Phase", JarlInfoUtil.PRIMITIVE_DISPLAY);
176 }
177
178 public float getStartFrequency() {
179 return m_startFrequency;
180 }
181
182 public float getStartAmplitude() {
183 return m_startAmplitude;
184 }
185
186 public float getStartPhase() {
187 return m_startPhase;
188 }
189
190 public float getEndFrequency() {
191 return m_endFrequency;
192 }
193
194 public float getEndAmplitude() {
195 return m_endAmplitude;
196 }
197
198 public float getEndPhase() {
199 return m_endPhase;
200 }
201 }
202 }