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.Rectangle; 036import java.awt.image.BufferedImage; 037 038import lucee.runtime.engine.ThreadLocalPageContext; 039import lucee.runtime.exp.FunctionException; 040import lucee.runtime.exp.PageException; 041import lucee.runtime.img.ImageUtil; 042import lucee.runtime.type.KeyImpl; 043import lucee.runtime.type.Struct; 044import lucee.runtime.type.util.CollectionUtil; 045 046/** 047 * A filter which produces a "oil-painting" effect. 048 */ 049public class OilFilter extends WholeImageFilter implements DynFiltering { 050 051 private int range = 3; 052 private int levels = 256; 053 054 public OilFilter() { 055 } 056 057 /** 058 * Set the range of the effect in pixels. 059 * @param range the range 060 * @see #getRange 061 */ 062 public void setRange( int range ) { 063 this.range = range; 064 } 065 066 /** 067 * Get the range of the effect in pixels. 068 * @return the range 069 * @see #setRange 070 */ 071 public int getRange() { 072 return range; 073 } 074 075 /** 076 * Set the number of levels for the effect. 077 * @param levels the number of levels 078 * @see #getLevels 079 */ 080 public void setLevels( int levels ) { 081 this.levels = levels; 082 } 083 084 /** 085 * Get the number of levels for the effect. 086 * @return the number of levels 087 * @see #setLevels 088 */ 089 public int getLevels() { 090 return levels; 091 } 092 093 protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) { 094 int index = 0; 095 int[] rHistogram = new int[levels]; 096 int[] gHistogram = new int[levels]; 097 int[] bHistogram = new int[levels]; 098 int[] rTotal = new int[levels]; 099 int[] gTotal = new int[levels]; 100 int[] bTotal = new int[levels]; 101 int[] outPixels = new int[width * height]; 102 103 for (int y = 0; y < height; y++) { 104 for (int x = 0; x < width; x++) { 105 for (int i = 0; i < levels; i++) 106 rHistogram[i] = gHistogram[i] = bHistogram[i] = rTotal[i] = gTotal[i] = bTotal[i] = 0; 107 108 for (int row = -range; row <= range; row++) { 109 int iy = y+row; 110 int ioffset; 111 if (0 <= iy && iy < height) { 112 ioffset = iy*width; 113 for (int col = -range; col <= range; col++) { 114 int ix = x+col; 115 if (0 <= ix && ix < width) { 116 int rgb = inPixels[ioffset+ix]; 117 int r = (rgb >> 16) & 0xff; 118 int g = (rgb >> 8) & 0xff; 119 int b = rgb & 0xff; 120 int ri = r*levels/256; 121 int gi = g*levels/256; 122 int bi = b*levels/256; 123 rTotal[ri] += r; 124 gTotal[gi] += g; 125 bTotal[bi] += b; 126 rHistogram[ri]++; 127 gHistogram[gi]++; 128 bHistogram[bi]++; 129 } 130 } 131 } 132 } 133 134 int r = 0, g = 0, b = 0; 135 for (int i = 1; i < levels; i++) { 136 if (rHistogram[i] > rHistogram[r]) 137 r = i; 138 if (gHistogram[i] > gHistogram[g]) 139 g = i; 140 if (bHistogram[i] > bHistogram[b]) 141 b = i; 142 } 143 r = rTotal[r] / rHistogram[r]; 144 g = gTotal[g] / gHistogram[g]; 145 b = bTotal[b] / bHistogram[b]; 146 outPixels[index++] = 0xff000000 | ( r << 16 ) | ( g << 8 ) | b; 147 } 148 } 149 return outPixels; 150 } 151 152 public String toString() { 153 return "Stylize/Oil..."; 154 } 155 156 public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src); 157 Object o; 158 if((o=parameters.removeEL(KeyImpl.init("Levels")))!=null)setLevels(ImageFilterUtil.toIntValue(o,"Levels")); 159 if((o=parameters.removeEL(KeyImpl.init("Range")))!=null)setRange(ImageFilterUtil.toIntValue(o,"Range")); 160 161 // check for arguments not supported 162 if(parameters.size()>0) { 163 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 [Levels, Range]"); 164 } 165 166 return filter(src, dst); 167 } 168} 169