1 18 package org.apache.batik.extension.svg; 19 20 import java.awt.image.Raster ; 21 import java.awt.image.WritableRaster ; 22 23 import org.apache.batik.ext.awt.image.rendered.AbstractRed; 24 import org.apache.batik.ext.awt.image.rendered.CachableRed; 25 26 31 public class HistogramRed extends AbstractRed { 32 33 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 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 val = samples[x++]*5; val += samples[x++]*9; val += samples[x++]*2; 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 r = src.getTile(x+xt0, y+yt0); 84 tallyTile(r); 85 computed[idx]=true; 86 } 87 } 88 return bins; 89 } 90 91 public WritableRaster copyData(WritableRaster wr) { 92 copyToRaster(wr); 93 return wr; 94 } 95 96 public Raster 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 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 |