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.Graphics2D; 018 import java.awt.geom.AffineTransform; 019 import java.awt.image.BufferedImage; 020 021 import railo.runtime.engine.ThreadLocalPageContext; 022 import railo.runtime.exp.FunctionException; 023 import railo.runtime.exp.PageException; 024 import railo.runtime.img.ImageUtil; 025 import railo.runtime.type.KeyImpl; 026 import railo.runtime.type.Struct; 027 import railo.runtime.type.util.CollectionUtil; 028 029 /** 030 * A filter which crops an image to a given rectangle. 031 */ 032 public class CropFilter extends AbstractBufferedImageOp implements DynFiltering { 033 034 private int x; 035 private int y; 036 private int width; 037 private int height; 038 039 /** 040 * Construct a CropFilter. 041 */ 042 public CropFilter() { 043 this(0, 0, 32, 32); 044 } 045 046 /** 047 * Construct a CropFilter. 048 * @param x the left edge of the crop rectangle 049 * @param y the top edge of the crop rectangle 050 * @param width the width of the crop rectangle 051 * @param height the height of the crop rectangle 052 */ 053 public CropFilter(int x, int y, int width, int height) { 054 this.x = x; 055 this.y = y; 056 this.width = width; 057 this.height = height; 058 } 059 060 /** 061 * Set the left edge of the crop rectangle. 062 * @param x the left edge of the crop rectangle 063 * @see #getX 064 */ 065 public void setX(int x) { 066 this.x = x; 067 } 068 069 /** 070 * Get the left edge of the crop rectangle. 071 * @return the left edge of the crop rectangle 072 * @see #setX 073 */ 074 public int getX() { 075 return x; 076 } 077 078 /** 079 * Set the top edge of the crop rectangle. 080 * @param y the top edge of the crop rectangle 081 * @see #getY 082 */ 083 public void setY(int y) { 084 this.y = y; 085 } 086 087 /** 088 * Get the top edge of the crop rectangle. 089 * @return the top edge of the crop rectangle 090 * @see #setY 091 */ 092 public int getY() { 093 return y; 094 } 095 096 /** 097 * Set the width of the crop rectangle. 098 * @param width the width of the crop rectangle 099 * @see #getWidth 100 */ 101 public void setWidth(int width) { 102 this.width = width; 103 } 104 105 /** 106 * Get the width of the crop rectangle. 107 * @return the width of the crop rectangle 108 * @see #setWidth 109 */ 110 public int getWidth() { 111 return width; 112 } 113 114 /** 115 * Set the height of the crop rectangle. 116 * @param height the height of the crop rectangle 117 * @see #getHeight 118 */ 119 public void setHeight(int height) { 120 this.height = height; 121 } 122 123 /** 124 * Get the height of the crop rectangle. 125 * @return the height of the crop rectangle 126 * @see #setHeight 127 */ 128 public int getHeight() { 129 return height; 130 } 131 132 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 133 134 int w = src.getWidth(); 135 int h = src.getHeight(); 136 137 if(x<0)x=0; 138 if(y<0)y=0; 139 if(x>w)x=w; 140 if(y>h)y=h; 141 142 143 dst=ImageUtil.createBufferedImage(src,width,height); 144 145 146 147 148 Graphics2D g = dst.createGraphics(); 149 g.drawRenderedImage( src, AffineTransform.getTranslateInstance(-x, -y) ); 150 g.dispose(); 151 152 return dst; 153 } 154 155 public String toString() { 156 return "Distort/Crop"; 157 } 158 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException { 159 Object o; 160 if((o=parameters.removeEL(KeyImpl.init("X")))!=null)setX(ImageFilterUtil.toIntValue(o,"X")); 161 if((o=parameters.removeEL(KeyImpl.init("Y")))!=null)setY(ImageFilterUtil.toIntValue(o,"Y")); 162 if((o=parameters.removeEL(KeyImpl.init("Width")))!=null)setWidth(ImageFilterUtil.toIntValue(o,"Width")); 163 if((o=parameters.removeEL(KeyImpl.init("Height")))!=null)setHeight(ImageFilterUtil.toIntValue(o,"Height")); 164 165 // check for arguments not supported 166 if(parameters.size()>0) { 167 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 [X, Y, Width, Height]"); 168 } 169 170 return filter(src, (BufferedImage)null); 171 } 172 }