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.image.BufferedImage;
036
037import lucee.runtime.engine.ThreadLocalPageContext;
038import lucee.runtime.exp.FunctionException;
039import lucee.runtime.exp.PageException;
040import lucee.runtime.img.ImageUtil;
041import lucee.runtime.type.KeyImpl;
042import lucee.runtime.type.Struct;
043import lucee.runtime.type.util.CollectionUtil;
044
045
046
047/**
048 * A filter to posterize an image.
049 */
050public class PosterizeFilter extends PointFilter  implements DynFiltering {
051
052        private int numLevels;
053        private int[] levels;
054    private boolean initialized = false;
055
056        public PosterizeFilter() {
057                setNumLevels(6);
058        }
059        
060        /**
061     * Set the number of levels in the output image.
062     * @param numLevels the number of levels
063     * @see #getNumLevels
064     */
065    public void setNumLevels(int numLevels) {
066                this.numLevels = numLevels;
067                initialized = false;
068        }
069
070        /**
071     * Get the number of levels in the output image.
072     * @return the number of levels
073     * @see #setNumLevels
074     */
075        public int getNumLevels() {
076                return numLevels;
077        }
078
079        /**
080     * Initialize the filter.
081     */
082    protected void initialize() {
083                levels = new int[256];
084                if (numLevels != 1)
085                        for (int i = 0; i < 256; i++)
086                                levels[i] = 255 * (numLevels*i / 256) / (numLevels-1);
087        }
088        
089        public int filterRGB(int x, int y, int rgb) {
090                if (!initialized) {
091                        initialized = true;
092                        initialize();
093                }
094                int a = rgb & 0xff000000;
095                int r = (rgb >> 16) & 0xff;
096                int g = (rgb >> 8) & 0xff;
097                int b = rgb & 0xff;
098                r = levels[r];
099                g = levels[g];
100                b = levels[b];
101                return a | (r << 16) | (g << 8) | b;
102        }
103
104        public String toString() {
105                return "Colors/Posterize...";
106        }
107
108        public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
109                Object o;
110                if((o=parameters.removeEL(KeyImpl.init("NumLevels")))!=null)setNumLevels(ImageFilterUtil.toIntValue(o,"NumLevels"));
111                if((o=parameters.removeEL(KeyImpl.init("Dimensions")))!=null){
112                        int[] dim=ImageFilterUtil.toDimensions(o,"Dimensions");
113                        setDimensions(dim[0],dim[1]);
114                }
115
116                // check for arguments not supported
117                if(parameters.size()>0) {
118                        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 [NumLevels, Dimensions]");
119                }
120
121                return filter(src, dst);
122        }
123}
124