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.List; 025 import railo.runtime.type.Struct; 026 027 public class OffsetFilter extends TransformFilter implements DynFiltering { 028 029 private int width, height; 030 private int xOffset, yOffset; 031 private boolean wrap; 032 033 public OffsetFilter() { 034 this(0, 0, true); 035 } 036 037 public OffsetFilter(int xOffset, int yOffset, boolean wrap) { 038 super(ConvolveFilter.ZERO_EDGES ); 039 this.xOffset = xOffset; 040 this.yOffset = yOffset; 041 this.wrap = wrap; 042 } 043 044 public void setXOffset(int xOffset) { 045 this.xOffset = xOffset; 046 } 047 048 public int getXOffset() { 049 return xOffset; 050 } 051 052 public void setYOffset(int yOffset) { 053 this.yOffset = yOffset; 054 } 055 056 public int getYOffset() { 057 return yOffset; 058 } 059 060 public void setWrap(boolean wrap) { 061 this.wrap = wrap; 062 } 063 064 public boolean getWrap() { 065 return wrap; 066 } 067 068 protected void transformInverse(int x, int y, float[] out) { 069 if ( wrap ) { 070 out[0] = (x+width-xOffset) % width; 071 out[1] = (y+height-yOffset) % height; 072 } else { 073 out[0] = x-xOffset; 074 out[1] = y-yOffset; 075 } 076 } 077 078 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 079 this.width = src.getWidth(); 080 this.height = src.getHeight(); 081 if ( wrap ) { 082 while (xOffset < 0) 083 xOffset += width; 084 while (yOffset < 0) 085 yOffset += height; 086 xOffset %= width; 087 yOffset %= height; 088 } 089 return super.filter( src, dst ); 090 } 091 092 public String toString() { 093 return "Distort/Offset..."; 094 } 095 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 096 Object o; 097 if((o=parameters.removeEL(KeyImpl.init("XOffset")))!=null)setXOffset(ImageFilterUtil.toIntValue(o,"XOffset")); 098 if((o=parameters.removeEL(KeyImpl.init("YOffset")))!=null)setYOffset(ImageFilterUtil.toIntValue(o,"YOffset")); 099 if((o=parameters.removeEL(KeyImpl.init("Wrap")))!=null)setWrap(ImageFilterUtil.toBooleanValue(o,"Wrap")); 100 if((o=parameters.removeEL(KeyImpl.init("EdgeAction")))!=null)setEdgeAction(ImageFilterUtil.toString(o,"EdgeAction")); 101 if((o=parameters.removeEL(KeyImpl.init("Interpolation")))!=null)setInterpolation(ImageFilterUtil.toString(o,"Interpolation")); 102 103 // check for arguments not supported 104 if(parameters.size()>0) { 105 throw new FunctionException(ThreadLocalPageContext.get(), "ImageFilter", 3, "parameters", "the parameter"+(parameters.size()>1?"s":"")+" ["+List.arrayToList(parameters.keysAsString(),", ")+"] "+(parameters.size()>1?"are":"is")+" not allowed, only the following parameters are supported [XOffset, YOffset, Wrap, EdgeAction, Interpolation]"); 106 } 107 108 return filter(src, dst); 109 } 110 }