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.Rectangle;
018    import java.awt.image.BufferedImage;
019    
020    import railo.runtime.engine.ThreadLocalPageContext;
021    import railo.runtime.exp.FunctionException;
022    import railo.runtime.exp.PageException;
023    import railo.runtime.img.ImageUtil;
024    import railo.runtime.type.Struct;
025    import railo.runtime.type.util.CollectionUtil;
026    
027    /**
028     * A filter to perform auto-equalization on an image.
029     */
030    public class EqualizeFilter extends WholeImageFilter  implements DynFiltering {
031    
032        private int[][] lut;
033    
034            public EqualizeFilter() {
035            }
036    
037            protected int[] filterPixels( int width, int height, int[] inPixels, Rectangle transformedSpace ) {
038                    Histogram histogram = new Histogram(inPixels, width, height, 0, width);
039    
040                    int i, j;
041    
042                    if (histogram.getNumSamples() > 0) {
043                            float scale = 255.0f / histogram.getNumSamples();
044                            lut = new int[3][256];
045                            for (i = 0; i < 3; i++) {
046                                    lut[i][0] = histogram.getFrequency(i, 0);
047                                    for (j = 1; j < 256; j++)
048                                            lut[i][j] = lut[i][j-1] + histogram.getFrequency(i, j);
049                                    for (j = 0; j < 256; j++)
050                                            lut[i][j] = Math.round(lut[i][j]*scale);
051                            }
052                    } else
053                            lut = null;
054    
055                    i = 0;
056                    for (int y = 0; y < height; y++)
057                            for (int x = 0; x < width; x++) {
058                                    inPixels[i] = filterRGB(x, y, inPixels[i]);
059                                    i++;
060                            }
061                    lut = null;
062                    
063                    return inPixels;
064            }
065    
066            private int filterRGB(int x, int y, int rgb) {
067                    if (lut != null) {
068                            int a = rgb & 0xff000000;
069                            int r = lut[Histogram.RED][(rgb >> 16) & 0xff];
070                            int g = lut[Histogram.GREEN][(rgb >> 8) & 0xff];
071                            int b = lut[Histogram.BLUE][rgb & 0xff];
072    
073                            return a | (r << 16) | (g << 8) | b;
074                    }
075                    return rgb;
076            }
077    
078            public String toString() {
079                    return "Colors/Equalize";
080            }
081            public BufferedImage filter(BufferedImage src, Struct parameters) throws PageException {BufferedImage dst=ImageUtil.createBufferedImage(src);
082                    //Object o;
083    
084                    // check for arguments not supported
085                    if(parameters.size()>0) {
086                            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 []");
087                    }
088    
089                    return filter(src, dst);
090            }
091    }