@@ -0,0 +1,1772 @@
+/* (C) 2017 kagix%0A** Some code is heavily based on stackoverflow DOT com%0A*/%0Apackage net.kagix.gtfs.ratptools.buslinecolor;%0Aimport java.lang.*;%0Aimport java.util.*;%0Aimport java.awt.*;%0Aimport java.awt.event.*;%0Aimport java.awt.image.*;%0Aimport java.io.*;%0Aimport javax.imageio.*;%0Aimport javax.swing.*;%0A%0Apublic class HistogramCalculator%0A%7B%0A%09private HashMap%3CInteger,Integer%3E histogram = null;%0A %0A%09public HistogramCalculator(BufferedImage image) %7B%0A%0A%09%09if (image != null) %7B%0A%09%09%09histogram = new HashMap%3CInteger,Integer%3E();%0A%09%09%09calculateHistogram(image);%0A%09%09%7D%0A%09%7D%0A%0A%09public HashMap%3CInteger,Integer%3E getHistogram() %7B%0A%09%09return histogram;%0A%09%7D%0A%0A%09private int calculateKey(int%5B%5D rgb) %7B%0A%09%09int key = rgb%5BImage.RED%5D   * Image.RED_SHIFT%0A%09%09%09 +rgb%5BImage.GREEN%5D * Image.GREEN_SHIFT%0A%09%09%09 +rgb%5BImage.BLUE%5D  * Image.BLUE_SHIFT;%0A%09%09return key;%0A%09%7D%0A%0A%09private void addOrIncrementKeyCount( int key) %7B%0A%0A%09%09if (!histogram.containsKey(key)) %7B%0A%09%09%09histogram.put(key,0);%0A%09%09%7D%0A%0A%09%09histogram.put(key,histogram.get(key)+1);%0A%09%7D%0A%0A%09private void processPixel( int%5B%5D pixel) %7B%0A%09%09int key = calculateKey(pixel);%0A%09%09%0A%09%09addOrIncrementKeyCount( key );%0A%09%7D%0A%0A%09private void calculateHistogram(BufferedImage image) %7B%0A%09%09for (int row = 0; row %3C image.getHeight(null); row++) %7B%0A%09%09%09for (int column = 0; column %3Cimage.getWidth(null); column++) %7B%0A%0A%09%09%09%09int%5B%5D pixel = image.getRaster().getPixel( column, %0A%09%09%09%09%09%09%09%09%09  row, %0A%09%09%09%09%09%09%09%09%09  new int%5B3%5D);%0A%09%09%09%09processPixel( pixel );%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%0A%09public int getRedValue(int key) %7B%0A%0A%09%09return (key & Image.RED_MASK) / Image.RED_SHIFT;%0A%09%7D%0A%09%0A%09public int getGreenValue(int key) %7B%0A%0A%09%09return (key & Image.GREEN_MASK) / Image.GREEN_SHIFT;%0A%09%7D%0A%09%0A%09public int getBlueValue(int key) %7B%0A%0A%09%09return (key & Image.BLUE_MASK) / Image.BLUE_SHIFT;%0A%09%7D%0A%09%0A%09public String getHexValue(int key) %7B%0A%0A%09%09return String.format(%22%2506X%22,key);%0A%09%7D%0A%0A%7D%0A

Build a histogram of pixel color repetitions