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 allows the red, green and blue channels of an image to be mixed into each other.
024 */
025public class ChannelMixFilter extends PointFilter {
026        
027        private int blueGreen, redBlue, greenRed;
028        private int intoR, intoG, intoB;
029        
030        public ChannelMixFilter() {
031                canFilterIndexColorModel = true;
032        }
033
034        public void setBlueGreen(int blueGreen) {
035                this.blueGreen = blueGreen;
036        }
037
038        public int getBlueGreen() {
039                return blueGreen;
040        }
041
042        public void setRedBlue(int redBlue) {
043                this.redBlue = redBlue;
044        }
045
046        public int getRedBlue() {
047                return redBlue;
048        }
049
050        public void setGreenRed(int greenRed) {
051                this.greenRed = greenRed;
052        }
053
054        public int getGreenRed() {
055                return greenRed;
056        }
057
058        public void setIntoR(int intoR) {
059                this.intoR = intoR;
060        }
061
062        public int getIntoR() {
063                return intoR;
064        }
065
066        public void setIntoG(int intoG) {
067                this.intoG = intoG;
068        }
069
070        public int getIntoG() {
071                return intoG;
072        }
073
074        public void setIntoB(int intoB) {
075                this.intoB = intoB;
076        }
077
078        public int getIntoB() {
079                return intoB;
080        }
081
082        public int filterRGB(int x, int y, int rgb) {
083                int a = rgb & 0xff000000;
084                int r = (rgb >> 16) & 0xff;
085                int g = (rgb >> 8) & 0xff;
086                int b = rgb & 0xff;
087                int nr = PixelUtils.clamp((intoR * (blueGreen*g+(255-blueGreen)*b)/255 + (255-intoR)*r)/255);
088                int ng = PixelUtils.clamp((intoG * (redBlue*b+(255-redBlue)*r)/255 + (255-intoG)*g)/255);
089                int nb = PixelUtils.clamp((intoB * (greenRed*r+(255-greenRed)*g)/255 + (255-intoB)*b)/255);
090                return a | (nr << 16) | (ng << 8) | nb;
091        }
092
093        public String toString() {
094                return "Colors/Mix Channels...";
095        }
096}
097