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.image.BufferedImage;
019    import java.awt.image.ColorModel;
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 tiles an image into a lerger one.
031     */
032    public class TileImageFilter extends AbstractBufferedImageOp  implements DynFiltering {
033    
034            private int width;
035            private int height;
036            private int tileWidth;
037            private int tileHeight;
038    
039            /**
040         * Construct a TileImageFilter.
041         */
042        public TileImageFilter() {
043                    this(32, 32);
044            }
045    
046            /**
047         * Construct a TileImageFilter.
048         * @param width the output image width
049         * @param height the output image height
050         */
051            public TileImageFilter(int width, int height) {
052                    this.width = width;
053                    this.height = height;
054            }
055    
056            /**
057         * Set the output image width.
058         * @param width the width
059         * @see #getWidth
060         */
061            public void setWidth(int width) {
062                    this.width = width;
063            }
064    
065            /**
066         * Get the output image width.
067         * @return the width
068         * @see #setWidth
069         */
070            public int getWidth() {
071                    return width;
072            }
073    
074            /**
075         * Set the output image height.
076         * @param height the height
077         * @see #getHeight
078         */
079            public void setHeight(int height) {
080                    this.height = height;
081            }
082    
083            /**
084         * Get the output image height.
085         * @return the height
086         * @see #setHeight
087         */
088            public int getHeight() {
089                    return height;
090            }
091    
092        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
093            int tileWidth = src.getWidth();
094            int tileHeight = src.getHeight();
095    
096            if ( dst == null ) {
097                ColorModel dstCM = src.getColorModel();
098                            dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height), dstCM.isAlphaPremultiplied(), null);
099                    }
100    
101                    Graphics2D g = dst.createGraphics();
102                    for ( int y = 0; y < height; y += tileHeight) {
103                            for ( int x = 0; x < width; x += tileWidth ) {
104                                    g.drawImage( src, null, x, y );
105                            }
106                    }
107                    g.dispose();
108    
109            return dst;
110        }
111    
112            public String toString() {
113                    return "Tile";
114            }
115            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
116                    Object o;
117                    if((o=parameters.removeEL(KeyImpl.init("Width")))!=null)setWidth(ImageFilterUtil.toIntValue(o,"Width"));
118                    if((o=parameters.removeEL(KeyImpl.init("Height")))!=null)setHeight(ImageFilterUtil.toIntValue(o,"Height"));
119    
120                    // check for arguments not supported
121                    if(parameters.size()>0) {
122                            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 [Width, Height]");
123                    }
124    
125                    return filter(src, dst);
126            }
127    }