KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > extension > svg > BatikHistogramNormalizationFilter8Bit


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.extension.svg;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21 import java.awt.image.RenderedImage JavaDoc;
22 import java.awt.image.SampleModel JavaDoc;
23 import java.awt.image.renderable.RenderContext JavaDoc;
24
25 import org.apache.batik.ext.awt.image.LinearTransfer;
26 import org.apache.batik.ext.awt.image.TransferFunction;
27 import org.apache.batik.ext.awt.image.renderable.AbstractColorInterpolationRable;
28 import org.apache.batik.ext.awt.image.renderable.Filter;
29 import org.apache.batik.ext.awt.image.rendered.ComponentTransferRed;
30
31 public class BatikHistogramNormalizationFilter8Bit
32     extends AbstractColorInterpolationRable
33     implements BatikHistogramNormalizationFilter {
34
35     private float trim = .01f;
36
37     /**
38      * Sets the source of the operation
39      */

40     public void setSource(Filter src){
41         init(src, null);
42     }
43
44     /**
45      * Returns the source of the operation
46      */

47     public Filter getSource(){
48         return (Filter)getSources().get(0);
49     }
50
51     /**
52      * Returns the trim percent for this normalization.
53      */

54     public float getTrim() {
55         return trim;
56     }
57
58     /**
59      * Sets the trim percent for this normalization.
60      */

61     public void setTrim(float trim) {
62         this.trim = trim;
63         touch();
64     }
65
66     public BatikHistogramNormalizationFilter8Bit(Filter src, float trim) {
67         setSource(src);
68         setTrim(trim);
69     }
70
71     protected int [] histo = null;
72     protected float slope, intercept;
73
74     /**
75      * This method computes the histogram of the image and
76      * from that the appropriate clipping points, which leads
77      * to a slope and intercept for a LinearTransfer function
78      *
79      * @param rc We get the set of rendering hints from rc.
80      */

81     public void computeHistogram(RenderContext JavaDoc rc) {
82         if (histo != null)
83             return;
84
85         Filter src = getSource();
86         
87         float scale = 100f/src.getWidth();
88         float yscale = 100f/src.getHeight();
89
90         if (scale > yscale) scale=yscale;
91         
92         AffineTransform JavaDoc at = AffineTransform.getScaleInstance(scale, scale);
93         rc = new RenderContext JavaDoc(at, rc.getRenderingHints());
94         RenderedImage JavaDoc histRI = getSource().createRendering(rc);
95
96         histo = new HistogramRed(convertSourceCS(histRI)).getHistogram();
97
98         int t = (int)(histRI.getWidth()*histRI.getHeight()*trim+0.5);
99         int c, i;
100         for (c=0, i=0; i<255; i++) {
101             c+=histo[i];
102             // System.out.println("C[" + i + "] = " + c + " T: " + t);
103
if (c>=t) break;
104         }
105         int low = i;
106         
107         for (c=0, i=255; i>0; i--) {
108             c+=histo[i];
109             // System.out.println("C[" + i + "] = " + c + " T: " + t);
110
if (c>=t) break;
111         }
112         int hi = i;
113
114         slope = 255f/(hi-low);
115         intercept = (slope*-low)/255f;
116     }
117
118     
119     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
120         //
121
// Get source's rendered image
122
//
123
RenderedImage JavaDoc srcRI = getSource().createRendering(rc);
124
125         if(srcRI == null)
126             return null;
127
128         computeHistogram(rc);
129
130         SampleModel JavaDoc sm = srcRI.getSampleModel();
131         int bands = sm.getNumBands();
132
133         // System.out.println("Slope, Intercept: " + slope + ", " + intercept);
134
TransferFunction [] tfs = new TransferFunction[bands];
135         TransferFunction tf = new LinearTransfer(slope, intercept);
136         for (int i=0; i<tfs.length; i++)
137             tfs[i] = tf;
138
139         return new ComponentTransferRed(convertSourceCS(srcRI), tfs, null);
140     }
141 }
142
Popular Tags