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 to change the brightness and contrast of an image.
024 */
025public class ContrastFilter extends TransferFilter {
026
027        private float brightness = 1.0f;
028        private float contrast = 1.0f;
029        
030        protected float transferFunction( float f ) {
031                f = f*brightness;
032                f = (f-0.5f)*contrast+0.5f;
033                return f;
034        }
035
036    /**
037     * Set the filter brightness.
038     * @param brightness the brightness in the range 0 to 1
039     * @min-value 0
040     * @max-value 0
041     * @see #getBrightness
042     */
043        public void setBrightness(float brightness) {
044                this.brightness = brightness;
045                initialized = false;
046        }
047        
048    /**
049     * Get the filter brightness.
050     * @return the brightness in the range 0 to 1
051     * @see #setBrightness
052     */
053        public float getBrightness() {
054                return brightness;
055        }
056
057    /**
058     * Set the filter contrast.
059     * @param contrast the contrast in the range 0 to 1
060     * @min-value 0
061     * @max-value 0
062     * @see #getContrast
063     */
064        public void setContrast(float contrast) {
065                this.contrast = contrast;
066                initialized = false;
067        }
068        
069    /**
070     * Get the filter contrast.
071     * @return the contrast in the range 0 to 1
072     * @see #setContrast
073     */
074        public float getContrast() {
075                return contrast;
076        }
077
078        public String toString() {
079                return "Colors/Contrast...";
080        }
081
082}
083