1 18 package org.apache.batik.ext.awt.image.renderable; 19 20 import java.awt.image.RenderedImage ; 21 import java.awt.image.renderable.RenderContext ; 22 23 import org.apache.batik.ext.awt.image.ComponentTransferFunction; 24 import org.apache.batik.ext.awt.image.DiscreteTransfer; 25 import org.apache.batik.ext.awt.image.GammaTransfer; 26 import org.apache.batik.ext.awt.image.IdentityTransfer; 27 import org.apache.batik.ext.awt.image.LinearTransfer; 28 import org.apache.batik.ext.awt.image.TableTransfer; 29 import org.apache.batik.ext.awt.image.TransferFunction; 30 import org.apache.batik.ext.awt.image.rendered.ComponentTransferRed; 31 32 39 public class ComponentTransferRable8Bit 40 extends AbstractColorInterpolationRable 41 implements ComponentTransferRable { 42 43 public static final int ALPHA = 0; 44 public static final int RED = 1; 45 public static final int GREEN = 2; 46 public static final int BLUE = 3; 47 48 52 private ComponentTransferFunction 53 functions[] = new ComponentTransferFunction[4]; 54 55 59 private TransferFunction 60 txfFunc[] = new TransferFunction[4]; 61 62 public ComponentTransferRable8Bit(Filter src, 63 ComponentTransferFunction alphaFunction, 64 ComponentTransferFunction redFunction, 65 ComponentTransferFunction greenFunction, 66 ComponentTransferFunction blueFunction){ 67 super(src, null); 68 setAlphaFunction(alphaFunction); 69 setRedFunction(redFunction); 70 setGreenFunction(greenFunction); 71 setBlueFunction(blueFunction); 72 } 73 74 77 public void setSource(Filter src){ 78 init(src, null); 79 } 80 81 84 public Filter getSource(){ 85 return (Filter)getSources().get(0); 86 } 87 88 91 public ComponentTransferFunction getAlphaFunction(){ 92 return functions[ALPHA]; 93 } 94 95 98 public void setAlphaFunction(ComponentTransferFunction alphaFunction){ 99 touch(); 100 functions[ALPHA] = alphaFunction; 101 txfFunc[ALPHA] = null; 102 } 103 104 107 public ComponentTransferFunction getRedFunction(){ 108 return functions[RED]; 109 } 110 111 114 public void setRedFunction(ComponentTransferFunction redFunction){ 115 touch(); 116 functions[RED] = redFunction; 117 txfFunc[RED] = null; 118 } 119 120 123 public ComponentTransferFunction getGreenFunction(){ 124 return functions[GREEN]; 125 } 126 127 130 public void setGreenFunction(ComponentTransferFunction greenFunction){ 131 touch(); 132 functions[GREEN] = greenFunction; 133 txfFunc[GREEN] = null; 134 } 135 136 139 public ComponentTransferFunction getBlueFunction(){ 140 return functions[BLUE]; 141 } 142 143 146 public void setBlueFunction(ComponentTransferFunction blueFunction){ 147 touch(); 148 functions[BLUE] = blueFunction; 149 txfFunc[BLUE] = null; 150 } 151 152 public RenderedImage createRendering(RenderContext rc){ 153 RenderedImage srcRI = getSource().createRendering(rc); 157 158 if(srcRI == null) 159 return null; 160 161 return new ComponentTransferRed(convertSourceCS(srcRI), 162 getTransferFunctions(), 163 rc.getRenderingHints()); 164 } 165 166 170 private TransferFunction[] getTransferFunctions(){ 171 TransferFunction txfFunc[] = new TransferFunction[4]; 176 System.arraycopy(this.txfFunc, 0, txfFunc, 0, 4); 177 178 ComponentTransferFunction functions[]; 179 functions = new ComponentTransferFunction[4]; 180 System.arraycopy(this.functions, 0, functions, 0, 4); 181 182 for(int i=0; i<4; i++){ 183 if(txfFunc[i] == null){ 184 txfFunc[i] = getTransferFunction(functions[i]); 185 synchronized(this.functions){ 186 if(this.functions[i] == functions[i]){ 187 this.txfFunc[i] = txfFunc[i]; 188 } 189 } 190 } 191 } 192 193 return txfFunc; 194 } 195 196 199 private static TransferFunction getTransferFunction 200 (ComponentTransferFunction function){ 201 202 TransferFunction txfFunc = null; 203 if(function == null){ 204 txfFunc = new IdentityTransfer(); 205 } 206 else{ 207 switch(function.getType()){ 208 case ComponentTransferFunction.IDENTITY: 209 txfFunc = new IdentityTransfer(); 210 break; 211 case ComponentTransferFunction.TABLE: 212 txfFunc = new TableTransfer(tableFloatToInt(function.getTableValues())); 213 break; 214 case ComponentTransferFunction.DISCRETE: 215 txfFunc = new DiscreteTransfer(tableFloatToInt(function.getTableValues())); 216 break; 217 case ComponentTransferFunction.LINEAR: 218 txfFunc = new LinearTransfer(function.getSlope(), 219 function.getIntercept()); 220 break; 221 case ComponentTransferFunction.GAMMA: 222 txfFunc = new GammaTransfer(function.getAmplitude(), 223 function.getExponent(), 224 function.getOffset()); 225 break; 226 default: 227 throw new Error (); 229 } 230 } 231 232 return txfFunc; 233 } 234 235 238 private static int[] tableFloatToInt(float tableValues[]){ 239 int values[] = new int[tableValues.length]; 240 for(int i=0; i<tableValues.length; i++){ 241 values[i] = (int)(tableValues[i]*255f); 242 } 243 244 return values; 245 } 246 247 } 248 | Popular Tags |