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.image.*;
020
021/**
022 * A filter which performs a threshold operation on an image.
023 */
024public class ThresholdFilter extends PointFilter {
025
026        private int lowerThreshold;
027        private int upperThreshold;
028        private int white = 0xffffff;
029        private int black = 0x000000;
030        
031        /**
032     * Construct a ThresholdFilter.
033     */
034    public ThresholdFilter() {
035                this(127);
036        }
037
038        /**
039     * Construct a ThresholdFilter.
040     * @param t the threshold value
041     */
042        public ThresholdFilter(int t) {
043                setLowerThreshold(t);
044                setUpperThreshold(t);
045        }
046
047        /**
048     * Set the lower threshold value.
049     * @param lowerThreshold the threshold value
050     * @see #getLowerThreshold
051     */
052        public void setLowerThreshold(int lowerThreshold) {
053                this.lowerThreshold = lowerThreshold;
054        }
055        
056        /**
057     * Get the lower threshold value.
058     * @return the threshold value
059     * @see #setLowerThreshold
060     */
061        public int getLowerThreshold() {
062                return lowerThreshold;
063        }
064        
065        /**
066     * Set the upper threshold value.
067     * @param upperThreshold the threshold value
068     * @see #getUpperThreshold
069     */
070        public void setUpperThreshold(int upperThreshold) {
071                this.upperThreshold = upperThreshold;
072        }
073
074        /**
075     * Get the upper threshold value.
076     * @return the threshold value
077     * @see #setUpperThreshold
078     */
079        public int getUpperThreshold() {
080                return upperThreshold;
081        }
082
083        /**
084     * Set the color to be used for pixels above the upper threshold.
085     * @param white the color
086     * @see #getWhite
087     */
088        public void setWhite(int white) {
089                this.white = white;
090        }
091
092        /**
093     * Get the color to be used for pixels above the upper threshold.
094     * @return the color
095     * @see #setWhite
096     */
097        public int getWhite() {
098                return white;
099        }
100
101        /**
102     * Set the color to be used for pixels below the lower threshold.
103     * @param black the color
104     * @see #getBlack
105     */
106        public void setBlack(int black) {
107                this.black = black;
108        }
109
110        /**
111     * Set the color to be used for pixels below the lower threshold.
112     * @return the color
113     * @see #setBlack
114     */
115        public int getBlack() {
116                return black;
117        }
118
119        public int filterRGB(int x, int y, int rgb) {
120        int v = PixelUtils.brightness( rgb );
121        float f = ImageMath.smoothStep( lowerThreshold, upperThreshold, v );
122        return (rgb & 0xff000000) | (ImageMath.mixColors( f, black, white ) & 0xffffff);
123        }
124
125        public String toString() {
126                return "Stylize/Threshold...";
127        }
128}