Skip to content

Instantly share code, notes, and snippets.

@tily
Created January 14, 2011 14:30
Show Gist options
  • Save tily/779671 to your computer and use it in GitHub Desktop.
Save tily/779671 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;
class JpegGlitch {
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;
int r = new Random().nextInt(5) + 1;
if(i == 48 && new Random().nextInt(r) == 1) i = new Random().nextInt(10) + 47;
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