Skip to content

Instantly share code, notes, and snippets.

@micahrj
Created January 30, 2014 06:08
Show Gist options
  • Save micahrj/8703393 to your computer and use it in GitHub Desktop.
Save micahrj/8703393 to your computer and use it in GitHub Desktop.
package asmt2;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
public class Main {
// define the three drawing modes
private static final int EDGE_DETECTOR = 0;
private static final int GRAYSCALE = 1;
private static final int EDGE_ENHANCEMENT = 2;
private static final int COLOR_OUTLINES = 3;
public static void main(String[] args) {
if (args.length >= 3) {
BufferedImage img = null, edges = null, result = null;
int mode = Integer.parseInt(args[2]);
try {
img = ImageIO.read(new File(args[0]));
edges = ImageIO.read(new File(args[0]));
result = ImageIO.read(new File(args[0]));
} catch (IOException e) {
System.out.println("Could not load file.");
System.exit(1);
}
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) {
// if we are on the left edge
left = new Color(img.getRGB(x, y));
right = new Color(img.getRGB(x+1, y));
} else if (x == img.getWidth() - 1) {
// if we are on the right edge
left = new Color(img.getRGB(x-1, y));
right = new Color(img.getRGB(x, y));
} else {
// if we are in the middle of the image
left = new Color(img.getRGB(x-1, y));
right = new Color(img.getRGB(x+1, y));
}
// sum up the red, green, and blue horizontal components of the gradient
gradX = Math.abs(right.getRed() - left.getRed()) + Math.abs(right.getGreen() - left.getGreen()) + Math.abs(right.getBlue() - left.getBlue());
if (y == 0) {
// if we are on the top edge
up = new Color(img.getRGB(x, y));
down = new Color(img.getRGB(x, y+1));
} else if (y == img.getHeight() - 1) {
// if we are on the bottom edge
up = new Color(img.getRGB(x, y-1));
down = new Color(img.getRGB(x, y));
} else {
// if we are the middle of the image
up = new Color(img.getRGB(x, y-1));
down = new Color(img.getRGB(x, y+1));
}
// sum up the red, green, and blue vertical components of the gradient
gradY = Math.abs(down.getRed() - up.getRed()) + Math.abs(down.getGreen() - up.getGreen()) + Math.abs(down.getBlue() - up.getBlue());
// sum up the horizontal and vertical components of the gradient, with a maximum value of 1530
int grad = gradX + gradY;
// Map the total gradient onto a logistic curve from 0 to 255.
// A logistic curve is like a smoothed out step function, so that brighter values get brighter and darker values get darker.
int brightness = (int)(255.0 * (1.0 / (1.0 + Math.exp(- (( ((float)grad) * (12.0 / 1530.0) ) - 3.0)))));
switch (mode) {
case EDGE_DETECTOR:
// Set a pixel to white if the gradient is above a threshold and to black if otherwise.
if (grad > 200) {
result.setRGB(x, y, 0xffffffff);
} else {
result.setRGB(x, y, 0x00000000);
}
break;
case GRAYSCALE:
// Simply set the brightness of a pixel to the gradient there.
result.setRGB(x, y, (new Color(brightness, brightness, brightness)).getRGB());
break;
case EDGE_ENHANCEMENT:
Color edgeColor = (new Color(brightness, brightness, brightness));
edges.setRGB(x, y, edgeColor.getRGB());
Color resultColor = new Color(result.getRGB(x, y));
// Brighten the image where edges are found.
result.setRGB(x, y, (new Color(Math.min(255, resultColor.getRed() + edgeColor.getRed()), Math.min(255, resultColor.getGreen() + edgeColor.getGreen()), Math.min(255, resultColor.getBlue() + edgeColor.getBlue()))).getRGB());
break;
case COLOR_OUTLINES:
edges.setRGB(x, y, (new Color(brightness, brightness, brightness)).getRGB());
// Superimpose red, green, and blue versions of the edges at offsets from their original positions.
if (x + 2 < result.getWidth() && y + 3 < result.getHeight()) {
result.setRGB(x + 2, y + 3, result.getRGB(x + 2, y + 3) + Math.min(edges.getRGB(x, y) & 0x00ff0000, (0x00ff0000 - (result.getRGB(x + 2, y + 3) & 0x00ff0000))));
}
if (x - 3 > 0 && y + 2 < result.getHeight()) {
result.setRGB(x - 3, y + 2, result.getRGB(x - 3, y + 2) + Math.min(edges.getRGB(x, y) & 0x0000ff00, (0x0000ff00 - (result.getRGB(x - 3, y + 2) & 0x0000ff00))));
}
if (y - 3 > 0) {
result.setRGB(x, y - 3, result.getRGB(x, y - 3) + Math.min(edges.getRGB(x, y) & 0x000000ff, (0x000000ff - (result.getRGB(x, y - 3) & 0x000000ff))));
}
break;
}
}
}
try {
ImageIO.write(result, "jpg", new File(args[1]));
} catch (IOException e) {
System.out.println("Could not save file.");
System.exit(1);
}
} else {
System.out.println("Please provide an input file, an output file, and a mode (0, 1, 2, or 3).");
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment