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 * Sets the opacity (alpha) of every pixel in an image to a constant value. 031 */ 032 public class OpacityFilter extends PointFilter implements DynFiltering { 033 034 private int opacity; 035 private int opacity24; 036 037 /** 038 * Construct an OpacityFilter with 50% opacity. 039 */ 040 public OpacityFilter() { 041 this(0x88); 042 } 043 044 /** 045 * Construct an OpacityFilter with the given opacity (alpha). 046 * @param opacity the opacity (alpha) in the range 0..255 047 */ 048 public OpacityFilter(int opacity) { 049 setOpacity(opacity); 050 } 051 052 /** 053 * Set the opacity. 054 * @param opacity the opacity (alpha) in the range 0..255 055 * @see #getOpacity 056 */ 057 public void setOpacity(int opacity) { 058 this.opacity = opacity; 059 opacity24 = opacity << 24; 060 } 061 062 /** 063 * Get the opacity setting. 064 * @return the opacity 065 * @see #setOpacity 066 */ 067 public int getOpacity() { 068 return opacity; 069 } 070 071 public int filterRGB(int x, int y, int rgb) { 072 if ((rgb & 0xff000000) != 0) 073 return (rgb & 0xffffff) | opacity24; 074 return rgb; 075 } 076 077 public String toString() { 078 return "Colors/Transparency..."; 079 } 080 081 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 082 Object o; 083 if((o=parameters.removeEL(KeyImpl.init("Opacity")))!=null)setOpacity(ImageFilterUtil.toIntValue(o,"Opacity")); 084 if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){ 085 int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions"); 086 setDimensions(dim[0],dim[1]); 087 } 088 089 // check for arguments not supported 090 if(parameters.size()>0) { 091 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 [Opacity, Dimensions]"); 092 } 093 094 return filter(src, dst); 095 } 096 } 097