001 /* 002 * 003 004 Licensed under the Apache License, Version 2.0 (the "License"); 005 you may not use this file except in compliance with the License. 006 You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, 012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 See the License for the specific language governing permissions and 014 limitations under the License. 015 */ 016 017 package railo.runtime.img.filter;import java.awt.image.BufferedImage; 018 019 import railo.runtime.engine.ThreadLocalPageContext; 020 import railo.runtime.exp.FunctionException; 021 import railo.runtime.exp.PageException; 022 import railo.runtime.img.ImageUtil; 023 import railo.runtime.type.KeyImpl; 024 import railo.runtime.type.Struct; 025 import railo.runtime.type.util.CollectionUtil; 026 027 028 029 /** 030 * A filter which draws a gradient interpolated between four colors defined at the corners of the image. 031 */ 032 public class FourColorFilter extends PointFilter implements DynFiltering { 033 034 private int width; 035 private int height; 036 private int colorNW; 037 private int colorNE; 038 private int colorSW; 039 private int colorSE; 040 private int rNW, gNW, bNW; 041 private int rNE, gNE, bNE; 042 private int rSW, gSW, bSW; 043 private int rSE, gSE, bSE; 044 045 public FourColorFilter() { 046 setColorNW( 0xffff0000 ); 047 setColorNE( 0xffff00ff ); 048 setColorSW( 0xff0000ff ); 049 setColorSE( 0xff00ffff ); 050 } 051 052 public void setColorNW( int color ) { 053 this.colorNW = color; 054 rNW = (color >> 16) & 0xff; 055 gNW = (color >> 8) & 0xff; 056 bNW = color & 0xff; 057 } 058 059 public int getColorNW() { 060 return colorNW; 061 } 062 063 public void setColorNE( int color ) { 064 this.colorNE = color; 065 rNE = (color >> 16) & 0xff; 066 gNE = (color >> 8) & 0xff; 067 bNE = color & 0xff; 068 } 069 070 public int getColorNE() { 071 return colorNE; 072 } 073 074 public void setColorSW( int color ) { 075 this.colorSW = color; 076 rSW = (color >> 16) & 0xff; 077 gSW = (color >> 8) & 0xff; 078 bSW = color & 0xff; 079 } 080 081 public int getColorSW() { 082 return colorSW; 083 } 084 085 public void setColorSE( int color ) { 086 this.colorSE = color; 087 rSE = (color >> 16) & 0xff; 088 gSE = (color >> 8) & 0xff; 089 bSE = color & 0xff; 090 } 091 092 public int getColorSE() { 093 return colorSE; 094 } 095 096 public void setDimensions(int width, int height) { 097 this.width = width; 098 this.height = height; 099 super.setDimensions(width, height); 100 } 101 102 public int filterRGB(int x, int y, int rgb) { 103 float fx = (float)x / width; 104 float fy = (float)y / height; 105 float p, q; 106 107 p = rNW + (rNE - rNW) * fx; 108 q = rSW + (rSE - rSW) * fx; 109 int r = (int)( p + (q - p) * fy + 0.5f ); 110 111 p = gNW + (gNE - gNW) * fx; 112 q = gSW + (gSE - gSW) * fx; 113 int g = (int)( p + (q - p) * fy + 0.5f ); 114 115 p = bNW + (bNE - bNW) * fx; 116 q = bSW + (bSE - bSW) * fx; 117 int b = (int)( p + (q - p) * fy + 0.5f ); 118 119 return 0xff000000 | (r << 16) | (g << 8) | b; 120 } 121 122 public String toString() { 123 return "Texture/Four Color Fill..."; 124 } 125 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 126 Object o; 127 if((o=parameters.removeEL(KeyImpl.init("ColorNW")))!=null)setColorNW(ImageFilterUtil.toIntValue(o,"ColorNW")); 128 if((o=parameters.removeEL(KeyImpl.init("ColorNE")))!=null)setColorNE(ImageFilterUtil.toIntValue(o,"ColorNE")); 129 if((o=parameters.removeEL(KeyImpl.init("ColorSW")))!=null)setColorSW(ImageFilterUtil.toIntValue(o,"ColorSW")); 130 if((o=parameters.removeEL(KeyImpl.init("ColorSE")))!=null)setColorSE(ImageFilterUtil.toIntValue(o,"ColorSE")); 131 if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){ 132 int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions"); 133 setDimensions(dim[0],dim[1]); 134 } 135 136 // check for arguments not supported 137 if(parameters.size()>0) { 138 throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+CollectionUtil.getKeyList(parameters,", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [ColorNW, ColorNE, ColorSW, ColorSE, Dimensions]"); 139 } 140 141 return filter(src, dst); 142 } 143 }