Skip to content

Instantly share code, notes, and snippets.

@rdlester
Forked from tily/JpegGlitch.java
Created September 13, 2011 05:47
Show Gist options
  • Save rdlester/1213209 to your computer and use it in GitHub Desktop.
Save rdlester/1213209 to your computer and use it in GitHub Desktop.
glitch jpeg file in java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
//Changes: constants broken out of algorithm into variables for easy experimentation
class JpegGlitch2 {
public static final int TOGGLE = 3;
public static final int INDEX = 12;
public static final int RAND_GLITCH = 100;
public static final int GLITCH = 30;
public static void main(String[] args) {
FileInputStream in = null;
FileOutputStream out = null;
if(args.length < 2) {
abort("Usage: java JpegGlitch in.jpg out.jpg");
}
try {
in = new FileInputStream(args[0]);
out = new FileOutputStream(args[1]);
while(true) {
int i = in.read();
if(i == -1) break;
if(i == INDEX && new Random().nextInt(TOGGLE) == 1) i = new Random().nextInt(RAND_GLITCH) + GLITCH;
out.write(i);
}
} catch(FileNotFoundException e) {
abort("Error: " + e.getMessage());
} catch(IOException e) {
abort("Error: " + e.getMessage());
} finally {
try {
in.close();
out.close();
} catch(IOException e) {
abort("Error: " + e.getMessage());
}
}
}
private static void abort(String message) {
System.out.println(message);
System.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment