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.Color; 018 import java.awt.Graphics2D; 019 import java.awt.Paint; 020 import java.awt.geom.AffineTransform; 021 import java.awt.image.BufferedImage; 022 023 import railo.runtime.engine.ThreadLocalPageContext; 024 import railo.runtime.exp.FunctionException; 025 import railo.runtime.exp.PageException; 026 import railo.runtime.img.ImageUtil; 027 import railo.runtime.type.KeyImpl; 028 import railo.runtime.type.List; 029 import railo.runtime.type.Struct; 030 031 /** 032 * A filter to add a border around an image using the supplied Paint, which may be null for no painting. 033 */ 034 public class BorderFilter extends AbstractBufferedImageOp implements DynFiltering { 035 036 private int leftBorder, rightBorder; 037 private int topBorder, bottomBorder; 038 private Paint borderPaint; 039 040 /** 041 * Construct a BorderFilter which does nothing. 042 */ 043 public BorderFilter() { 044 } 045 046 /** 047 * Construct a BorderFilter. 048 * @param leftBorder the left border value 049 * @param topBorder the top border value 050 * @param rightBorder the right border value 051 * @param bottomBorder the bottom border value 052 * @param borderPaint the paint with which to fill the border 053 */ 054 public BorderFilter( int leftBorder, int topBorder, int rightBorder, int bottomBorder, Paint borderPaint ) { 055 this.leftBorder = leftBorder; 056 this.topBorder = topBorder; 057 this.rightBorder = rightBorder; 058 this.bottomBorder = bottomBorder; 059 this.borderPaint = borderPaint; 060 } 061 062 /** 063 * Set the border size on the left edge. 064 * @param leftBorder the number of pixels of border to add to the edge 065 * @min-value 0 066 * @see #getLeftBorder 067 */ 068 public void setLeft(int leftBorder) { 069 this.leftBorder = leftBorder; 070 } 071 072 /** 073 * Returns the left border value. 074 * @return the left border value. 075 * @see #setLeftBorder 076 */ 077 public int getLeftBorder() { 078 return leftBorder; 079 } 080 081 /** 082 * Set the border size on the right edge. 083 * @param rightBorder the number of pixels of border to add to the edge 084 * @min-value 0 085 * @see #getRightBorder 086 */ 087 public void setRight(int rightBorder) { 088 this.rightBorder = rightBorder; 089 } 090 091 /** 092 * Returns the right border value. 093 * @return the right border value. 094 * @see #setRightBorder 095 */ 096 public int getRightBorder() { 097 return rightBorder; 098 } 099 100 /** 101 * Set the border size on the top edge. 102 * @param topBorder the number of pixels of border to add to the edge 103 * @min-value 0 104 * @see #getTopBorder 105 */ 106 public void setTop(int topBorder) { 107 this.topBorder = topBorder; 108 } 109 110 /** 111 * Returns the top border value. 112 * @return the top border value. 113 * @see #setTopBorder 114 */ 115 public int getTopBorder() { 116 return topBorder; 117 } 118 119 /** 120 * Set the border size on the bottom edge. 121 * @param bottomBorder the number of pixels of border to add to the edge 122 * @min-value 0 123 * @see #getBottomBorder 124 */ 125 public void setBottom(int bottomBorder) { 126 this.bottomBorder = bottomBorder; 127 } 128 129 /** 130 * Returns the border border value. 131 * @return the border border value. 132 * @see #setBottomBorder 133 */ 134 public int getBottomBorder() { 135 return bottomBorder; 136 } 137 138 /** 139 * Set the border color. 140 * @param borderColor the color with which to fill the border 141 */ 142 public void setColor( Color borderColor ) { 143 this.borderPaint = borderColor; 144 } 145 146 /** 147 * Get the border paint. 148 * @return the paint with which to fill the border 149 * @see #setBorderPaint 150 */ 151 public Paint getBorderPaint() { 152 return borderPaint; 153 } 154 155 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 156 int width = src.getWidth(); 157 int height = src.getHeight(); 158 159 int totalWidth=width+leftBorder+rightBorder; 160 int totalHeight=height+topBorder+bottomBorder; 161 162 dst=ImageUtil.createBufferedImage(src,totalWidth,totalHeight); 163 164 165 Graphics2D g = dst.createGraphics(); 166 if ( borderPaint != null ) { 167 g.setPaint( borderPaint ); 168 if ( leftBorder > 0 ) g.fillRect( 0, 0, leftBorder, totalHeight ); 169 if ( rightBorder > 0 ) g.fillRect( totalWidth-rightBorder, 0, rightBorder, totalHeight ); 170 if ( topBorder > 0 ) g.fillRect( leftBorder, 0, totalWidth-leftBorder-rightBorder, topBorder ); 171 if ( bottomBorder > 0 ) g.fillRect( leftBorder, totalHeight-bottomBorder, totalWidth-leftBorder-rightBorder, bottomBorder ); 172 } 173 g.drawRenderedImage( src, AffineTransform.getTranslateInstance( leftBorder, rightBorder ) ); 174 g.dispose(); 175 return dst; 176 } 177 178 public String toString() { 179 return "Distort/Border..."; 180 } 181 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException { 182 183 Object o; 184 if((o=parameters.removeEL(KeyImpl.init("Left")))!=null)setLeft(ImageFilterUtil.toIntValue(o,"Left")); 185 if((o=parameters.removeEL(KeyImpl.init("Right")))!=null)setRight(ImageFilterUtil.toIntValue(o,"Right")); 186 if((o=parameters.removeEL(KeyImpl.init("Top")))!=null)setTop(ImageFilterUtil.toIntValue(o,"Top")); 187 if((o=parameters.removeEL(KeyImpl.init("Bottom")))!=null)setBottom(ImageFilterUtil.toIntValue(o,"Bottom")); 188 if((o=parameters.removeEL(KeyImpl.init("Color")))!=null)setColor(ImageFilterUtil.toColor(o,"Color")); 189 190 // check for arguments not supported 191 if(parameters.size()>0) { 192 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 [LeftBorder, RightBorder, TopBorder, BottomBorder, BorderPaint]"); 193 } 194 195 196 //BufferedImage dst=ImageUtil.createBufferedImage(src); 197 198 199 return filter(src, (BufferedImage)null); 200 } 201 }