Created
December 8, 2015 21:08
-
-
Save Cxarli/e6d17dd1f40b396bf275 to your computer and use it in GitHub Desktop.
Histrogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyz.mycharlie.colourhistogram; | |
import java.awt.*; | |
import java.awt.image.*; | |
import java.io.*; | |
import javax.imageio.*; | |
public class Main { | |
public int[][] createRGBData(BufferedImage img) { | |
int[] R=new int[256], G=new int[256], B=new int[256]; | |
for(int x=0; x<img.getWidth(); x++) { | |
for(int y=0; y<img.getHeight(); y++) { | |
int c = img.getRGB(x, y); | |
int r = (c & 0xff0000) >> 16, | |
g = (c & 0xff00 ) >> 8, | |
b = (c & 0xff ) >> 0; | |
R[r]++; | |
G[g]++; | |
B[b]++; | |
} | |
} | |
return new int[][]{R, G, B}; | |
} | |
public BufferedImage createImage(int[] data, char colour) { | |
int MAX_Y=128; | |
// Get max | |
double max=0; | |
for(int i=1; i<data.length; i++) | |
if(data[i]>max) | |
max=data[i]; | |
BufferedImage img = new BufferedImage(data.length, MAX_Y+1, BufferedImage.TYPE_3BYTE_BGR); | |
// TODO: Set white background on left side, and black background on right side | |
int r = (colour=='r' || colour=='G') ? 1 : 0, | |
g = (colour=='g' || colour=='G') ? 1 : 0, | |
b = (colour=='b' || colour=='G') ? 1 : 0; | |
for(int x=1; x<data.length; x++) { | |
int c = new Color(x*r, x*g, x*b).getRGB(); | |
// Line from (x, MAX_Y) to (x, data[x]/max*MAX_Y), | |
// horizontally reversed so the line goes from bottom to top | |
double end = MAX_Y - data[x] / max * MAX_Y; | |
for(int y=MAX_Y; y > end; y--) { | |
img.setRGB(x, y, c); | |
} | |
} | |
return img; | |
} | |
public Main(String[] args) { | |
String filename = "LARGE.png"; | |
// Parse arguments | |
for(int i=0; i<args.length; i++) { | |
String arg = args[i]; | |
switch(arg) { | |
case "--file": case "-f": | |
filename=args[++i]; | |
break; | |
default: | |
System.err.println("Unknown argument '"+arg+"'."); | |
} | |
} | |
// Create | |
try { | |
System.out.println("Reading image"); | |
BufferedImage img = ImageIO.read(new File(filename)); | |
System.out.println("Collecting data"); | |
int[][] data = createRGBData(img); | |
System.out.println("Creating images"); | |
BufferedImage red = createImage(data[0], 'r'); | |
BufferedImage green = createImage(data[1], 'g'); | |
BufferedImage blue = createImage(data[2], 'b'); | |
BufferedImage grey = createImage(data[2], 'G'); | |
System.out.println("Writing images"); | |
System.out.println("- Red"); | |
ImageIO.write(red, "png", new File("red.png")); | |
System.out.println("- Green"); | |
ImageIO.write(green, "png", new File("green.png")); | |
System.out.println("- Blue"); | |
ImageIO.write(blue, "png", new File("blue.png")); | |
System.out.println("- Grey"); | |
ImageIO.write(grey, "png", new File("grey.png")); | |
System.out.println("Done"); | |
} catch(Exception e) { | |
e.printStackTrace(System.out); | |
} | |
} | |
public static Main main; | |
public static void main(String[] args) { | |
main = new Main(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment