Skip to content

Instantly share code, notes, and snippets.

@micahrj
Last active January 4, 2016 05:49
Show Gist options
  • Save micahrj/8577155 to your computer and use it in GitHub Desktop.
Save micahrj/8577155 to your computer and use it in GitHub Desktop.
package asmt2;
import java.awt.Color;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
public class Main {
public static void main(String[] args) {
if (args.length >= 2) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(args[0]));
} catch (IOException e) {
System.out.println("Could not load file.");
System.exit(1);
}
BufferedImage edges = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < edges.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
edges.setRGB(x, y, 0xff000000);
}
}
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int gradX, gradY;
Color left, right, up, down;
if (x == 0) {
// left edge
left = new Color(img.getRGB(x, y));
right = new Color(img.getRGB(x+1, y));
} else if (x == img.getWidth() - 1) {
// right edge
left = new Color(img.getRGB(x-1, y));
right = new Color(img.getRGB(x, y));
} else {
left = new Color(img.getRGB(x-1, y));
right = new Color(img.getRGB(x+1, y));
}
gradX = Math.abs(right.getRed() - left.getRed()) + Math.abs(right.getGreen() - left.getGreen()) + Math.abs(right.getBlue() - left.getBlue());
if (y == 0) {
// top edge
up = new Color(img.getRGB(x, y));
down = new Color(img.getRGB(x, y+1));
} else if (y == img.getHeight() - 1) {
// bottom edge
up = new Color(img.getRGB(x, y-1));
down = new Color(img.getRGB(x, y));
} else {
up = new Color(img.getRGB(x, y-1));
down = new Color(img.getRGB(x, y+1));
}
gradY = Math.abs(down.getRed() - up.getRed()) + Math.abs(down.getGreen() - up.getGreen()) + Math.abs(down.getBlue() - up.getBlue());
int grad = gradX + gradY;
if (grad > 300) {
edges.setRGB(x, y, 0xffffffff);
} else {
edges.setRGB(x, y, 0x00000000);
}
//edges.setRGB(x, y, (new Color(grad, grad, grad)).getRGB());
}
}
try {
ImageIO.write(edges, "jpg", new File(args[1]));
} catch (IOException e) {
System.out.println("Could not save file.");
System.exit(1);
}
} else {
System.out.println("Please provide input and output files.");
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment