Skip to content

Instantly share code, notes, and snippets.

@cilice
Created October 23, 2011 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cilice/1307427 to your computer and use it in GitHub Desktop.
Save cilice/1307427 to your computer and use it in GitHub Desktop.
package hs.algo.excercise2;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
@SuppressWarnings("serial")
public class Algos2_1 extends Frame {
private int W;
private int H;
private Image imgFile, imgFileRed;
private int[] imgFilePixels, imgColors, imgColorFreq, imgColorsRed;
MemoryImageSource imgFileRedSrc;
@Override
public void paint(Graphics g) {
g.drawImage(imgFile, getInsets().left, getInsets().top, this);
g.drawImage(imgFileRed, getInsets().left + imgFile.getWidth(this), getInsets().top, this);
}
@Override
public void update(Graphics g) {
paint(g);
}
private int[] getColours(int[] pixels) {
int colorNumber = 0;
int[] temp_colors = new int[pixels.length];
boolean hadWeZeroYet = false;
boolean zeroKick = false;
for(int i = 0; i < pixels.length; i++) {
boolean colorAlreadyThere = false;
if (pixels[i] == 0) hadWeZeroYet = true;
for (int k = 0; k < temp_colors.length && !colorAlreadyThere; k++) {
if (temp_colors[k] == pixels[i]) colorAlreadyThere = true;
if ((temp_colors[k] == 0 && zeroKick) || (temp_colors[k+1] == 0)) break;
}
if (hadWeZeroYet && colorAlreadyThere && !zeroKick) {
temp_colors[colorNumber] = pixels[i];
colorNumber++;
zeroKick = true;
}
if (!colorAlreadyThere) {
temp_colors[colorNumber] = pixels[i];
colorNumber++;
}
}
int[] ImgColRef = new int [colorNumber];
for(int i = 0; i < colorNumber; i++) {
ImgColRef[i] = temp_colors[i];
}
return ImgColRef;
}
private int[] countColours(int[] pixels, int[] imgCol) {
int[] imgFreq = new int [imgCol.length];
for(int i = 0; i < imgCol.length; i++) {
for(int j = 0; j < pixels.length; j++) {
if(imgCol[i] == pixels[j]) imgFreq[i]++;
}
}
return imgFreq;
}
public static void bubbleSort(int[] comparable, int[] comparable2) {
boolean changed = false;
do {
changed = false;
for (int a = 0; a < comparable.length - 1; a++) {
if (comparable[a] > comparable[a + 1]) {
int tmp = comparable[a];
comparable[a] = comparable[a + 1];
comparable[a + 1] = tmp;
int tmp2 = comparable2[a];
comparable2[a] = comparable2[a + 1];
comparable2[a + 1] = tmp2;
changed = true;
}
}
} while (changed);
}
private Image openImage(String imgPath) {
Image img = getToolkit().getImage(imgPath);
try {
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 1);
mt.waitForAll();
} catch (InterruptedException e) { e.printStackTrace(); }
return img;
}
private int[] grabPixels(Image img) {
int[] array = new int[W*H];
PixelGrabber grab = new PixelGrabber(img, 0, 0, W, H, array, 0, W);
try {
grab.grabPixels();
} catch (InterruptedException e) { e.printStackTrace(); }
return array;
}
private int[] reduceColours(int[] pixels, int[] imgCol) {
int[] popular = new int[pixels.length];
int halfColors = (imgCol.length % 2 == 0) ? imgCol.length / 2 : imgCol.length +1 / 2;
for (int i = 0; i < pixels.length; i++) {
boolean isRare = false;
for (int j = 0; j < halfColors || !isRare; j++) {
if (imgCol[j] == pixels[i]) {
isRare = true;
}
}
popular[i] = (!isRare)? pixels[i] : findSimilarColor(pixels[i], imgCol, halfColors);
}
return popular;
}
private int getColorSum(int a) {
return (a & 0x000000ff) + ((a & 0x0000ff00) >> 8) + ((a & 0x00ff0000) >> 16);
}
private int findSimilarColor(int givenColor, int[] colorArray, int halfColors) {
int bestColor = colorArray[0];
int givenSum = getColorSum(givenColor);
int bestDelta = Math.abs(getColorSum(bestColor) - givenSum);
for (int i = halfColors; i < colorArray.length; i++) {
if (Math.abs(getColorSum(colorArray[i]) - givenSum) < bestDelta) {
bestColor = colorArray[i];
bestDelta = Math.abs(getColorSum(bestColor) - givenSum);
}
}
return bestColor;
}
public Algos2_1() {
imgFile = openImage("src/hs/algo/excercise2/Shallow-Grave.jpg");
// imgFile = openImage("src/hs/algo/excercise2/images.jpg");
// imgFile = openImage("src/hs/algo/excercise2/Untitled-1.gif");
W = imgFile.getWidth(this);
H = imgFile.getHeight(this);
setBounds(600, 200, W * 2, H);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
System.out.println("Grabbing pixels.");
imgFilePixels = grabPixels(imgFile);
System.out.println("Getting unique colors.");
imgColors = getColours(imgFilePixels);
System.out.println(imgFilePixels.length);
System.out.println(imgColors.length);
System.out.println("Counting.");
imgColorFreq = countColours(imgFilePixels, imgColors);
System.out.println("Sorting");
bubbleSort(imgColorFreq, imgColors);
for (int i = 0; i < imgColors.length; i++) {
String hex = Integer.toHexString(imgColors[i]);
String hexstring = "#" + hex + " " + imgColorFreq[i] + " " + i;
System.out.println(hexstring);
}
System.out.println("Reducing Colors");
imgColorsRed = reduceColours(imgFilePixels, imgColors);
imgFileRedSrc = new MemoryImageSource(W, H, imgColorsRed, 0, W);
imgFileRed = createImage(imgFileRedSrc);
}
public static void main(String[] args) {
new Algos2_1();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment