-
-
Save rdlester/1213209 to your computer and use it in GitHub Desktop.
glitch jpeg file in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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