Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active November 6, 2017 09:18
Show Gist options
  • Save KrabCode/6e7cc1bc46a2a81a0bd48fae21d1190c to your computer and use it in GitHub Desktop.
Save KrabCode/6e7cc1bc46a2a81a0bd48fae21d1190c to your computer and use it in GitHub Desktop.
Pixel sorts an image gradually from start to end, then brings the start over to the end and loops it. Records an .mp4 of exactly one loop for your convenience.
import com.hamoid.VideoExport;
import processing.core.PApplet;
import processing.core.PImage;
import java.util.Arrays;
public class MainApp extends PApplet{
public static void main(String[] args)
{
PApplet.main("MainApp", args);
}
PImage backup, img;
VideoExport vx;
boolean rec = true;
boolean in = true;
int start = 0;
int end = 200;
int scaledown = 1;
public void settings()
{
backup = loadImage("C:\\PerlinDistort\\8.jpg");
backup.resize(backup.width/scaledown, backup.height/scaledown);
// size(backup.width, backup.height); //true scaled size
fullScreen(); //stretch to fullscreen
}
public void setup(){
vx = new VideoExport(this);
vx.setFrameRate(60);
vx.startMovie();
rec = true;
}
public void draw()
{
//make a fresh working copy
try {
img = (PImage) backup.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
img = distort(img);
image(img, 0, 0, width, height);
//record video
if(rec){
vx.saveFrame();
}else{
if(vx != null)
{
vx.endMovie();
}
}
println(start + ":" + end);
}
private PImage distort(PImage img) {
img = fancySortImage(img,
start, end);
if(in){
end++;
}else{
start++;
}
if(end > img.width){
in = false;
}
if(start > img.width && end > img.width){
start = 0;
end = 0;
in = true;
rec = false;
}
if(key == 'f'){
rec = false;
}
return img;
}
private PImage fancySortImage(PImage img, int start, int end) {
for(int y = 0; y < img.height; y++){
sortLine(img, start, end, y);
}
return img;
}
private void sortLine(PImage img, int start, int end, int y) {
if(end > start){
int band = end - start;
int[] pixs = new int[band];
for(int x = 0; x < band; x++){
pixs[x] = img.get(start + x, y);
}
Arrays.sort(pixs);
for(int x = 0; x < band; x++){
img.set(start + x, y, pixs[x]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment