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
022public abstract class TransferFilter extends PointFilter {
023
024        protected int[] rTable, gTable, bTable;
025        protected boolean initialized = false;
026        
027        public TransferFilter() {
028                canFilterIndexColorModel = true;
029        }
030
031        public int filterRGB(int x, int y, int rgb) {
032                int a = rgb & 0xff000000;
033                int r = (rgb >> 16) & 0xff;
034                int g = (rgb >> 8) & 0xff;
035                int b = rgb & 0xff;
036                r = rTable[r];
037                g = gTable[g];
038                b = bTable[b];
039                return a | (r << 16) | (g << 8) | b;
040        }
041
042        public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
043                if (!initialized)
044                        initialize();
045                return super.filter( src, dst );
046        }
047
048        protected void initialize() {
049                initialized = true;
050                rTable = gTable = bTable = makeTable();
051        }
052
053        protected int[] makeTable() {
054                int[] table = new int[256];
055                for (int i = 0; i < 256; i++)
056                        table[i] = PixelUtils.clamp( (int)( 255 * transferFunction( i / 255.0f ) ) );
057                return table;
058        }
059
060        protected float transferFunction( float v ) {
061                return 0;
062        }
063
064        public int[] getLUT() {
065                if (!initialized)
066                        initialize();
067                int[] lut = new int[256];
068                for ( int i = 0; i < 256; i++ ) {
069                        lut[i] = filterRGB( 0, 0, (i << 24) | (i << 16) | (i << 8) | i );
070                }
071                return lut;
072        }
073        
074}
075