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 * A filter which interpolates between two images. You can set the interpolation factor outside the range 0 to 1 029 * to extrapolate images. 030 */ 031 public class InterpolateFilter extends AbstractBufferedImageOp implements DynFiltering { 032 033 private BufferedImage destination; 034 private float interpolation; 035 036 public InterpolateFilter() { 037 } 038 039 /** 040 * Set the destination image. 041 * @param destination the destination image 042 * @see #getDestination 043 */ 044 public void setDestination( BufferedImage destination ) { 045 this.destination = destination; 046 } 047 048 /** 049 * Get the destination image. 050 * @return the destination image 051 * @see #setDestination 052 */ 053 public BufferedImage getDestination() { 054 return destination; 055 } 056 057 /** 058 * Set the interpolation factor. 059 * @param interpolation the interpolation factor 060 * @see #getInterpolation 061 */ 062 public void setInterpolation( float interpolation ) { 063 this.interpolation = interpolation; 064 } 065 066 /** 067 * Get the interpolation factor. 068 * @return the interpolation factor 069 * @see #setInterpolation 070 */ 071 public float getInterpolation() { 072 return interpolation; 073 } 074 075 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 076 int width = src.getWidth(); 077 int height = src.getHeight(); 078 src.getType(); 079 src.getRaster(); 080 081 if ( dst == null ) 082 dst = createCompatibleDestImage( src, null ); 083 dst.getRaster(); 084 085 if ( destination != null ) { 086 width = Math.min( width, destination.getWidth() ); 087 height = Math.min( height, destination.getWidth() ); 088 int[] pixels1 = null; 089 int[] pixels2 = null; 090 091 for (int y = 0; y < height; y++) { 092 pixels1 = getRGB( src, 0, y, width, 1, pixels1 ); 093 pixels2 = getRGB( destination, 0, y, width, 1, pixels2 ); 094 for (int x = 0; x < width; x++) { 095 int rgb1 = pixels1[x]; 096 int rgb2 = pixels2[x]; 097 int a1 = (rgb1 >> 24) & 0xff; 098 int r1 = (rgb1 >> 16) & 0xff; 099 int g1 = (rgb1 >> 8) & 0xff; 100 int b1 = rgb1 & 0xff; 101 //int a2 = (rgb2 >> 24) & 0xff; 102 int r2 = (rgb2 >> 16) & 0xff; 103 int g2 = (rgb2 >> 8) & 0xff; 104 int b2 = rgb2 & 0xff; 105 r1 = PixelUtils.clamp( ImageMath.lerp( interpolation, r1, r2 ) ); 106 g1 = PixelUtils.clamp( ImageMath.lerp( interpolation, g1, g2 ) ); 107 b1 = PixelUtils.clamp( ImageMath.lerp( interpolation, b1, b2 ) ); 108 pixels1[x] = (a1 << 24) | (r1 << 16) | (g1 << 8) | b1; 109 } 110 setRGB( dst, 0, y, width, 1, pixels1 ); 111 } 112 } 113 114 return dst; 115 } 116 117 public String toString() { 118 return "Effects/Interpolate..."; 119 } 120 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 121 Object o; 122 if((o=parameters.removeEL(KeyImpl.init("Interpolation")))!=null)setInterpolation(ImageFilterUtil.toFloatValue(o,"Interpolation")); 123 if((o=parameters.removeEL(KeyImpl.init("destination")))!=null)setDestination(ImageFilterUtil.toBufferedImage(o,"destination")); 124 125 // check for arguments not supported 126 if(parameters.size()>0) { 127 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 [Interpolation]"); 128 } 129 130 return filter(src, dst); 131 } 132 }