001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.image;
018
019import java.awt.*;
020import java.awt.image.*;
021
022/**
023 * A filter which performs a tritone conversion on an image. Given three colors for shadows, midtones and highlights,
024 * it converts the image to grayscale and then applies a color mapping based on the colors.
025 */
026public class TritoneFilter extends PointFilter {
027
028        private int shadowColor = 0xff000000;
029        private int midColor = 0xff888888;
030        private int highColor = 0xffffffff;
031    private int[] lut;
032        
033    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
034        lut = new int[256];
035        for ( int i = 0; i < 128; i++ ) {
036            float t = i / 127.0f;
037            lut[i] = ImageMath.mixColors( t, shadowColor, midColor );
038        }
039        for ( int i = 128; i < 256; i++ ) {
040            float t = (i-127) / 128.0f;
041            lut[i] = ImageMath.mixColors( t, midColor, highColor );
042        }
043        dst = super.filter( src, dst );
044        lut = null;
045        return dst;
046    }
047    
048        public int filterRGB( int x, int y, int rgb ) {
049        return lut[ PixelUtils.brightness( rgb ) ];
050        }
051
052    /**
053     * Set the shadow color.
054     * @param shadowColor the shadow color
055     * @see #getShadowColor
056     */
057        public void setShadowColor( int shadowColor ) {
058                this.shadowColor = shadowColor;
059        }
060        
061    /**
062     * Get the shadow color.
063     * @return the shadow color
064     * @see #setShadowColor
065     */
066        public int getShadowColor() {
067                return shadowColor;
068        }
069
070    /**
071     * Set the mid color.
072     * @param midColor the mid color
073     * @see #getmidColor
074     */
075        public void setMidColor( int midColor ) {
076                this.midColor = midColor;
077        }
078        
079    /**
080     * Get the mid color.
081     * @return the mid color
082     * @see #setmidColor
083     */
084        public int getMidColor() {
085                return midColor;
086        }
087
088    /**
089     * Set the high color.
090     * @param highColor the high color
091     * @see #gethighColor
092     */
093        public void setHighColor( int highColor ) {
094                this.highColor = highColor;
095        }
096        
097    /**
098     * Get the high color.
099     * @return the high color
100     * @see #sethighColor
101     */
102        public int getHighColor() {
103                return highColor;
104        }
105
106
107        public String toString() {
108                return "Colors/Tritone...";
109        }
110
111}
112