package bmpviewer.bmp; /** * color(RGB 24-bit) counter. *

* generates color table.
* converts color to index.
*

* @author Hiroyuki Murata * @since JDK1.1 */ public class RGBCounter { private int[][][] tree; private int[] pix,tbl; private int lim,count, rrggbb,rr,gg,bb; /** * constructor *

* @param pix array of pixels. * @param lim counter limit. */ public RGBCounter(int[] pix, int lim) { this.pix = pix; this.lim = lim; tree = new int[256][][]; tbl = new int[lim+1]; } //------------------------------------------------------ /** * count number of colors */ public int count() { for (int i=0; i= lim; if (over) count = -1; return over; } /** * RGB -> R,G,B */ private void splitRGB(int col) { rrggbb = col & 0xffffff; rr = (rrggbb >> 16) ; gg = (rrggbb >> 8) & 0xff; bb = rrggbb & 0xff; } /** * register new color */ private void addRGB(boolean init) { if (init) { for (int i=0; i<256; i++) tree[rr][gg][i] = -1; } tree[rr][gg][bb] = count; tbl[count] = rrggbb; count++; } //------------------------------------------------------ /** * get color table (RGBQuad) */ public int[] getTable() { if (count > lim) return null; else return tbl; } /** * color -> index */ public int getIndex(int col) { splitRGB(col); return tree[rr][gg][bb]; } }