static int colorCount332_max(int max, BufferedImage img) { if (max == 0) ret 0; int w = img.getWidth(), h = img.getHeight(), n = 0; new BitSet bs; for y to h: for x to w: { int idx = ubyteToInt(to332(img.getRGB(x, y))); if (!bs.get(idx)) { if (++n >= max) ret n; setBit(bs, idx); } } ret n; }