001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019/* 020* 021 022Licensed under the Apache License, Version 2.0 (the "License"); 023you may not use this file except in compliance with the License. 024You may obtain a copy of the License at 025 026 http://www.apache.org/licenses/LICENSE-2.0 027 028Unless required by applicable law or agreed to in writing, software 029distributed under the License is distributed on an "AS IS" BASIS, 030WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 031See the License for the specific language governing permissions and 032limitations under the License. 033*/ 034 035package lucee.runtime.img.filter;import java.awt.Graphics2D; 036import java.awt.image.BufferedImage; 037import java.awt.image.ColorModel; 038 039import lucee.runtime.engine.ThreadLocalPageContext; 040import lucee.runtime.exp.FunctionException; 041import lucee.runtime.exp.PageException; 042import lucee.runtime.img.ImageUtil; 043import lucee.runtime.type.KeyImpl; 044import lucee.runtime.type.Struct; 045import lucee.runtime.type.util.CollectionUtil; 046 047/** 048 * A filter which tiles an image into a lerger one. 049 */ 050public class TileImageFilter extends AbstractBufferedImageOp implements DynFiltering { 051 052 private int width; 053 private int height; 054 private int tileWidth; 055 private int tileHeight; 056 057 /** 058 * Construct a TileImageFilter. 059 */ 060 public TileImageFilter() { 061 this(32, 32); 062 } 063 064 /** 065 * Construct a TileImageFilter. 066 * @param width the output image width 067 * @param height the output image height 068 */ 069 public TileImageFilter(int width, int height) { 070 this.width = width; 071 this.height = height; 072 } 073 074 /** 075 * Set the output image width. 076 * @param width the width 077 * @see #getWidth 078 */ 079 public void setWidth(int width) { 080 this.width = width; 081 } 082 083 /** 084 * Get the output image width. 085 * @return the width 086 * @see #setWidth 087 */ 088 public int getWidth() { 089 return width; 090 } 091 092 /** 093 * Set the output image height. 094 * @param height the height 095 * @see #getHeight 096 */ 097 public void setHeight(int height) { 098 this.height = height; 099 } 100 101 /** 102 * Get the output image height. 103 * @return the height 104 * @see #setHeight 105 */ 106 public int getHeight() { 107 return height; 108 } 109 110 public BufferedImage filter( BufferedImage src, BufferedImage dst ) { 111 int tileWidth = src.getWidth(); 112 int tileHeight = src.getHeight(); 113 114 if ( dst == null ) { 115 ColorModel dstCM = src.getColorModel(); 116 dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(width, height), dstCM.isAlphaPremultiplied(), null); 117 } 118 119 Graphics2D g = dst.createGraphics(); 120 for ( int y = 0; y < height; y += tileHeight) { 121 for ( int x = 0; x < width; x += tileWidth ) { 122 g.drawImage( src, null, x, y ); 123 } 124 } 125 g.dispose(); 126 127 return dst; 128 } 129 130 public String toString() { 131 return "Tile"; 132 } 133 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 134 Object o; 135 if((o=parameters.removeEL(KeyImpl.init("Width")))!=null)setWidth(ImageFilterUtil.toIntValue(o,"Width")); 136 if((o=parameters.removeEL(KeyImpl.init("Height")))!=null)setHeight(ImageFilterUtil.toIntValue(o,"Height")); 137 138 // check for arguments not supported 139 if(parameters.size()>0) { 140 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]"); 141 } 142 143 return filter(src, dst); 144 } 145}