KickJava   Java API By Example, From Geeks To Geeks.

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


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.image.Raster JavaDoc;
21 import java.awt.image.WritableRaster JavaDoc;
22
23 import org.apache.batik.ext.awt.image.rendered.AbstractRed;
24 import org.apache.batik.ext.awt.image.rendered.CachableRed;
25
26 /**
27  *
28  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
29  * @version $Id: HistogramRed.java,v 1.5 2004/08/18 07:14:22 vhardy Exp $
30  */

31 public class HistogramRed extends AbstractRed {
32
33     // This is used to track which tiles we have computed
34
// a histogram for.
35
boolean [] computed;
36     int tallied = 0;
37
38     int [] bins = new int[256];
39
40     public HistogramRed(CachableRed src){
41         super(src, null);
42
43         int tiles = getNumXTiles()*getNumYTiles();
44         computed = new boolean[tiles];
45     }
46     
47     public void tallyTile(Raster JavaDoc r) {
48         final int minX = r.getMinX();
49         final int minY = r.getMinY();
50         final int w = r.getWidth();
51         final int h = r.getHeight();
52         
53         int [] samples = null;
54         int val;
55         for (int y=minY; y<minY+h; y++) {
56             samples = r.getPixels(minX, y, w, 1, samples);
57             for (int x=0; x<3*w; x++) {
58                 // Simple fixed point conversion to lumincence.
59
val = samples[x++]*5; // Red
60
val += samples[x++]*9; // Green
61
val += samples[x++]*2; // blue
62
bins[val>>4]++;
63             }
64         }
65         tallied++;
66     }
67
68     public int [] getHistogram() {
69         if (tallied == computed.length)
70             return bins;
71
72         CachableRed src = (CachableRed)getSources().elementAt(0);
73         int yt0 = src.getMinTileY();
74
75         int xtiles = src.getNumXTiles();
76         int xt0 = src.getMinTileX();
77             
78         for (int y=0; y<src.getNumYTiles(); y++) {
79             for (int x=0; x<xtiles; x++) {
80                 int idx = (x+xt0)+y*xtiles;
81                 if (computed[idx]) continue;
82
83                 Raster JavaDoc r = src.getTile(x+xt0, y+yt0);
84                 tallyTile(r);
85                 computed[idx]=true;
86             }
87         }
88         return bins;
89     }
90
91     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
92         copyToRaster(wr);
93         return wr;
94     }
95     
96     public Raster JavaDoc getTile(int tileX, int tileY) {
97         int yt = tileY-getMinTileY();
98         int xt = tileX-getMinTileX();
99
100         CachableRed src = (CachableRed)getSources().elementAt(0);
101         Raster JavaDoc r = src.getTile(tileX, tileY);
102         
103         int idx = xt+yt*getNumXTiles();
104
105         if (computed[idx])
106             return r;
107
108         tallyTile(r);
109         computed[idx] = true;
110         return r;
111     }
112 }
113
114
Popular Tags