Source code: com/arranger/jarl/filter/base/Lookup.java
1 package com.arranger.jarl.filter.base;
2
3 import com.arranger.jarl.base.IContext;
4 import com.arranger.jarl.base.IGradientManager;
5 import com.arranger.jarl.base.IJarlObjectInfo;
6 import com.arranger.jarl.filter.BaseSegmentFilter;
7 import com.arranger.jarl.util.*;
8 import com.jhlabs.image.Gradient;
9 import com.jhlabs.image.LookupFilter;
10 import org.w3c.dom.Element;
11
12 /**
13 * Lookup will replace colors in the image with a gradient
14 * @filterAttribute gradientIndex ## xs:integer ## the index of the gradient
15 *
16 * @filterAttribute startGradientIndex ## xs:integer ## the starting gradient index
17 * @filterAttribute endGradientIndex ## xs:integer ## the ending gradient index
18 */
19 public class Lookup extends BaseSegmentFilter {
20
21 protected int m_gradientIndex = Integer.MAX_VALUE;
22 protected int m_startGradientIndex = Integer.MAX_VALUE;
23 protected int m_endGradientIndex = Integer.MAX_VALUE;
24
25 /**
26 * @return the name of the underlying filter class
27 */
28 protected String getImageFilterClassName() {
29 return LookupFilter.class.getName();
30 }
31
32 /**
33 * Prepare the filter with any changes as necessary
34 * @param context the current context
35 */
36 protected void prepareFilter(IContext context) {
37 LookupFilter lookupFilter = (LookupFilter) getImageFilter();
38
39 IGradientManager gradientManager = context.getGradientManager();
40
41 Gradient gradient;
42 if (m_gradientIndex != Integer.MAX_VALUE) {
43 gradient = gradientManager.getGradient(m_gradientIndex);
44 } else if (m_startGradientIndex != Integer.MAX_VALUE && m_endGradientIndex != Integer.MAX_VALUE) {
45 //interpolate
46 double currentTimePct = InterpolateUtil.interpolate(this, context.getTime());
47 Gradient gradient1 = gradientManager.getGradient(m_startGradientIndex);
48 Gradient gradient2 = gradientManager.getGradient(m_endGradientIndex);
49 gradient = gradientManager.interpolate(gradient1, gradient2, currentTimePct);
50 } else {
51 LookupConfigSegment lookupConfigSegment = (LookupConfigSegment) getCurrentSegment(context);
52 double currentTimePct = getCurrentSegmentTimePct(context, lookupConfigSegment);
53
54 //interpolate
55 Gradient gradient1 = gradientManager.getGradient(lookupConfigSegment.getStartGradientIndex());
56 Gradient gradient2 = gradientManager.getGradient(lookupConfigSegment.getEndGradientIndex());
57 gradient = gradientManager.interpolate(gradient1, gradient2, currentTimePct);
58 }
59
60 lookupFilter.setColormap(gradient);
61 }
62
63 /**
64 * Always remember some attrs might not be there
65 * @param context
66 */
67 protected void initAttributes(IContext context) {
68 super.initAttributes(context);
69
70 ObjectUtil.initializeField("gradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
71 ObjectUtil.initializeField("startGradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
72 ObjectUtil.initializeField("endGradientIndex", m_configElement, this, ObjectUtil.PRIMITIVE_CONVERSION);
73 }
74
75 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
76 super.addJarlObjectInfo(jarlObjectInfo);
77 populateInfo(jarlObjectInfo, "gradientIndex", "Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
78 populateInfo(jarlObjectInfo, "startGradientIndex", "Start Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
79 populateInfo(jarlObjectInfo, "endGradientIndex", "End Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
80 }
81
82 /**
83 * Create a concrete {@link WidgetConfigSegment} based on this element
84 * @param element
85 * @return a specific {@link WidgetConfigSegment}
86 */
87 public WidgetConfigSegment createSegment(Element element) {
88 return new LookupConfigSegment(element);
89 }
90
91 public static class LookupConfigSegment extends WidgetConfigSegment {
92
93 protected int m_startGradientIndex = Integer.MAX_VALUE;
94 protected int m_endGradientIndex = Integer.MAX_VALUE;
95
96 public LookupConfigSegment(Element element) {
97 super(element);
98
99 ObjectUtil.initializeFieldStrict("startGradientIndex", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
100 ObjectUtil.initializeFieldStrict("endGradientIndex", element, this, ObjectUtil.PRIMITIVE_CONVERSION);
101 }
102
103 protected void addJarlObjectInfo(IJarlObjectInfo jarlObjectInfo) {
104 super.addJarlObjectInfo(jarlObjectInfo);
105 populateInfo(jarlObjectInfo, "startGradientIndex", "Start Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
106 populateInfo(jarlObjectInfo, "endGradientIndex", "End Gradient Index", JarlInfoUtil.PRIMITIVE_DISPLAY);
107 }
108
109 public int getStartGradientIndex() {
110 return m_startGradientIndex;
111 }
112
113 public int getEndGradientIndex() {
114 return m_endGradientIndex;
115 }
116 }
117 }