Source code: com/arranger/jarl/filter/base/Sphere.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.SphereFilter;
11 import org.w3c.dom.Element;
12
13 /**
14 * Spere makes a sphere out of the image
15 *
16 * @filterAttribute refraction ## xs:float ## the refraction of the sphere (between 1 & 3)
17 * @filterAttribute startRefraction ## xs:float ## the start refraction of the sphere (between 1 & 3)
18 * @filterAttribute endRefraction ## xs:float ## the end refraction of the sphere (between 1 & 3)
19 */
20 public class Sphere extends BaseSegmentFilter {
21
22 protected float m_refraction = Float.NEGATIVE_INFINITY;
23 protected float m_startRefraction = Float.NEGATIVE_INFINITY;
24 protected float m_endRefraction = Float.NEGATIVE_INFINITY;
25
26 /**
27 * Prepare the filter with any changes as necessary
28 * @param context the current context
29 */
30 protected void prepareFilter(IContext context) {
31 float refraction;
32 if (m_refraction != Float.NEGATIVE_INFINITY) {
33 refraction = m_refraction;
34 } else if (m_startRefraction != Float.NEGATIVE_INFINITY && m_endRefraction != Float.NEGATIVE_INFINITY) {
35 refraction = (float) InterpolateUtil.interpolate(getStartTime(),
36 getEndTime(),
37 m_startRefraction,
38 m_endRefraction,
39 context.getTime());
40 } else {
41 SphereConfigSegment sphereConfigSegment = (SphereConfigSegment) getCurrentSegment(context);
42 double currentTimePct = getCurrentSegmentTimePct(context, sphereConfigSegment);
43
44 refraction = (float) InterpolateUtil.interpolate(0,
45 1,
46 sphereConfigSegment.getStartRefraction(),
47 sphereConfigSegment.getEndRefraction(),
48 currentTimePct);
49 }
50
51 ((SphereFilter) getImageFilter()).setRefractionIndex(refraction);
52 }
53
54 /**
55 * @return the name of the underlying filter class
56 */
57 protected String getImageFilterClassName() {
58 return SphereFilter.class.getName();
59 }
60
61 /**
62 * Always remember some attrs might not be there
63 * @param context
64 */
65 protected void initAttributes(IContext context) {
66 super.initAttributes(context);
67
68 ObjectUtil.initializeField("refraction", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
69 ObjectUtil.initializeField("startRefraction", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
70 ObjectUtil.initializeField("endRefraction", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
71 }
72
73 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
74 super.addJarlObjectInfo(jarlObjectInfo);
75 populateInfo(jarlObjectInfo, "refraction", "Refraction", JarlInfoUtil.PRIMITIVE_DISPLAY);
76 populateInfo(jarlObjectInfo, "startRefraction", "Start Refraction", JarlInfoUtil.PRIMITIVE_DISPLAY);
77 populateInfo(jarlObjectInfo, "endRefraction", "End Refraction", JarlInfoUtil.PRIMITIVE_DISPLAY);
78 }
79
80 /**
81 * Create a concrete {@link WidgetConfigSegment} based on this element
82 * @param element
83 * @return a specific {@link WidgetConfigSegment}
84 */
85 public WidgetConfigSegment createSegment(Element element) {
86 return new SphereConfigSegment(element);
87 }
88
89 protected static class SphereConfigSegment extends WidgetConfigSegment {
90
91 protected float m_startRefraction = Float.NEGATIVE_INFINITY;
92 protected float m_endRefraction = Float.NEGATIVE_INFINITY;
93
94 public SphereConfigSegment(Element element) {
95 super(element);
96
97 ObjectUtil.initializeFieldStrict("startRefraction", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
98 ObjectUtil.initializeFieldStrict("endRefraction", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
99 }
100
101 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
102 super.addJarlObjectInfo(jarlObjectInfo);
103 populateInfo(jarlObjectInfo, "startRefraction", "Start Refraction", JarlInfoUtil.PRIMITIVE_DISPLAY);
104 populateInfo(jarlObjectInfo, "endRefraction", "End Refraction", JarlInfoUtil.PRIMITIVE_DISPLAY);
105 }
106
107 public float getStartRefraction() {
108 return m_startRefraction;
109 }
110
111 public float getEndRefraction() {
112 return m_endRefraction;
113 }
114 }
115 }